4 Copyright (C) Andrew Tridgell 2006
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 2 of the License, or (at your option) any later version.
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.
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
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
26 enum my_functions {FUNC_SORT=1, FUNC_FETCH=2};
28 static int int_compare(int *i1, int *i2)
34 add an integer into a record in sorted order
36 static int sort_func(struct ctdb_call *call)
38 if (call->call_data == NULL ||
39 call->call_data->dsize != sizeof(int)) {
40 return CTDB_ERR_INVALID;
42 call->new_data = talloc(call, TDB_DATA);
43 if (call->new_data == NULL) {
44 return CTDB_ERR_NOMEM;
46 call->new_data->dptr = talloc_size(call,
47 call->record_data.dsize +
48 call->call_data->dsize);
49 if (call->new_data->dptr == NULL) {
50 return CTDB_ERR_NOMEM;
52 call->new_data->dsize = call->record_data.dsize + call->call_data->dsize;
53 memcpy(call->new_data->dptr+call->record_data.dsize,
54 call->call_data->dptr, call->call_data->dsize);
55 qsort(call->new_data->dptr, call->new_data->dsize / sizeof(int),
56 sizeof(int), (comparison_fn_t)int_compare);
61 ctdb call function to fetch a record
63 static int fetch_func(struct ctdb_call *call)
65 call->reply_data = &call->record_data;
72 int main(int argc, const char *argv[])
74 struct ctdb_context *ctdb;
75 const char *nlist = NULL;
76 const char *myaddress = NULL;
78 struct poptOption popt_options[] = {
80 { "nlist", 0, POPT_ARG_STRING, &nlist, 0, "node list file", "filename" },
81 { "listen", 0, POPT_ARG_STRING, &myaddress, 0, "address to listen on", "address" },
84 const char **extra_argv;
89 struct event_context *ev;
91 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
93 while ((opt = poptGetNextOpt(pc)) != -1) {
96 fprintf(stderr, "Invalid option %s: %s\n",
97 poptBadOption(pc, 0), poptStrerror(opt));
102 /* setup the remaining options for the main program to use */
103 extra_argv = poptGetArgs(pc);
106 while (extra_argv[extra_argc]) extra_argc++;
109 if (nlist == NULL || myaddress == NULL) {
110 printf("You must provide a node list with --nlist and an address with --listen\n");
114 ev = event_context_init(NULL);
116 /* initialise ctdb */
117 ctdb = ctdb_init(ev);
119 printf("Failed to init ctdb\n");
123 /* tell ctdb what address to listen on */
124 ret = ctdb_set_address(ctdb, myaddress);
126 printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
130 /* tell ctdb what nodes are available */
131 ret = ctdb_set_nlist(ctdb, nlist);
133 printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
137 /* setup a ctdb call function */
138 ret = ctdb_set_call(ctdb, sort_func, FUNC_SORT);
139 ret = ctdb_set_call(ctdb, fetch_func, FUNC_FETCH);
141 /* attach to a specific database */
142 ret = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
144 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
148 /* start the protocol running */
149 ret = ctdb_start(ctdb);
152 key.dsize = strlen("test")+1;
154 /* add some random data */
155 for (i=0;i<100;i++) {
157 data.dptr = (uint8_t *)&v;
158 data.dsize = sizeof(v);
159 ret = ctdb_call(ctdb, key, FUNC_SORT, &data, NULL);
161 printf("ctdb_call FUNC_SORT failed - %s\n", ctdb_errstr(ctdb));
166 /* fetch the record */
167 ret = ctdb_call(ctdb, key, FUNC_FETCH, NULL, &data);
169 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
173 for (i=0;i<data.dsize/sizeof(int);i++) {
174 printf("%3d\n", ((int *)data.dptr)[i]);