testamf6.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2002-2003 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Sun Microsystems, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <signal.h>
  39. #include <unistd.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/un.h>
  43. #include <sched.h>
  44. #include "ais_types.h"
  45. #include "saAmf.h"
  46. void printSaNameT (SaNameT *name)
  47. {
  48. int i;
  49. for (i = 0; i < name->length; i++) {
  50. printf ("%c", name->value[i]);
  51. }
  52. }
  53. void setSanameT (SaNameT *name, char *str) {
  54. name->length = strlen (str);
  55. memcpy (name->value, str, name->length);
  56. }
  57. static int health_flag = -1;
  58. static unsigned int healthcheck_count = 0;
  59. static unsigned int healthcheck_no = 0;
  60. #ifdef COMPILE_OUT
  61. void HealthcheckCallback (SaInvocationT invocation,
  62. const SaNameT *compName,
  63. SaAmfHealthcheckT checkType)
  64. {
  65. SaErrorT res;
  66. healthcheck_no ++;
  67. if (health_flag == -1 || healthcheck_no%healthcheck_count == 0) {
  68. printf ("%u HealthcheckCallback have occured for component: ",healthcheck_no);
  69. printSaNameT ((SaNameT *)compName);
  70. printf ("\n");
  71. }
  72. res = saAmfResponse (invocation, SA_OK);
  73. if (res != SA_OK) {
  74. printf ("response res is %d\n", res);
  75. }
  76. }
  77. void ReadinessStateSetCallback (SaInvocationT invocation,
  78. const SaNameT *compName,
  79. SaAmfReadinessStateT readinessState)
  80. {
  81. switch (readinessState) {
  82. case SA_AMF_IN_SERVICE:
  83. printf ("ReadinessStateSetCallback: '");
  84. printSaNameT ((SaNameT *)compName);
  85. printf ("' requested to enter operational state SA_AMF_IN_SERVICE.\n");
  86. saAmfResponse (invocation, SA_OK);
  87. break;
  88. case SA_AMF_OUT_OF_SERVICE:
  89. printf ("ReadinessStateSetCallback: '");
  90. printSaNameT ((SaNameT *)compName);
  91. printf ("' requested to enter operational state SA_AMF_OUT_OF_SERVICE.\n");
  92. saAmfResponse (invocation, SA_OK);
  93. break;
  94. case SA_AMF_STOPPING:
  95. printf ("ReadinessStateSetCallback: '");
  96. printSaNameT ((SaNameT *)compName);
  97. printf ("' requested to enter operational state SA_AMF_STOPPING.\n");
  98. saAmfStoppingComplete (invocation, SA_OK);
  99. break;
  100. }
  101. }
  102. void ComponentTerminateCallback (
  103. SaInvocationT invocation,
  104. const SaNameT *compName)
  105. {
  106. printf ("ComponentTerminateCallback\n");
  107. }
  108. #endif
  109. void CSISetCallback (
  110. SaInvocationT invocation,
  111. const SaNameT *compName,
  112. SaAmfHAStateT haState,
  113. SaAmfCSIDescriptorT *csiDescriptor)
  114. {
  115. switch (haState) {
  116. case SA_AMF_HA_ACTIVE:
  117. printf ("CSISetCallback: '");
  118. printSaNameT ((SaNameT *)&csiDescriptor->csiName);
  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_HA_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. }
  135. }
  136. #ifdef COMPILE_OUT
  137. void CSIRemoveCallback (
  138. SaInvocationT invocation,
  139. const SaNameT *compName,
  140. const SaNameT *csiName,
  141. const SaAmfCSIFlagsT *csiFlags)
  142. {
  143. printf ("CSIRemoveCallback for component '");
  144. printSaNameT ((SaNameT *)compName);
  145. printf ("' in CSI '");
  146. printSaNameT ((SaNameT *)csiName);
  147. printf ("'\n");
  148. saAmfResponse (invocation, SA_OK);
  149. }
  150. void ProtectionGroupTrackCallback (
  151. const SaNameT *csiName,
  152. SaAmfProtectionGroupNotificationT *notificationBuffer,
  153. SaUint32T numberOfItems,
  154. SaUint32T numberOfMembers,
  155. SaErrorT error)
  156. {
  157. int i;
  158. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  159. printf ("buffer is %p\n", notificationBuffer);
  160. for (i = 0; i < numberOfItems; i++) {
  161. printf ("component name");
  162. printSaNameT (&notificationBuffer[i].member.compName);
  163. printf ("\n");
  164. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  165. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  166. printf ("\tchange is %d\n", notificationBuffer[i].change);
  167. }
  168. }
  169. void ExternalComponentRestartCallback (
  170. const SaInvocationT invocation,
  171. const SaNameT *externalCompName)
  172. {
  173. printf ("ExternalComponentRestartCallback\n");
  174. }
  175. void ExternalComponentControlCallback (
  176. const SaInvocationT invocation,
  177. const SaNameT *externalCompName,
  178. SaAmfExternalComponentActionT controlAction)
  179. {
  180. printf ("ExternalComponentControlCallback\n");
  181. }
  182. void PendingOperationConfirmCallback (
  183. const SaInvocationT invocation,
  184. const SaNameT *compName,
  185. SaAmfPendingOperationFlagsT pendingOperationFlags)
  186. {
  187. printf ("PendingOperationConfirmCallback\n");
  188. }
  189. void PendingOperationExpiredCallback (
  190. const SaNameT *compName,
  191. SaAmfPendingOperationFlagsT pendingOperationFlags)
  192. {
  193. printf ("PendingOperationExpiredCallback\n");
  194. }
  195. #endif
  196. SaAmfCallbacksT amfCallbacks = {
  197. .saAmfCSISetCallback = CSISetCallback,
  198. };
  199. SaAmfCallbacksT amfCallbacks;
  200. SaVersionT version = { 'B', 1, 1 };
  201. static struct sched_param sched_param = {
  202. sched_priority: 99
  203. };
  204. void sigintr_handler (int signum) {
  205. exit (0);
  206. }
  207. int main (int argc, char **argv) {
  208. SaAmfHandleT handle;
  209. int result;
  210. SaSelectionObjectT select_fd;
  211. fd_set read_fds;
  212. SaNameT compName;
  213. extern char *optarg;
  214. extern int optind;
  215. int c;
  216. memset (&compName, 0, sizeof (SaNameT));
  217. signal (SIGINT, sigintr_handler);
  218. result = sched_setscheduler (0, SCHED_RR, &sched_param);
  219. if (result == -1) {
  220. printf ("couldn't set sched priority\n");
  221. }
  222. for (;;){
  223. c = getopt(argc,argv,"h:n:");
  224. if (c==-1) {
  225. break;
  226. }
  227. switch (c) {
  228. case 0 :
  229. break;
  230. case 'h':
  231. health_flag = 0;
  232. sscanf (optarg,"%ud" ,&healthcheck_count);
  233. break;
  234. case 'n':
  235. setSanameT (&compName, optarg);
  236. break;
  237. default :
  238. break;
  239. }
  240. }
  241. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  242. if (result != SA_OK) {
  243. printf ("initialize result is %d\n", result);
  244. exit (1);
  245. }
  246. FD_ZERO (&read_fds);
  247. saAmfSelectionObjectGet (handle, &select_fd);
  248. FD_SET (select_fd, &read_fds);
  249. if (compName.length <= 0) {
  250. setSanameT (&compName, "comp_b_in_su_3");
  251. }
  252. result = saAmfComponentRegister (handle, &compName, NULL);
  253. printf ("register result is %d (should be 1)\n", result);
  254. do {
  255. select (select_fd + 1, &read_fds, 0, 0, 0);
  256. saAmfDispatch (handle, SA_DISPATCH_ALL);
  257. } while (result);
  258. saAmfFinalize (handle);
  259. exit (0);
  260. }