let the ctdb_createdb*() fucntions return the db_id
[sahlberg/ctdb.git] / libctdb / tst.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <poll.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include "lib/tdb/include/tdb.h"
8 #include "include/ctdb.h"
9
10 void msg_h(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data, void *private_data)
11 {
12         printf("Message received on port %d : %s\n", (int)srvid, data.dptr);
13 }
14
15
16 void pnn_cb(int32_t status, int32_t pnn, void *private_data)
17 {
18         printf("status:%d pnn:%d\n", status, pnn);
19 }
20
21 void rm_cb(int32_t status, int32_t recmaster, void *private_data)
22 {
23         printf("status:%d recmaster:%d\n", status, recmaster);
24 }
25
26 int main(int argc, char *argv[])
27 {
28         struct ctdb_context *ctdb_context;
29         ctdb_handle *handle;
30         struct pollfd pfd;
31         int ret;
32         TDB_DATA msg;
33
34         ctdb_context = ctdb_connect("/tmp/ctdb.socket");
35
36         handle = ctdb_set_message_handler_send(ctdb_context, 55, NULL, msg_h, NULL);
37         if (handle == NULL) {
38                 printf("Failed to register message port\n");
39                 exit(10);
40         }
41         ret = ctdb_set_message_handler_recv(ctdb_context, handle);
42         if (ret != 0) {
43                 printf("Failed to receive set_message_handler reply\n");
44                 exit(10);
45         }
46
47         msg.dptr="HelloWorld";
48         msg.dsize = strlen(msg.dptr);
49
50         ret = ctdb_send_message(ctdb_context, 0, 55, msg);
51         if (ret != 0) {
52                 printf("Failed to send message. Aborting\n");
53                 exit(10);
54         }
55
56         handle = ctdb_getpnn_send(ctdb_context, CTDB_CURRENT_NODE, pnn_cb, NULL);
57         if (handle == NULL) {
58                 printf("Failed to send get_pnn control\n");
59                 exit(10);
60         }
61
62         handle = ctdb_getrecmaster_send(ctdb_context, CTDB_CURRENT_NODE, rm_cb, NULL);
63         if (handle == NULL) {
64                 printf("Failed to send get_recmaster control\n");
65                 exit(10);
66         }
67
68         pfd.fd = ctdb_get_fd(ctdb_context);
69         for (;;) {
70           pfd.events = ctdb_which_events(ctdb_context);
71           if (poll(&pfd, 1, -1) < 0) {
72             printf("Poll failed");
73             exit(10);
74           }
75           ctdb_service(ctdb_context);
76         }
77
78         return 0;
79 }