testamf2.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "saAis.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. /*
  74. printf ("Healthcheck %u for key '%s' for component ",
  75. healthcheck_no, healthcheckKey->key);
  76. printSaNameT ((SaNameT *)compName);
  77. printf ("\n");
  78. */
  79. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  80. if (healthcheck_no == 3) {
  81. res = saAmfHealthcheckStop (handle, &compNameGlobal, &key0);
  82. stop = 1;
  83. }
  84. }
  85. void ComponentTerminateCallback (
  86. SaInvocationT invocation,
  87. const SaNameT *compName)
  88. {
  89. printf ("ComponentTerminateCallback\n");
  90. saAmfResponse (handle, invocation, SA_AIS_OK);
  91. exit (0);
  92. }
  93. void CSISetCallback (
  94. SaInvocationT invocation,
  95. const SaNameT *compName,
  96. SaAmfHAStateT haState,
  97. SaAmfCSIDescriptorT *csiDescriptor)
  98. {
  99. int res;
  100. switch (haState) {
  101. case SA_AMF_HA_ACTIVE:
  102. printf ("CSISetCallback:");
  103. printf ("for CSI '");
  104. printSaNameT ((SaNameT *)&csiDescriptor->csiName);
  105. printf ("' for component ");
  106. printSaNameT ((SaNameT *)compName);
  107. printf ("'");
  108. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  109. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  110. break;
  111. case SA_AMF_HA_STANDBY:
  112. printf ("CSISetCallback:");
  113. printf ("for CSI '");
  114. printSaNameT ((SaNameT *)compName);
  115. printf ("' for component ");
  116. printSaNameT ((SaNameT *)compName);
  117. printf ("'");
  118. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  119. saAmfResponse (handle, invocation, SA_AIS_OK);
  120. break;
  121. }
  122. }
  123. void CSIRemoveCallback (
  124. SaInvocationT invocation,
  125. const SaNameT *compName,
  126. const SaNameT *csiName,
  127. const SaAmfCSIFlagsT *csiFlags)
  128. {
  129. int res;
  130. printf ("CSIRemoveCallback for component '");
  131. printSaNameT ((SaNameT *)compName);
  132. printf ("' in CSI '");
  133. printSaNameT ((SaNameT *)csiName);
  134. printf ("'\n");
  135. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  136. }
  137. #ifdef COMPILE_OUT
  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. #endif
  158. SaAmfCallbacksT amfCallbacks = {
  159. .saAmfHealthcheckCallback = HealthcheckCallback,
  160. .saAmfComponentTerminateCallback = ComponentTerminateCallback,
  161. .saAmfCSISetCallback = CSISetCallback,
  162. .saAmfCSIRemoveCallback = CSIRemoveCallback,
  163. };
  164. SaAmfCallbacksT amfCallbacks;
  165. SaVersionT version = { 'B', 1, 1 };
  166. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  167. static struct sched_param sched_param = {
  168. sched_priority: 99
  169. };
  170. #endif
  171. void sigintr_handler (int signum) {
  172. exit (0);
  173. }
  174. int main (int argc, char **argv) {
  175. int result;
  176. SaSelectionObjectT select_fd;
  177. fd_set read_fds;
  178. extern char *optarg;
  179. extern int optind;
  180. int c;
  181. printf ("testamf2 pid %d\n", getpid());
  182. memset (&compNameGlobal, 0, sizeof (SaNameT));
  183. signal (SIGINT, sigintr_handler);
  184. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  185. result = sched_setscheduler (0, SCHED_RR, &sched_param);
  186. if (result == -1) {
  187. printf ("couldn't set sched priority\n");
  188. }
  189. #endif
  190. for (;;){
  191. c = getopt(argc,argv,"h:n:");
  192. if (c==-1) {
  193. break;
  194. }
  195. switch (c) {
  196. case 0 :
  197. break;
  198. case 'h':
  199. health_flag = 0;
  200. sscanf (optarg,"%ud" ,&healthcheck_count);
  201. break;
  202. case 'n':
  203. setSanameT (&compNameGlobal, optarg);
  204. break;
  205. default :
  206. break;
  207. }
  208. }
  209. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  210. if (result != SA_AIS_OK) {
  211. printf ("initialize result is %d\n", result);
  212. exit (1);
  213. }
  214. FD_ZERO (&read_fds);
  215. saAmfSelectionObjectGet (handle, &select_fd);
  216. FD_SET (select_fd, &read_fds);
  217. if (compNameGlobal.length <= 0) {
  218. setSanameT (&compNameGlobal, "comp_b_in_su_1");
  219. }
  220. result = saAmfHealthcheckStart (handle,
  221. &compNameGlobal,
  222. &key0,
  223. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  224. SA_AMF_COMPONENT_FAILOVER);
  225. printf ("start %d\n", result);
  226. result = saAmfHealthcheckStart (handle,
  227. &compNameGlobal,
  228. &key0,
  229. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  230. SA_AMF_COMPONENT_FAILOVER);
  231. printf ("start %d\n", result);
  232. result = saAmfComponentRegister (handle, &compNameGlobal, NULL);
  233. printf ("register result is %d (should be 1)\n", result);
  234. do {
  235. select (select_fd + 1, &read_fds, 0, 0, 0);
  236. saAmfDispatch (handle, SA_DISPATCH_ALL);
  237. } while (result && stop == 0);
  238. printf ("healthchecks stopped for 5 seconds\n");
  239. sleep (5);
  240. result = saAmfHealthcheckStart (handle,
  241. &compNameGlobal,
  242. &key0,
  243. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  244. SA_AMF_COMPONENT_FAILOVER);
  245. do {
  246. select (select_fd + 1, &read_fds, 0, 0, 0);
  247. saAmfDispatch (handle, SA_DISPATCH_ALL);
  248. } while (result);
  249. saAmfFinalize (handle);
  250. exit (0);
  251. }