update getpnn to use a generic "set_callback" call to register the callback on the...
[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 TDB_DATA key;
11
12 void msg_h(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data, void *private_data)
13 {
14         printf("Message received on port %d : %s\n", (int)srvid, data.dptr);
15 }
16
17
18 void pnn_cb(int32_t status, struct ctdb_context *ctdb, ctdb_handle *handle, void *private_data)
19 {
20         uint32_t pnn;
21         int ret;
22
23         if (status != 0) {
24                 printf("Error reading PNN\n");
25                 return;
26         }
27
28         ret = ctdb_getpnn_recv(ctdb, handle, &pnn);
29         if (ret != 0) {
30                 printf("Failed to read getpnn reply\n");
31                 return;
32         }
33
34         printf("status:%d pnn:%d\n", status, pnn);
35 }
36
37 void rm_cb(int32_t status, int32_t recmaster, void *private_data)
38 {
39         printf("status:%d recmaster:%d\n", status, recmaster);
40 }
41
42 /*
43  * example on how to first read(non-existing recortds are implicitely created
44  * on demand) a record and change it in the callback.
45  * This forms the atom for the read-modify-write cycle.
46  *
47  * Pure read, or pure write are just special cases of this cycle.
48  */
49 void rrl_cb(int32_t status, ctdb_handle *handle, TDB_DATA outdata, void *private_data)
50 {
51         TDB_DATA data;
52         char tmp[256];
53
54         if (status != 0) {
55                 printf("rrl_cb returned error: status %d\n", status);
56                 if (handle) {
57                         ctdb_free(handle);
58                 }
59                 return;
60         }
61
62         printf("rrl size:%d data:%s\n", outdata.dsize, outdata.dptr);
63         if (outdata.dsize == 0) {
64                 tmp[0] = 0;
65         } else {
66                 strcpy(tmp, outdata.dptr);
67         }
68         strcat(tmp, "*");
69
70         data.dptr  = tmp;
71         data.dsize = strlen(tmp) + 1;
72         ctdb_writerecord(handle, key, data);
73
74         printf("Wrote new record : %s\n", tmp);
75
76         ctdb_free(handle);
77 }
78
79
80
81
82 int main(int argc, char *argv[])
83 {
84         struct ctdb_context *ctdb;
85         struct ctdb_db_context *ctdb_db_context;
86         ctdb_handle *handle;
87         struct pollfd pfd;
88         int ret;
89         TDB_DATA msg;
90
91         key.dptr  = "Test Record";
92         key.dsize = strlen(key.dptr);
93
94         ctdb = ctdb_connect("/tmp/ctdb.socket");
95
96         handle = ctdb_set_message_handler_send(ctdb, 55, NULL, msg_h, NULL);
97         if (handle == NULL) {
98                 printf("Failed to register message port\n");
99                 exit(10);
100         }
101         ret = ctdb_set_message_handler_recv(ctdb, handle);
102         if (ret != 0) {
103                 printf("Failed to receive set_message_handler reply\n");
104                 exit(10);
105         }
106
107         msg.dptr="HelloWorld";
108         msg.dsize = strlen(msg.dptr);
109
110         ret = ctdb_send_message(ctdb, 0, 55, msg);
111         if (ret != 0) {
112                 printf("Failed to send message. Aborting\n");
113                 exit(10);
114         }
115
116         handle = ctdb_attachdb_send(ctdb, "test_test.tdb", 0, 0, NULL, NULL);
117         if (handle == NULL) {
118                 printf("Failed to send attachdb control\n");
119                 exit(10);
120         }
121         ret = ctdb_attachdb_recv(ctdb, handle, &ctdb_db_context);
122         if (ret != 0 ) {
123                 printf("Failed to attach to database\n");
124                 exit(10);
125         }
126
127
128
129
130         handle = ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE);
131         if (handle == NULL) {
132                 printf("Failed to send get_pnn control\n");
133                 exit(10);
134         }
135         ret = ctdb_set_callback(handle, pnn_cb, NULL);
136         if (ret != 0) {
137                 printf("Failed to set callback for getpnn\n");
138                 ctdb_free(handle);
139                 exit(10);
140         }
141
142
143
144
145         handle = ctdb_readrecordlock_send(ctdb, ctdb_db_context, key, rrl_cb, NULL);
146         if (handle == NULL) {
147                 printf("Failed to send READRECORDLOCK\n");
148                 exit(10);
149         }
150
151         pfd.fd = ctdb_get_fd(ctdb);
152         for (;;) {
153           pfd.events = ctdb_which_events(ctdb);
154           if (poll(&pfd, 1, -1) < 0) {
155             printf("Poll failed");
156             exit(10);
157           }
158           ctdb_service(ctdb);
159         }
160
161         return 0;
162 }