- split up tcp functions into more logical parts
authorAndrew Tridgell <tridge@samba.org>
Tue, 28 Nov 2006 00:51:33 +0000 (11:51 +1100)
committerAndrew Tridgell <tridge@samba.org>
Tue, 28 Nov 2006 00:51:33 +0000 (11:51 +1100)
- added upcall methods from transport to ctdb layer

(This used to be ctdb commit 59f0dab652000f1c755e59567b03cf84dad7e954)

ctdb/.bzrignore
ctdb/Makefile.in
ctdb/common/ctdb.c
ctdb/include/ctdb_private.h
ctdb/tcp/ctdb_tcp.h
ctdb/tcp/tcp_connect.c [moved from ctdb/tcp/ctdb_tcp.c with 67% similarity]
ctdb/tcp/tcp_init.c [new file with mode: 0644]
ctdb/tcp/tcp_io.c [new file with mode: 0644]

index 71bdda496b686c49f3bacaad17d887675cd1a5e3..26fa17de5c7a2742d3aa3d5e2556f8ae234c07fd 100644 (file)
@@ -5,3 +5,4 @@ common
 config.log
 push.sh
 ctdb_test
+config.cache
index 0bffc2e04a6c00d8f911c938f2e170d1843ea7cd..a818a2ce273a98e7370413eae82755c7c7e2d74b 100644 (file)
@@ -22,7 +22,7 @@ EVENTS_OBJ = lib/events/events.o lib/events/events_standard.o
 
 CTDB_COMMON_OBJ = common/ctdb.o common/util.o
 
-CTDB_TCP_OBJ = tcp/ctdb_tcp.o
+CTDB_TCP_OBJ = tcp/tcp_connect.o tcp/tcp_io.o tcp/tcp_init.o
 
 CTDB_OBJ = $(CTDB_COMMON_OBJ) $(CTDB_TCP_OBJ)
 
index 9d2a6585cfd15ce6deae0efa0f12f71d2a6f8d85..fc82e8cf17af2081079bd7a946eef5a0938ddec8 100644 (file)
 #include "system/filesys.h"
 #include "ctdb_private.h"
 
-/*
-  initialise the ctdb daemon. 
-
-  if the ctdb dispatcher daemon has already been started then this
-  does nothing. Otherwise it forks the ctdb dispatcher daemon and
-  starts the daemons connecting to each other
-  
-  NOTE: In current code the daemon does not fork. This is for testing purposes only
-  and to simplify the code.
-*/
-
-struct ctdb_context *ctdb_init(struct event_context *ev)
-{
-       struct ctdb_context *ctdb;
-
-       ctdb = talloc_zero(ev, struct ctdb_context);
-       ctdb->ev = ev;
-
-       return ctdb;
-}
-
 const char *ctdb_errstr(struct ctdb_context *ctdb)
 {
        return ctdb->err_msg;
@@ -212,3 +191,32 @@ bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
        return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
 }
 
+/*
+  called by the transport layer when a packet comes in
+*/
+static void ctdb_recv_pkt(struct ctdb_node *node, uint8_t *data, uint32_t length)
+{
+       printf("received pkt of length %d\n", length);
+}
+
+static const struct ctdb_upcalls ctdb_upcalls = {
+       .recv_pkt = ctdb_recv_pkt
+};
+
+/*
+  initialise the ctdb daemon. 
+
+  NOTE: In current code the daemon does not fork. This is for testing purposes only
+  and to simplify the code.
+*/
+struct ctdb_context *ctdb_init(struct event_context *ev)
+{
+       struct ctdb_context *ctdb;
+
+       ctdb = talloc_zero(ev, struct ctdb_context);
+       ctdb->ev = ev;
+       ctdb->upcalls = &ctdb_upcalls;
+
+       return ctdb;
+}
+
index 79a60fd8292a342367255eefc4ec8884bdf5bc62..bc13c935c5af7087cf5a6d284a2a94d76bd04cb7 100644 (file)
@@ -53,6 +53,14 @@ struct ctdb_node {
 struct ctdb_methods {
        int (*start)(struct ctdb_context *); /* start protocol processing */    
        int (*add_node)(struct ctdb_node *); /* setup a new node */     
+       int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
+};
+
+/*
+  transport calls up to the ctdb layer
+*/
+struct ctdb_upcalls {
+       void (*recv_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
 };
 
 /* main state of the ctdb daemon */
@@ -64,6 +72,7 @@ struct ctdb_context {
        char *err_msg;
        struct tdb_context *ltdb;
        const struct ctdb_methods *methods; /* transport methods */
+       const struct ctdb_upcalls *upcalls; /* transport upcalls */
        void *private; /* private to transport */
 };
 
index 63dab87ebaab9cd4b8d490019d574350af35b48b..571c20508b46f5a48e83be704eeb43a01a2852f3 100644 (file)
@@ -39,3 +39,14 @@ struct ctdb_tcp_node {
        int fd;
 };
 
+
+/* prototypes internal to tcp transport */
+void ctdb_tcp_node_read(struct event_context *ev, struct fd_event *fde, 
+                       uint16_t flags, void *private);
+void ctdb_tcp_incoming_read(struct event_context *ev, struct fd_event *fde, 
+                           uint16_t flags, void *private);
+int ctdb_tcp_listen(struct ctdb_context *ctdb);
+void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te, 
+                          struct timeval t, void *private);
+
+
similarity index 67%
rename from ctdb/tcp/ctdb_tcp.c
rename to ctdb/tcp/tcp_connect.c
index 19b48e12e9d596f0fac9e594917ebf05f69a373e..82871465832bc66fd45ec3df97ee2ec3782d6432 100644 (file)
 #include "ctdb_private.h"
 #include "ctdb_tcp.h"
 
-/*
-  called when socket becomes readable
-*/
-static void ctdb_node_read(struct event_context *ev, struct fd_event *fde, 
-                          uint16_t flags, void *private)
-{
-       struct ctdb_node *node = talloc_get_type(private, struct ctdb_node);
-       printf("connection to node %s:%u is readable\n", 
-              node->address.address, node->address.port);
-       event_set_fd_flags(fde, 0);
-}
-
-static void ctdb_node_connect(struct event_context *ev, struct timed_event *te, 
-                             struct timeval t, void *private);
-
 /*
   called when socket becomes writeable on connect
 */
@@ -64,7 +49,7 @@ static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *f
                close(tnode->fd);
                tnode->fd = -1;
                event_add_timed(ctdb->ev, node, timeval_current_ofs(1, 0), 
-                               ctdb_node_connect, node);
+                               ctdb_tcp_node_connect, node);
                return;
        }
 
