testamfth.c 8.0 KB

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