update getpnn to use a generic "set_callback" call to register the callback on the...
[sahlberg/ctdb.git] / libctdb / tst.c
index 452ebe1a1c0c69b6c2cb7259cc2150ff50993bdc..f8e8f7f7b81e310fc72d01a7599bc21c484a4bac 100644 (file)
@@ -15,8 +15,22 @@ void msg_h(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data, void *priva
 }
 
 
-void pnn_cb(int32_t status, int32_t pnn, void *private_data)
+void pnn_cb(int32_t status, struct ctdb_context *ctdb, ctdb_handle *handle, void *private_data)
 {
+       uint32_t pnn;
+       int ret;
+
+       if (status != 0) {
+               printf("Error reading PNN\n");
+               return;
+       }
+
+       ret = ctdb_getpnn_recv(ctdb, handle, &pnn);
+       if (ret != 0) {
+               printf("Failed to read getpnn reply\n");
+               return;
+       }
+
        printf("status:%d pnn:%d\n", status, pnn);
 }
 
@@ -67,7 +81,7 @@ void rrl_cb(int32_t status, ctdb_handle *handle, TDB_DATA outdata, void *private
 
 int main(int argc, char *argv[])
 {
-       struct ctdb_context *ctdb_context;
+       struct ctdb_context *ctdb;
        struct ctdb_db_context *ctdb_db_context;
        ctdb_handle *handle;
        struct pollfd pfd;
@@ -77,14 +91,14 @@ int main(int argc, char *argv[])
        key.dptr  = "Test Record";
        key.dsize = strlen(key.dptr);
 
-       ctdb_context = ctdb_connect("/tmp/ctdb.socket");
+       ctdb = ctdb_connect("/tmp/ctdb.socket");
 
-       handle = ctdb_set_message_handler_send(ctdb_context, 55, NULL, msg_h, NULL);
+       handle = ctdb_set_message_handler_send(ctdb, 55, NULL, msg_h, NULL);
        if (handle == NULL) {
                printf("Failed to register message port\n");
                exit(10);
        }
-       ret = ctdb_set_message_handler_recv(ctdb_context, handle);
+       ret = ctdb_set_message_handler_recv(ctdb, handle);
        if (ret != 0) {
                printf("Failed to receive set_message_handler reply\n");
                exit(10);
@@ -93,44 +107,55 @@ int main(int argc, char *argv[])
        msg.dptr="HelloWorld";
        msg.dsize = strlen(msg.dptr);
 
-       ret = ctdb_send_message(ctdb_context, 0, 55, msg);
+       ret = ctdb_send_message(ctdb, 0, 55, msg);
        if (ret != 0) {
                printf("Failed to send message. Aborting\n");
                exit(10);
        }
 
-       handle = ctdb_attachdb_send(ctdb_context, "test_test.tdb", 0, 0, NULL, NULL);
+       handle = ctdb_attachdb_send(ctdb, "test_test.tdb", 0, 0, NULL, NULL);
        if (handle == NULL) {
                printf("Failed to send attachdb control\n");
                exit(10);
        }
-       ret = ctdb_attachdb_recv(ctdb_context, handle, &ctdb_db_context);
+       ret = ctdb_attachdb_recv(ctdb, handle, &ctdb_db_context);
        if (ret != 0 ) {
                printf("Failed to attach to database\n");
                exit(10);
        }
 
 
-       handle = ctdb_getpnn_send(ctdb_context, CTDB_CURRENT_NODE, pnn_cb, NULL);
+
+
+       handle = ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE);
        if (handle == NULL) {
                printf("Failed to send get_pnn control\n");
                exit(10);
        }
+       ret = ctdb_set_callback(handle, pnn_cb, NULL);
+       if (ret != 0) {
+               printf("Failed to set callback for getpnn\n");
+               ctdb_free(handle);
+               exit(10);
+       }
+
+
+
 
-       handle = ctdb_readrecordlock_send(ctdb_context, ctdb_db_context, key, rrl_cb, NULL);
+       handle = ctdb_readrecordlock_send(ctdb, ctdb_db_context, key, rrl_cb, NULL);
        if (handle == NULL) {
                printf("Failed to send READRECORDLOCK\n");
                exit(10);
        }
 
-       pfd.fd = ctdb_get_fd(ctdb_context);
+       pfd.fd = ctdb_get_fd(ctdb);
        for (;;) {
-         pfd.events = ctdb_which_events(ctdb_context);
+         pfd.events = ctdb_which_events(ctdb);
          if (poll(&pfd, 1, -1) < 0) {
            printf("Poll failed");
            exit(10);
          }
-         ctdb_service(ctdb_context);
+         ctdb_service(ctdb);
        }
 
        return 0;