- added simple (fake) vnn system
[martins/samba.git] / ctdb / common / ctdb.c
1 /* 
2    ctdb main protocol code
3
4    Copyright (C) Andrew Tridgell  2006
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 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "ctdb_private.h"
26
27 /*
28   choose the transport we will use
29 */
30 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport)
31 {
32         int ctdb_tcp_init(struct ctdb_context *ctdb);
33
34         if (strcmp(transport, "tcp") == 0) {
35                 return ctdb_tcp_init(ctdb);
36         }
37         ctdb_set_error(ctdb, "Unknown transport '%s'\n", transport);
38         return -1;
39 }
40
41
42 /*
43   add a node to the list of active nodes
44 */
45 static int ctdb_add_node(struct ctdb_context *ctdb, char *nstr)
46 {
47         struct ctdb_node *node, **nodep;
48
49         nodep = talloc_realloc(ctdb, ctdb->nodes, struct ctdb_node *, ctdb->num_nodes+1);
50         CTDB_NO_MEMORY(ctdb, nodep);
51
52         ctdb->nodes = nodep;
53         nodep = &ctdb->nodes[ctdb->num_nodes];
54         (*nodep) = talloc_zero(ctdb->nodes, struct ctdb_node);
55         CTDB_NO_MEMORY(ctdb, *nodep);
56         node = *nodep;
57
58         if (ctdb_parse_address(ctdb, node, nstr, &node->address) != 0) {
59                 return -1;
60         }
61         node->ctdb = ctdb;
62         node->name = talloc_asprintf(node, "%s:%u", 
63                                      node->address.address, 
64                                      node->address.port);
65         /* for now we just set the vnn to the line in the file - this
66            will change! */
67         node->vnn = ctdb->num_nodes;
68
69         if (ctdb->methods->add_node(node) != 0) {
70                 talloc_free(node);
71                 return -1;
72         }
73
74         if (ctdb_same_address(&ctdb->address, &node->address)) {
75                 ctdb->vnn = node->vnn;
76         }
77
78         ctdb->num_nodes++;
79
80         return 0;
81 }
82
83 /*
84   setup the node list from a file
85 */
86 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist)
87 {
88         char **lines;
89         int nlines;
90         int i;
91
92         lines = file_lines_load(nlist, &nlines, ctdb);
93         if (lines == NULL) {
94                 ctdb_set_error(ctdb, "Failed to load nlist '%s'\n", nlist);
95                 return -1;
96         }
97
98         for (i=0;i<nlines;i++) {
99                 if (ctdb_add_node(ctdb, lines[i]) != 0) {
100                         talloc_free(lines);
101                         return -1;
102                 }
103         }
104         
105         talloc_free(lines);
106         return 0;
107 }
108
109 /*
110   setup the local node address
111 */
112 int ctdb_set_address(struct ctdb_context *ctdb, const char *address)
113 {
114         if (ctdb_parse_address(ctdb, ctdb, address, &ctdb->address) != 0) {
115                 return -1;
116         }
117         
118         ctdb->name = talloc_asprintf(ctdb, "%s:%u", 
119                                      ctdb->address.address, 
120                                      ctdb->address.port);
121         return 0;
122 }
123
124 /*
125   add a node to the list of active nodes
126 */
127 int ctdb_set_call(struct ctdb_context *ctdb, ctdb_fn_t fn, int id)
128 {
129         struct ctdb_registered_call *call;
130
131         call = talloc(ctdb, struct ctdb_registered_call);
132         call->fn = fn;
133         call->id = id;
134
135         DLIST_ADD(ctdb->calls, call);   
136         return 0;
137 }
138
139 /*
140   start the protocol going
141 */
142 int ctdb_start(struct ctdb_context *ctdb)
143 {
144         return ctdb->methods->start(ctdb);
145 }
146
147 /*
148   called by the transport layer when a packet comes in
149 */
150 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
151 {
152         printf("received pkt of length %d\n", length);
153 }
154
155 /*
156   called by the transport layer when a node is dead
157 */
158 static void ctdb_node_dead(struct ctdb_node *node)
159 {
160         printf("%s: node %s is dead\n", node->ctdb->name, node->name);
161 }
162
163 /*
164   called by the transport layer when a node is dead
165 */
166 static void ctdb_node_connected(struct ctdb_node *node)
167 {
168         printf("%s: connected to %s\n", node->ctdb->name, node->name);
169 }
170
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
175 };
176
177 /*
178   initialise the ctdb daemon. 
179
180   NOTE: In current code the daemon does not fork. This is for testing purposes only
181   and to simplify the code.
182 */
183 struct ctdb_context *ctdb_init(struct event_context *ev)
184 {
185         struct ctdb_context *ctdb;
186
187         ctdb = talloc_zero(ev, struct ctdb_context);
188         ctdb->ev = ev;
189         ctdb->upcalls = &ctdb_upcalls;
190
191         return ctdb;
192 }
193