peer destroy 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,
116                                  (struct GNUNET_MessageHeader *) msg);
117 }
118
119
120 /**
121  * Callback which will be called when peer_create type operation is released
122  *
123  * @param cls the closure from GNUNET_TESTBED_operation_create_()
124  */
125 static void 
126 oprelease_peer_destroy (void *cls)
127 {
128   struct OperationContext *opc = cls;
129   
130   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
131   GNUNET_free (opc);
132 }
133
134
135 /**
136  * Lookup a peer by ID.
137  * 
138  * @param id global peer ID assigned to the peer
139  * @return handle to the host, NULL on error
140  */
141 struct GNUNET_TESTBED_Peer *
142 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
143 {
144   GNUNET_break (0);
145   return NULL;
146 }
147
148
149 /**
150  * Create the given peer at the specified host using the given
151  * controller.  If the given controller is not running on the target
152  * host, it should find or create a controller at the target host and
153  * delegate creating the peer.  Explicit delegation paths can be setup
154  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
155  * path exists, a direct link with a subordinate controller is setup
156  * for the first delegated peer to a particular host; the subordinate
157  * controller is then destroyed once the last peer that was delegated
158  * to the remote host is stopped.  This function is used in particular
159  * if some other controller has already assigned a unique ID to the
160  * peer.
161  *
162  * Creating the peer only creates the handle to manipulate and further
163  * configure the peer; use "GNUNET_TESTBED_peer_start" and
164  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
165  * processes.
166  *
167  * Note that the given configuration will be adjusted by the
168  * controller to avoid port/path conflicts with other peers.
169  * The "final" configuration can be obtained using
170  * 'GNUNET_TESTBED_peer_get_information'.
171  *
172  * @param unique_id unique ID for this peer
173  * @param controller controller process to use
174  * @param host host to run the peer on
175  * @param cfg Template configuration to use for the peer. Should exist until
176  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
177  * @param cb the callback to call when the peer has been created
178  * @param cls the closure to the above callback
179  * @return the operation handle
180  */
181 struct GNUNET_TESTBED_Operation *
182 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
183                                      struct GNUNET_TESTBED_Controller *controller,
184                                      struct GNUNET_TESTBED_Host *host,
185                                      const struct GNUNET_CONFIGURATION_Handle *cfg,
186                                      GNUNET_TESTBED_PeerCreateCallback cb,
187                                      void *cls)
188 {
189   struct GNUNET_TESTBED_Peer *peer;
190   struct PeerCreateData *data;
191   struct OperationContext *opc;
192
193   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
194   peer->controller = controller;
195   peer->host = host;
196   peer->unique_id = unique_id;
197   peer->state = PS_INVALID;
198   data = GNUNET_malloc (sizeof (struct PeerCreateData));
199   data->host = host;
200   data->cfg = cfg;
201   data->cb = cb;
202   data->cls = cls;
203   data->peer = peer;
204   opc = GNUNET_malloc (sizeof (struct OperationContext));
205   opc->c = controller;
206   opc->data = data;
207   opc->id = controller->operation_counter++;
208   opc->type = OP_PEER_CREATE;
209   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
210                                               &oprelease_peer_create);
211   GNUNET_TESTBED_operation_queue_insert_ (controller->opq_peer_create, opc->op);
212   return opc->op;
213 }
214
215
216 /**
217  * Create the given peer at the specified host using the given
218  * controller.  If the given controller is not running on the target
219  * host, it should find or create a controller at the target host and
220  * delegate creating the peer.  Explicit delegation paths can be setup
221  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
222  * path exists, a direct link with a subordinate controller is setup
223  * for the first delegated peer to a particular host; the subordinate
224  * controller is then destroyed once the last peer that was delegated
225  * to the remote host is stopped.
226  *
227  * Creating the peer only creates the handle to manipulate and further
228  * configure the peer; use "GNUNET_TESTBED_peer_start" and
229  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
230  * processes.
231  *
232  * Note that the given configuration will be adjusted by the
233  * controller to avoid port/path conflicts with other peers.
234  * The "final" configuration can be obtained using
235  * 'GNUNET_TESTBED_peer_get_information'.
236  *
237  * @param controller controller process to use
238  * @param host host to run the peer on
239  * @param cfg Template configuration to use for the peer. Should exist until
240  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
241  * @param cb the callback to call when the peer has been created
242  * @param cls the closure to the above callback
243  * @return the operation handle
244  */
245 struct GNUNET_TESTBED_Operation *
246 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
247                             struct GNUNET_TESTBED_Host *host,
248                             const struct GNUNET_CONFIGURATION_Handle *cfg,
249                             GNUNET_TESTBED_PeerCreateCallback cb,
250                             void *cls)
251 {
252   static uint32_t id_gen;
253
254   return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
255                                               controller,
256                                               host,
257                                               cfg,
258                                               cb, cls);
259 }
260
261
262 /**
263  * Start the given peer.
264  *
265  * @param peer peer to start
266  * @return handle to the operation
267  */
268 struct GNUNET_TESTBED_Operation *
269 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
270 {
271   struct GNUNET_TESTBED_Operation *op;
272   struct GNUNET_TESTBED_PeerStartMessage *msg;
273
274   GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
275   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
276   op->operation_id = peer->controller->operation_counter++;
277   op->controller = peer->controller;
278   op->type = OP_PEER_START;
279   op->data = peer;
280   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
281   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
282   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
283   msg->peer_id = htonl (peer->unique_id);
284   msg->operation_id = GNUNET_htonll (op->operation_id);
285   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
286                                     peer->controller->op_tail, op);
287   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
288   return op;
289 }
290
291
292 /**
293  * Stop the given peer.  The handle remains valid (use
294  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
295  * state of the peer).
296  *
297  * @param peer peer to stop
298  * @return handle to the operation
299  */
300 struct GNUNET_TESTBED_Operation *
301 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
302 {
303   struct GNUNET_TESTBED_Operation *op;
304   struct GNUNET_TESTBED_PeerStopMessage *msg;
305
306   GNUNET_assert (PS_STARTED == peer->state);
307   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
308   op->operation_id = peer->controller->operation_counter++;
309   op->controller = peer->controller;
310   op->type = OP_PEER_STOP;
311   op->data = peer;
312   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
313   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
314   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
315   msg->peer_id = htonl (peer->unique_id);
316   msg->operation_id = GNUNET_htonll (op->operation_id);
317   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
318                                     peer->controller->op_tail, op);
319   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
320   return op;
321 }
322
323
324 /**
325  * Request information about a peer.
326  *
327  * @param peer peer to request information about
328  * @param pit desired information
329  * @return handle to the operation
330  */
331 struct GNUNET_TESTBED_Operation *
332 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
333                                      enum GNUNET_TESTBED_PeerInformationType pit)
334 {
335   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
336   struct GNUNET_TESTBED_Operation *op;
337   struct PeerInfoData *data;
338   
339   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
340   data = GNUNET_malloc (sizeof (struct PeerInfoData));
341   data->peer = peer;
342   data->pit = pit;
343   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
344   op->type = OP_PEER_INFO;
345   op->operation_id = peer->controller->operation_counter++;
346   op->controller = peer->controller;
347   op->data = data;
348   msg = GNUNET_malloc (sizeof (struct
349                                GNUNET_TESTBED_PeerGetConfigurationMessage));
350   msg->header.size = htons
351     (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
352   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
353   msg->peer_id = htonl (peer->unique_id);
354   msg->operation_id = GNUNET_htonll (op->operation_id);
355   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
356                                     peer->controller->op_tail, op);
357   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
358   return op;
359 }
360
361
362 /**
363  * Change peer configuration.  Must only be called while the
364  * peer is stopped.  Ports and paths cannot be changed this
365  * way.
366  *
367  * @param peer peer to change configuration for
368  * @param cfg new configuration (differences to existing
369  *            configuration only)
370  * @return handle to the operation
371  */
372 struct GNUNET_TESTBED_Operation *
373 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
374                                           const struct GNUNET_CONFIGURATION_Handle *cfg)
375 {
376   // FIXME: handle locally or delegate...
377   GNUNET_break (0);
378   return NULL;
379 }
380
381
382 /**
383  * Destroy the given peer; the peer should have been
384  * stopped first (if it was started).
385  *
386  * @param peer peer to stop
387  * @return handle to the operation
388  */
389 struct GNUNET_TESTBED_Operation *
390 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
391 {
392   struct OperationContext *opc;
393
394   opc = GNUNET_malloc (sizeof (struct OperationContext));
395   opc->data = peer;
396   opc->c = peer->controller;
397   opc->id = peer->controller->operation_counter++;
398   opc->type = OP_PEER_DESTROY;
399   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
400                                               &oprelease_peer_destroy);
401   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create,
402                                           opc->op);
403   return opc->op;
404 }
405
406
407 /**
408  * Manipulate the P2P underlay topology by configuring a link
409  * between two peers.  
410  *
411  * @param op_cls closure argument to give with the operation event
412  * @param p1 first peer
413  * @param p2 second peer
414  * @param co option to change
415  * @param ... option-specific values
416  * @return handle to the operation, NULL if configuring the link at this
417  *         time is not allowed
418  */
419 struct GNUNET_TESTBED_Operation *
420 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
421                                         struct GNUNET_TESTBED_Peer *p1,
422                                         struct GNUNET_TESTBED_Peer *p2,
423                                         enum GNUNET_TESTBED_ConnectOption co, ...)
424 {
425   GNUNET_break (0);
426   return NULL;
427 }
428
429
430
431 /**
432  * Both peers must have been started before calling this function.
433  * This function then obtains a HELLO from 'p1', gives it to 'p2'
434  * and asks 'p2' to connect to 'p1'.
435  *
436  * @param op_cls closure argument to give with the operation event
437  * @param p1 first peer
438  * @param p2 second peer
439  * @return handle to the operation, NULL if connecting these two
440  *         peers is fundamentally not possible at this time (peers
441  *         not running or underlay disallows)
442  */
443 struct GNUNET_TESTBED_Operation *
444 GNUNET_TESTBED_overlay_connect (void *op_cls,
445                                 struct GNUNET_TESTBED_Peer *p1,
446                                 struct GNUNET_TESTBED_Peer *p2)
447 {
448   struct GNUNET_TESTBED_Operation *op;
449   struct OverlayConnectData *data;
450   struct GNUNET_TESTBED_OverlayConnectMessage *msg;
451   
452   GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
453   GNUNET_assert (p1->controller == p2->controller);
454   data = GNUNET_malloc (sizeof (struct OverlayConnectData));
455   data->p1 = p1;
456   data->p2 = p2;  
457   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation)); 
458   op->controller = p1->controller;
459   op->operation_id = op->controller->operation_counter++;
460   op->type = OP_OVERLAY_CONNECT;
461   op->data = data;
462   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
463   msg->header.size = htons (sizeof (struct
464                                     GNUNET_TESTBED_OverlayConnectMessage));
465   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
466   msg->peer1 = htonl (p1->unique_id);
467   msg->peer2 = htonl (p2->unique_id);
468   msg->operation_id = GNUNET_htonll (op->operation_id);
469   GNUNET_CONTAINER_DLL_insert_tail (op->controller->op_head,
470                                     op->controller->op_tail, op);
471   GNUNET_TESTBED_queue_message_ (op->controller, 
472                                  (struct GNUNET_MessageHeader *) msg);
473   return NULL;
474 }
475
476
477
478 /* end of testbed_api_peers.c */