peer start/stop with new operations handling
[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 #include "testbed_api_operations.h"
33
34 /**
35  * Function to call to start a peer_create type operation once all
36  * queues the operation is part of declare that the
37  * operation can be activated.
38  *
39  * @param cls the closure from GNUNET_TESTBED_operation_create_()
40  */
41 static void 
42 opstart_peer_create (void *cls)
43 {
44   struct OperationContext *opc = cls;
45   struct PeerCreateData *data;
46   struct GNUNET_TESTBED_PeerCreateMessage *msg;
47   char *config;
48   char *xconfig;
49   size_t c_size;
50   size_t xc_size;
51   uint16_t msize;
52
53   GNUNET_assert (OP_PEER_CREATE == opc->type);  
54   data = opc->data;
55   GNUNET_assert (NULL != data);
56   GNUNET_assert (NULL != data->peer);
57   config = GNUNET_CONFIGURATION_serialize (data->cfg, &c_size);
58   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
59   GNUNET_free (config);
60   msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
61   msg = GNUNET_realloc (xconfig, msize);
62   memmove (&msg[1], msg, xc_size);
63   msg->header.size = htons (msize);
64   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
65   msg->operation_id = GNUNET_htonll (opc->id);
66   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->peer->host));
67   msg->peer_id = htonl (data->peer->unique_id);
68   msg->config_size = htonl (c_size);
69   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head,
70                                     opc->c->ocq_tail, opc);
71   GNUNET_TESTBED_queue_message_ (opc->c,
72                                  (struct GNUNET_MessageHeader *) msg);
73 };
74
75
76 /**
77  * Callback which will be called when peer_create type operation is released
78  *
79  * @param cls the closure from GNUNET_TESTBED_operation_create_()
80  */
81 static void 
82 oprelease_peer_create (void *cls)
83 {
84   struct OperationContext *opc = cls;  
85
86   GNUNET_assert (NULL != opc->data);
87   GNUNET_free (opc->data);  
88   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
89   GNUNET_free (opc);
90 }
91
92
93 /**
94  * Function to called when a peer destroy operation is ready
95  *
96  * @param cls the closure from GNUNET_TESTBED_operation_create_()
97  */
98 static void 
99 opstart_peer_destroy (void *cls)
100 {
101   struct OperationContext *opc = cls;
102   struct GNUNET_TESTBED_Peer *peer;
103   struct GNUNET_TESTBED_PeerDestroyMessage *msg;
104
105   GNUNET_assert (OP_PEER_DESTROY == opc->type);
106   peer = opc->data;
107   GNUNET_assert (NULL != peer);
108   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
109   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
110   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
111   msg->peer_id = htonl (peer->unique_id);
112   msg->operation_id = GNUNET_htonll (opc->id);
113   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head,
114                                     opc->c->ocq_tail, opc);
115   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
116 }
117
118
119 /**
120  * Callback which will be called when peer_create type operation is released
121  *
122  * @param cls the closure from GNUNET_TESTBED_operation_create_()
123  */
124 static void 
125 oprelease_peer_destroy (void *cls)
126 {
127   struct OperationContext *opc = cls;
128   
129   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
130   GNUNET_free (opc);
131 }
132
133
134 /**
135  * Function to called when a peer start operation is ready
136  *
137  * @param cls the closure from GNUNET_TESTBED_operation_create_()
138  */
139 static void 
140 opstart_peer_start (void *cls)
141 {
142   struct OperationContext *opc = cls;
143   struct GNUNET_TESTBED_PeerStartMessage *msg;
144   struct GNUNET_TESTBED_Peer *peer;
145
146   GNUNET_assert (OP_PEER_START == opc->type);
147   GNUNET_assert (NULL != opc->data);
148   peer = opc->data;
149   GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
150   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
151   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
152   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
153   msg->peer_id = htonl (peer->unique_id);
154   msg->operation_id = GNUNET_htonll (opc->id);
155   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
156   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
157 }
158
159
160 /**
161  * Callback which will be called when peer start type operation is released
162  *
163  * @param cls the closure from GNUNET_TESTBED_operation_create_()
164  */
165 static void 
166 oprelease_peer_start (void *cls)
167 {
168   struct OperationContext *opc = cls;
169   
170   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
171   GNUNET_free (opc);
172 }
173
174
175 /**
176  * Function to called when a peer stop operation is ready
177  *
178  * @param cls the closure from GNUNET_TESTBED_operation_create_()
179  */
180 static void 
181 opstart_peer_stop (void *cls)
182 {
183   struct OperationContext *opc = cls;
184   struct GNUNET_TESTBED_PeerStopMessage *msg;
185   struct GNUNET_TESTBED_Peer *peer;
186
187   GNUNET_assert (NULL != opc->data);
188   peer = opc->data;
189   GNUNET_assert (PS_STARTED == peer->state); 
190   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
191   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
192   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
193   msg->peer_id = htonl (peer->unique_id);
194   msg->operation_id = GNUNET_htonll (opc->id);
195   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
196   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
197 }
198
199
200 /**
201  * Callback which will be called when peer stop type operation is released
202  *
203  * @param cls the closure from GNUNET_TESTBED_operation_create_()
204  */
205 static void 
206 oprelease_peer_stop (void *cls)
207 {
208   struct OperationContext *opc = cls;
209   
210   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
211   GNUNET_free (opc);
212 }
213
214
215 /**
216  * Lookup a peer by ID.
217  * 
218  * @param id global peer ID assigned to the peer
219  * @return handle to the host, NULL on error
220  */
221 struct GNUNET_TESTBED_Peer *
222 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
223 {
224   GNUNET_break (0);
225   return NULL;
226 }
227
228
229 /**
230  * Create the given peer at the specified host using the given
231  * controller.  If the given controller is not running on the target
232  * host, it should find or create a controller at the target host and
233  * delegate creating the peer.  Explicit delegation paths can be setup
234  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
235  * path exists, a direct link with a subordinate controller is setup
236  * for the first delegated peer to a particular host; the subordinate
237  * controller is then destroyed once the last peer that was delegated
238  * to the remote host is stopped.  This function is used in particular
239  * if some other controller has already assigned a unique ID to the
240  * peer.
241  *
242  * Creating the peer only creates the handle to manipulate and further
243  * configure the peer; use "GNUNET_TESTBED_peer_start" and
244  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
245  * processes.
246  *
247  * Note that the given configuration will be adjusted by the
248  * controller to avoid port/path conflicts with other peers.
249  * The "final" configuration can be obtained using
250  * 'GNUNET_TESTBED_peer_get_information'.
251  *
252  * @param unique_id unique ID for this peer
253  * @param controller controller process to use
254  * @param host host to run the peer on
255  * @param cfg Template configuration to use for the peer. Should exist until
256  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
257  * @param cb the callback to call when the peer has been created
258  * @param cls the closure to the above callback
259  * @return the operation handle
260  */
261 struct GNUNET_TESTBED_Operation *
262 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
263                                      struct GNUNET_TESTBED_Controller *controller,
264                                      struct GNUNET_TESTBED_Host *host,
265                                      const struct GNUNET_CONFIGURATION_Handle *cfg,
266                                      GNUNET_TESTBED_PeerCreateCallback cb,
267                                      void *cls)
268 {
269   struct GNUNET_TESTBED_Peer *peer;
270   struct PeerCreateData *data;
271   struct OperationContext *opc;
272
273   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
274   peer->controller = controller;
275   peer->host = host;
276   peer->unique_id = unique_id;
277   peer->state = PS_INVALID;
278   data = GNUNET_malloc (sizeof (struct PeerCreateData));
279   data->host = host;
280   data->cfg = cfg;
281   data->cb = cb;
282   data->cls = cls;
283   data->peer = peer;
284   opc = GNUNET_malloc (sizeof (struct OperationContext));
285   opc->c = controller;
286   opc->data = data;
287   opc->id = controller->operation_counter++;
288   opc->type = OP_PEER_CREATE;
289   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
290                                               &oprelease_peer_create);
291   GNUNET_TESTBED_operation_queue_insert_ (controller->opq_peer_create, opc->op);
292   return opc->op;
293 }
294
295
296 /**
297  * Create the given peer at the specified host using the given
298  * controller.  If the given controller is not running on the target
299  * host, it should find or create a controller at the target host and
300  * delegate creating the peer.  Explicit delegation paths can be setup
301  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
302  * path exists, a direct link with a subordinate controller is setup
303  * for the first delegated peer to a particular host; the subordinate
304  * controller is then destroyed once the last peer that was delegated
305  * to the remote host is stopped.
306  *
307  * Creating the peer only creates the handle to manipulate and further
308  * configure the peer; use "GNUNET_TESTBED_peer_start" and
309  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
310  * processes.
311  *
312  * Note that the given configuration will be adjusted by the
313  * controller to avoid port/path conflicts with other peers.
314  * The "final" configuration can be obtained using
315  * 'GNUNET_TESTBED_peer_get_information'.
316  *
317  * @param controller controller process to use
318  * @param host host to run the peer on
319  * @param cfg Template configuration to use for the peer. Should exist until
320  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
321  * @param cb the callback to call when the peer has been created
322  * @param cls the closure to the above callback
323  * @return the operation handle
324  */
325 struct GNUNET_TESTBED_Operation *
326 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
327                             struct GNUNET_TESTBED_Host *host,
328                             const struct GNUNET_CONFIGURATION_Handle *cfg,
329                             GNUNET_TESTBED_PeerCreateCallback cb,
330                             void *cls)
331 {
332   static uint32_t id_gen;
333
334   return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
335                                               controller,
336                                               host,
337                                               cfg,
338                                               cb, cls);
339 }
340
341
342 /**
343  * Start the given peer.
344  *
345  * @param peer peer to start
346  * @return handle to the operation
347  */
348 struct GNUNET_TESTBED_Operation *
349 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
350 {
351   struct OperationContext *opc;
352
353   opc = GNUNET_malloc (sizeof (struct OperationContext));
354   opc->c = peer->controller;
355   opc->data = peer;
356   opc->id = opc->c->operation_counter++;
357   opc->type = OP_PEER_START;
358   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
359                                              &oprelease_peer_start);
360   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
361   return opc->op;  
362 }
363
364
365 /**
366  * Stop the given peer.  The handle remains valid (use
367  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
368  * state of the peer).
369  *
370  * @param peer peer to stop
371  * @return handle to the operation
372  */
373 struct GNUNET_TESTBED_Operation *
374 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
375 {
376   struct OperationContext *opc;
377
378   opc = GNUNET_malloc (sizeof (struct OperationContext));
379   opc->c = peer->controller;
380   opc->data = peer;
381   opc->id = opc->c->operation_counter++;
382   opc->type = OP_PEER_STOP;
383   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
384                                               &oprelease_peer_stop);
385   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
386   return opc->op;
387 }
388
389
390 /**
391  * Request information about a peer.
392  *
393  * @param peer peer to request information about
394  * @param pit desired information
395  * @return handle to the operation
396  */
397 struct GNUNET_TESTBED_Operation *
398 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
399                                      enum GNUNET_TESTBED_PeerInformationType pit)
400 {
401   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
402   struct GNUNET_TESTBED_Operation *op;
403   struct PeerInfoData *data;
404   
405   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
406   data = GNUNET_malloc (sizeof (struct PeerInfoData));
407   data->peer = peer;
408   data->pit = pit;
409   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
410   op->type = OP_PEER_INFO;
411   op->operation_id = peer->controller->operation_counter++;
412   op->controller = peer->controller;
413   op->data = data;
414   msg = GNUNET_malloc (sizeof (struct
415                                GNUNET_TESTBED_PeerGetConfigurationMessage));
416   msg->header.size = htons
417     (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
418   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
419   msg->peer_id = htonl (peer->unique_id);
420   msg->operation_id = GNUNET_htonll (op->operation_id);
421   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
422                                     peer->controller->op_tail, op);
423   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
424   return op;
425 }
426
427
428 /**
429  * Change peer configuration.  Must only be called while the
430  * peer is stopped.  Ports and paths cannot be changed this
431  * way.
432  *
433  * @param peer peer to change configuration for
434  * @param cfg new configuration (differences to existing
435  *            configuration only)
436  * @return handle to the operation
437  */
438 struct GNUNET_TESTBED_Operation *
439 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
440                                           const struct GNUNET_CONFIGURATION_Handle *cfg)
441 {
442   // FIXME: handle locally or delegate...
443   GNUNET_break (0);
444   return NULL;
445 }
446
447
448 /**
449  * Destroy the given peer; the peer should have been
450  * stopped first (if it was started).
451  *
452  * @param peer peer to stop
453  * @return handle to the operation
454  */
455 struct GNUNET_TESTBED_Operation *
456 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
457 {
458   struct OperationContext *opc;
459
460   opc = GNUNET_malloc (sizeof (struct OperationContext));
461   opc->data = peer;
462   opc->c = peer->controller;
463   opc->id = peer->controller->operation_counter++;
464   opc->type = OP_PEER_DESTROY;
465   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
466                                               &oprelease_peer_destroy);
467   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create,
468                                           opc->op);
469   return opc->op;
470 }
471
472
473 /**
474  * Manipulate the P2P underlay topology by configuring a link
475  * between two peers.  
476  *
477  * @param op_cls closure argument to give with the operation event
478  * @param p1 first peer
479  * @param p2 second peer
480  * @param co option to change
481  * @param ... option-specific values
482  * @return handle to the operation, NULL if configuring the link at this
483  *         time is not allowed
484  */
485 struct GNUNET_TESTBED_Operation *
486 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
487                                         struct GNUNET_TESTBED_Peer *p1,
488                                         struct GNUNET_TESTBED_Peer *p2,
489                                         enum GNUNET_TESTBED_ConnectOption co, ...)
490 {
491   GNUNET_break (0);
492   return NULL;
493 }
494
495
496
497 /**
498  * Both peers must have been started before calling this function.
499  * This function then obtains a HELLO from 'p1', gives it to 'p2'
500  * and asks 'p2' to connect to 'p1'.
501  *
502  * @param op_cls closure argument to give with the operation event
503  * @param p1 first peer
504  * @param p2 second peer
505  * @return handle to the operation, NULL if connecting these two
506  *         peers is fundamentally not possible at this time (peers
507  *         not running or underlay disallows)
508  */
509 struct GNUNET_TESTBED_Operation *
510 GNUNET_TESTBED_overlay_connect (void *op_cls,
511                                 struct GNUNET_TESTBED_Peer *p1,
512                                 struct GNUNET_TESTBED_Peer *p2)
513 {
514   struct GNUNET_TESTBED_Operation *op;
515   struct OverlayConnectData *data;
516   struct GNUNET_TESTBED_OverlayConnectMessage *msg;
517   
518   GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
519   GNUNET_assert (p1->controller == p2->controller);
520   data = GNUNET_malloc (sizeof (struct OverlayConnectData));
521   data->p1 = p1;
522   data->p2 = p2;  
523   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation)); 
524   op->controller = p1->controller;
525   op->operation_id = op->controller->operation_counter++;
526   op->type = OP_OVERLAY_CONNECT;
527   op->data = data;
528   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
529   msg->header.size = htons (sizeof (struct
530                                     GNUNET_TESTBED_OverlayConnectMessage));
531   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
532   msg->peer1 = htonl (p1->unique_id);
533   msg->peer2 = htonl (p2->unique_id);
534   msg->operation_id = GNUNET_htonll (op->operation_id);
535   GNUNET_CONTAINER_DLL_insert_tail (op->controller->op_head,
536                                     op->controller->op_tail, op);
537   GNUNET_TESTBED_queue_message_ (op->controller, 
538                                  (struct GNUNET_MessageHeader *) msg);
539   return NULL;
540 }
541
542
543
544 /* end of testbed_api_peers.c */