testamf6.c 7.7 KB

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