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 0x%016llx : %s\n", srvid, data.dptr);
18 void pnn_cb(int32_t status, struct ctdb_context *ctdb, struct ctdb_client_control_state *state, void *private_data)
24 printf("Error reading PNN\n");
28 ret = ctdb_getpnn_recv(ctdb, state, &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, struct ctdb_context *ctdb, struct ctdb_client_control_state *state, void *private_data)
43 printf("Error reading RECMASTER\n");
47 ret = ctdb_getrecmaster_recv(ctdb, state, &rm);
49 printf("Failed to read getpnn reply\n");
53 printf("GETRECMASTER ASYNC: status:%d recmaster:%d\n", status, rm);
58 * example on how to first read(non-existing recortds are implicitely created
59 * on demand) a record and change it in the callback.
60 * This forms the atom for the read-modify-write cycle.
62 * Pure read, or pure write are just special cases of this cycle.
64 void rrl_cb(int32_t status, ctdb_handle *handle, TDB_DATA outdata, void *private_data)
70 printf("rrl_cb returned error: status %d\n", status);
77 printf("rrl size:%d data:%s\n", outdata.dsize, outdata.dptr);
78 if (outdata.dsize == 0) {
81 strcpy(tmp, outdata.dptr);
86 data.dsize = strlen(tmp) + 1;
87 ctdb_writerecord(handle, key, data);
89 printf("Wrote new record : %s\n", tmp);
97 int main(int argc, char *argv[])
99 struct ctdb_context *ctdb;
100 struct ctdb_db_context *ctdb_db_context;
101 struct ctdb_client_control_state *control_state;
108 key.dptr = "Test Record";
109 key.dsize = strlen(key.dptr);
111 ctdb = ctdb_connect("/tmp/ctdb.socket");
113 handle = ctdb_set_message_handler_send(ctdb, CTDB_SRVID_TEST|55, NULL, msg_h, NULL);
114 if (handle == NULL) {
115 printf("Failed to register message port\n");
118 ret = ctdb_set_message_handler_recv(ctdb, handle);
120 printf("Failed to receive set_message_handler reply\n");
124 msg.dptr="HelloWorld";
125 msg.dsize = strlen(msg.dptr);
127 ret = ctdb_send_message(ctdb, 0, CTDB_SRVID_TEST|55, msg);
129 printf("Failed to send message. Aborting\n");
133 handle = ctdb_attachdb_send(ctdb, "test_test.tdb", 0, 0, NULL, NULL);
134 if (handle == NULL) {
135 printf("Failed to send attachdb control\n");
138 ret = ctdb_attachdb_recv(ctdb, handle, &ctdb_db_context);
140 printf("Failed to attach to database\n");
147 * ASYNC call with callback to read the recmaster
148 * this is the preferred way to use libctdb
150 control_state = ctdb_getrecmaster_send(ctdb, CTDB_CURRENT_NODE, rm_cb, NULL);
151 if (control_state == NULL) {
152 printf("Failed to send get_recmaster control\n");
157 * SEMI-SYNC call with callback to read the recmaster
158 * calls the blocking *_recv() function.
159 * Avoid this mode for performance critical tasks
161 control_state = ctdb_getrecmaster_send(ctdb, CTDB_CURRENT_NODE, NULL, NULL);
162 if (control_state == NULL) {
163 printf("Failed to send get_recmaster control\n");
166 ret = ctdb_getrecmaster_recv(ctdb, control_state, &recmaster);
168 printf("Failed to receive response to getrecmaster\n");
171 printf("GETRECMASTER SEMI-SYNC: status:%d recmaster:%d\n", ret, recmaster);
175 * SYNC call with callback to read the recmaster
176 * calls the blocking sync function.
177 * Avoid this mode for performance critical tasks
179 ret = ctdb_getrecmaster(ctdb, CTDB_CURRENT_NODE, &recmaster);
181 printf("Failed to receive response to getrecmaster\n");
184 printf("GETRECMASTER SYNC: status:%d recmaster:%d\n", ret, recmaster);
189 handle = ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, pnn_cb, NULL);
190 if (handle == NULL) {
191 printf("Failed to send get_pnn control\n");
197 handle = ctdb_readrecordlock_send(ctdb, ctdb_db_context, key, rrl_cb, NULL);
198 if (handle == NULL) {
199 printf("Failed to send READRECORDLOCK\n");
203 pfd.fd = ctdb_get_fd(ctdb);
205 pfd.events = ctdb_which_events(ctdb);
206 if (poll(&pfd, 1, -1) < 0) {
207 printf("Poll failed");