r23795: more v2->v3 conversion
[metze/samba/wip.git] / source4 / cluster / ctdb / tests / ctdb_messaging.c
1 /* 
2    test of messaging
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 3 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
26
27 static int timelimit = 10;
28 static int num_records = 10;
29 static int num_msgs = 1;
30 static int num_clients = 2;
31
32
33 /*
34   handler for messages in bench_ring()
35 */
36 static void message_handler(struct ctdb_context *ctdb, uint32_t srvid, 
37                                  TDB_DATA data, void *private_data)
38 {
39         printf("client vnn:%d received a message to srvid:%d [%s]\n",ctdb_get_vnn(ctdb),srvid,data.dptr);
40         fflush(stdout);
41 }
42
43 /*
44   main program
45 */
46 int main(int argc, const char *argv[])
47 {
48         struct ctdb_context *ctdb;
49         struct ctdb_db_context *ctdb_db;
50         char buf[256];
51
52         struct poptOption popt_options[] = {
53                 POPT_AUTOHELP
54                 POPT_CTDB_CMDLINE
55                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
56                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
57                 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
58                 { "num-clients", 0, POPT_ARG_INT, &num_clients, 0, "num_clients", "integer" },
59                 POPT_TABLEEND
60         };
61         int opt;
62         const char **extra_argv;
63         int extra_argc = 0;
64         int ret, i, j;
65         poptContext pc;
66         struct event_context *ev;
67         pid_t pid;
68         int srvid;
69         TDB_DATA data;
70
71         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
72
73         while ((opt = poptGetNextOpt(pc)) != -1) {
74                 switch (opt) {
75                 default:
76                         fprintf(stderr, "Invalid option %s: %s\n", 
77                                 poptBadOption(pc, 0), poptStrerror(opt));
78                         exit(1);
79                 }
80         }
81
82         /* setup the remaining options for the main program to use */
83         extra_argv = poptGetArgs(pc);
84         if (extra_argv) {
85                 extra_argv++;
86                 while (extra_argv[extra_argc]) extra_argc++;
87         }
88
89         ev = event_context_init(NULL);
90
91         /* initialise ctdb */
92         ctdb = ctdb_cmdline_init(ev);
93         if (ctdb == NULL) {
94                 printf("Failed to init ctdb\n");
95                 exit(1);
96         }
97
98         /* attach to a specific database */
99         ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
100         if (!ctdb_db) {
101                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
102                 exit(1);
103         }
104
105         /* start the protocol running */
106         ret = ctdb_start(ctdb);
107
108         srvid = -1;
109         for (i=0;i<num_clients-1;i++) {
110                 pid=fork();
111                 if (pid) {
112                         srvid = i;
113                         break;
114                 }
115         }
116         if (srvid == -1) {
117                 srvid = num_clients-1;
118         }
119
120         ctdb_set_message_handler(ctdb, srvid, message_handler, NULL);
121
122         /* wait until all nodes are connected (should not be needed
123            outside of test code) */
124         ctdb_connect_wait(ctdb);
125
126         sleep(3);
127
128         printf("sending message from vnn:%d to vnn:%d/srvid:%d\n",ctdb_get_vnn(ctdb),ctdb_get_vnn(ctdb), 1-srvid);
129         for (i=0;i<ctdb_get_num_nodes(ctdb);i++) {
130                 for (j=0;j<num_clients;j++) {
131                         printf("sending message to %d:%d\n", i, j);
132                         sprintf(buf,"Message from %d to vnn:%d srvid:%d",ctdb_get_vnn(ctdb),i,j);
133                         data.dptr = (unsigned char *)buf;
134                         data.dsize=strlen(buf)+1;
135                         ctdb_send_message(ctdb, i, j, data);
136                 }
137         }
138
139         while (1) {
140                 event_loop_once(ev);
141         }
142        
143         /* shut it down */
144         ctdb_shutdown(ctdb);
145
146         return 0;
147 }