Fix build error.
[openais.git] / test / testevsth.c
blob6d26bc5de34678fcaf936e35e95ebf31fe84e9b6
1 /*
2 * Copyright (c) 2004 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.
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <errno.h>
40 #include <pthread.h>
41 #include "../include/evs.h"
43 char *delivery_string;
45 #define CALLBACKS 200000
46 int callback_count = 0;
47 void evs_deliver_fn (struct in_addr source_addr, void *msg, int msg_len)
49 #ifdef PRINT_OUTPUT
50 char *buf;
51 buf += 100000;
52 printf ("Delivery callback\n");
53 printf ("callback %d '%s' msg '%s'\n", callback_count, delivery_string, buf);
54 #endif
55 callback_count += 1;
56 if (callback_count % 50 == 0) {
57 printf ("Callback %d\n", callback_count);
61 void evs_confchg_fn (
62 struct in_addr *member_list, int member_list_entries,
63 struct in_addr *left_list, int left_list_entries,
64 struct in_addr *joined_list, int joined_list_entries)
66 int i;
68 printf ("CONFIGURATION CHANGE\n");
69 printf ("--------------------\n");
70 printf ("New configuration\n");
71 for (i = 0; i < member_list_entries; i++) {
72 printf ("%s\n", inet_ntoa (member_list[i]));
74 printf ("Members Left:\n");
75 for (i = 0; i < left_list_entries; i++) {
76 printf ("%s\n", inet_ntoa (left_list[i]));
78 printf ("Members Joined:\n");
79 for (i = 0; i < joined_list_entries; i++) {
80 printf ("%s\n", inet_ntoa (joined_list[i]));
84 evs_callbacks_t callbacks = {
85 evs_deliver_fn,
86 evs_confchg_fn
89 struct evs_group groups[3] = {
90 { "key1" },
91 { "key2" },
92 { "key3" }
95 char buffer[1000];
96 struct iovec iov = {
97 .iov_base = buffer,
98 .iov_len = sizeof (buffer)
101 void *th_dispatch (void *arg)
103 evs_error_t result;
104 evs_handle_t handle = *(evs_handle_t *)arg;
106 printf ("THREAD DISPATCH starting.\n");
107 result = evs_dispatch (handle, EVS_DISPATCH_BLOCKING);
108 printf ("THREAD DISPATCH return result is %d\n", result);
109 return (0);
112 static struct sched_param sched_param = {
113 sched_priority: 99
116 int main (void)
118 evs_handle_t handle;
119 evs_error_t result;
120 int i = 0;
121 pthread_t dispatch_thread;
122 pthread_attr_t dispatch_thread_attribute;
124 result = evs_initialize (&handle, &callbacks);
125 if (result != EVS_OK) {
126 printf ("Couldn't initialize EVS service %d\n", result);
127 exit (0);
130 pthread_attr_init (&dispatch_thread_attribute);
131 pthread_attr_setschedpolicy (&dispatch_thread_attribute, SCHED_FIFO);
132 pthread_attr_setschedparam (&dispatch_thread_attribute, &sched_param);
134 pthread_create (&dispatch_thread, NULL, th_dispatch, &handle);
136 printf ("Init result %d\n", result);
137 result = evs_join (handle, groups, 3);
138 printf ("Join result %d\n", result);
139 result = evs_leave (handle, &groups[0], 1);
140 printf ("Leave result %d\n", result);
141 delivery_string = "evs_mcast_joined";
144 * Demonstrate evs_mcast_joined
146 for (i = 0; i < CALLBACKS/2; i++) {
147 sprintf (buffer, "evs_mcast_joined: This is message %d", i);
148 try_again_one:
149 result = evs_mcast_joined (handle, EVS_TYPE_AGREED, &iov, 1);
150 if (result == EVS_ERR_TRY_AGAIN) {
151 goto try_again_one;
152 } else
153 if (result != EVS_OK) {
154 printf ("Got error result, exiting %d\n", result);
155 exit (1);
160 * Demonstrate evs_mcast_joined
162 delivery_string = "evs_mcast_groups";
163 for (i = 0; i < CALLBACKS/2; i++) {
164 sprintf (buffer, "evs_mcast_groups: This is message %d", i);
165 try_again_two:
166 result = evs_mcast_groups (handle, EVS_TYPE_AGREED,
167 &groups[1], 1, &iov, 1);
168 if (result == EVS_ERR_TRY_AGAIN) {
169 goto try_again_two;
174 * Wait until all callbacks have been executed by dispatch thread
176 for (;;) {
177 if (callback_count == CALLBACKS) {
178 printf ("Test completed successfully\n");
179 exit (0);
182 return (0);