2 Copyright (C) 2009 Proxmox Server Solutions GmbH
4 Copyright: This program is under GNU GPL, the GNU General Public License.
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.
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.
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
20 Author: Dietmar Maurer <dietmar@proxmox.com>
29 #include <corosync/corotypes.h>
30 #include <corosync/cpg.h>
32 static int cpg_mode_leave
;
34 static void my_cpg_deliver_callback (
36 const struct cpg_name
*groupName
,
42 printf("got message form %d/%d\n", nodeid
, pid
);
49 static void my_cpg_confchg_callback (
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
)
58 printf("cpg_confchg_callback %ld joined, %ld left, %ld members\n",
59 joined_list_entries
, left_list_entries
, member_list_entries
);
61 for (i
= 0; i
< member_list_entries
; i
++) {
62 printf("cpg member %d/%d\n", member_list
[i
].nodeid
, member_list
[i
].pid
);
65 /* send update message */
66 char *inbuf
= "This is just a test message\n";
69 iov
.iov_len
= strlen(inbuf
)+1;
73 result
= cpg_mcast_joined(handle
, CPG_TYPE_AGREED
, &iov
, 1);
74 if (result
== CS_ERR_TRY_AGAIN
) {
76 printf("cpg_send_message retry");
81 printf("cpg_send_message failed: %d\n", result
);
85 static cpg_callbacks_t callbacks
= {
86 .cpg_deliver_fn
= my_cpg_deliver_callback
,
87 .cpg_confchg_fn
= my_cpg_confchg_callback
,
91 int main(int argc
, char *argv
[])
93 struct cpg_name group_name
;
94 char *gn
= "TESTGROUP";
95 strcpy(group_name
.value
, gn
);
96 group_name
.length
= strlen(gn
) + 1;
102 printf("starting cpgtest\n");
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
);
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
);
121 if (result
!= CS_OK
) {
122 printf("cpg_join failed: %d\n", result
);
130 cpg_fd_get(handle
, &cpg_fd
);
132 printf("starting main loop\n");
135 FD_SET(cpg_fd
, &read_fds
);
136 struct timeval timeout
= { 1, 0};
137 result
= select(cpg_fd
+ 1, &read_fds
, 0, 0, &timeout
);
140 printf("select error: %d\n", result
);
145 if (FD_ISSET(cpg_fd
, &read_fds
)) {
146 cs_error_t res
= cpg_dispatch(handle
, CS_DISPATCH_ALL
);
148 printf("cpg_dispatch failed: %d\n", res
);
161 printf("end loop - trying to restart\n");
167 result
= cpg_finalize(handle
);
168 if (result
!= CS_OK
) {
169 printf("cpg_finalize failed: %d\n", result
);