08d515eb3aa6caddcebff203c156f39cc1169742
[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 void adb_cb(int32_t status, struct ctdb_db_context *ctdb_db, void *private_data)
27 {
28         printf("status:%d db:%p\n", status, ctdb_db);
29 }
30
31
32 int main(int argc, char *argv[])
33 {
34         struct ctdb_context *ctdb_context;
35         ctdb_handle *handle;
36         struct pollfd pfd;
37         int ret;
38         TDB_DATA msg;
39
40         ctdb_context = ctdb_connect("/tmp/ctdb.socket");
41
42         handle = ctdb_set_message_handler_send(ctdb_context, 55, NULL, msg_h, NULL);
43         if (handle == NULL) {
44                 printf("Failed to register message port\n");
45                 exit(10);
46         }
47         ret = ctdb_set_message_handler_recv(ctdb_context, handle);
48         if (ret != 0) {
49                 printf("Failed to receive set_message_handler reply\n");
50                 exit(10);
51         }
52
53         msg.dptr="HelloWorld";
54         msg.dsize = strlen(msg.dptr);
55
56         ret = ctdb_send_message(ctdb_context, 0, 55, msg);
57         if (ret != 0) {
58                 printf("Failed to send message. Aborting\n");
59                 exit(10);
60         }
61
62         handle = ctdb_getpnn_send(ctdb_context, CTDB_CURRENT_NODE, pnn_cb, NULL);
63         if (handle == NULL) {
64                 printf("Failed to send get_pnn control\n");
65                 exit(10);
66         }
67
68         handle = ctdb_getrecmaster_send(ctdb_context, CTDB_CURRENT_NODE, rm_cb, NULL);
69         if (handle == NULL) {
70                 printf("Failed to send get_recmaster control\n");
71                 exit(10);
72         }
73
74         handle = ctdb_attachdb_send(ctdb_context, CTDB_CURRENT_NODE, "test_test.tdb", 0,0, adb_cb, NULL);
75         if (handle == NULL) {
76                 printf("Failed to send attachdb control\n");
77                 exit(10);
78         }
79
80         pfd.fd = ctdb_get_fd(ctdb_context);
81         for (;;) {
82           pfd.events = ctdb_which_events(ctdb_context);
83           if (poll(&pfd, 1, -1) < 0) {
84             printf("Poll failed");
85             exit(10);
86           }
87           ctdb_service(ctdb_context);
88         }
89
90         return 0;
91 }