~ubuntu-branches/ubuntu/precise/corosync/precise-proposed

« back to all changes in this revision

Viewing changes to test/testcpgzc.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2009-08-21 09:29:56 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090821092956-w9qxxxx3zeoh8dem
Tags: 1.0.0-4ubuntu2
* debian/control:
  - 'Ubuntu Developers' instead of 'Ubuntu Core Developers'
    as maintainer
  - Bump debhelper dependecy to 7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2006-2009 Red Hat Inc
 
3
 *
 
4
 * All rights reserved.
 
5
 *
 
6
 * Author: Christine Caulfield <ccaulfie@redhat.com>
 
7
 *
 
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:
 
12
 *
 
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.
 
21
 *
 
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.
 
33
 */
 
34
 
 
35
#include <config.h>
 
36
 
 
37
#include <stdio.h>
 
38
#include <stdlib.h>
 
39
#include <errno.h>
 
40
#include <unistd.h>
 
41
#include <string.h>
 
42
#include <sys/types.h>
 
43
#include <sys/socket.h>
 
44
#include <sys/select.h>
 
45
#include <sys/un.h>
 
46
#include <netinet/in.h>
 
47
#include <arpa/inet.h>
 
48
 
 
49
#include <corosync/corotypes.h>
 
50
#include <corosync/cpg.h>
 
51
 
 
52
static int quit = 0;
 
53
static int show_ip = 0;
 
54
 
 
55
static void print_cpgname (const struct cpg_name *name)
 
56
{
 
57
        int i;
 
58
 
 
59
        for (i = 0; i < name->length; i++) {
 
60
                printf ("%c", name->value[i]);
 
61
        }
 
62
}
 
63
 
 
64
static void DeliverCallback (
 
65
        cpg_handle_t handle,
 
66
        const struct cpg_name *groupName,
 
67
        uint32_t nodeid,
 
68
        uint32_t pid,
 
69
        void *msg,
 
70
        size_t msg_len)
 
71
{
 
72
        if (show_ip) {
 
73
                struct in_addr saddr;
 
74
                saddr.s_addr = nodeid;
 
75
                printf("DeliverCallback: message (len=%lu)from node/pid %s/%d: '%s'\n",
 
76
                       (unsigned long int) msg_len,
 
77
                       inet_ntoa(saddr), pid, (const char *)msg);
 
78
        }
 
79
        else {
 
80
                printf("DeliverCallback: message (len=%lu)from node/pid %d/%d: '%s'\n",
 
81
                       (unsigned long int) msg_len, nodeid, pid,
 
82
                       (const char *)msg);
 
83
        }
 
84
}
 
85
 
 
86
static void ConfchgCallback (
 
87
        cpg_handle_t handle,
 
88
        const struct cpg_name *groupName,
 
89
        const struct cpg_address *member_list, size_t member_list_entries,
 
90
        const struct cpg_address *left_list, size_t left_list_entries,
 
91
        const struct cpg_address *joined_list, size_t joined_list_entries)
 
92
{
 
93
        int i;
 
94
        struct in_addr saddr;
 
95
 
 
96
        printf("\nConfchgCallback: group '");
 
97
        print_cpgname(groupName);
 
98
        printf("'\n");
 
99
        for (i=0; i<joined_list_entries; i++) {
 
100
                if (show_ip) {
 
101
                        saddr.s_addr = joined_list[i].nodeid;
 
102
                        printf("joined node/pid: %s/%d reason: %d\n",
 
103
                               inet_ntoa (saddr), joined_list[i].pid,
 
104
                               joined_list[i].reason);
 
105
                }
 
106
                else {
 
107
                        printf("joined node/pid: %d/%d reason: %d\n",
 
108
                               joined_list[i].nodeid, joined_list[i].pid,
 
109
                               joined_list[i].reason);
 
110
                }
 
111
        }
 
112
 
 
113
        for (i=0; i<left_list_entries; i++) {
 
114
                if (show_ip) {
 
115
                        saddr.s_addr = left_list[i].nodeid;
 
116
                        printf("left node/pid: %s/%d reason: %d\n",
 
117
                               inet_ntoa (saddr), left_list[i].pid,
 
118
                               left_list[i].reason);
 
119
                }
 
120
                else {
 
121
                        printf("left node/pid: %d/%d reason: %d\n",
 
122
                               left_list[i].nodeid, left_list[i].pid,
 
123
                               left_list[i].reason);
 
124
                }
 
125
        }
 
126
 
 
127
        printf("nodes in group now %lu\n",
 
128
               (unsigned long int) member_list_entries);
 
129
        for (i=0; i<member_list_entries; i++) {
 
130
                if (show_ip) {
 
131
                        saddr.s_addr = member_list[i].nodeid;
 
132
                        printf("node/pid: %s/%d\n",
 
133
                               inet_ntoa (saddr), member_list[i].pid);
 
134
                }
 
135
                else {
 
136
                        printf("node/pid: %d/%d\n",
 
137
                               member_list[i].nodeid, member_list[i].pid);
 
138
                }
 
139
        }
 
140
 
 
141
        /* Is it us??
 
142
           NOTE: in reality we should also check the nodeid */
 
143
        if (left_list_entries && left_list[0].pid == getpid()) {
 
144
                printf("We have left the building\n");
 
145
                quit = 1;
 
146
        }
 
147
}
 
