]>
Commit | Line | Data |
---|---|---|
fe000966 DM |
1 | /* |
2 | Copyright (C) 2009 Proxmox Server Solutions GmbH | |
3 | ||
4 | Copyright: This program is under GNU GPL, the GNU General Public License. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; version 2 dated June, 1991. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
18 | 02111-1307, USA. | |
19 | ||
20 | Author: Dietmar Maurer <dietmar@proxmox.com> | |
21 | ||
22 | */ | |
23 | ||
24 | #include <stdlib.h> | |
25 | #include <stdio.h> | |
26 | #include <string.h> | |
27 | #include <unistd.h> | |
28 | ||
29 | #include <corosync/corotypes.h> | |
30 | #include <corosync/cpg.h> | |
31 | ||
32 | static int cpg_mode_leave; | |
33 | ||
34 | static void my_cpg_deliver_callback ( | |
35 | cpg_handle_t handle, | |
36 | const struct cpg_name *groupName, | |
37 | uint32_t nodeid, | |
38 | uint32_t pid, | |
39 | void *msg, | |
40 | size_t msg_len) | |
41 | { | |
42 | printf("got message form %d/%d\n", nodeid, pid); | |
43 | ||
44 | cpg_mode_leave = 1; | |
45 | ||
46 | return; | |
47 | } | |
48 | ||
49 | static void my_cpg_confchg_callback ( | |
50 | cpg_handle_t handle, | |
51 | const struct cpg_name *groupName, | |
52 | const struct cpg_address *member_list, size_t member_list_entries, | |
53 | const struct cpg_address *left_list, size_t left_list_entries, | |
54 | const struct cpg_address *joined_list, size_t joined_list_entries) | |
55 | { | |
56 | int i; | |
57 | ||
58 | printf("cpg_confchg_callback %ld joined, %ld left, %ld members\n", | |
59 | joined_list_entries, left_list_entries, member_list_entries); | |
60 | ||
61 | for (i = 0; i < member_list_entries; i++) { | |
62 | printf("cpg member %d/%d\n", member_list[i].nodeid, member_list[i].pid); | |
63 | } | |
64 | ||
65 | /* send update message */ | |
a8bf13d0 | 66 | char *inbuf = "This is just a test message\n"; |
fe000966 DM |
67 | struct iovec iov; |
68 | iov.iov_base = inbuf; | |
69 | iov.iov_len = strlen(inbuf)+1; | |
70 | ||
a8bf13d0 | 71 | cs_error_t result; |
fe000966 DM |
72 | loop: |
73 | result = cpg_mcast_joined(handle, CPG_TYPE_AGREED, &iov, 1); | |
a8bf13d0 | 74 | if (result == CS_ERR_TRY_AGAIN) { |
fe000966 DM |
75 | usleep(1000); |
76 | printf("cpg_send_message retry"); | |
77 | goto loop; | |
78 | } | |
79 | ||
80 | if (result != CS_OK) | |
81 | printf("cpg_send_message failed: %d\n", result); | |
82 | ||
83 | } | |
84 | ||
85 | static cpg_callbacks_t callbacks = { | |
86 | .cpg_deliver_fn = my_cpg_deliver_callback, | |
87 | .cpg_confchg_fn = my_cpg_confchg_callback, | |
88 | }; | |
89 | ||
90 | ||
91 | int main(int argc, char *argv[]) | |
92 | { | |
93 | struct cpg_name group_name; | |
94 | char *gn = "TESTGROUP"; | |
95 | strcpy(group_name.value, gn); | |
96 | group_name.length = strlen(gn) + 1; | |
97 | ||
98 | cs_error_t result; | |
99 | cpg_handle_t handle; | |
100 | ||
101 | start: | |
102 | printf("starting cpgtest\n"); | |
103 | ||
104 | cpg_mode_leave = 0; | |
105 | ||
106 | handle = 0; | |
107 | ||
108 | printf("calling cpg_initialize\n"); | |
109 | result = cpg_initialize(&handle, &callbacks); | |
110 | if (result != CS_OK) { | |
111 | printf("cpg_initialize failed: %d\n", result); | |
112 | goto retry; | |
113 | } | |
114 | ||
115 | printf("calling cpg_join\n"); | |
116 | while ((result = cpg_join(handle, &group_name)) == CS_ERR_TRY_AGAIN) { | |
117 | printf("cpg_join returned %d\n", result); | |
118 | sleep (1); | |
119 | } | |
120 | ||
121 | if (result != CS_OK) { | |
122 | printf("cpg_join failed: %d\n", result); | |
123 | exit(-1); | |
124 | } | |
125 | ||
126 | fd_set read_fds; | |
127 | FD_ZERO(&read_fds); | |
128 | int cpg_fd; | |
129 | ||
130 | cpg_fd_get(handle, &cpg_fd); | |
131 | ||
132 | printf("starting main loop\n"); | |
133 | ||
134 | do { | |
135 | FD_SET(cpg_fd, &read_fds); | |
136 | struct timeval timeout = { 1, 0}; | |
137 | result = select(cpg_fd + 1, &read_fds, 0, 0, &timeout); | |
138 | ||
139 | if (result == -1) { | |
140 | printf("select error: %d\n", result); | |
141 | break; | |
142 | } | |
143 | if (result > 0) { | |
144 | ||
145 | if (FD_ISSET(cpg_fd, &read_fds)) { | |
a8bf13d0 | 146 | cs_error_t res = cpg_dispatch(handle, CS_DISPATCH_ALL); |
fe000966 DM |
147 | if (res != CS_OK) { |
148 | printf("cpg_dispatch failed: %d\n", res); | |
149 | break; | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
154 | if (cpg_mode_leave) | |
155 | break; | |
156 | ||
157 | } while(1); | |
158 | ||
159 | retry: | |
160 | ||
161 | printf("end loop - trying to restart\n"); | |
162 | ||
163 | usleep (1000); | |
164 | ||
165 | if (handle) { | |
166 | ||
167 | result = cpg_finalize(handle); | |
168 | if (result != CS_OK) { | |
169 | printf("cpg_finalize failed: %d\n", result); | |
170 | exit(-1); | |
171 | } | |
172 | } | |
173 | ||
174 | goto start; | |
175 | ||
176 | exit(0); | |
177 | } |