@@ -72,14 +57,14 @@ static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *f
               node->address.address, node->address.port);
        talloc_free(fde);
        event_add_fd(node->ctdb->ev, node, tnode->fd, EVENT_FD_READ, 
-                    ctdb_node_read, node);
+                    ctdb_tcp_node_read, node);
 }
 
 /*
   called when we should try and establish a tcp connection to a node
 */
-static void ctdb_node_connect(struct event_context *ev, struct timed_event *te, 
-                             struct timeval t, void *private)
+void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te, 
+                          struct timeval t, void *private)
 {
        struct ctdb_node *node = talloc_get_type(private, struct ctdb_node);
        struct ctdb_tcp_node *tnode = talloc_get_type(node->private, 
@@ -102,7 +87,7 @@ static void ctdb_node_connect(struct event_context *ev, struct timed_event *te,
                /* try again once a second */
                close(tnode->fd);
                event_add_timed(ctdb->ev, node, timeval_current_ofs(1, 0), 
-                               ctdb_node_connect, node);
+                               ctdb_tcp_node_connect, node);
                return;
        }
 
@@ -111,23 +96,6 @@ static void ctdb_node_connect(struct event_context *ev, struct timed_event *te,
                     ctdb_node_connect_write, node);
 }
 
-/*
-  called when an incoming connection is readable
-*/
-static void ctdb_incoming_read(struct event_context *ev, struct fd_event *fde, 
-                              uint16_t flags, void *private)
-{
-       struct ctdb_incoming *in = talloc_get_type(private, struct ctdb_incoming);
-       char c;
-       printf("Incoming data\n");
-       if (read(in->fd, &c, 1) <= 0) {
-               /* socket is dead */
-               close(in->fd);
-               talloc_free(in);
-       }
-}
-
-
 /*
   called when we get contacted by another node
   currently makes no attempt to check if the connection is really from a ctdb
@@ -155,7 +123,7 @@ static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde,
        in->ctdb = ctdb;
 
        event_add_fd(ctdb->ev, in, in->fd, EVENT_FD_READ, 
-                    ctdb_incoming_read, in);   
+                    ctdb_tcp_incoming_read, in);       
 
        printf("New incoming socket %d\n", in->fd);
 }
@@ -164,7 +132,7 @@ static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde,
 /*
   listen on our own address
 */
-static int ctdb_listen(struct ctdb_context *ctdb)
+int ctdb_tcp_listen(struct ctdb_context *ctdb)
 {
        struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private, struct ctdb_tcp);
         struct sockaddr_in sock;
@@ -202,60 +170,3 @@ static int ctdb_listen(struct ctdb_context *ctdb)
        return 0;
 }
 
