partially completed work towards full messaging system which will work in both daemon...
[sahlberg/ctdb.git] / common / ctdb_message.c
1 /* 
2    ctdb_message protocol code
3
4    Copyright (C) Andrew Tridgell  2007
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20 /*
21   see http://wiki.samba.org/index.php/Samba_%26_Clustering for
22   protocol design and packet details
23 */
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "lib/tdb/include/tdb.h"
27 #include "system/network.h"
28 #include "system/filesys.h"
29 #include "../include/ctdb_private.h"
30
31
32 /*
33   called when a CTDB_REQ_MESSAGE packet comes in
34
35   this dispatches the messages to the registered ctdb message handler
36 */
37 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
38 {
39         struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
40         TDB_DATA data;
41         if (ctdb->message_handler == NULL) {
42                 printf("no msg handler\n");
43                 /* no registered message handler */
44                 return;
45         }
46         data.dptr = &c->data[0];
47         data.dsize = c->datalen;
48         ctdb->message_handler(ctdb, c->srvid, data, ctdb->message_private);
49 }
50
51
52 /*
53   send a ctdb message
54 */
55 int ctdb_send_message(struct ctdb_context *ctdb, uint32_t vnn,
56                       uint32_t srvid, TDB_DATA data)
57 {
58         struct ctdb_req_message *r;
59         int len;
60
61         len = offsetof(struct ctdb_req_message, data) + data.dsize;
62         r = ctdb->methods->allocate_pkt(ctdb, len);
63         CTDB_NO_MEMORY(ctdb, r);
64         talloc_set_name_const(r, "req_message packet");
65
66         r->hdr.length    = len;
67         r->hdr.ctdb_magic = CTDB_MAGIC;
68         r->hdr.ctdb_version = CTDB_VERSION;
69         r->hdr.operation = CTDB_REQ_MESSAGE;
70         r->hdr.destnode  = vnn;
71         r->hdr.srcnode   = ctdb->vnn;
72         r->hdr.reqid     = 0;
73         r->srvid         = srvid;
74         r->datalen       = data.dsize;
75         memcpy(&r->data[0], data.dptr, data.dsize);
76         
77         ctdb_queue_packet(ctdb, &r->hdr);
78
79         talloc_free(r);
80         return 0;
81 }
82
83 /*
84   when a client goes away, we need to remove its srvid handler from the list
85  */
86 static int message_handler_destructor(struct ctdb_message_list *m)
87 {
88         DLIST_REMOVE(m->ctdb->message_list, m);
89 }
90
91 /*
92   setup handler for receipt of ctdb messages from ctdb_send_message()
93 */
94 int ctdb_register_message_handler(struct ctdb_context *ctdb, 
95                                   TALLOC_CTX *mem_ctx,
96                                   uint32_t srvid,
97                                   ctdb_message_fn_t handler,
98                                   void *private)
99 {
100         struct ctdb_message_list *m;
101
102         m = talloc(mem_ctx, struct ctdb_message_list);
103         CTDB_NO_MEMORY(ctdb, m);
104
105         m->ctdb            = ctdb;
106         m->srvid           = srvid;
107         m->message_handler = handler;
108         m->message_private = private;
109         
110         DLIST_ADD(ctdb->message_list, m);
111
112         talloc_set_destructor(m, message_handler_destructor);
113
114         return 0;
115 }