testamf2.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <sys/un.h>
  41. #include <sched.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. int healthcheck_count = 0;
  62. void HealthcheckCallback (SaInvocationT invocation,
  63. const SaNameT *compName,
  64. SaAmfHealthcheckT checkType)
  65. {
  66. // if (healthcheck_count++ % 20 == 19) {
  67. printf ("20 HealthcheckCallback have occured for component: ");
  68. printSaNameT ((SaNameT *)compName);
  69. printf ("\n");
  70. // }
  71. saAmfResponse (invocation, SA_OK);
  72. printf ("Healthcheck Count is %d\n", healthcheck_count++);
  73. }
  74. void ReadinessStateSetCallback (SaInvocationT invocation,
  75. const SaNameT *compName,
  76. SaAmfReadinessStateT readinessState)
  77. {
  78. switch (readinessState) {
  79. case SA_AMF_IN_SERVICE:
  80. printf ("ReadinessStateSetCallback: '");
  81. printSaNameT ((SaNameT *)compName);
  82. printf ("' requested to enter operational state SA_AMF_IN_SERVICE.\n");
  83. saAmfResponse (invocation, SA_OK);
  84. break;
  85. case SA_AMF_OUT_OF_SERVICE:
  86. printf ("ReadinessStateSetCallback: '");
  87. printSaNameT ((SaNameT *)compName);
  88. printf ("' requested to enter operational state SA_AMF_OUT_OF_SERVICE.\n");
  89. saAmfResponse (invocation, SA_OK);
  90. break;
  91. case SA_AMF_STOPPING:
  92. printf ("ReadinessStateSetCallback: '");
  93. printSaNameT ((SaNameT *)compName);
  94. printf ("' requested to enter operational state SA_AMF_STOPPING.\n"); saAmfStoppingComplete (invocation, SA_OK);
  95. break;
  96. }
  97. }
  98. void ComponentTerminateCallback (
  99. SaInvocationT invocation,
  100. const SaNameT *compName)
  101. {
  102. printf ("ComponentTerminateCallback\n");
  103. }
  104. void CSISetCallback (
  105. SaInvocationT invocation,
  106. const SaNameT *compName,
  107. const SaNameT *csiName,
  108. SaAmfCSIFlagsT csiFlags,
  109. SaAmfHAStateT *haState,
  110. SaNameT *activeCompName,
  111. SaAmfCSITransitionDescriptorT transitionDescriptor)
  112. {
  113. switch (*haState) {
  114. case SA_AMF_ACTIVE:
  115. printf ("CSISetCallback: '");
  116. printSaNameT ((SaNameT *)compName);
  117. printf ("' for CSI '");
  118. printSaNameT ((SaNameT *)compName);
  119. printf ("'");
  120. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  121. saAmfResponse (invocation, SA_OK);
  122. break;
  123. case SA_AMF_STANDBY:
  124. printf ("CSISetCallback: '");
  125. printSaNameT ((SaNameT *)compName);
  126. printf ("' for CSI '");
  127. printSaNameT ((SaNameT *)compName);
  128. printf ("'");
  129. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  130. saAmfResponse (invocation, SA_OK);
  131. break;
  132. case SA_AMF_QUIESCED:
  133. printf ("CSISetCallback: '");
  134. printSaNameT ((SaNameT *)compName);
  135. printf ("' for CSI '");
  136. printSaNameT ((SaNameT *)compName);
  137. printf ("'");
  138. printf (" requested to enter hastate SA_AMF_QUIESCED.\n");
  139. saAmfResponse (invocation, SA_OK);
  140. break;
  141. }
  142. }
  143. void CSIRemoveCallback (
  144. SaInvocationT invocation,
  145. const SaNameT *compName,
  146. const SaNameT *csiName,
  147. const SaAmfCSIFlagsT *csiFlags)
  148. {
  149. printf ("CSIRemoveCallback for component '");
  150. printSaNameT ((SaNameT *)compName);
  151. printf ("' in CSI '");
  152. printSaNameT ((SaNameT *)csiName);
  153. printf ("'\n");
  154. saAmfResponse (invocation, SA_OK);
  155. }
  156. void ProtectionGroupTrackCallback (
  157. const SaNameT *csiName,
  158. SaAmfProtectionGroupNotificationT *notificationBuffer,
  159. SaUint32T numberOfItems,
  160. SaUint32T numberOfMembers,
  161. SaErrorT error)
  162. {
  163. int i;
  164. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  165. printf ("buffer is %p\n", notificationBuffer);
  166. for (i = 0; i < numberOfItems; i++) {
  167. printf ("component name");
  168. printSaNameT (&notificationBuffer[i].member.compName);
  169. printf ("\n");
  170. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  171. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  172. printf ("\tchange is %d\n", notificationBuffer[i].change);
  173. }
  174. }
  175. void ExternalComponentRestartCallback (
  176. const SaInvocationT invocation,
  177. const SaNameT *externalCompName)
  178. {
  179. printf ("ExternalComponentRestartCallback\n");
  180. }
  181. void ExternalComponentControlCallback (
  182. const SaInvocationT invocation,
  183. const SaNameT *externalCompName,
  184. SaAmfExternalComponentActionT controlAction)
  185. {
  186. printf ("ExternalComponentControlCallback\n");
  187. }
  188. void PendingOperationConfirmCallback (
  189. const SaInvocationT invocation,
  190. const SaNameT *compName,
  191. SaAmfPendingOperationFlagsT pendingOperationFlags)
  192. {
  193. printf ("PendingOperationConfirmCallback\n");
  194. }
  195. void PendingOperationExpiredCallback (
  196. const SaNameT *compName,
  197. SaAmfPendingOperationFlagsT pendingOperationFlags)
  198. {
  199. printf ("PendingOperationExpiredCallback\n");
  200. }
  201. SaAmfCallbacksT amfCallbacks = {
  202. HealthcheckCallback,
  203. ReadinessStateSetCallback,
  204. ComponentTerminateCallback,
  205. CSISetCallback,
  206. CSIRemoveCallback,
  207. ProtectionGroupTrackCallback,
  208. ExternalComponentRestartCallback,
  209. ExternalComponentControlCallback,
  210. PendingOperationConfirmCallback,
  211. PendingOperationExpiredCallback
  212. };
  213. SaVersionT version = { 'A', 1, 1 };
  214. int main (void) {
  215. SaAmfHandleT handle;
  216. int result;
  217. int select_fd;
  218. fd_set read_fds;
  219. SaNameT compName;
  220. int i;
  221. SaAmfErrorDescriptorT errorDescriptor;
  222. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  223. if (result != SA_OK) {
  224. printf ("initialize result is %d\n", result);
  225. exit (1);
  226. }
  227. FD_ZERO (&read_fds);
  228. saAmfSelectionObjectGet (&handle, &select_fd);
  229. FD_SET (select_fd, &read_fds);
  230. setSanameT (&compName, "comp_b_in_su_x");
  231. result = saAmfComponentRegister (&handle, &compName, NULL);
  232. printf ("register result is %d (should be 1)\n", result);
  233. i = 0;
  234. do {
  235. select (select_fd + 1, &read_fds, 0, 0, 0);
  236. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  237. // } while (1);
  238. } while (i++ < 4);
  239. printf ("Unregistering component\n");
  240. result = saAmfComponentUnregister (&handle, &compName, NULL);
  241. // select (select_fd + 1, &read_fds, 0, 0, 0);
  242. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  243. sleep (1);
  244. printf ("Registering component\n");
  245. result = saAmfComponentRegister (&handle, &compName, NULL);
  246. sleep (1);
  247. select (select_fd + 1, &read_fds, 0, 0, 0);
  248. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  249. printf ("REPORTING ERROR.\n");
  250. errorDescriptor.probableCause = 5;
  251. result = saAmfErrorReport (&compName, &compName,
  252. 0,
  253. &errorDescriptor,
  254. NULL);
  255. select (select_fd + 1, &read_fds, 0, 0, 0);
  256. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  257. sleep (1);
  258. printf ("CANCELING ERROR.\n");
  259. result = saAmfErrorCancelAll (&compName);
  260. do {
  261. select (select_fd + 1, &read_fds, 0, 0, 0);
  262. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  263. } while (result);
  264. saAmfFinalize (&handle);
  265. return (0);
  266. }