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