testamf3.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. SaAmfHandleT handle;
  46. SaAmfHealthcheckKeyT key0 = {
  47. .key = "key1",
  48. .keyLen = 4
  49. };
  50. SaNameT compNameGlobal;
  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. static int health_flag = -1;
  63. static unsigned int healthcheck_count = 0;
  64. static unsigned int healthcheck_no = 0;
  65. int stop = 0;
  66. void HealthcheckCallback (SaInvocationT invocation,
  67. const SaNameT *compName,
  68. SaAmfHealthcheckKeyT *healthcheckKey)
  69. {
  70. SaErrorT res;
  71. healthcheck_no++;
  72. printf ("Healthcheck %u for key '%s' for component ",
  73. healthcheck_no, healthcheckKey->key);
  74. printSaNameT ((SaNameT *)compName);
  75. printf ("\n");
  76. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  77. if (res != SA_OK) {
  78. printf ("response res is %d\n", res);
  79. }
  80. if (healthcheck_no == 20) {
  81. res = saAmfHealthcheckStop (handle, &compNameGlobal, &key0);
  82. stop = 1;
  83. }
  84. printf ("done res = %d\n", res);
  85. }
  86. #ifdef COMPILE_OUT
  87. void ComponentTerminateCallback (
  88. SaInvocationT invocation,
  89. const SaNameT *compName)
  90. {
  91. printf ("ComponentTerminateCallback\n");
  92. }
  93. #endif
  94. void CSISetCallback (
  95. SaInvocationT invocation,
  96. const SaNameT *compName,
  97. SaAmfHAStateT haState,
  98. SaAmfCSIDescriptorT *csiDescriptor)
  99. {
  100. int res;
  101. switch (haState) {
  102. case SA_AMF_HA_ACTIVE:
  103. printf ("CSISetCallback:");
  104. printf ("for CSI '");
  105. printSaNameT ((SaNameT *)&csiDescriptor->csiName);
  106. printf ("' for component ");
  107. printSaNameT ((SaNameT *)compName);
  108. printf ("'");
  109. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  110. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  111. break;
  112. case SA_AMF_HA_STANDBY:
  113. printf ("CSISetCallback:");
  114. printf ("for CSI '");
  115. printSaNameT ((SaNameT *)compName);
  116. printf ("' for component ");
  117. printSaNameT ((SaNameT *)compName);
  118. printf ("'");
  119. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  120. saAmfResponse (handle, invocation, SA_AIS_OK);
  121. break;
  122. }
  123. }
  124. #ifdef COMPILE_OUT
  125. void CSIRemoveCallback (
  126. SaInvocationT invocation,
  127. const SaNameT *compName,
  128. const SaNameT *csiName,
  129. const SaAmfCSIFlagsT *csiFlags)
  130. {
  131. printf ("CSIRemoveCallback for component '");
  132. printSaNameT ((SaNameT *)compName);
  133. printf ("' in CSI '");
  134. printSaNameT ((SaNameT *)csiName);
  135. printf ("'\n");
  136. saAmfResponse (invocation, SA_OK);
  137. }
  138. void ProtectionGroupTrackCallback (
  139. const SaNameT *csiName,
  140. SaAmfProtectionGroupNotificationT *notificationBuffer,
  141. SaUint32T numberOfItems,
  142. SaUint32T numberOfMembers,
  143. SaErrorT error)
  144. {
  145. int i;
  146. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  147. printf ("buffer is %p\n", notificationBuffer);
  148. for (i = 0; i < numberOfItems; i++) {
  149. printf ("component name");
  150. printSaNameT (&notificationBuffer[i].member.compName);
  151. printf ("\n");
  152. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  153. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  154. printf ("\tchange is %d\n", notificationBuffer[i].change);
  155. }
  156. }
  157. void ExternalComponentRestartCallback (
  158. const SaInvocationT invocation,
  159. const SaNameT *externalCompName)
  160. {
  161. printf ("ExternalComponentRestartCallback\n");
  162. }
  163. void ExternalComponentControlCallback (
  164. const SaInvocationT invocation,
  165. const SaNameT *externalCompName,
  166. SaAmfExternalComponentActionT controlAction)
  167. {
  168. printf ("ExternalComponentControlCallback\n");
  169. }
  170. void PendingOperationConfirmCallback (
  171. const SaInvocationT invocation,
  172. const SaNameT *compName,
  173. SaAmfPendingOperationFlagsT pendingOperationFlags)
  174. {
  175. printf ("PendingOperationConfirmCallback\n");
  176. }
  177. void PendingOperationExpiredCallback (
  178. const SaNameT *compName,
  179. SaAmfPendingOperationFlagsT pendingOperationFlags)
  180. {
  181. printf ("PendingOperationExpiredCallback\n");
  182. }
  183. #endif
  184. SaAmfCallbacksT amfCallbacks = {
  185. .saAmfHealthcheckCallback = HealthcheckCallback,
  186. .saAmfCSISetCallback = CSISetCallback,
  187. };
  188. SaAmfCallbacksT amfCallbacks;
  189. SaVersionT version = { 'B', 1, 1 };
  190. static struct sched_param sched_param = {
  191. sched_priority: 99
  192. };
  193. void sigintr_handler (int signum) {
  194. exit (0);
  195. }
  196. int main (int argc, char **argv) {
  197. int result;
  198. SaSelectionObjectT select_fd;
  199. fd_set read_fds;
  200. extern char *optarg;
  201. extern int optind;
  202. int c;
  203. memset (&compNameGlobal, 0, sizeof (SaNameT));
  204. signal (SIGINT, sigintr_handler);
  205. result = sched_setscheduler (0, SCHED_RR, &sched_param);
  206. if (result == -1) {
  207. printf ("couldn't set sched priority\n");
  208. }
  209. for (;;){
  210. c = getopt(argc,argv,"h:n:");
  211. if (c==-1) {
  212. break;
  213. }
  214. switch (c) {
  215. case 0 :
  216. break;
  217. case 'h':
  218. health_flag = 0;
  219. sscanf (optarg,"%ud" ,&healthcheck_count);
  220. break;
  221. case 'n':
  222. setSanameT (&compNameGlobal, optarg);
  223. break;
  224. default :
  225. break;
  226. }
  227. }
  228. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  229. if (result != SA_OK) {
  230. printf ("initialize result is %d\n", result);
  231. exit (1);
  232. }
  233. FD_ZERO (&read_fds);
  234. saAmfSelectionObjectGet (handle, &select_fd);
  235. FD_SET (select_fd, &read_fds);
  236. if (compNameGlobal.length <= 0) {
  237. setSanameT (&compNameGlobal, "comp_a_in_su_2");
  238. }
  239. result = saAmfHealthcheckStart (handle,
  240. &compNameGlobal,
  241. &key0,
  242. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  243. SA_AMF_COMPONENT_FAILOVER);
  244. printf ("start %d\n", result);
  245. result = saAmfHealthcheckStart (handle,
  246. &compNameGlobal,
  247. &key0,
  248. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  249. SA_AMF_COMPONENT_FAILOVER);
  250. printf ("start %d\n", result);
  251. result = saAmfComponentRegister (handle, &compNameGlobal, 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 && stop == 0);
  257. sleep (5);
  258. result = saAmfHealthcheckStart (handle,
  259. &compNameGlobal,
  260. &key0,
  261. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  262. SA_AMF_COMPONENT_FAILOVER);
  263. do {
  264. select (select_fd + 1, &read_fds, 0, 0, 0);
  265. saAmfDispatch (handle, SA_DISPATCH_ALL);
  266. } while (result);
  267. saAmfFinalize (handle);
  268. exit (0);
  269. }