148
 
 
149
static cpg_callbacks_t callbacks = {
 
150
        .cpg_deliver_fn =            DeliverCallback,
 
151
        .cpg_confchg_fn =            ConfchgCallback,
 
152
};
 
153
 
 
154
static void sigintr_handler (int signum) __attribute__((__noreturn__));
 
155
static void sigintr_handler (int signum) {
 
156
        exit (0);
 
157
}
 
158
static struct cpg_name group_name;
 
159
 
 
160
int main (int argc, char *argv[]) {
 
161
        cpg_handle_t handle;
 
162
        fd_set read_fds;
 
163
        int select_fd;
 
164
        int result;
 
165
        const char *options = "i";
 
166
        int opt;
 
167
        unsigned int nodeid;
 
168
        char *fgets_res;
 
169
        void *buffer;
 
170
 
 
171
        while ( (opt = getopt(argc, argv, options)) != -1 ) {
 
172
                switch (opt) {
 
173
                case 'i':
 
174
                        show_ip = 1;
 
175
                        break;
 
176
                }
 
177
        }
 
178
 
 
179
        if (argc > optind) {
 
180
                strcpy(group_name.value, argv[optind]);
 
181
                group_name.length = strlen(argv[optind])+1;
 
182
        }
 
183
        else {
 
184
                strcpy(group_name.value, "GROUP");
 
185
                group_name.length = 6;
 
186
        }
 
187
 
 
188
        result = cpg_initialize (&handle, &callbacks);
 
189
        if (result != CS_OK) {
 
190
                printf ("Could not initialize Cluster Process Group API instance error %d\n", result);
 
191
                exit (1);
 
192
        }
 
193
        cpg_zcb_alloc (handle, 8192, &buffer);
 
194
        cpg_zcb_free (handle, buffer);
 
195
        cpg_zcb_alloc (handle, 8192, &buffer);
 
196
 
 
197
        result = cpg_local_get (handle, &nodeid);
 
198
        if (result != CS_OK) {
 
199
                printf ("Could not get local node id\n");
 
200
                exit (1);
 
201
        }
 
202
 
 
203
        printf ("Local node id is %x\n", nodeid);
 
204
        result = cpg_join(handle, &group_name);
 
205
        if (result != CS_OK) {
 
206
                printf ("Could not join process group, error %d\n", result);
 
207
                exit (1);
 
208
        }
 
209
 
 
210
        FD_ZERO (&read_fds);
 
211
        cpg_fd_get(handle, &select_fd);
 
212
        printf ("Type EXIT to finish\n");
 
213
        do {
 
214
                FD_SET (select_fd, &read_fds);
 
215
                FD_SET (STDIN_FILENO, &read_fds);
 
216
                result = select (select_fd + 1, &read_fds, 0, 0, 0);
 
217
                if (result == -1) {
 
218
                        perror ("select\n");
 
219
                }
 
220
                if (FD_ISSET (STDIN_FILENO, &read_fds)) {
 
221
                        fgets_res = fgets(buffer, sizeof(buffer), stdin);
 
222
                        if (fgets_res == NULL) {
 
223
                                cpg_leave(handle, &group_name);
 
224
                        }
 
225
                        if (strncmp(buffer, "EXIT", 4) == 0) {
 
226
                                cpg_leave(handle, &group_name);
 
227
                        }
 
228
                        else {
 
229
                                cpg_zcb_mcast_joined (handle, CPG_TYPE_AGREED,
 
230
                                        buffer, strlen (buffer) + 1);
 
231
                        }
 
232
                }
 
233
                if (FD_ISSET (select_fd, &read_fds)) {
 
234
                        if (cpg_dispatch (handle, CS_DISPATCH_ALL) != CS_OK)
 
235
                                exit(1);
 
236
                }
 
237
        } while (result && !quit);
 
238
 
 
239
 
 
240
        result = cpg_finalize (handle);
 
241
        printf ("Finalize  result is %d (should be 1)\n", result);
 
242
        return (0);
 
243
}