2 ctdb main protocol code
4 Copyright (C) Andrew Tridgell 2006
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.
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.
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
22 #include "lib/events/events.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "ctdb_private.h"
28 choose the transport we will use
30 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport)
32 int ctdb_tcp_init(struct ctdb_context *ctdb);
34 if (strcmp(transport, "tcp") == 0) {
35 return ctdb_tcp_init(ctdb);
37 ctdb_set_error(ctdb, "Unknown transport '%s'\n", transport);
43 add a node to the list of active nodes
45 static int ctdb_add_node(struct ctdb_context *ctdb, char *nstr)
47 struct ctdb_node *node, **nodep;
49 nodep = talloc_realloc(ctdb, ctdb->nodes, struct ctdb_node *, ctdb->num_nodes+1);
50 CTDB_NO_MEMORY(ctdb, nodep);
53 nodep = &ctdb->nodes[ctdb->num_nodes];
54 (*nodep) = talloc_zero(ctdb->nodes, struct ctdb_node);
55 CTDB_NO_MEMORY(ctdb, *nodep);
58 if (ctdb_parse_address(ctdb, node, nstr, &node->address) != 0) {
62 node->name = talloc_asprintf(node, "%s:%u",
63 node->address.address,
65 /* for now we just set the vnn to the line in the file - this
67 node->vnn = ctdb->num_nodes;
69 if (ctdb->methods->add_node(node) != 0) {
74 if (ctdb_same_address(&ctdb->address, &node->address)) {
75 ctdb->vnn = node->vnn;
84 setup the node list from a file
86 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist)
92 lines = file_lines_load(nlist, &nlines, ctdb);
94 ctdb_set_error(ctdb, "Failed to load nlist '%s'\n", nlist);
98 for (i=0;i<nlines;i++) {
99 if (ctdb_add_node(ctdb, lines[i]) != 0) {
110 setup the local node address
112 int ctdb_set_address(struct ctdb_context *ctdb, const char *address)
114 if (ctdb_parse_address(ctdb, ctdb, address, &ctdb->address) != 0) {
118 ctdb->name = talloc_asprintf(ctdb, "%s:%u",
119 ctdb->address.address,
125 add a node to the list of active nodes
127 int ctdb_set_call(struct ctdb_context *ctdb, ctdb_fn_t fn, int id)
129 struct ctdb_registered_call *call;
131 call = talloc(ctdb, struct ctdb_registered_call);
135 DLIST_ADD(ctdb->calls, call);
140 start the protocol going
142 int ctdb_start(struct ctdb_context *ctdb)
144 return ctdb->methods->start(ctdb);
148 called by the transport layer when a packet comes in
150 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
152 printf("received pkt of length %d\n", length);
156 called by the transport layer when a node is dead
158 static void ctdb_node_dead(struct ctdb_node *node)
160 printf("%s: node %s is dead\n", node->ctdb->name, node->name);
164 called by the transport layer when a node is dead
166 static void ctdb_node_connected(struct ctdb_node *node)
168 printf("%s: connected to %s\n", node->ctdb->name, node->name);
171 static const struct ctdb_upcalls ctdb_upcalls = {
172 .recv_pkt = ctdb_recv_pkt,
173 .node_dead = ctdb_node_dead,
174 .node_connected = ctdb_node_connected
178 initialise the ctdb daemon.
180 NOTE: In current code the daemon does not fork. This is for testing purposes only
181 and to simplify the code.
183 struct ctdb_context *ctdb_init(struct event_context *ev)
185 struct ctdb_context *ctdb;
187 ctdb = talloc_zero(ev, struct ctdb_context);
189 ctdb->upcalls = &ctdb_upcalls;