do persistent writes in a child process
[metze/ctdb/wip.git] / common / ctdb_message.c
index 6e79042b85559cb7aee85f2367071e7b039b751e..1aea28fd35d8a9f10801eccb6b298b1eb963cb64 100644 (file)
@@ -3,19 +3,18 @@
 
    Copyright (C) Andrew Tridgell  2007
 
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 /*
   see http://wiki.samba.org/index.php/Samba_%26_Clustering for
 #include "system/network.h"
 #include "system/filesys.h"
 #include "../include/ctdb_private.h"
+#include "lib/util/dlinklist.h"
+
+/*
+  this dispatches the messages to the registered ctdb message handler
+*/
+int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data)
+{
+       struct ctdb_message_list *ml;
 
+       for (ml=ctdb->message_list;ml;ml=ml->next) {
+               if (ml->srvid == srvid || ml->srvid == CTDB_SRVID_ALL) {
+                       ml->message_handler(ctdb, srvid, data, ml->message_private);
+               }
+       }
+
+       return 0;
+}
 
 /*
   called when a CTDB_REQ_MESSAGE packet comes in
-
-  this dispatches the messages to the registered ctdb message handler
 */
 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
 {
        struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
        TDB_DATA data;
-       if (ctdb->message_handler == NULL) {
-               printf("no msg handler\n");
-               /* no registered message handler */
-               talloc_free(hdr);
-               return;
-       }
+
        data.dptr = &c->data[0];
        data.dsize = c->datalen;
-       ctdb->message_handler(ctdb, c->srvid, data, ctdb->message_private);
-       talloc_free(hdr);
+
+       ctdb_dispatch_message(ctdb, c->srvid, data);
 }
 
 
 /*
-  send a ctdb message
+  when a client goes away, we need to remove its srvid handler from the list
+ */
+static int message_handler_destructor(struct ctdb_message_list *m)
+{
+       DLIST_REMOVE(m->ctdb->message_list, m);
+       return 0;
+}
+
+/*
+  setup handler for receipt of ctdb messages from ctdb_send_message()
 */
-int ctdb_send_message(struct ctdb_context *ctdb, uint32_t vnn,
-                     uint32_t srvid, TDB_DATA data)
+int ctdb_register_message_handler(struct ctdb_context *ctdb, 
+                                 TALLOC_CTX *mem_ctx,
+                                 uint64_t srvid,
+                                 ctdb_message_fn_t handler,
+                                 void *private_data)
 {
-       struct ctdb_req_message *r;
-       int len;
-
-       len = offsetof(struct ctdb_req_message, data) + data.dsize;
-       r = ctdb->methods->allocate_pkt(ctdb, len);
-       CTDB_NO_MEMORY(ctdb, r);
-
-       r->hdr.length    = len;
-       r->hdr.operation = CTDB_REQ_MESSAGE;
-       r->hdr.destnode  = vnn;
-       r->hdr.srcnode   = ctdb->vnn;
-       r->hdr.reqid     = 0;
-       r->srvid         = srvid;
-       r->datalen       = data.dsize;
-       memcpy(&r->data[0], data.dptr, data.dsize);
+       struct ctdb_message_list *m;
+
+       m = talloc(mem_ctx, struct ctdb_message_list);
+       CTDB_NO_MEMORY(ctdb, m);
+
+       m->ctdb            = ctdb;
+       m->srvid           = srvid;
+       m->message_handler = handler;
+       m->message_private = private_data;
        
-       ctdb_queue_packet(ctdb, &r->hdr);
+       DLIST_ADD(ctdb->message_list, m);
+
+       talloc_set_destructor(m, message_handler_destructor);
 
-       talloc_free(r);
        return 0;
 }
 
+
 /*
   setup handler for receipt of ctdb messages from ctdb_send_message()
 */
-int ctdb_set_message_handler(struct ctdb_context *ctdb, ctdb_message_fn_t handler,
-                            void *private)
+int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data)
 {
-       ctdb->message_handler = handler;
-       ctdb->message_private = private;
-       return 0;
-}
+       struct ctdb_message_list *m;
 
+       for (m=ctdb->message_list;m;m=m->next) {
+               if (m->srvid == srvid && m->message_private == private_data) {
+                       talloc_free(m);
+                       return 0;
+               }
+       }
+       return -1;
+}