additional cleanup.
authorRonnie Sahlberg <ronniesahlberg@gmail.com>
Tue, 11 May 2010 23:22:12 +0000 (09:22 +1000)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Tue, 11 May 2010 23:22:12 +0000 (09:22 +1000)
create async version of ctdb_set_message_handler()

client/ctdb_client.c
include/ctdb.h
libctdb/libctdb.c
libctdb/tst.c

index f8e98b54fc66d9856212cfcd445e0e615bc189f8..a0d5997d50ea23ecffb140c188143e36d10ae3f9 100644 (file)
@@ -2219,37 +2219,6 @@ int ctdb_ctrl_get_server_id_list(struct ctdb_context *ctdb,
        return 0;
 }
 
-/*
-  initialise the ctdb daemon for client applications
-
-  NOTE: In current code the daemon does not fork. This is for testing purposes only
-  and to simplify the code.
-*/
-struct ctdb_context *ctdb_init(struct event_context *ev)
-{
-       int ret;
-       struct ctdb_context *ctdb;
-
-       ctdb = talloc_zero(ev, struct ctdb_context);
-       if (ctdb == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " talloc_zero failed.\n"));
-               return NULL;
-       }
-       ctdb->ev  = ev;
-       ctdb->idr = idr_init(ctdb);
-       CTDB_NO_MEMORY_NULL(ctdb, ctdb->idr);
-
-       ret = ctdb_set_socketname(ctdb, CTDB_PATH);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,(__location__ " ctdb_set_socketname failed.\n"));
-               talloc_free(ctdb);
-               return NULL;
-       }
-
-       return ctdb;
-}
-
-
 /*
   set some ctdb flags
 */
index df9f54c10c9a74226b6c607bd46699e05633d67e..807204b27fac3b1092ef9250f8717ef7bf2ee893 100644 (file)
@@ -45,8 +45,19 @@ int ctdb_cancel(ctdb_handle *);
  */
 typedef void (*ctdb_message_fn_t)(struct ctdb_context *, uint64_t srvid, TDB_DATA data, void *);
 
+typedef void (*ctdb_set_message_handler_cb)(int32_t status, void *private_data);
+
+ctdb_handle *
+ctdb_set_message_handler_send(struct ctdb_context *ctdb, uint64_t srvid,
+                             ctdb_set_message_handler_cb callback,
+                             ctdb_message_fn_t handler, void *private_data);
+
+int ctdb_set_message_handler_recv(struct ctdb_context *ctdb,
+                                 ctdb_handle *handle);
+
 int ctdb_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, ctdb_message_fn_t handler, void *private_data);
 
