4711aba7d24c0629adabbe9661081668e3bebd38
[oweals/gnunet.git] / src / testbed / testbed_api_peers.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file testbed/testbed_api_peers.c
23  * @brief management of the knowledge about peers in this library
24  *        (we know the peer ID, its host, pending operations, etc.)
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "testbed_api_peers.h"
29 #include "testbed_api.h"
30 #include "testbed.h"
31 #include "testbed_api_hosts.h"
32
33
34 /**
35  * Lookup a peer by ID.
36  * 
37  * @param id global peer ID assigned to the peer
38  * @return handle to the host, NULL on error
39  */
40 struct GNUNET_TESTBED_Peer *
41 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
42 {
43   GNUNET_break (0);
44   return NULL;
45 }
46
47
48 /**
49  * Create the given peer at the specified host using the given
50  * controller.  If the given controller is not running on the target
51  * host, it should find or create a controller at the target host and
52  * delegate creating the peer.  Explicit delegation paths can be setup
53  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
54  * path exists, a direct link with a subordinate controller is setup
55  * for the first delegated peer to a particular host; the subordinate
56  * controller is then destroyed once the last peer that was delegated
57  * to the remote host is stopped.  This function is used in particular
58  * if some other controller has already assigned a unique ID to the
59  * peer.
60  *
61  * Creating the peer only creates the handle to manipulate and further
62  * configure the peer; use "GNUNET_TESTBED_peer_start" and
63  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
64  * processes.
65  *
66  * Note that the given configuration will be adjusted by the
67  * controller to avoid port/path conflicts with other peers.
68  * The "final" configuration can be obtained using
69  * 'GNUNET_TESTBED_peer_get_information'.
70  *
71  * @param unique_id unique ID for this peer
72  * @param controller controller process to use
73  * @param host host to run the peer on
74  * @param cfg configuration to use for the peer
75  * @param cb the callback to call when the peer has been created
76  * @param cls the closure to the above callback
77  * @return the operation handle
78  */
79 struct GNUNET_TESTBED_Operation *
80 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
81                                      struct GNUNET_TESTBED_Controller *controller,
82                                      struct GNUNET_TESTBED_Host *host,
83                                      const struct GNUNET_CONFIGURATION_Handle *cfg,
84                                      GNUNET_TESTBED_PeerCreateCallback cb,
85                                      void *cls)
86 {
87   struct GNUNET_TESTBED_Peer *peer;
88   struct PeerCreateData *data;
89   struct GNUNET_TESTBED_Operation *op;
90   struct GNUNET_TESTBED_PeerCreateMessage *msg;
91   char *config;
92   char *xconfig;
93   size_t c_size;
94   size_t xc_size;
95   uint16_t msize;
96
97   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
98   peer->controller = controller;
99   peer->host = host;
100   peer->unique_id = unique_id;
101   data = GNUNET_malloc (sizeof (struct PeerCreateData));
102   data->cb = cb;
103   data->cls = cls;
104   data->peer = peer;
105   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
106   op->controller = controller;
107   op->operation_id = controller->operation_counter++;
108   op->type = OP_PEER_CREATE;
109   op->data = data;
110   config = GNUNET_CONFIGURATION_serialize (cfg, &c_size);
111   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
112   GNUNET_free (config);
113   msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
114   msg = GNUNET_realloc (xconfig, msize);
115   memmove (&msg[1], msg, xc_size); /* Move the compressed config */
116   msg->header.size = htons (msize);
117   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
118   msg->operation_id = GNUNET_htonll (op->operation_id);
119   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (peer->host));
120   msg->peer_id = htonl (peer->unique_id);
121   msg->config_size = htonl (c_size);
122   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
123                                     peer->controller->op_tail, op);
124   GNUNET_TESTBED_queue_message_ (controller,
125                                  (struct GNUNET_MessageHeader *) msg);
126   return op;
127 }
128
129
130 /**
131  * Create the given peer at the specified host using the given
132  * controller.  If the given controller is not running on the target
133  * host, it should find or create a controller at the target host and
134  * delegate creating the peer.  Explicit delegation paths can be setup
135  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
136  * path exists, a direct link with a subordinate controller is setup
137  * for the first delegated peer to a particular host; the subordinate
138  * controller is then destroyed once the last peer that was delegated
139  * to the remote host is stopped.
140  *
141  * Creating the peer only creates the handle to manipulate and further
142  * configure the peer; use "GNUNET_TESTBED_peer_start" and
143  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
144  * processes.
145  *
146  * Note that the given configuration will be adjusted by the
147  * controller to avoid port/path conflicts with other peers.
148  * The "final" configuration can be obtained using
149  * 'GNUNET_TESTBED_peer_get_information'.
150  *
151  * @param controller controller process to use
152  * @param host host to run the peer on
153  * @param cfg configuration to use for the peer
154  * @param cb the callback to call when the peer has been created
155  * @param cls the closure to the above callback
156  * @return the operation handle
157  */
158 struct GNUNET_TESTBED_Operation *
159 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
160                             struct GNUNET_TESTBED_Host *host,
161                             const struct GNUNET_CONFIGURATION_Handle *cfg,
162                             GNUNET_TESTBED_PeerCreateCallback cb,
163                             void *cls)
164 {
165   static uint32_t id_gen;
166
167   return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
168                                               controller,
169                                               host,
170                                               cfg,
171                                               cb, cls);
172 }
173
174
175 /**
176  * Start the given peer.
177  *
178  * @param peer peer to start
179  * @return handle to the operation
180  */
181 struct GNUNET_TESTBED_Operation *
182 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
183 {
184   struct GNUNET_TESTBED_Operation *op;
185   struct GNUNET_TESTBED_PeerStartMessage *msg;
186
187   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
188   op->operation_id = peer->controller->operation_counter++;
189   op->controller = peer->controller;
190   op->type = OP_PEER_START;
191   op->data = peer;
192   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
193   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
194   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
195   msg->peer_id = htonl (peer->unique_id);
196   msg->operation_id = GNUNET_htonll (op->operation_id);
197   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
198                                     peer->controller->op_tail, op);
199   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
200   return op;
201 }
202
203
204 /**
205  * Stop the given peer.  The handle remains valid (use
206  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
207  * state of the peer).
208  *
209  * @param peer peer to stop
210  * @return handle to the operation
211  */
212 struct GNUNET_TESTBED_Operation *
213 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
214 {
215   struct GNUNET_TESTBED_Operation *op;
216   struct GNUNET_TESTBED_PeerStopMessage *msg;
217
218   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
219   op->operation_id = peer->controller->operation_counter++;
220   op->controller = peer->controller;
221   op->type = OP_PEER_STOP;
222   op->data = peer;
223   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
224   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
225   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
226   msg->peer_id = htonl (peer->unique_id);
227   msg->operation_id = GNUNET_htonll (op->operation_id);
228   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
229                                     peer->controller->op_tail, op);
230   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
231   return op;
232 }
233
234
235 /**
236  * Request information about a peer.
237  *
238  * @param peer peer to request information about
239  * @param pit desired information
240  * @return handle to the operation
241  */
242 struct GNUNET_TESTBED_Operation *
243 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
244                                      enum GNUNET_TESTBED_PeerInformationType pit)
245 {
246   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
247   struct GNUNET_TESTBED_Operation *op;
248   struct PeerInfoData *data;
249   
250   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
251   data = GNUNET_malloc (sizeof (struct PeerInfoData));
252   data->peer = peer;
253   data->pit = pit;
254   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
255   op->type = OP_PEER_INFO;
256   op->operation_id = peer->controller->operation_counter++;
257   op->controller = peer->controller;
258   op->data = data;
259   msg = GNUNET_malloc (sizeof (struct
260                                GNUNET_TESTBED_PeerGetConfigurationMessage));
261   msg->header.size = htons
262     (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
263   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
264   msg->peer_id = htonl (peer->unique_id);
265   msg->operation_id = GNUNET_htonll (op->operation_id);
266   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
267                                     peer->controller->op_tail, op);
268   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
269   return op;
270 }
271
272
273 /**
274  * Change peer configuration.  Must only be called while the
275  * peer is stopped.  Ports and paths cannot be changed this
276  * way.
277  *
278  * @param peer peer to change configuration for
279  * @param cfg new configuration (differences to existing
280  *            configuration only)
281  * @return handle to the operation
282  */
283 struct GNUNET_TESTBED_Operation *
284 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
285                                           const struct GNUNET_CONFIGURATION_Handle *cfg)
286 {
287   // FIXME: handle locally or delegate...
288   GNUNET_break (0);
289   return NULL;
290 }
291
292
293 /**
294  * Destroy the given peer; the peer should have been
295  * stopped first (if it was started).
296  *
297  * @param peer peer to stop
298  * @return handle to the operation
299  */
300 struct GNUNET_TESTBED_Operation *
301 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
302 {
303   struct GNUNET_TESTBED_Operation *op;
304   struct PeerDestroyData *data;
305   struct GNUNET_TESTBED_PeerDestroyMessage *msg;
306   
307   data = GNUNET_malloc (sizeof (struct PeerDestroyData));
308   data->peer = peer;
309   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
310   op->operation_id = peer->controller->operation_counter++;
311   op->controller = peer->controller;
312   op->type = OP_PEER_DESTROY;
313   op->data = data;
314   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
315   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
316   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
317   msg->peer_id = htonl (peer->unique_id);
318   msg->operation_id = GNUNET_htonll (op->operation_id);
319   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
320                                     peer->controller->op_tail, op);
321   GNUNET_TESTBED_queue_message_ (peer->controller, 
322                                  (struct GNUNET_MessageHeader *) msg);
323   return op;
324 }
325
326
327 /**
328  * Manipulate the P2P underlay topology by configuring a link
329  * between two peers.  
330  *
331  * @param op_cls closure argument to give with the operation event
332  * @param p1 first peer
333  * @param p2 second peer
334  * @param co option to change
335  * @param ... option-specific values
336  * @return handle to the operation, NULL if configuring the link at this
337  *         time is not allowed
338  */
339 struct GNUNET_TESTBED_Operation *
340 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
341                                         struct GNUNET_TESTBED_Peer *p1,
342                                         struct GNUNET_TESTBED_Peer *p2,
343                                         enum GNUNET_TESTBED_ConnectOption co, ...)
344 {
345   GNUNET_break (0);
346   return NULL;
347 }
348
349
350
351 /**
352  * Both peers must have been started before calling this function.
353  * This function then obtains a HELLO from 'p1', gives it to 'p2'
354  * and asks 'p2' to connect to 'p1'.
355  *
356  * @param op_cls closure argument to give with the operation event
357  * @param p1 first peer
358  * @param p2 second peer
359  * @return handle to the operation, NULL if connecting these two
360  *         peers is fundamentally not possible at this time (peers
361  *         not running or underlay disallows)
362  */
363 struct GNUNET_TESTBED_Operation *
364 GNUNET_TESTBED_overlay_connect (void *op_cls,
365                                 struct GNUNET_TESTBED_Peer *p1,
366                                 struct GNUNET_TESTBED_Peer *p2)
367 {
368   GNUNET_break (0);
369   return NULL;
370 }
371
372
373
374 /* end of testbed_api_peers.c */