Fix build error.
[openais.git] / test / testamf2.c
blobf0f2b73d6ddb3fedf1124676b8fb55346f57bf70
1 /*
2 * Copyright (c) 2002-2003 MontaVista Software, Inc.
3 * Copyright (c) 2006 Sun Microsystems, Inc.
5 * All rights reserved.
7 * Author: Steven Dake (sdake@mvista.com)
9 * This software licensed under BSD license, the text of which follows:
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
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.
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.
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <sched.h>
46 #include "saAis.h"
47 #include "saAmf.h"
49 SaAmfHandleT handle;
51 SaAmfHealthcheckKeyT key0 = {
52 .key = "key1",
53 .keyLen = 4
55 SaNameT compNameGlobal;
57 void printSaNameT (SaNameT *name)
59 int i;
61 for (i = 0; i < name->length; i++) {
62 printf ("%c", name->value[i]);
66 void setSanameT (SaNameT *name, char *str) {
67 name->length = strlen (str);
68 memcpy (name->value, str, name->length);
71 static int health_flag = -1;
72 static unsigned int healthcheck_count = 0;
73 static unsigned int healthcheck_no = 0;
75 int stop = 0;
77 void HealthcheckCallback (SaInvocationT invocation,
78 const SaNameT *compName,
79 SaAmfHealthcheckKeyT *healthcheckKey)
81 SaErrorT res;
83 healthcheck_no++;
85 printf ("Healthcheck %u for key '%s' for component ",
86 healthcheck_no, healthcheckKey->key);
88 printSaNameT ((SaNameT *)compName);
89 printf ("\n");
91 res = saAmfResponse (handle, invocation, SA_AIS_OK);
92 if (healthcheck_no == 3) {
93 res = saAmfHealthcheckStop (handle, &compNameGlobal, &key0);
94 stop = 1;
98 void ComponentTerminateCallback (
99 SaInvocationT invocation,
100 const SaNameT *compName)
102 printf ("ComponentTerminateCallback\n");
103 saAmfResponse (handle, invocation, SA_AIS_OK);
104 exit (0);
107 void CSISetCallback (
108 SaInvocationT invocation,
109 const SaNameT *compName,
110 SaAmfHAStateT haState,
111 SaAmfCSIDescriptorT *csiDescriptor)
113 int res;
114 switch (haState) {
115 case SA_AMF_HA_ACTIVE:
116 printf ("CSISetCallback:");
117 printf ("for CSI '");
118 printSaNameT ((SaNameT *)&csiDescriptor->csiName);
119 printf ("' for component ");
120 printSaNameT ((SaNameT *)compName);
121 printf ("'");
122 printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
123 res = saAmfResponse (handle, invocation, SA_AIS_OK);
124 break;
126 case SA_AMF_HA_STANDBY:
127 printf ("CSISetCallback:");
128 printf ("for CSI '");
129 printSaNameT ((SaNameT *)compName);
130 printf ("' for component ");
131 printSaNameT ((SaNameT *)compName);
132 printf ("'");
133 printf (" requested to enter hastate SA_AMF_STANDBY.\n");
134 saAmfResponse (handle, invocation, SA_AIS_OK);
135 break;
139 void CSIRemoveCallback (
140 SaInvocationT invocation,
141 const SaNameT *compName,
142 const SaNameT *csiName,
143 const SaAmfCSIFlagsT *csiFlags)
145 int res;
147 printf ("CSIRemoveCallback for component '");
148 printSaNameT ((SaNameT *)compName);
149 printf ("' in CSI '");
150 printSaNameT ((SaNameT *)csiName);
151 printf ("'\n");
152 res = saAmfResponse (handle, invocation, SA_AIS_OK);
155 #ifdef COMPILE_OUT
156 void ProtectionGroupTrackCallback (
157 const SaNameT *csiName,
158 SaAmfProtectionGroupNotificationT *notificationBuffer,
159 SaUint32T numberOfItems,
160 SaUint32T numberOfMembers,
161 SaErrorT error)
163 int i;
165 printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
166 printf ("buffer is %p\n", notificationBuffer);
167 for (i = 0; i < numberOfItems; i++) {
168 printf ("component name");
169 printSaNameT (&notificationBuffer[i].member.compName);
170 printf ("\n");
171 printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
172 printf ("\thastate %d\n", notificationBuffer[i].member.haState);
173 printf ("\tchange is %d\n", notificationBuffer[i].change);
178 #endif
180 SaAmfCallbacksT amfCallbacks = {
181 .saAmfHealthcheckCallback = HealthcheckCallback,
182 .saAmfComponentTerminateCallback = ComponentTerminateCallback,
183 .saAmfCSISetCallback = CSISetCallback,
184 .saAmfCSIRemoveCallback = CSIRemoveCallback,
187 SaAmfCallbacksT amfCallbacks;
189 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
195 #endif
197 void sigintr_handler (int signum) {
198 exit (0);
201 int main (int argc, char **argv) {
202 int result;
203 SaSelectionObjectT select_fd;
204 fd_set read_fds;
205 extern char *optarg;
206 extern int optind;
207 int c;
209 printf ("testamf2 pid %d\n", getpid());
210 memset (&compNameGlobal, 0, sizeof (SaNameT));
211 signal (SIGINT, sigintr_handler);
212 #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
213 result = sched_setscheduler (0, SCHED_RR, &sched_param);
214 if (result == -1) {
215 printf ("couldn't set sched priority\n");
217 #endif
219 for (;;){
220 c = getopt(argc,argv,"h:n:");
221 if (c==-1) {
222 break;
224 switch (c) {
225 case 0 :
226 break;
227 case 'h':
228 health_flag = 0;
229 sscanf (optarg,"%ud" ,&healthcheck_count);
230 break;
231 case 'n':
232 setSanameT (&compNameGlobal, optarg);
233 break;
234 default :
235 break;
239 result = saAmfInitialize (&handle, &amfCallbacks, &version);
240 if (result != SA_AIS_OK) {
241 printf ("initialize result is %d\n", result);
242 exit (1);
245 FD_ZERO (&read_fds);
246 saAmfSelectionObjectGet (handle, &select_fd);
247 FD_SET (select_fd, &read_fds);
248 if (compNameGlobal.length <= 0) {
249 setSanameT (&compNameGlobal, "comp_b_in_su_1");
252 result = saAmfHealthcheckStart (handle,
253 &compNameGlobal,
254 &key0,
255 SA_AMF_HEALTHCHECK_AMF_INVOKED,
256 SA_AMF_COMPONENT_FAILOVER);
257 printf ("start %d\n", result);
259 result = saAmfHealthcheckStart (handle,
260 &compNameGlobal,
261 &key0,
262 SA_AMF_HEALTHCHECK_AMF_INVOKED,
263 SA_AMF_COMPONENT_FAILOVER);
264 printf ("start %d\n", result);
265 result = saAmfComponentRegister (handle, &compNameGlobal, NULL);
266 printf ("register result is %d (should be 1)\n", result);
268 do {
269 select (select_fd + 1, &read_fds, 0, 0, 0);
270 saAmfDispatch (handle, SA_DISPATCH_ALL);
271 } while (result && stop == 0);
273 printf ("healthchecks stopped for 5 seconds\n");
274 sleep (5);
275 result = saAmfHealthcheckStart (handle,
276 &compNameGlobal,
277 &key0,
278 SA_AMF_HEALTHCHECK_AMF_INVOKED,
279 SA_AMF_COMPONENT_FAILOVER);
281 do {
282 select (select_fd + 1, &read_fds, 0, 0, 0);
283 saAmfDispatch (handle, SA_DISPATCH_ALL);
284 } while (result);
286 saAmfFinalize (handle);
288 exit (0);