-/*
-  start the protocol going
-*/
-int ctdb_tcp_start(struct ctdb_context *ctdb)
-{
-       struct ctdb_node *node;
-
-       /* listen on our own address */
-       if (ctdb_listen(ctdb) != 0) return -1;
-
-       /* startup connections to the other servers - will happen on
-          next event loop */
-       for (node=ctdb->nodes;node;node=node->next) {
-               if (ctdb_same_address(&ctdb->address, &node->address)) continue;
-               event_add_timed(ctdb->ev, node, timeval_zero(), 
-                               ctdb_node_connect, node);
-       }
-
-       return 0;
-}
-
-
-/*
-  initialise tcp portion of a ctdb node 
-*/
-int ctdb_tcp_add_node(struct ctdb_node *node)
-{
-       struct ctdb_tcp_node *tnode;
-       tnode = talloc_zero(node, struct ctdb_tcp_node);
-       CTDB_NO_MEMORY(node->ctdb, tnode);
-
-       tnode->fd = -1;
-       node->private = tnode;
-       return 0;
-}
-
-
-static const struct ctdb_methods ctdb_tcp_methods = {
-       .start    = ctdb_tcp_start,
-       .add_node = ctdb_tcp_add_node
-};
-
-/*
-  initialise tcp portion of ctdb 
-*/
-int ctdb_tcp_init(struct ctdb_context *ctdb)
-{
-       struct ctdb_tcp *ctcp;
-       ctcp = talloc_zero(ctdb, struct ctdb_tcp);
-       CTDB_NO_MEMORY(ctdb, ctcp);
-
-       ctcp->listen_fd = -1;
-       ctdb->private = ctcp;
-       ctdb->methods = &ctdb_tcp_methods;
-       return 0;
-}
-
diff --git a/ctdb/tcp/tcp_init.c b/ctdb/tcp/tcp_init.c
new file mode 100644 (file)
index 0000000..d3ca1e5
--- /dev/null
@@ -0,0 +1,84 @@
+/* 
+   ctdb over TCP
+
+   Copyright (C) Andrew Tridgell  2006
+
+   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,
+   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
+*/
+
+#include "includes.h"
+#include "lib/events/events.h"
+#include "system/network.h"
+#include "system/filesys.h"
+#include "ctdb_private.h"
+#include "ctdb_tcp.h"
+
+/*
+  start the protocol going
+*/
+int ctdb_tcp_start(struct ctdb_context *ctdb)
+{
+       struct ctdb_node *node;
+
+       /* listen on our own address */
+       if (ctdb_tcp_listen(ctdb) != 0) return -1;
+
+       /* startup connections to the other servers - will happen on
+          next event loop */
+       for (node=ctdb->nodes;node;node=node->next) {
+               if (ctdb_same_address(&ctdb->address, &node->address)) continue;
+               event_add_timed(ctdb->ev, node, timeval_zero(), 
+                               ctdb_tcp_node_connect, node);
+       }
+
+       return 0;
+}
+
+
+/*
+  initialise tcp portion of a ctdb node 
+*/
+int ctdb_tcp_add_node(struct ctdb_node *node)
+{
+       struct ctdb_tcp_node *tnode;
+       tnode = talloc_zero(node, struct ctdb_tcp_node);
+       CTDB_NO_MEMORY(node->ctdb, tnode);
+
+       tnode->fd = -1;
+       node->private = tnode;
+       return 0;
+}
+
+
+static const struct ctdb_methods ctdb_tcp_methods = {
+       .start    = ctdb_tcp_start,
+       .add_node = ctdb_tcp_add_node
+};
+
+/*
+  initialise tcp portion of ctdb 
+*/
+int ctdb_tcp_init(struct ctdb_context *ctdb)
+{
+       struct ctdb_tcp *ctcp;
+       ctcp = talloc_zero(ctdb, struct ctdb_tcp);
+       CTDB_NO_MEMORY(ctdb, ctcp);
+
+       ctcp->listen_fd = -1;
+       ctdb->private = ctcp;
+       ctdb->methods = &ctdb_tcp_methods;
+       return 0;
+}
+
diff --git a/ctdb/tcp/tcp_io.c b/ctdb/tcp/tcp_io.c
new file mode 100644 (file)
index 0000000..d522472
--- /dev/null
@@ -0,0 +1,56 @@
+/* 
+   ctdb over TCP
+
+   Copyright (C) Andrew Tridgell  2006
+
+   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,
+   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
+*/
+
+#include "includes.h"
+#include "lib/events/events.h"
+#include "system/network.h"
+#include "system/filesys.h"
+#include "ctdb_private.h"
+#include "ctdb_tcp.h"
+
+/*
+  called when socket becomes readable
+*/
+void ctdb_tcp_node_read(struct event_context *ev, struct fd_event *fde, 
+                       uint16_t flags, void *private)
+{
+       struct ctdb_node *node = talloc_get_type(private, struct ctdb_node);
+       printf("connection to node %s:%u is readable\n", 
+              node->address.address, node->address.port);
+       event_set_fd_flags(fde, 0);
+}
+
+
+/*
+  called when an incoming connection is readable
+*/
+void ctdb_tcp_incoming_read(struct event_context *ev, struct fd_event *fde, 
+                           uint16_t flags, void *private)
+{
+       struct ctdb_incoming *in = talloc_get_type(private, struct ctdb_incoming);
+       char c;
+       printf("Incoming data\n");
+       if (read(in->fd, &c, 1) <= 0) {
+               /* socket is dead */
+               close(in->fd);
+               talloc_free(in);
+       }
+}
+