Fix build error.
[openais.git] / test / testamf4.c
blob0f625f0f60cd770ebe7367f2f0c4d6e090c52111
1 /*
2 * Copyright (c) 2002-2003 MontaVista Software, Inc.
4 * All rights reserved.
6 * Author: Steven Dake (sdake@mvista.com)
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:
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.
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.
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>
45 #include "ais_types.h"
46 #include "saAmf.h"
48 SaAmfHandleT handle;
50 SaAmfHealthcheckKeyT key0 = {
51 .key = "key1",
52 .keyLen = 4
54 SaNameT compNameGlobal;
56 void printSaNameT (SaNameT *name)
58 int i;
60 for (i = 0; i < name->length; i++) {
61 printf ("%c", name->value[i]);
65 void setSanameT (SaNameT *name, char *str) {
66 name->length = strlen (str);
67 memcpy (name->value, str, name->length);
70 static int health_flag = -1;
71 static unsigned int healthcheck_count = 0;
72 static unsigned int healthcheck_no = 0;
74 int stop = 0;
75 void HealthcheckCallback (SaInvocationT invocation,
76 const SaNameT *compName,
77 SaAmfHealthcheckKeyT *healthcheckKey)
79 SaErrorT res;
81 healthcheck_no++;
82 printf ("Healthcheck %u for key '%s' for component ",
83 healthcheck_no, healthcheckKey->key);
85 printSaNameT ((SaNameT *)compName);
86 printf ("\n");
87 res = saAmfResponse (handle, invocation, SA_AIS_OK);
88 if (res != SA_OK) {
89 printf ("response res is %d\n", res);
91 if (healthcheck_no == 20) {
92 res = saAmfHealthcheckStop (handle, &compNameGlobal, &key0);
93 stop = 1;
95 printf ("done res = %d\n", res);
98 #ifdef COMPILE_OUT
100 void ComponentTerminateCallback (
101 SaInvocationT invocation,
102 const SaNameT *compName)
104 printf ("ComponentTerminateCallback\n");
107 #endif
108 void CSISetCallback (
109 SaInvocationT invocation,
110 const SaNameT *compName,
111 SaAmfHAStateT haState,
112 SaAmfCSIDescriptorT *csiDescriptor)
114 int res;
115 switch (haState) {
116 case SA_AMF_HA_ACTIVE:
117 printf ("CSISetCallback:");
118 printf ("for CSI '");
119 printSaNameT ((SaNameT *)&csiDescriptor->csiName);
120 printf ("' for component ");
121 printSaNameT ((SaNameT *)compName);
122 printf ("'");
123 printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
124 res = saAmfResponse (handle, invocation, SA_AIS_OK);
125 break;
127 case SA_AMF_HA_STANDBY:
128 printf ("CSISetCallback:");
129 printf ("for CSI '");
130 printSaNameT ((SaNameT *)compName);
131 printf ("' for component ");
132 printSaNameT ((SaNameT *)compName);
133 printf ("'");
134 printf (" requested to enter hastate SA_AMF_STANDBY.\n");
135 saAmfResponse (handle, invocation, SA_AIS_OK);
136 break;
140 #ifdef COMPILE_OUT
141 void CSIRemoveCallback (
142 SaInvocationT invocation,
143 const SaNameT *compName,
144 const SaNameT *csiName,
145 const SaAmfCSIFlagsT *csiFlags)
147 printf ("CSIRemoveCallback for component '");
148 printSaNameT ((SaNameT *)compName);
149 printf ("' in CSI '");
150 printSaNameT ((SaNameT *)csiName);
151 printf ("'\n");
152 saAmfResponse (invocation, SA_OK);
155 void ProtectionGroupTrackCallback (
156 const SaNameT *csiName,
157 SaAmfProtectionGroupNotificationT *notificationBuffer,
158 SaUint32T numberOfItems,
159 SaUint32T numberOfMembers,
160 SaErrorT error)
162 int i;
164 printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
165 printf ("buffer is %p\n", notificationBuffer);
166 for (i = 0; i < numberOfItems; i++) {
167 printf ("component name");
168 printSaNameT (&notificationBuffer[i].member.compName);
169 printf ("\n");
170 printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
171 printf ("\thastate %d\n", notificationBuffer[i].member.haState);
172 printf ("\tchange is %d\n", notificationBuffer[i].change);
177 void ExternalComponentRestartCallback (
178 const SaInvocationT invocation,
179 const SaNameT *externalCompName)
181 printf ("ExternalComponentRestartCallback\n");
184 void ExternalComponentControlCallback (
185 const SaInvocationT invocation,
186 const SaNameT *externalCompName,
187 SaAmfExternalComponentActionT controlAction)
189 printf ("ExternalComponentControlCallback\n");
192 void PendingOperationConfirmCallback (
193 const SaInvocationT invocation,
194 const SaNameT *compName,
195 SaAmfPendingOperationFlagsT pendingOperationFlags)
197 printf ("PendingOperationConfirmCallback\n");
200 void PendingOperationExpiredCallback (
201 const SaNameT *compName,
202 SaAmfPendingOperationFlagsT pendingOperationFlags)
204 printf ("PendingOperationExpiredCallback\n");
206 #endif
208 SaAmfCallbacksT amfCallbacks = {
209 .saAmfHealthcheckCallback = HealthcheckCallback,
210 .saAmfCSISetCallback = CSISetCallback,
213 SaAmfCallbacksT amfCallbacks;
215 SaVersionT version = { 'B', 1, 1 };
217 #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
218 static struct sched_param sched_param = {
219 sched_priority: 99
221 #endif
223 void sigintr_handler (int signum) {
224 exit (0);
227 int main (int argc, char **argv) {
228 int result;
229 SaSelectionObjectT select_fd;
230 fd_set read_fds;
231 extern char *optarg;
232 extern int optind;
233 int c;
235 memset (&compNameGlobal, 0, sizeof (SaNameT));
236 signal (SIGINT, sigintr_handler);
237 #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
238 result = sched_setscheduler (0, SCHED_RR, &sched_param);
239 if (result == -1) {
240 printf ("couldn't set sched priority\n");
242 #endif
244 for (;;){
245 c = getopt(argc,argv,"h:n:");
246 if (c==-1) {
247 break;
249 switch (c) {
250 case 0 :
251 break;
252 case 'h':
253 health_flag = 0;
254 sscanf (optarg,"%ud" ,&healthcheck_count);
255 break;
256 case 'n':
257 setSanameT (&compNameGlobal, optarg);
258 break;
259 default :
260 break;
264 result = saAmfInitialize (&handle, &amfCallbacks, &version);
265 if (result != SA_OK) {
266 printf ("initialize result is %d\n", result);
267 exit (1);
270 FD_ZERO (&read_fds);
271 saAmfSelectionObjectGet (handle, &select_fd);
272 FD_SET (select_fd, &read_fds);
273 if (compNameGlobal.length <= 0) {
274 setSanameT (&compNameGlobal, "comp_b_in_su_2");
277 result = saAmfHealthcheckStart (handle,
278 &compNameGlobal,
279 &key0,
280 SA_AMF_HEALTHCHECK_AMF_INVOKED,
281 SA_AMF_COMPONENT_FAILOVER);
282 printf ("start %d\n", result);
284 result = saAmfHealthcheckStart (handle,
285 &compNameGlobal,
286 &key0,
287 SA_AMF_HEALTHCHECK_AMF_INVOKED,
288 SA_AMF_COMPONENT_FAILOVER);
289 printf ("start %d\n", result);
290 result = saAmfComponentRegister (handle, &compNameGlobal, NULL);
291 printf ("register result is %d (should be 1)\n", result);
293 do {
294 select (select_fd + 1, &read_fds, 0, 0, 0);
295 saAmfDispatch (handle, SA_DISPATCH_ALL);
296 } while (result && stop == 0);
298 sleep (5);
299 result = saAmfHealthcheckStart (handle,
300 &compNameGlobal,
301 &key0,
302 SA_AMF_HEALTHCHECK_AMF_INVOKED,
303 SA_AMF_COMPONENT_FAILOVER);
305 do {
306 select (select_fd + 1, &read_fds, 0, 0, 0);
307 saAmfDispatch (handle, SA_DISPATCH_ALL);
308 } while (result);
310 saAmfFinalize (handle);
312 exit (0);