1 #include "libctdb_private.h"
6 #include <ctdb_protocol.h>
10 /* Remove type-safety macros. */
11 #undef ctdb_set_message_handler_send
12 #undef ctdb_set_message_handler_recv
13 #undef ctdb_remove_message_handler_send
15 struct message_handler_info {
16 struct message_handler_info *next, *prev;
19 ctdb_message_fn_t handler;
23 void deliver_message(struct ctdb_connection *ctdb, struct ctdb_req_header *hdr)
25 struct message_handler_info *i;
26 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
29 data.dptr = msg->data;
30 data.dsize = msg->datalen;
32 for (i = ctdb->message_handlers; i; i = i->next) {
33 if (i->srvid == msg->srvid) {
34 i->handler(ctdb, msg->srvid, data, i->private_data);
37 /* FIXME: Report unknown messages */
40 int ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
41 struct ctdb_request *req)
43 struct message_handler_info *info = req->extra;
44 struct ctdb_reply_control *reply;
46 reply = unpack_reply_control(ctdb, req, CTDB_CONTROL_REGISTER_SRVID);
47 if (!reply || reply->status != 0) {
51 /* Put ourselves in list of handlers. */
52 DLIST_ADD(ctdb->message_handlers, info);
53 /* Keep safe from destructor */
58 static void free_info(struct ctdb_connection *ctdb, struct ctdb_request *req)
64 ctdb_set_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
65 ctdb_message_fn_t handler,
66 ctdb_callback_t callback, void *private_data)
68 struct message_handler_info *info;
69 struct ctdb_request *req;
71 info = malloc(sizeof(*info));
76 req = new_ctdb_control_request(ctdb, CTDB_CONTROL_REGISTER_SRVID,
77 CTDB_CURRENT_NODE, NULL, 0,
78 callback, private_data);
84 req->extra_destructor = free_info;
85 req->hdr.control->srvid = srvid;
88 info->handler = handler;
89 info->private_data = private_data;
94 int ctdb_send_message(struct ctdb_connection *ctdb,
95 uint32_t pnn, uint64_t srvid,
98 struct ctdb_request *req;
99 struct ctdb_req_message *pkt;
101 /* We just discard it once it's finished: no reply. */
102 req = new_ctdb_request(offsetof(struct ctdb_req_message, data) + data.dsize,
103 ctdb_cancel_callback, NULL);
108 io_elem_init_req_header(req->io,
109 CTDB_REQ_MESSAGE, pnn, new_reqid(ctdb));
111 pkt = req->hdr.message;
113 pkt->datalen = data.dsize;
114 memcpy(pkt->data, data.dptr, data.dsize);
115 DLIST_ADD_END(ctdb->outq, req, struct ctdb_request);