#ifndef _CTDB_H
#define _CTDB_H
+/* Functions are not thread safe so all function calls must be wrapped
+ * inside a pthread_mutex for threaded applications.
+ *
+ * All _send() functions are guaranteed to be non-blocking and fully
+ * asynchronous.
+ *
+ * Avoid using the synchronous calls
+ */
+
+/*
+ * Connect to ctdb using the specified domain socket.
+ * Returns a ctdb context if successful or NULL.
+ */
struct ctdb_context *ctdb_connect(const char *addr);
int ctdb_get_fd(struct ctdb_context *ctdb);
/*
- * function to cancel a request/call
+ * messaging functions
+ * these functions provide a messaging layer for applications to communicate
+ * with eachother across
*/
-int ctdb_cancel(ctdb_handle *);
-
-
+typedef void (*ctdb_message_fn_t)(struct ctdb_context *, uint64_t srvid, TDB_DATA data, void *);
/*
- * messaging functions
+ * register a message handler and start listening on a service port
*/
-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_message_fn_t handler, void *private_data);
+
+/*
+ * unregister a message handler and stop listening on teh specified port
+ */
typedef void (*ctdb_remove_message_handler_cb)(int32_t status, void *private_data);
ctdb_handle *
int ctdb_remove_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
void *private_data);
+/*
+ * send a message to a specific node/port
+ * this function is non-blocking
+ */
int ctdb_send_message(struct ctdb_context *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
+/*
+ * functions to read the pnn number of the local node
+ */
+typedef void (*ctdb_getpnn_cb)(int32_t status, int32_t pnn, void *private_data);
+
+ctdb_handle *
+ctdb_getpnn_send(struct ctdb_context *ctdb,
+ ctdb_getpnn_cb callback,
+ void *private_data);
+int ctdb_getpnn_recv(struct ctdb_context *ctdb,
+ ctdb_handle *handle,
+ uint32_t *pnn);
+int ctdb_getpnn(struct ctdb_context *ctdb,
+ uint32_t *pnn);
+
+
+
+
/*
* functions to read the recovery master of a node
*/
-typedef void (*ctdb_get_recmaster_cb)(int32_t status, int32_t recmaster, void *private_data);
+typedef void (*ctdb_getrecmaster_cb)(int32_t status, int32_t recmaster, void *private_data);
ctdb_handle *
ctdb_getrecmaster_send(struct ctdb_context *ctdb,
uint32_t destnode,
- ctdb_get_recmaster_cb callback,
+ ctdb_getrecmaster_cb callback,
void *private_data);
int ctdb_getrecmaster_recv(struct ctdb_context *ctdb,
ctdb_handle *handle,
uint32_t *recmaster);
+
+
+/*
+ * cancel a request/call
+ */
+int ctdb_cancel(ctdb_handle *);
+
+
+
#endif
void *private_data;
};
+
+
+
+/*************************
+ * GET PNN of local node *
+ *************************/
static void
-ctdb_getrecmaster_recv_cb(struct ctdb_client_control_state *state)
+ctdb_getpnn_recv_cb(struct ctdb_client_control_state *state)
{
struct ctdb_control_cb_data *cb_data = state->async.private_data;
- ctdb_get_recmaster_cb callback = (ctdb_get_recmaster_cb)cb_data->callback;
+ ctdb_getpnn_cb callback = (ctdb_getpnn_cb)cb_data->callback;
callback(0, state->status, cb_data->private_data);
}
+ctdb_handle *
+ctdb_getpnn_send(struct ctdb_context *ctdb,
+ ctdb_getpnn_cb callback,
+ void *private_data)
+{
+ struct ctdb_client_control_state *state;
+ struct ctdb_control_cb_data *cb_data;
+
+ state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0,
+ CTDB_CONTROL_GET_PNN, 0, tdb_null,
+ ctdb, NULL);
+
+ if (state == NULL) {
+ DEBUG(DEBUG_ERR,(__location__ " Failed to send GET_PNN 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_getpnn_recv_cb;
+ state->async.private_data = cb_data;
+ }
+
+ return (ctdb_handle *)state;
+}
+
+int ctdb_getpnn_recv(struct ctdb_context *ctdb, ctdb_handle *handle, uint32_t *pnn)
+{
+ 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) {
+ DEBUG(DEBUG_ERR,(__location__ " ctdb_getpnn_recv failed\n"));
+ return -1;
+ }
+
+ if (pnn != NULL) {
+ *pnn = (uint32_t)res;
+ }
+
+ return state->status;
+}
+
+int ctdb_getpnn(struct ctdb_context *ctdb, uint32_t *pnn)
+{
+ struct ctdb_client_control_state *state;
+
+ state = ctdb_getpnn_send(ctdb, NULL, NULL);
+ if (state == NULL) {
+ DEBUG(DEBUG_ERR,(__location__ " ctdb_getpnn_send() failed.\n"));
+ return -1;
+ }
+
+ return ctdb_getpnn_recv(ctdb, state, pnn);
+}
+
+
+
+/***********************
+ * GET RECOVERY MASTER *
+ ***********************/
+static void
+ctdb_getrecmaster_recv_cb(struct ctdb_client_control_state *state)
+{
+ struct ctdb_control_cb_data *cb_data = state->async.private_data;
+ ctdb_getrecmaster_cb callback = (ctdb_getrecmaster_cb)cb_data->callback;
+
+ callback(0, state->status, cb_data->private_data);
+}
-/*
- get the recovery master of a remote node
- */
ctdb_handle *
ctdb_getrecmaster_send(struct ctdb_context *ctdb,
uint32_t destnode,
- ctdb_get_recmaster_cb callback,
+ ctdb_getrecmaster_cb callback,
void *private_data)
{
struct ctdb_client_control_state *state;
return -1;
}
- if (recmaster) {
+ if (recmaster != NULL) {
*recmaster = (uint32_t)res;
}
{
struct ctdb_client_control_state *state;
- state = ctdb_getrecmaster_send(ctdb, destnode, NULL, recmaster);
+ state = ctdb_getrecmaster_send(ctdb, destnode, NULL, NULL);
if (state == NULL) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_getrecmaster_send() failed.\n"));
return -1;
}
+
+
+
+
+
+
+
+
+
static void
ctdb_set_message_handler_recv_cb(struct ctdb_client_control_state *state)
{
#include <stdio.h>
#include <stdint.h>
+#include <stdlib.h>
#include <poll.h>
#include <fcntl.h>
+#include <string.h>
#include "lib/tdb/include/tdb.h"
#include "include/ctdb.h"
}
+void pnn_cb(int32_t status, int32_t pnn, void *private_data)
+{
+ printf("status:%d pnn:%d\n", status, pnn);
+}
+
void rm_cb(int32_t status, int32_t recmaster, void *private_data)
{
- printf("recmaster:%d\n", recmaster);
+ printf("status:%d recmaster:%d\n", status, recmaster);
}
int main(int argc, char *argv[])
ctdb_context = ctdb_connect("/tmp/ctdb.socket");
- pfd.fd = ctdb_get_fd(ctdb_context);
-
handle = ctdb_set_message_handler_send(ctdb_context, 55, NULL, msg_h, NULL);
if (handle == NULL) {
printf("Failed to register message port\n");
exit(10);
}
+ handle = ctdb_getpnn_send(ctdb_context, pnn_cb, NULL);
+ if (handle == NULL) {
+ printf("Failed to send get_pnn control\n");
+ exit(10);
+ }
+
handle = ctdb_getrecmaster_send(ctdb_context, 0, rm_cb, NULL);
if (handle == NULL) {
printf("Failed to send get_recmaster control\n");
}
+ pfd.fd = ctdb_get_fd(ctdb_context);
for (;;) {
-
pfd.events = ctdb_which_events(ctdb_context);
if (poll(&pfd, 1, -1) < 0) {
printf("Poll failed");