testamfth.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <unistd.h>
  38. #include <pthread.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. #include "ais_msg.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. int healthcheck_count = 0;
  63. void HealthcheckCallback (SaInvocationT invocation,
  64. const SaNameT *compName,
  65. SaAmfHealthcheckT checkType)
  66. {
  67. // if (healthcheck_count++ % 20 == 19) {
  68. printf ("20 HealthcheckCallback have occured for component: ");
  69. printSaNameT ((SaNameT *)compName);
  70. printf ("\n");
  71. // }
  72. saAmfResponse (invocation, SA_OK);
  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");
  95. saAmfStoppingComplete (invocation, SA_OK);
  96. break;
  97. }
  98. }
  99. void ComponentTerminateCallback (
  100. SaInvocationT invocation,
  101. const SaNameT *compName)
  102. {
  103. printf ("ComponentTerminateCallback\n");
  104. }
  105. void CSISetCallback (
  106. SaInvocationT invocation,
  107. const SaNameT *compName,
  108. const SaNameT *csiName,
  109. SaAmfCSIFlagsT csiFlags,
  110. SaAmfHAStateT *haState,
  111. SaNameT *activeCompName,
  112. SaAmfCSITransitionDescriptorT transitionDescriptor)
  113. {
  114. switch (*haState) {
  115. case SA_AMF_ACTIVE:
  116. printf ("CSISetCallback: '");
  117. printSaNameT ((SaNameT *)compName);
  118. printf ("' for CSI '");
  119. printSaNameT ((SaNameT *)compName);
  120. printf ("'");
  121. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  122. saAmfResponse (invocation, SA_OK);
  123. break;
  124. case SA_AMF_STANDBY:
  125. printf ("CSISetCallback: '");
  126. printSaNameT ((SaNameT *)compName);
  127. printf ("' for CSI '");
  128. printSaNameT ((SaNameT *)compName);
  129. printf ("'");
  130. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  131. saAmfResponse (invocation, SA_OK);
  132. break;
  133. case SA_AMF_QUIESCED:
  134. printf ("CSISetCallback: '");
  135. printSaNameT ((SaNameT *)compName);
  136. printf ("' for CSI '");
  137. printSaNameT ((SaNameT *)compName);
  138. printf ("'");
  139. printf (" requested to enter hastate SA_AMF_QUIESCED.\n");
  140. saAmfResponse (invocation, SA_OK);
  141. break;
  142. }
  143. }
  144. void CSIRemoveCallback (
  145. SaInvocationT invocation,
  146. const SaNameT *compName,
  147. const SaNameT *csiName,
  148. const SaAmfCSIFlagsT *csiFlags)
  149. {
  150. printf ("CSIRemoveCallback for component '");
  151. printSaNameT ((SaNameT *)compName);
  152. printf ("' in CSI '");
  153. printSaNameT ((SaNameT *)csiName);
  154. printf ("'\n");
  155. saAmfResponse (invocation, SA_OK);
  156. }
  157. void ProtectionGroupTrackCallback (
  158. const SaNameT *csiName,
  159. SaAmfProtectionGroupNotificationT *notificationBuffer,
  160. SaUint32T numberOfItems,
  161. SaUint32T numberOfMembers,
  162. SaErrorT error)
  163. {
  164. int i;
  165. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  166. printf ("buffer is %p\n", notificationBuffer);
  167. for (i = 0; i < numberOfItems; i++) {
  168. printf ("component name");
  169. printSaNameT (&notificationBuffer[i].member.compName);
  170. printf ("\n");
  171. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  172. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  173. printf ("\tchange is %d\n", notificationBuffer[i].change);
  174. }
  175. }
  176. void ExternalComponentRestartCallback (
  177. const SaInvocationT invocation,
  178. const SaNameT *externalCompName)
  179. {
  180. printf ("ExternalComponentRestartCallback\n");
  181. }
  182. void ExternalComponentControlCallback (
  183. const SaInvocationT invocation,
  184. const SaNameT *externalCompName,
  185. SaAmfExternalComponentActionT controlAction)
  186. {
  187. printf ("ExternalComponentControlCallback\n");
  188. }
  189. void PendingOperationConfirmCallback (
  190. const SaInvocationT invocation,
  191. const SaNameT *compName,
  192. SaAmfPendingOperationFlagsT pendingOperationFlags)
  193. {
  194. printf ("PendingOperationConfirmCallback\n");
  195. }
  196. void PendingOperationExpiredCallback (
  197. const SaNameT *compName,
  198. SaAmfPendingOperationFlagsT pendingOperationFlags)
  199. {
  200. printf ("PendingOperationExpiredCallback\n");
  201. }
  202. SaAmfCallbacksT amfCallbacks = {
  203. HealthcheckCallback,
  204. ReadinessStateSetCallback,
  205. ComponentTerminateCallback,
  206. CSISetCallback,
  207. CSIRemoveCallback,
  208. ProtectionGroupTrackCallback,
  209. ExternalComponentRestartCallback,
  210. ExternalComponentControlCallback,
  211. PendingOperationConfirmCallback,
  212. PendingOperationExpiredCallback
  213. };
  214. SaVersionT version = { 'A', 1, 1 };
  215. void *th_dispatch (void *arg)
  216. {
  217. SaErrorT result;
  218. SaAmfHandleT *handle = (SaAmfHandleT *)arg;
  219. printf ("THREAD DISPATCH starting.\n");
  220. result = saAmfDispatch (handle, SA_DISPATCH_BLOCKING);
  221. printf ("THREAD DISPATCH return result is %d\n", result);
  222. return (0);
  223. }
  224. int main (void) {
  225. SaAmfHandleT handle;
  226. int result;
  227. SaNameT compName;
  228. pthread_t dispatch_thread;
  229. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  230. if (result != SA_OK) {
  231. printf ("initialize result is %d\n", result);
  232. exit (1);
  233. }
  234. setSanameT (&compName, "raidupdate1");
  235. result = saAmfComponentRegister (&handle, &compName, NULL);
  236. printf ("register result is %d (should be 1)\n", result);
  237. pthread_create (&dispatch_thread, NULL, th_dispatch, &handle);
  238. sleep (5);
  239. printf ("Finalizing handle\n");
  240. saAmfFinalize (&handle);
  241. sleep (1); /* this sleep isn't really necessary */
  242. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  243. result = saAmfComponentRegister (&handle, &compName, NULL);
  244. pthread_create (&dispatch_thread, NULL, th_dispatch, &handle);
  245. sleep (10);
  246. exit (0);
  247. }