2 * Example program to demonstrate the libctdb api
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 * This program needs to be linked with libtdb and libctdb
20 * (You need these packages installed: libtdb libtdb-devel
21 * ctdb and ctdb-devel)
23 * This program can then be compiled using
24 * gcc -o tst tst.c -ltdb -lctdb
41 void msg_h(struct ctdb_connection *ctdb, uint64_t srvid, TDB_DATA data, void *private_data)
43 printf("Message received on port %d : %s\n", (int)srvid, data.dptr);
46 static void pnn_cb(struct ctdb_connection *ctdb,
47 struct ctdb_request *req, void *private)
52 status = ctdb_getpnn_recv(req, &pnn);
53 ctdb_request_free(req);
55 printf("Error reading PNN\n");
58 printf("status:%d pnn:%d\n", status, pnn);
61 static void rm_cb(struct ctdb_connection *ctdb,
62 struct ctdb_request *req, void *private)
67 status = ctdb_getrecmaster_recv(req, &rm);
68 ctdb_request_free(req);
70 printf("Error reading RECMASTER\n");
74 printf("GETRECMASTER ASYNC: status:%d recmaster:%d\n", status, rm);
78 * example on how to first read(non-existing recortds are implicitely created
79 * on demand) a record and change it in the callback.
80 * This forms the atom for the read-modify-write cycle.
82 * Pure read, or pure write are just special cases of this cycle.
84 static void rrl_cb(struct ctdb_connection *ctdb,
85 struct ctdb_request *req, void *private)
87 struct ctdb_lock *lock;
92 lock = ctdb_readrecordlock_recv(private, req, &outdata);
94 printf("rrl_cb returned error\n");
98 printf("rrl size:%d data:%.*s\n", outdata.dsize,
99 outdata.dsize, outdata.dptr);
100 if (outdata.dsize == 0) {
103 strcpy(tmp, outdata.dptr);
108 data.dsize = strlen(tmp) + 1;
109 ctdb_writerecord(lock, data);
111 printf("Wrote new record : %s\n", tmp);
113 ctdb_release_lock(lock);
116 static bool registered = false;
117 void message_handler_cb(struct ctdb_connection *ctdb,
118 struct ctdb_request *req, void *private)
120 if (ctdb_set_message_handler_recv(ctdb, req) != 0) {
121 err(1, "registering message");
123 ctdb_request_free(req);
124 printf("Message handler registered\n");
128 int main(int argc, char *argv[])
130 struct ctdb_connection *ctdb_connection;
131 struct ctdb_request *handle;
132 struct ctdb_db *ctdb_db_context;
138 ctdb_connection = ctdb_connect("/tmp/ctdb.socket");
139 if (!ctdb_connection)
140 err(1, "Connecting to /tmp/ctdb.socket");
142 pfd.fd = ctdb_get_fd(ctdb_connection);
144 handle = ctdb_set_message_handler_send(ctdb_connection, 55, msg_h,
145 message_handler_cb, NULL);
146 if (handle == NULL) {
147 printf("Failed to register message port\n");
151 /* Hack for testing: this makes sure registration goes out. */
152 while (!registered) {
153 ctdb_service(ctdb_connection, POLLIN|POLLOUT);
156 msg.dptr="HelloWorld";
157 msg.dsize = strlen(msg.dptr);
159 ret = ctdb_send_message(ctdb_connection, 0, 55, msg);
161 printf("Failed to send message. Aborting\n");
165 handle = ctdb_getrecmaster_send(ctdb_connection, 0, rm_cb, NULL);
166 if (handle == NULL) {
167 printf("Failed to send get_recmaster control\n");
171 ctdb_db_context = ctdb_attachdb(ctdb_connection, "test_test.tdb", 0, 0);
172 if (!ctdb_db_context) {
173 printf("Failed to attach to database\n");
178 * SYNC call with callback to read the recmaster
179 * calls the blocking sync function.
180 * Avoid this mode for performance critical tasks
182 ret = ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster);
184 printf("Failed to receive response to getrecmaster\n");
187 printf("GETRECMASTER SYNC: status:%d recmaster:%d\n", ret, recmaster);
190 handle = ctdb_getpnn_send(ctdb_connection, CTDB_CURRENT_NODE,
192 if (handle == NULL) {
193 printf("Failed to send get_pnn control\n");
197 if (!ctdb_readrecordlock_send(ctdb_db_context, key, &handle,
198 rrl_cb, ctdb_db_context)) {
199 printf("Failed to send READRECORDLOCK\n");
203 printf("READRECORDLOCK is async\n");
207 pfd.events = ctdb_which_events(ctdb_connection);
208 if (poll(&pfd, 1, -1) < 0) {
209 printf("Poll failed");
212 if (ctdb_service(ctdb_connection, pfd.revents) < 0) {
213 err(1, "Failed to service");