7 #include "lib/tdb/include/tdb.h"
8 #include "include/ctdb.h"
12 void msg_h(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data, void *private_data)
14 printf("Message received on port %d : %s\n", (int)srvid, data.dptr);
18 void pnn_cb(int32_t status, struct ctdb_context *ctdb, ctdb_handle *handle, void *private_data)
24 printf("Error reading PNN\n");
28 ret = ctdb_getpnn_recv(ctdb, handle, &pnn);
30 printf("Failed to read getpnn reply\n");
34 printf("status:%d pnn:%d\n", status, pnn);
37 void rm_cb(int32_t status, int32_t recmaster, void *private_data)
39 printf("status:%d recmaster:%d\n", status, recmaster);
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.
47 * Pure read, or pure write are just special cases of this cycle.
49 void rrl_cb(int32_t status, ctdb_handle *handle, TDB_DATA outdata, void *private_data)
55 printf("rrl_cb returned error: status %d\n", status);
62 printf("rrl size:%d data:%s\n", outdata.dsize, outdata.dptr);
63 if (outdata.dsize == 0) {
66 strcpy(tmp, outdata.dptr);
71 data.dsize = strlen(tmp) + 1;
72 ctdb_writerecord(handle, key, data);
74 printf("Wrote new record : %s\n", tmp);
82 int main(int argc, char *argv[])
84 struct ctdb_context *ctdb;
85 struct ctdb_db_context *ctdb_db_context;
91 key.dptr = "Test Record";
92 key.dsize = strlen(key.dptr);
94 ctdb = ctdb_connect("/tmp/ctdb.socket");
96 handle = ctdb_set_message_handler_send(ctdb, 55, NULL, msg_h, NULL);
98 printf("Failed to register message port\n");
101 ret = ctdb_set_message_handler_recv(ctdb, handle);
103 printf("Failed to receive set_message_handler reply\n");
107 msg.dptr="HelloWorld";
108 msg.dsize = strlen(msg.dptr);
110 ret = ctdb_send_message(ctdb, 0, 55, msg);
112 printf("Failed to send message. Aborting\n");
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");
121 ret = ctdb_attachdb_recv(ctdb, handle, &ctdb_db_context);
123 printf("Failed to attach to database\n");
130 handle = ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE);
131 if (handle == NULL) {
132 printf("Failed to send get_pnn control\n");
135 ret = ctdb_set_callback(handle, pnn_cb, NULL);
137 printf("Failed to set callback for getpnn\n");
145 handle = ctdb_readrecordlock_send(ctdb, ctdb_db_context, key, rrl_cb, NULL);
146 if (handle == NULL) {
147 printf("Failed to send READRECORDLOCK\n");
151 pfd.fd = ctdb_get_fd(ctdb);
153 pfd.events = ctdb_which_events(ctdb);
154 if (poll(&pfd, 1, -1) < 0) {
155 printf("Poll failed");