testamf2.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2002-2004 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 <unistd.h>
  38. #include <signal.h>
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/un.h>
  42. #include <sched.h>
  43. #include "ais_types.h"
  44. #include "ais_amf.h"
  45. void printSaClmNodeAddressT (SaClmNodeAddressT *nodeAddress) {
  46. int i;
  47. for (i = 0; i < nodeAddress->length; i++) {
  48. printf ("%d.", nodeAddress->value[i]);
  49. }
  50. }
  51. void printSaNameT (SaNameT *name)
  52. {
  53. int i;
  54. for (i = 0; i < name->length; i++) {
  55. printf ("%c", name->value[i]);
  56. }
  57. }
  58. void setSanameT (SaNameT *name, char *str) {
  59. name->length = strlen (str);
  60. memcpy (name->value, str, name->length);
  61. }
  62. static int health_flag = -1;
  63. static unsigned int healthcheck_count = 0;
  64. static unsigned int healthcheck_no = 0;
  65. void HealthcheckCallback (SaInvocationT invocation,
  66. const SaNameT *compName,
  67. SaAmfHealthcheckT checkType)
  68. {
  69. SaErrorT res;
  70. healthcheck_no++;
  71. if (health_flag == -1 || healthcheck_no%healthcheck_count == 0) {
  72. printf ("%u HealthcheckCallback have occured for component: ",healthcheck_no);
  73. printSaNameT ((SaNameT *)compName);
  74. printf ("\n");
  75. }
  76. res = saAmfResponse (invocation, SA_OK);
  77. if (res != SA_OK) {
  78. printf ("response res is %d\n", res);
  79. }
  80. }
  81. void ReadinessStateSetCallback (SaInvocationT invocation,
  82. const SaNameT *compName,
  83. SaAmfReadinessStateT readinessState)
  84. {
  85. switch (readinessState) {
  86. case SA_AMF_IN_SERVICE:
  87. printf ("ReadinessStateSetCallback: '");
  88. printSaNameT ((SaNameT *)compName);
  89. printf ("' requested to enter operational state SA_AMF_IN_SERVICE.\n");
  90. saAmfResponse (invocation, SA_OK);
  91. break;
  92. case SA_AMF_OUT_OF_SERVICE:
  93. printf ("ReadinessStateSetCallback: '");
  94. printSaNameT ((SaNameT *)compName);
  95. printf ("' requested to enter operational state SA_AMF_OUT_OF_SERVICE.\n");
  96. saAmfResponse (invocation, SA_OK);
  97. break;
  98. case SA_AMF_STOPPING:
  99. printf ("ReadinessStateSetCallback: '");
  100. printSaNameT ((SaNameT *)compName);
  101. printf ("' requested to enter operational state SA_AMF_STOPPING.\n"); saAmfStoppingComplete (invocation, SA_OK);
  102. break;
  103. }
  104. }
  105. void ComponentTerminateCallback (
  106. SaInvocationT invocation,
  107. const SaNameT *compName)
  108. {
  109. printf ("ComponentTerminateCallback\n");
  110. }
  111. void CSISetCallback (
  112. SaInvocationT invocation,
  113. const SaNameT *compName,
  114. const SaNameT *csiName,
  115. SaAmfCSIFlagsT csiFlags,
  116. SaAmfHAStateT *haState,
  117. SaNameT *activeCompName,
  118. SaAmfCSITransitionDescriptorT transitionDescriptor)
  119. {
  120. switch (*haState) {
  121. case SA_AMF_ACTIVE:
  122. printf ("CSISetCallback: '");
  123. printSaNameT ((SaNameT *)compName);
  124. printf ("' for CSI '");
  125. printSaNameT ((SaNameT *)compName);
  126. printf ("'");
  127. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  128. saAmfResponse (invocation, SA_OK);
  129. break;
  130. case SA_AMF_STANDBY:
  131. printf ("CSISetCallback: '");
  132. printSaNameT ((SaNameT *)compName);
  133. printf ("' for CSI '");
  134. printSaNameT ((SaNameT *)compName);
  135. printf ("'");
  136. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  137. saAmfResponse (invocation, SA_OK);
  138. break;
  139. case SA_AMF_QUIESCED:
  140. printf ("CSISetCallback: '");
  141. printSaNameT ((SaNameT *)compName);
  142. printf ("' for CSI '");
  143. printSaNameT ((SaNameT *)compName);
  144. printf ("'");
  145. printf (" requested to enter hastate SA_AMF_QUIESCED.\n");
  146. saAmfResponse (invocation, SA_OK);
  147. break;
  148. }
  149. }
  150. void CSIRemoveCallback (
  151. SaInvocationT invocation,
  152. const SaNameT *compName,
  153. const SaNameT *csiName,
  154. const SaAmfCSIFlagsT *csiFlags)
  155. {
  156. printf ("CSIRemoveCallback for component '");
  157. printSaNameT ((SaNameT *)compName);
  158. printf ("' in CSI '");
  159. printSaNameT ((SaNameT *)csiName);
  160. printf ("'\n");
  161. saAmfResponse (invocation, SA_OK);
  162. }
  163. void ProtectionGroupTrackCallback (
  164. const SaNameT *csiName,
  165. SaAmfProtectionGroupNotificationT *notificationBuffer,
  166. SaUint32T numberOfItems,
  167. SaUint32T numberOfMembers,
  168. SaErrorT error)
  169. {
  170. int i;
  171. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  172. printf ("buffer is %p\n", notificationBuffer);
  173. for (i = 0; i < numberOfItems; i++) {
  174. printf ("component name");
  175. printSaNameT (&notificationBuffer[i].member.compName);
  176. printf ("\n");
  177. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  178. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  179. printf ("\tchange is %d\n", notificationBuffer[i].change);
  180. }
  181. }
  182. void ExternalComponentRestartCallback (
  183. const SaInvocationT invocation,
  184. const SaNameT *externalCompName)
  185. {
  186. printf ("ExternalComponentRestartCallback\n");
  187. }
  188. void ExternalComponentControlCallback (
  189. const SaInvocationT invocation,
  190. const SaNameT *externalCompName,
  191. SaAmfExternalComponentActionT controlAction)
  192. {
  193. printf ("ExternalComponentControlCallback\n");
  194. }
  195. void PendingOperationConfirmCallback (
  196. const SaInvocationT invocation,
  197. const SaNameT *compName,
  198. SaAmfPendingOperationFlagsT pendingOperationFlags)
  199. {
  200. printf ("PendingOperationConfirmCallback\n");
  201. }
  202. void PendingOperationExpiredCallback (
  203. const SaNameT *compName,
  204. SaAmfPendingOperationFlagsT pendingOperationFlags)
  205. {
  206. printf ("PendingOperationExpiredCallback\n");
  207. }
  208. SaAmfCallbacksT amfCallbacks = {
  209. HealthcheckCallback,
  210. ReadinessStateSetCallback,
  211. ComponentTerminateCallback,
  212. CSISetCallback,
  213. CSIRemoveCallback,
  214. ProtectionGroupTrackCallback,
  215. ExternalComponentRestartCallback,
  216. ExternalComponentControlCallback,
  217. PendingOperationConfirmCallback,
  218. PendingOperationExpiredCallback
  219. };
  220. SaVersionT version = { 'A', 1, 1 };
  221. void sigintr_handler (int signum) {
  222. exit (0);
  223. }
  224. int main (int argc, char **argv) {
  225. SaAmfHandleT handle;
  226. int result;
  227. int select_fd;
  228. fd_set read_fds;
  229. SaNameT compName;
  230. int c,i;
  231. SaAmfErrorDescriptorT errorDescriptor;
  232. extern char *optarg;
  233. extern int optind;
  234. memset (&compName, 0, sizeof (SaNameT));
  235. signal (SIGINT, sigintr_handler);
  236. for (;;) {
  237. c = getopt(argc,argv,"h:n:");
  238. if (c==-1) {
  239. break;
  240. }
  241. switch (c) {
  242. case 0 :
  243. break;
  244. case 'h':
  245. health_flag = 0;
  246. sscanf (optarg,"%ud" ,&healthcheck_count);
  247. break;
  248. case 'n':
  249. setSanameT (&compName, optarg);
  250. break;
  251. default :
  252. break;
  253. }
  254. }
  255. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  256. if (result != SA_OK) {
  257. printf ("initialize result is %d\n", result);
  258. exit (1);
  259. }
  260. FD_ZERO (&read_fds);
  261. saAmfSelectionObjectGet (&handle, &select_fd);
  262. FD_SET (select_fd, &read_fds);
  263. if (compName.length <= 0){
  264. setSanameT (&compName, "comp_b_in_su_x");
  265. }
  266. result = saAmfComponentRegister (&handle, &compName, NULL);
  267. printf ("register result is %d (should be 1)\n", result);
  268. i = 0;
  269. do {
  270. select (select_fd + 1, &read_fds, 0, 0, 0);
  271. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  272. // } while (1);
  273. } while (i++ < 4);
  274. printf ("Unregistering component\n");
  275. result = saAmfComponentUnregister (&handle, &compName, NULL);
  276. // select (select_fd + 1, &read_fds, 0, 0, 0);
  277. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  278. sleep (1);
  279. printf ("Registering component\n");
  280. result = saAmfComponentRegister (&handle, &compName, NULL);
  281. sleep (1);
  282. select (select_fd + 1, &read_fds, 0, 0, 0);
  283. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  284. printf ("REPORTING ERROR.\n");
  285. errorDescriptor.probableCause = 5;
  286. result = saAmfErrorReport (&compName, &compName,
  287. 0,
  288. &errorDescriptor,
  289. NULL);
  290. select (select_fd + 1, &read_fds, 0, 0, 0);
  291. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  292. sleep (1);
  293. printf ("CANCELING ERROR.\n");
  294. result = saAmfErrorCancelAll (&compName);
  295. do {
  296. select (select_fd + 1, &read_fds, 0, 0, 0);
  297. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  298. } while (result);
  299. saAmfFinalize (&handle);
  300. return (0);
  301. }