testamf3.c 8.1 KB

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