testamf4.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2002-2003 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <signal.h>
  38. #include <unistd.h>
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/un.h>
  42. #include "ais_types.h"
  43. #include "ais_amf.h"
  44. void printSaClmNodeAddressT (SaClmNodeAddressT *nodeAddress) {
  45. int i;
  46. for (i = 0; i < nodeAddress->length; i++) {
  47. printf ("%d.", nodeAddress->value[i]);
  48. }
  49. }
  50. void printSaNameT (SaNameT *name)
  51. {
  52. int i;
  53. for (i = 0; i < name->length; i++) {
  54. printf ("%c", name->value[i]);
  55. }
  56. }
  57. void setSanameT (SaNameT *name, char *str) {
  58. name->length = strlen (str);
  59. memcpy (name->value, str, name->length);
  60. }
  61. static int health_flag = -1;
  62. static unsigned int healthcheck_count = 0;
  63. static unsigned int healthcheck_no = 0;
  64. void HealthcheckCallback (SaInvocationT invocation,
  65. const SaNameT *compName,
  66. SaAmfHealthcheckT checkType)
  67. {
  68. SaErrorT res;
  69. healthcheck_no ++;
  70. if (health_flag == -1 || healthcheck_no%healthcheck_count == 0) {
  71. printf ("%u HealthcheckCallback have occured for component: ",healthcheck_no);
  72. printSaNameT ((SaNameT *)compName);
  73. printf ("\n");
  74. }
  75. res = saAmfResponse (invocation, SA_OK);
  76. if (res != SA_OK) {
  77. printf ("response res is %d\n", res);
  78. }
  79. }
  80. void ReadinessStateSetCallback (SaInvocationT invocation,
  81. const SaNameT *compName,
  82. SaAmfReadinessStateT readinessState)
  83. {
  84. switch (readinessState) {
  85. case SA_AMF_IN_SERVICE:
  86. printf ("ReadinessStateSetCallback: '");
  87. printSaNameT ((SaNameT *)compName);
  88. printf ("' requested to enter operational state SA_AMF_IN_SERVICE.\n");
  89. saAmfResponse (invocation, SA_OK);
  90. break;
  91. case SA_AMF_OUT_OF_SERVICE:
  92. printf ("ReadinessStateSetCallback: '");
  93. printSaNameT ((SaNameT *)compName);
  94. printf ("' requested to enter operational state SA_AMF_OUT_OF_SERVICE.\n");
  95. saAmfResponse (invocation, SA_OK);
  96. break;
  97. case SA_AMF_STOPPING:
  98. printf ("ReadinessStateSetCallback: '");
  99. printSaNameT ((SaNameT *)compName);
  100. printf ("' requested to enter operational state SA_AMF_STOPPING.\n"); saAmfStoppingComplete (invocation, SA_OK);
  101. break;
  102. }
  103. }
  104. void ComponentTerminateCallback (
  105. SaInvocationT invocation,
  106. const SaNameT *compName)
  107. {
  108. printf ("ComponentTerminateCallback\n");
  109. }
  110. void CSISetCallback (
  111. SaInvocationT invocation,
  112. const SaNameT *compName,
  113. const SaNameT *csiName,
  114. SaAmfCSIFlagsT csiFlags,
  115. SaAmfHAStateT *haState,
  116. SaNameT *activeCompName,
  117. SaAmfCSITransitionDescriptorT transitionDescriptor)
  118. {
  119. switch (*haState) {
  120. case SA_AMF_ACTIVE:
  121. printf ("CSISetCallback: '");
  122. printSaNameT ((SaNameT *)compName);
  123. printf ("' for CSI '");
  124. printSaNameT ((SaNameT *)compName);
  125. printf ("'");
  126. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  127. saAmfResponse (invocation, SA_OK);
  128. break;
  129. case SA_AMF_STANDBY:
  130. printf ("CSISetCallback: '");
  131. printSaNameT ((SaNameT *)compName);
  132. printf ("' for CSI '");
  133. printSaNameT ((SaNameT *)compName);
  134. printf ("'");
  135. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  136. saAmfResponse (invocation, SA_OK);
  137. break;
  138. case SA_AMF_QUIESCED:
  139. printf ("CSISetCallback: '");
  140. printSaNameT ((SaNameT *)compName);
  141. printf ("' for CSI '");
  142. printSaNameT ((SaNameT *)compName);
  143. printf ("'");
  144. printf (" requested to enter hastate SA_AMF_QUIESCED.\n");
  145. saAmfResponse (invocation, SA_OK);
  146. break;
  147. }
  148. }
  149. void CSIRemoveCallback (
  150. SaInvocationT invocation,
  151. const SaNameT *compName,
  152. const SaNameT *csiName,
  153. const SaAmfCSIFlagsT *csiFlags)
  154. {
  155. printf ("CSIRemoveCallback for component '");
  156. printSaNameT ((SaNameT *)compName);
  157. printf ("' in CSI '");
  158. printSaNameT ((SaNameT *)csiName);
  159. printf ("'\n");
  160. saAmfResponse (invocation, SA_OK);
  161. }
  162. void ProtectionGroupTrackCallback (
  163. const SaNameT *csiName,
  164. SaAmfProtectionGroupNotificationT *notificationBuffer,
  165. SaUint32T numberOfItems,
  166. SaUint32T numberOfMembers,
  167. SaErrorT error)
  168. {
  169. int i;
  170. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  171. printf ("buffer is %p\n", notificationBuffer);
  172. for (i = 0; i < numberOfItems; i++) {
  173. printf ("component name");
  174. printSaNameT (&notificationBuffer[i].member.compName);
  175. printf ("\n");
  176. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  177. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  178. printf ("\tchange is %d\n", notificationBuffer[i].change);
  179. }
  180. }
  181. void ExternalComponentRestartCallback (
  182. const SaInvocationT invocation,
  183. const SaNameT *externalCompName)
  184. {
  185. printf ("ExternalComponentRestartCallback\n");
  186. }
  187. void ExternalComponentControlCallback (
  188. const SaInvocationT invocation,
  189. const SaNameT *externalCompName,
  190. SaAmfExternalComponentActionT controlAction)
  191. {
  192. printf ("ExternalComponentControlCallback\n");
  193. }
  194. void PendingOperationConfirmCallback (
  195. const SaInvocationT invocation,
  196. const SaNameT *compName,
  197. SaAmfPendingOperationFlagsT pendingOperationFlags)
  198. {
  199. printf ("PendingOperationConfirmCallback\n");
  200. }
  201. void PendingOperationExpiredCallback (
  202. const SaNameT *compName,
  203. SaAmfPendingOperationFlagsT pendingOperationFlags)
  204. {
  205. printf ("PendingOperationExpiredCallback\n");
  206. }
  207. SaAmfCallbacksT amfCallbacks = {
  208. HealthcheckCallback,
  209. ReadinessStateSetCallback,
  210. ComponentTerminateCallback,
  211. CSISetCallback,
  212. CSIRemoveCallback,
  213. ProtectionGroupTrackCallback,
  214. ExternalComponentRestartCallback,
  215. ExternalComponentControlCallback,
  216. PendingOperationConfirmCallback,
  217. PendingOperationExpiredCallback
  218. };
  219. SaVersionT version = { 'A', 1, 1 };
  220. void sigintr_handler (int signum) {
  221. exit (0);
  222. }
  223. int main (int argc, char **argv) {
  224. SaAmfHandleT handle;
  225. int result;
  226. int select_fd;
  227. fd_set read_fds;
  228. SaNameT compName;
  229. extern char *optarg;
  230. extern int optind;
  231. int c;
  232. memset (&compName, 0, sizeof (SaNameT));
  233. signal (SIGINT, sigintr_handler);
  234. for (;;) {
  235. c = getopt(argc,argv,"h:n:");
  236. if (c==-1) {
  237. break;
  238. }
  239. switch (c) {
  240. case 0 :
  241. break;
  242. case 'h':
  243. health_flag = 0;
  244. sscanf (optarg,"%ud" ,&healthcheck_count);
  245. break;
  246. case 'n':
  247. setSanameT (&compName, optarg);
  248. break;
  249. default :
  250. break;
  251. }
  252. }
  253. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  254. if (result != SA_OK) {
  255. printf ("initialize result is %d\n", result);
  256. exit (1);
  257. }
  258. FD_ZERO (&read_fds);
  259. saAmfSelectionObjectGet (&handle, &select_fd);
  260. FD_SET (select_fd, &read_fds);
  261. if (compName.length <= 0){
  262. setSanameT (&compName, "comp_b_in_su_y");
  263. }
  264. result = saAmfComponentRegister (&handle, &compName, NULL);
  265. printf ("register result is %d (should be 1)\n", result);
  266. do {
  267. select (select_fd + 1, &read_fds, 0, 0, 0);
  268. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  269. } while (result);
  270. saAmfFinalize (&handle);
  271. exit (0);
  272. }