testamf1.c 8.2 KB

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