+
 int ctdb_remove_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data);
 
 int ctdb_send_message(struct ctdb_context *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
@@ -57,12 +68,12 @@ int ctdb_send_message(struct ctdb_context *ctdb, uint32_t pnn, uint64_t srvid, T
 /*
  * functions to read the recovery master of a node
  */
-typedef void (*get_recmaster_cb)(int32_t status, int32_t recmaster, void *private_data);
+typedef void (*ctdb_get_recmaster_cb)(int32_t status, int32_t recmaster, void *private_data);
 
 ctdb_handle *
 ctdb_getrecmaster_send(struct ctdb_context *ctdb,
                        uint32_t destnode,
-                       get_recmaster_cb callback,
+                       ctdb_get_recmaster_cb callback,
                        void *private_data);
 int ctdb_getrecmaster_recv(struct ctdb_context *ctdb,
                        ctdb_handle *handle,
index 40369be36b8dd365bdb421aaaf4356d89d002151..61470de4e05faa0dfc39a4f76655bfa09d6e3197 100644 (file)
@@ -144,7 +144,7 @@ static void
 ctdb_getrecmaster_recv_cb(struct ctdb_client_control_state *state)
 {
        struct ctdb_control_cb_data *cb_data = state->async.private_data;
-       get_recmaster_cb callback = (get_recmaster_cb)cb_data->callback;
+       ctdb_get_recmaster_cb callback = (ctdb_get_recmaster_cb)cb_data->callback;
 
        callback(0, state->status, cb_data->private_data);
 }
@@ -156,7 +156,7 @@ ctdb_getrecmaster_recv_cb(struct ctdb_client_control_state *state)
 ctdb_handle *
 ctdb_getrecmaster_send(struct ctdb_context *ctdb,
                        uint32_t destnode,
-                       get_recmaster_cb callback,
+                       ctdb_get_recmaster_cb callback,
                        void *private_data)
 {
        struct ctdb_client_control_state *state;
@@ -166,6 +166,11 @@ ctdb_getrecmaster_send(struct ctdb_context *ctdb,
                           CTDB_CONTROL_GET_RECMASTER, 0, tdb_null, 
                           ctdb, NULL, NULL);
 
+       if (state == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to send GET_RECMASTER control\n"));
+               return NULL;
+       }
+
        if (callback != NULL) {
                cb_data = talloc(state, struct ctdb_control_cb_data);
                cb_data->callback     = callback;
@@ -194,7 +199,7 @@ int ctdb_getrecmaster_recv(struct ctdb_context *ctdb, ctdb_handle *handle, uint3
                *recmaster = (uint32_t)res;
        }
 
-       return 0;
+       return state->status;
 }
 
 int ctdb_getrecmaster(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *recmaster)
@@ -212,3 +217,69 @@ int ctdb_getrecmaster(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *re
 
 
 
+/*
+  tell the daemon what messaging srvid we will use, and register the message
+  handler function in the client
+*/
+ctdb_handle *
+ctdb_set_message_handler_send(struct ctdb_context *ctdb, uint64_t srvid, 
+                            ctdb_set_message_handler_cb callback,
+                            ctdb_message_fn_t handler,
+                            void *private_data)
+                                   
+{
+       struct ctdb_client_control_state *state;
+       struct ctdb_control_cb_data *cb_data;
+
+       if (ctdb_register_message_handler(ctdb, ctdb, srvid, handler, private_data) != 0) {
+               return NULL;
+       }
+
+       state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, srvid, 
+                          CTDB_CONTROL_REGISTER_SRVID, 0, tdb_null, 
+                          ctdb, NULL, NULL);
+
+       if (state == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to send REGISTER_SRVID control\n"));
+               return NULL;
+       }
+
+       if (callback != NULL) {
+               cb_data = talloc(state, struct ctdb_control_cb_data);
+               cb_data->callback     = callback;
+               cb_data->private_data = private_data;
+
+               state->async.fn           = ctdb_getrecmaster_recv_cb;
+               state->async.private_data = cb_data;
+       }
+
+       return (ctdb_handle *)state;
+}
+
+int ctdb_set_message_handler_recv(struct ctdb_context *ctdb, ctdb_handle *handle)
+{
+       struct ctdb_client_control_state *state = talloc_get_type(handle, struct ctdb_client_control_state);
+       int ret;
+       int32_t res;
+
+       ret = ctdb_control_recv(ctdb, state, state, NULL, &res, NULL);
+       if (ret != 0 || res != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb_set_message_handler_recv failed\n"));
+               return -1;
+       }
+
+       return state->status;
+}
+
+int ctdb_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, ctdb_message_fn_t handler, void *private_data)
+{
+       struct ctdb_client_control_state *state;
+       
+       state = ctdb_set_message_handler_send(ctdb, srvid, NULL, handler, private_data);
+       if (state == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb_set_message_handler_send() failed.\n"));
+               return -1;
+       }
+
+       return ctdb_set_message_handler_recv(ctdb, state);
+}
index c6dde12c247629bd87c6cd3e369b0cabe41c991f..51829b5f71582bb53fc13ed579279240f00dbe35 100644 (file)
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <poll.h>
+#include <fcntl.h>
+#include "lib/tdb/include/tdb.h"
 #include "include/ctdb.h"