28811b77f15960efb22ea2843f918bf0280719e1
[jra/samba/.git] / source4 / cluster / ctdb / common / ctdb_message.c
1 /* 
2    ctdb_message protocol code
3
4    Copyright (C) Andrew Tridgell  2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program 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
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20   see http://wiki.samba.org/index.php/Samba_%26_Clustering for
21   protocol design and packet details
22 */
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "../tdb/include/tdb.h"
26 #include "system/network.h"
27 #include "system/filesys.h"
28 #include "../include/ctdb_private.h"
29 #include "lib/util/dlinklist.h"
30
31 /*
32   this dispatches the messages to the registered ctdb message handler
33 */
34 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data)
35 {
36         struct ctdb_message_list *ml;
37
38         for (ml=ctdb->message_list;ml;ml=ml->next) {
39                 if (ml->srvid == srvid || ml->srvid == CTDB_SRVID_ALL) {
40                         ml->message_handler(ctdb, srvid, data, ml->message_private);
41                 }
42         }
43
44         return 0;
45 }
46
47 /*
48   called when a CTDB_REQ_MESSAGE packet comes in
49 */
50 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
51 {
52         struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
53         TDB_DATA data;
54
55         data.dptr = &c->data[0];
56         data.dsize = c->datalen;
57
58         ctdb_dispatch_message(ctdb, c->srvid, data);
59 }
60
61
62 /*
63   when a client goes away, we need to remove its srvid handler from the list
64  */
65 static int message_handler_destructor(struct ctdb_message_list *m)
66 {
67         DLIST_REMOVE(m->ctdb->message_list, m);
68         return 0;
69 }
70
71 /*
72   setup handler for receipt of ctdb messages from ctdb_send_message()
73 */
74 int ctdb_register_message_handler(struct ctdb_context *ctdb, 
75                                   TALLOC_CTX *mem_ctx,
76                                   uint64_t srvid,
77                                   ctdb_message_fn_t handler,
78                                   void *private_data)
79 {
80         struct ctdb_message_list *m;
81
82         m = talloc(mem_ctx, struct ctdb_message_list);
83         CTDB_NO_MEMORY(ctdb, m);
84
85         m->ctdb            = ctdb;
86         m->srvid           = srvid;
87         m->message_handler = handler;
88         m->message_private = private_data;
89         
90         DLIST_ADD(ctdb->message_list, m);
91
92         talloc_set_destructor(m, message_handler_destructor);
93
94         return 0;
95 }
96
97
98 /*
99   setup handler for receipt of ctdb messages from ctdb_send_message()
100 */
101 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data)
102 {
103         struct ctdb_message_list *m;
104
105         for (m=ctdb->message_list;m;m=m->next) {
106                 if (m->srvid == srvid && m->message_private == private_data) {
107                         talloc_free(m);
108                         return 0;
109                 }
110         }
111         return -1;
112 }