Allow libconfdb to run standalone (without aisexec)
[openais.git] / test / testcpg.c
blob46ad04974068efbde9c1f5490fcada5e6c307cf4
1 /*
2 * Copyright (c) 2006-2007 Red Hat Inc
4 * All rights reserved.
6 * Author: Patrick Caulfield <pcaulfie@redhat.com>
8 * This software licensed under BSD license, the text of which follows:
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 <string.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/select.h>
44 #include <sys/un.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 #include "saAis.h"
49 #include "cpg.h"
51 static int quit = 0;
52 static int show_ip = 0;
54 void print_cpgname (struct cpg_name *name)
56 int i;
58 for (i = 0; i < name->length; i++) {
59 printf ("%c", name->value[i]);
63 void DeliverCallback (
64 cpg_handle_t handle,
65 struct cpg_name *groupName,
66 uint32_t nodeid,
67 uint32_t pid,
68 void *msg,
69 int msg_len)
71 if (show_ip) {
72 struct in_addr saddr;
73 saddr.s_addr = nodeid;
74 printf("DeliverCallback: message (len=%d)from node/pid %s/%d: '%s'\n",
75 msg_len, inet_ntoa(saddr), pid, (char *)msg);
77 else {
78 printf("DeliverCallback: message (len=%d)from node/pid %d/%d: '%s'\n", msg_len, nodeid, pid, (char *)msg);
82 void ConfchgCallback (
83 cpg_handle_t handle,
84 struct cpg_name *groupName,
85 struct cpg_address *member_list, int member_list_entries,
86 struct cpg_address *left_list, int left_list_entries,
87 struct cpg_address *joined_list, int joined_list_entries)
89 int i;
90 struct in_addr saddr;
92 printf("\nConfchgCallback: group '");
93 print_cpgname(groupName);
94 printf("'\n");
95 for (i=0; i<joined_list_entries; i++) {
96 if (show_ip) {
97 saddr.s_addr = joined_list[i].nodeid;
98 printf("joined node/pid: %s/%d reason: %d\n",
99 inet_ntoa (saddr), joined_list[i].pid,
100 joined_list[i].reason);
102 else {
103 printf("joined node/pid: %d/%d reason: %d\n",
104 joined_list[i].nodeid, joined_list[i].pid,
105 joined_list[i].reason);
109 for (i=0; i<left_list_entries; i++) {
110 if (show_ip) {
111 saddr.s_addr = left_list[i].nodeid;
112 printf("left node/pid: %s/%d reason: %d\n",
113 inet_ntoa (saddr), left_list[i].pid,
114 left_list[i].reason);
116 else {
117 printf("left node/pid: %d/%d reason: %d\n",
118 left_list[i].nodeid, left_list[i].pid,
119 left_list[i].reason);
123 printf("nodes in group now %d\n", member_list_entries);
124 for (i=0; i<member_list_entries; i++) {
125 if (show_ip) {
126 saddr.s_addr = member_list[i].nodeid;
127 printf("node/pid: %s/%d\n",
128 inet_ntoa (saddr), member_list[i].pid);
130 else {
131 printf("node/pid: %d/%d\n",
132 member_list[i].nodeid, member_list[i].pid);
136 /* Is it us??
137 NOTE: in reality we should also check the nodeid */
138 if (left_list_entries && left_list[0].pid == getpid()) {
139 printf("We have left the building\n");
140 quit = 1;
144 cpg_callbacks_t callbacks = {
145 .cpg_deliver_fn = DeliverCallback,
146 .cpg_confchg_fn = ConfchgCallback,
149 void sigintr_handler (int signum) {
150 exit (0);
152 static struct cpg_name group_name;
154 int main (int argc, char *argv[]) {
155 cpg_handle_t handle;
156 fd_set read_fds;
157 int select_fd;
158 int result;
159 const char *options = "i";
160 int opt;
161 unsigned int nodeid;
163 while ( (opt = getopt(argc, argv, options)) != -1 ) {
164 switch (opt) {
165 case 'i':
166 show_ip = 1;
167 break;
171 if (argc > optind) {
172 strcpy(group_name.value, argv[optind]);
173 group_name.length = strlen(argv[optind])+1;
175 else {
176 strcpy(group_name.value, "GROUP");
177 group_name.length = 6;
180 result = cpg_initialize (&handle, &callbacks);
181 if (result != SA_AIS_OK) {
182 printf ("Could not initialize Cluster Process Group API instance error %d\n", result);
183 exit (1);
185 result = cpg_local_get (handle, &nodeid);
186 if (result != SA_AIS_OK) {
187 printf ("Could not get local node id\n");
188 exit (1);
191 printf ("Local node id is %x\n", nodeid);
192 result = cpg_join(handle, &group_name);
193 if (result != SA_AIS_OK) {
194 printf ("Could not join process group, error %d\n", result);
195 exit (1);
198 FD_ZERO (&read_fds);
199 cpg_fd_get(handle, &select_fd);
200 printf ("Type EXIT to finish\n");
201 do {
202 FD_SET (select_fd, &read_fds);
203 FD_SET (STDIN_FILENO, &read_fds);
204 result = select (select_fd + 1, &read_fds, 0, 0, 0);
205 if (result == -1) {
206 perror ("select\n");
208 if (FD_ISSET (STDIN_FILENO, &read_fds)) {
209 char inbuf[132];
210 struct iovec iov;
212 fgets(inbuf, sizeof(inbuf), stdin);
213 if (strncmp(inbuf, "EXIT", 4) == 0) {
214 cpg_leave(handle, &group_name);
216 else {
217 iov.iov_base = inbuf;
218 iov.iov_len = strlen(inbuf)+1;
219 cpg_mcast_joined(handle, CPG_TYPE_AGREED, &iov, 1);
222 if (FD_ISSET (select_fd, &read_fds)) {
223 if (cpg_dispatch (handle, CPG_DISPATCH_ALL) != SA_AIS_OK)
224 exit(1);
226 } while (result && !quit);
229 result = cpg_finalize (handle);
230 printf ("Finalize result is %d (should be 1)\n", result);
231 return (0);