testamf1.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <sys/un.h>
  40. #include <sched.h>
  41. #include "ais_types.h"
  42. #include "ais_amf.h"
  43. void printSaClmNodeAddressT (SaClmNodeAddressT *nodeAddress) {
  44. int i;
  45. for (i = 0; i < nodeAddress->length; i++) {
  46. printf ("%d.", nodeAddress->value[i]);
  47. }
  48. }
  49. void printSaNameT (SaNameT *name)
  50. {
  51. int i;
  52. for (i = 0; i < name->length; i++) {
  53. printf ("%c", name->value[i]);
  54. }
  55. }
  56. void setSanameT (SaNameT *name, char *str) {
  57. name->length = strlen (str);
  58. memcpy (name->value, str, name->length);
  59. }
  60. int healthcheck_count = 0;
  61. void HealthcheckCallback (SaInvocationT invocation,
  62. const SaNameT *compName,
  63. SaAmfHealthcheckT checkType)
  64. {
  65. // if (healthcheck_count++ % 20 == 19) {
  66. printf ("20 HealthcheckCallback have occured for component: ");
  67. printSaNameT ((SaNameT *)compName);
  68. printf ("\n");
  69. // }
  70. saAmfResponse (invocation, SA_OK);
  71. }
  72. void ReadinessStateSetCallback (SaInvocationT invocation,
  73. const SaNameT *compName,
  74. SaAmfReadinessStateT readinessState)
  75. {
  76. switch (readinessState) {
  77. case SA_AMF_IN_SERVICE:
  78. printf ("ReadinessStateSetCallback: '");
  79. printSaNameT ((SaNameT *)compName);
  80. printf ("' requested to enter operational state SA_AMF_IN_SERVICE.\n");
  81. saAmfResponse (invocation, SA_OK);
  82. break;
  83. case SA_AMF_OUT_OF_SERVICE:
  84. printf ("ReadinessStateSetCallback: '");
  85. printSaNameT ((SaNameT *)compName);
  86. printf ("' requested to enter operational state SA_AMF_OUT_OF_SERVICE.\n");
  87. saAmfResponse (invocation, SA_OK);
  88. break;
  89. case SA_AMF_STOPPING:
  90. printf ("ReadinessStateSetCallback: '");
  91. printSaNameT ((SaNameT *)compName);
  92. printf ("' requested to enter operational state SA_AMF_STOPPING.\n");
  93. saAmfStoppingComplete (invocation, SA_OK);
  94. break;
  95. }
  96. }
  97. void ComponentTerminateCallback (
  98. SaInvocationT invocation,
  99. const SaNameT *compName)
  100. {
  101. printf ("ComponentTerminateCallback\n");
  102. }
  103. void CSISetCallback (
  104. SaInvocationT invocation,
  105. const SaNameT *compName,
  106. const SaNameT *csiName,
  107. SaAmfCSIFlagsT csiFlags,
  108. SaAmfHAStateT *haState,
  109. SaNameT *activeCompName,
  110. SaAmfCSITransitionDescriptorT transitionDescriptor)
  111. {
  112. switch (*haState) {
  113. case SA_AMF_ACTIVE:
  114. printf ("CSISetCallback: '");
  115. printSaNameT ((SaNameT *)compName);
  116. printf ("' for CSI '");
  117. printSaNameT ((SaNameT *)compName);
  118. printf ("'");
  119. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  120. saAmfResponse (invocation, SA_OK);
  121. break;
  122. case SA_AMF_STANDBY:
  123. printf ("CSISetCallback: '");
  124. printSaNameT ((SaNameT *)compName);
  125. printf ("' for CSI '");
  126. printSaNameT ((SaNameT *)compName);
  127. printf ("'");
  128. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  129. saAmfResponse (invocation, SA_OK);
  130. break;
  131. case SA_AMF_QUIESCED:
  132. printf ("CSISetCallback: '");
  133. printSaNameT ((SaNameT *)compName);
  134. printf ("' for CSI '");
  135. printSaNameT ((SaNameT *)compName);
  136. printf ("'");
  137. printf (" requested to enter hastate SA_AMF_QUIESCED.\n");
  138. saAmfResponse (invocation, SA_OK);
  139. break;
  140. }
  141. }
  142. void CSIRemoveCallback (
  143. SaInvocationT invocation,
  144. const SaNameT *compName,
  145. const SaNameT *csiName,
  146. const SaAmfCSIFlagsT *csiFlags)
  147. {
  148. printf ("CSIRemoveCallback for component '");
  149. printSaNameT ((SaNameT *)compName);
  150. printf ("' in CSI '");
  151. printSaNameT ((SaNameT *)csiName);
  152. printf ("'\n");
  153. saAmfResponse (invocation, SA_OK);
  154. }
  155. void ProtectionGroupTrackCallback (
  156. const SaNameT *csiName,
  157. SaAmfProtectionGroupNotificationT *notificationBuffer,
  158. SaUint32T numberOfItems,
  159. SaUint32T numberOfMembers,
  160. SaErrorT error)
  161. {
  162. int i;
  163. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  164. printf ("buffer is %p\n", notificationBuffer);
  165. for (i = 0; i < numberOfItems; i++) {
  166. printf ("component name");
  167. printSaNameT (&notificationBuffer[i].member.compName);
  168. printf ("\n");
  169. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  170. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  171. printf ("\tchange is %d\n", notificationBuffer[i].change);
  172. }
  173. }
  174. void ExternalComponentRestartCallback (
  175. const SaInvocationT invocation,
  176. const SaNameT *externalCompName)
  177. {
  178. printf ("ExternalComponentRestartCallback\n");
  179. }
  180. void ExternalComponentControlCallback (
  181. const SaInvocationT invocation,
  182. const SaNameT *externalCompName,
  183. SaAmfExternalComponentActionT controlAction)
  184. {
  185. printf ("ExternalComponentControlCallback\n");
  186. }
  187. void PendingOperationConfirmCallback (
  188. const SaInvocationT invocation,
  189. const SaNameT *compName,
  190. SaAmfPendingOperationFlagsT pendingOperationFlags)
  191. {
  192. printf ("PendingOperationConfirmCallback\n");
  193. }
  194. void PendingOperationExpiredCallback (
  195. const SaNameT *compName,
  196. SaAmfPendingOperationFlagsT pendingOperationFlags)
  197. {
  198. printf ("PendingOperationExpiredCallback\n");
  199. }
  200. SaAmfCallbacksT amfCallbacks = {
  201. HealthcheckCallback,
  202. ReadinessStateSetCallback,
  203. ComponentTerminateCallback,
  204. CSISetCallback,
  205. CSIRemoveCallback,
  206. ProtectionGroupTrackCallback,
  207. ExternalComponentRestartCallback,
  208. ExternalComponentControlCallback,
  209. PendingOperationConfirmCallback,
  210. PendingOperationExpiredCallback
  211. };
  212. SaVersionT version = { 'A', 1, 1 };
  213. static struct sched_param sched_param = {
  214. sched_priority: 99
  215. };
  216. int main (void) {
  217. SaAmfHandleT handle;
  218. int result;
  219. int select_fd;
  220. fd_set read_fds;
  221. SaNameT compName;
  222. result = sched_setscheduler (0, SCHED_RR, &sched_param);
  223. if (result == -1) {
  224. printf ("couldn't set sched priority\n");
  225. }
  226. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  227. if (result != SA_OK) {
  228. printf ("initialize result is %d\n", result);
  229. exit (1);
  230. }
  231. FD_ZERO (&read_fds);
  232. saAmfSelectionObjectGet (&handle, &select_fd);
  233. FD_SET (select_fd, &read_fds);
  234. setSanameT (&compName, "comp_a_in_su_x");
  235. result = saAmfComponentRegister (&handle, &compName, NULL);
  236. printf ("register result is %d (should be 1)\n", result);
  237. do {
  238. select (select_fd + 1, &read_fds, 0, 0, 0);
  239. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  240. } while (result);
  241. saAmfFinalize (&handle);
  242. exit (0);
  243. }