- distribute peers equally among island nodes on SuperMUC
[oweals/gnunet.git] / src / testbed / testbed_api_peers.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 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  * @author Sree Harsha Totakura
27  */
28
29 #include "platform.h"
30 #include "testbed_api_peers.h"
31 #include "testbed_api.h"
32 #include "testbed.h"
33 #include "testbed_api_hosts.h"
34 #include "testbed_api_operations.h"
35
36
37 /**
38  * Peer list DLL head
39  */
40 static struct GNUNET_TESTBED_Peer *peer_list_head;
41
42 /**
43  * Peer list DLL tail
44  */
45 static struct GNUNET_TESTBED_Peer *peer_list_tail;
46
47
48 /**
49  * Adds a peer to the peer list
50  *
51  * @param peer the peer to add to the peer list
52  */
53 void
54 GNUNET_TESTBED_peer_register_ (struct GNUNET_TESTBED_Peer *peer)
55 {
56   GNUNET_CONTAINER_DLL_insert_tail (peer_list_head, peer_list_tail, peer);
57 }
58
59
60 /**
61  * Removes a peer from the peer list
62  *
63  * @param peer the peer to remove
64  */
65 void
66 GNUNET_TESTBED_peer_deregister_ (struct GNUNET_TESTBED_Peer *peer)
67 {
68   GNUNET_CONTAINER_DLL_remove (peer_list_head, peer_list_tail, peer);
69 }
70
71
72 /**
73  * Frees all peers
74  */
75 void
76 GNUNET_TESTBED_cleanup_peers_ (void)
77 {
78   struct GNUNET_TESTBED_Peer *peer;
79
80   while (NULL != (peer = peer_list_head))
81   {
82     GNUNET_TESTBED_peer_deregister_ (peer);
83     GNUNET_free (peer);
84   }
85 }
86
87
88
89 /**
90  * Function to call to start a peer_create type operation once all
91  * queues the operation is part of declare that the
92  * operation can be activated.
93  *
94  * @param cls the closure from GNUNET_TESTBED_operation_create_()
95  */
96 static void
97 opstart_peer_create (void *cls)
98 {
99   struct OperationContext *opc = cls;
100   struct PeerCreateData *data;
101   struct GNUNET_TESTBED_PeerCreateMessage *msg;
102   char *config;
103   char *xconfig;
104   size_t c_size;
105   size_t xc_size;
106   uint16_t msize;
107
108   GNUNET_assert (OP_PEER_CREATE == opc->type);  
109   GNUNET_assert (NULL != (data = opc->data));
110   GNUNET_assert (NULL != data->peer);
111   opc->state = OPC_STATE_STARTED;
112   config = GNUNET_CONFIGURATION_serialize (data->cfg, &c_size);
113   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
114   GNUNET_free (config);
115   msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
116   msg = GNUNET_realloc (xconfig, msize);
117   memmove (&msg[1], msg, xc_size);
118   msg->header.size = htons (msize);
119   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER);
120   msg->operation_id = GNUNET_htonll (opc->id);
121   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->peer->host));
122   msg->peer_id = htonl (data->peer->unique_id);
123   msg->config_size = htons ((uint16_t) c_size);
124   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
125   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
126 }
127
128
129 /**
130  * Callback which will be called when peer_create type operation is released
131  *
132  * @param cls the closure from GNUNET_TESTBED_operation_create_()
133  */
134 static void
135 oprelease_peer_create (void *cls)
136 {
137   struct OperationContext *opc = cls;
138
139   switch (opc->state)
140   {
141   case OPC_STATE_STARTED:
142     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
143     /* No break we continue flow */
144   case OPC_STATE_INIT:
145     GNUNET_free (((struct PeerCreateData *) opc->data)->peer);
146     GNUNET_free (opc->data);
147     break;
148   case OPC_STATE_FINISHED:
149     break;
150   }
151   GNUNET_free (opc);
152 }
153
154
155 /**
156  * Function called when a peer destroy operation is ready
157  *
158  * @param cls the closure from GNUNET_TESTBED_operation_create_()
159  */
160 static void
161 opstart_peer_destroy (void *cls)
162 {
163   struct OperationContext *opc = cls;
164   struct GNUNET_TESTBED_Peer *peer;
165   struct GNUNET_TESTBED_PeerDestroyMessage *msg;
166
167   GNUNET_assert (OP_PEER_DESTROY == opc->type);
168   GNUNET_assert (NULL != (peer = opc->data));
169   opc->state = OPC_STATE_STARTED;
170   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
171   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
172   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROY_PEER);
173   msg->peer_id = htonl (peer->unique_id);
174   msg->operation_id = GNUNET_htonll (opc->id);
175   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
176   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
177 }
178
179
180 /**
181  * Callback which will be called when peer_create type operation is released
182  *
183  * @param cls the closure from GNUNET_TESTBED_operation_create_()
184  */
185 static void
186 oprelease_peer_destroy (void *cls)
187 {
188   struct OperationContext *opc = cls;
189
190   switch (opc->state)
191   {
192   case OPC_STATE_STARTED:
193     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
194     /* no break; continue */
195   case OPC_STATE_INIT:
196     break;
197   case OPC_STATE_FINISHED:
198     break;
199   }
200   GNUNET_free (opc);
201 }
202
203
204 /**
205  * Function called when a peer start operation is ready
206  *
207  * @param cls the closure from GNUNET_TESTBED_operation_create_()
208  */
209 static void
210 opstart_peer_start (void *cls)
211 {
212   struct OperationContext *opc = cls;
213   struct GNUNET_TESTBED_PeerStartMessage *msg;
214   struct PeerEventData *data;
215   struct GNUNET_TESTBED_Peer *peer;
216
217   GNUNET_assert (OP_PEER_START == opc->type);
218   GNUNET_assert (NULL != (data = opc->data));
219   GNUNET_assert (NULL != (peer = data->peer));
220   GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
221   opc->state = OPC_STATE_STARTED;
222   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
223   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
224   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_START_PEER);
225   msg->peer_id = htonl (peer->unique_id);
226   msg->operation_id = GNUNET_htonll (opc->id);
227   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
228   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
229 }
230
231
232 /**
233  * Callback which will be called when peer start type operation is released
234  *
235  * @param cls the closure from GNUNET_TESTBED_operation_create_()
236  */
237 static void
238 oprelease_peer_start (void *cls)
239 {
240   struct OperationContext *opc = cls;
241
242   switch (opc->state)
243   {
244   case OPC_STATE_STARTED:
245     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
246     /* no break; continue */
247   case OPC_STATE_INIT:
248     GNUNET_free (opc->data);
249     break;
250   case OPC_STATE_FINISHED:
251     break;
252   }
253   GNUNET_free (opc);
254 }
255
256
257 /**
258  * Function called when a peer stop operation is ready
259  *
260  * @param cls the closure from GNUNET_TESTBED_operation_create_()
261  */
262 static void
263 opstart_peer_stop (void *cls)
264 {
265   struct OperationContext *opc = cls;
266   struct GNUNET_TESTBED_PeerStopMessage *msg;
267   struct PeerEventData *data;
268   struct GNUNET_TESTBED_Peer *peer;
269
270   GNUNET_assert (NULL != (data = opc->data));
271   GNUNET_assert (NULL != (peer = data->peer));
272   GNUNET_assert (PS_STARTED == peer->state);
273   opc->state = OPC_STATE_STARTED;
274   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
275   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER);
276   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
277   msg->peer_id = htonl (peer->unique_id);
278   msg->operation_id = GNUNET_htonll (opc->id);
279   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
280   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
281 }
282
283
284 /**
285  * Callback which will be called when peer stop type operation is released
286  *
287  * @param cls the closure from GNUNET_TESTBED_operation_create_()
288  */
289 static void
290 oprelease_peer_stop (void *cls)
291 {
292   struct OperationContext *opc = cls;
293
294   switch (opc->state)
295   {
296   case OPC_STATE_STARTED:
297     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
298     /* no break; continue */
299   case OPC_STATE_INIT:
300     GNUNET_free (opc->data);
301     break;
302   case OPC_STATE_FINISHED:
303     break;
304   }
305   GNUNET_free (opc);
306 }
307
308
309 /**
310  * Generate PeerGetConfigurationMessage
311  *
312  * @param peer_id the id of the peer whose information we have to get
313  * @param operation_id the ip of the operation that should be represented in the
314  *          message
315  * @return the PeerGetConfigurationMessage
316  */
317 struct GNUNET_TESTBED_PeerGetConfigurationMessage *
318 GNUNET_TESTBED_generate_peergetconfig_msg_ (uint32_t peer_id,
319                                             uint64_t operation_id)
320 {
321   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
322
323   msg =
324       GNUNET_malloc (sizeof
325                      (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
326   msg->header.size =
327       htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
328   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_CONFIGURATION);
329   msg->peer_id = htonl (peer_id);
330   msg->operation_id = GNUNET_htonll (operation_id);
331   return msg;
332 }
333
334
335 /**
336  * Function called when a peer get information operation is ready
337  *
338  * @param cls the closure from GNUNET_TESTBED_operation_create_()
339  */
340 static void
341 opstart_peer_getinfo (void *cls)
342 {
343   struct OperationContext *opc = cls;
344   struct PeerInfoData *data;
345   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
346
347   GNUNET_assert (NULL != (data = opc->data));
348   opc->state = OPC_STATE_STARTED;
349   msg =
350       GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id,
351                                                   opc->id);
352   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
353   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
354 }
355
356
357 /**
358  * Callback which will be called when peer stop type operation is released
359  *
360  * @param cls the closure from GNUNET_TESTBED_operation_create_()
361  */
362 static void
363 oprelease_peer_getinfo (void *cls)
364 {
365   struct OperationContext *opc = cls;
366   struct GNUNET_TESTBED_PeerInformation *data;
367
368   switch (opc->state)
369   {
370   case OPC_STATE_STARTED:
371     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
372     /* no break; continue */
373   case OPC_STATE_INIT:
374     GNUNET_free (opc->data);
375     break;
376   case OPC_STATE_FINISHED:
377     GNUNET_assert (NULL != (data = opc->data));
378     switch (data->pit)
379     {
380     case GNUNET_TESTBED_PIT_CONFIGURATION:
381       if (NULL != data->result.cfg)
382         GNUNET_CONFIGURATION_destroy (data->result.cfg);
383       break;
384     case GNUNET_TESTBED_PIT_IDENTITY:
385       GNUNET_free (data->result.id);
386       break;
387     default:
388       GNUNET_assert (0);        /* We should never reach here */
389     }
390     GNUNET_free (data);
391     break;
392   }
393   GNUNET_free (opc);
394 }
395
396
397 /**
398  * Function called when a overlay connect operation is ready
399  *
400  * @param cls the closure from GNUNET_TESTBED_operation_create_()
401  */
402 static void
403 opstart_overlay_connect (void *cls)
404 {
405   struct OperationContext *opc = cls;
406   struct GNUNET_TESTBED_OverlayConnectMessage *msg;
407   struct OverlayConnectData *data;
408
409   opc->state = OPC_STATE_STARTED;
410   data = opc->data;
411   GNUNET_assert (NULL != data);
412   data->tslot_index = GNUNET_TESTBED_get_tslot_ (data->p1->host, data);
413   data->tstart = GNUNET_TIME_absolute_get ();
414   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
415   msg->header.size =
416       htons (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
417   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT);
418   msg->peer1 = htonl (data->p1->unique_id);
419   msg->peer2 = htonl (data->p2->unique_id);
420   msg->operation_id = GNUNET_htonll (opc->id);
421   msg->peer2_host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->p2->host));
422   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
423   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
424 }
425
426
427 /**
428  * Callback which will be called when overlay connect operation is released
429  *
430  * @param cls the closure from GNUNET_TESTBED_operation_create_()
431  */
432 static void
433 oprelease_overlay_connect (void *cls)
434 {
435   struct OperationContext *opc = cls;
436   struct GNUNET_TIME_Relative duration;
437   struct OverlayConnectData *data;
438
439   data = opc->data;
440   switch (opc->state)
441   {
442   case OPC_STATE_INIT:
443     break;
444   case OPC_STATE_STARTED:
445     (void) GNUNET_TESTBED_release_time_slot_ (data->p1->host, data->tslot_index,
446                                               data);
447     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
448     break;
449   case OPC_STATE_FINISHED:
450     duration = GNUNET_TIME_absolute_get_duration (data->tstart);
451     GNUNET_TESTBED_update_time_slot_ (data->p1->host, data->tslot_index, data,
452                                       duration, data->failed);
453   }
454   GNUNET_free (data);
455   GNUNET_free (opc);
456 }
457
458
459 /**
460  * Function called when a peer reconfigure operation is ready
461  *
462  * @param cls the closure from GNUNET_TESTBED_operation_create_()
463  */
464 static void
465 opstart_peer_reconfigure (void *cls)
466 {
467   struct OperationContext *opc = cls;
468   struct PeerReconfigureData *data = opc->data;
469   struct GNUNET_TESTBED_PeerReconfigureMessage *msg;
470   char *xconfig;
471   size_t xc_size;
472   uint16_t msize;
473   
474   opc->state = OPC_STATE_STARTED;
475   GNUNET_assert (NULL != data);
476   xc_size = GNUNET_TESTBED_compress_config_ (data->config, data->cfg_size,
477                                              &xconfig);
478   GNUNET_free (data->config);
479   data->config = NULL;
480   GNUNET_assert (xc_size <= UINT16_MAX);
481   msize = (uint16_t) xc_size + 
482       sizeof (struct GNUNET_TESTBED_PeerReconfigureMessage);
483   msg = GNUNET_realloc (xconfig, msize);
484   (void) memmove (&msg[1], msg, xc_size);
485   msg->header.size = htons (msize);
486   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_RECONFIGURE_PEER);
487   msg->peer_id = htonl (data->peer->unique_id);
488   msg->operation_id = GNUNET_htonll (opc->id);
489   msg->config_size = htons (data->cfg_size);
490   GNUNET_free (data);
491   opc->data = NULL;
492   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
493   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
494 }
495
496
497 /**
498  * Callback which will be called when a peer reconfigure operation is released
499  *
500  * @param cls the closure from GNUNET_TESTBED_operation_create_()
501  */
502 static void
503 oprelease_peer_reconfigure (void *cls)
504 {
505   struct OperationContext *opc = cls;
506   struct PeerReconfigureData *data = opc->data;
507  
508   switch (opc->state)
509   {
510   case OPC_STATE_INIT:
511     GNUNET_free (data->config);
512     GNUNET_free (data);
513     break;
514   case OPC_STATE_STARTED:
515     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
516     break;
517   case OPC_STATE_FINISHED:    
518     break;
519   }
520   GNUNET_free (opc);
521 }
522
523
524 /**
525  * Lookup a peer by ID.
526  *
527  * @param id global peer ID assigned to the peer
528  * @return handle to the host, NULL on error
529  */
530 struct GNUNET_TESTBED_Peer *
531 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
532 {
533   GNUNET_break (0);
534   return NULL;
535 }
536
537
538 /**
539  * Create the given peer at the specified host using the given
540  * controller.  If the given controller is not running on the target
541  * host, it should find or create a controller at the target host and
542  * delegate creating the peer.  Explicit delegation paths can be setup
543  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
544  * path exists, a direct link with a subordinate controller is setup
545  * for the first delegated peer to a particular host; the subordinate
546  * controller is then destroyed once the last peer that was delegated
547  * to the remote host is stopped.
548  *
549  * Creating the peer only creates the handle to manipulate and further
550  * configure the peer; use "GNUNET_TESTBED_peer_start" and
551  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
552  * processes.
553  *
554  * Note that the given configuration will be adjusted by the
555  * controller to avoid port/path conflicts with other peers.
556  * The "final" configuration can be obtained using
557  * 'GNUNET_TESTBED_peer_get_information'.
558  *
559  * @param controller controller process to use
560  * @param host host to run the peer on; cannot be NULL
561  * @param cfg Template configuration to use for the peer. Should exist until
562  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
563  * @param cb the callback to call when the peer has been created
564  * @param cls the closure to the above callback
565  * @return the operation handle
566  */
567 struct GNUNET_TESTBED_Operation *
568 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
569                             struct GNUNET_TESTBED_Host *host,
570                             const struct GNUNET_CONFIGURATION_Handle *cfg,
571                             GNUNET_TESTBED_PeerCreateCallback cb, void *cls)
572 {
573
574   struct GNUNET_TESTBED_Peer *peer;
575   struct PeerCreateData *data;
576   struct OperationContext *opc;
577   static uint32_t id_gen;
578
579   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
580   peer->controller = controller;
581   peer->host = host;
582   peer->unique_id = id_gen++;
583   peer->state = PS_INVALID;
584   data = GNUNET_malloc (sizeof (struct PeerCreateData));
585   data->host = host;
586   data->cfg = cfg;
587   data->cb = cb;
588   data->cls = cls;
589   data->peer = peer;
590   opc = GNUNET_malloc (sizeof (struct OperationContext));
591   opc->c = controller;
592   opc->data = data;
593   opc->id = GNUNET_TESTBED_get_next_op_id (controller);
594   opc->type = OP_PEER_CREATE;
595   opc->op =
596       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
597                                         &oprelease_peer_create);
598   GNUNET_TESTBED_operation_queue_insert_ (controller->opq_parallel_operations,
599                                           opc->op);
600   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
601   return opc->op;
602 }
603
604
605 /**
606  * Start the given peer.
607  *
608  * @param op_cls the closure for this operation; will be set in
609  *          event->details.operation_finished.op_cls when this operation fails.
610  * @param peer peer to start
611  * @param pcc function to call upon completion
612  * @param pcc_cls closure for 'pcc'
613  * @return handle to the operation
614  */
615 struct GNUNET_TESTBED_Operation *
616 GNUNET_TESTBED_peer_start (void *op_cls, struct GNUNET_TESTBED_Peer *peer,
617                            GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
618 {
619   struct OperationContext *opc;
620   struct PeerEventData *data;
621
622   data = GNUNET_malloc (sizeof (struct PeerEventData));
623   data->peer = peer;
624   data->pcc = pcc;
625   data->pcc_cls = pcc_cls;
626   opc = GNUNET_malloc (sizeof (struct OperationContext));
627   opc->c = peer->controller;
628   opc->data = data;
629   opc->op_cls = op_cls;
630   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
631   opc->type = OP_PEER_START;
632   opc->op =
633       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
634                                         &oprelease_peer_start);
635   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
636                                           opc->op);
637   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
638   return opc->op;
639 }
640
641
642 /**
643  * Stop the given peer.  The handle remains valid (use
644  * "GNUNET_TESTBED_peer_destroy" to fully clean up the
645  * state of the peer).
646  *
647  * @param op_cls the closure for this operation; will be set in the event
648  *          information
649  * @param peer peer to stop
650  * @param pcc function to call upon completion
651  * @param pcc_cls closure for 'pcc'
652  * @return handle to the operation
653  */
654 struct GNUNET_TESTBED_Operation *
655 GNUNET_TESTBED_peer_stop (void *op_cls,
656                           struct GNUNET_TESTBED_Peer *peer,
657                           GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
658 {
659   struct OperationContext *opc;
660   struct PeerEventData *data;
661
662   data = GNUNET_malloc (sizeof (struct PeerEventData));
663   data->peer = peer;
664   data->pcc = pcc;
665   data->pcc_cls = pcc_cls;
666   opc = GNUNET_malloc (sizeof (struct OperationContext));
667   opc->c = peer->controller;
668   opc->data = data;
669   opc->op_cls = op_cls;
670   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
671   opc->type = OP_PEER_STOP;
672   opc->op =
673       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
674                                         &oprelease_peer_stop);
675   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
676                                           opc->op);
677   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
678   return opc->op;
679 }
680
681
682 /**
683  * Request information about a peer. The controller callback will not be called
684  * with event type GNUNET_TESTBED_ET_OPERATION_FINISHED when result for this
685  * operation is available. Instead, the GNUNET_TESTBED_PeerInfoCallback() will
686  * be called.
687  *
688  * @param peer peer to request information about
689  * @param pit desired information
690  * @param cb the convenience callback to be called when results for this
691  *          operation are available
692  * @param cb_cls the closure for the above callback
693  * @return handle to the operation
694  */
695 struct GNUNET_TESTBED_Operation *
696 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
697                                      enum GNUNET_TESTBED_PeerInformationType
698                                      pit, GNUNET_TESTBED_PeerInfoCallback cb,
699                                      void *cb_cls)
700 {
701   struct OperationContext *opc;
702   struct PeerInfoData *data;
703
704   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
705   GNUNET_assert (NULL != cb);
706   data = GNUNET_malloc (sizeof (struct PeerInfoData));
707   data->peer = peer;
708   data->pit = pit;
709   data->cb = cb;
710   data->cb_cls = cb_cls;
711   opc = GNUNET_malloc (sizeof (struct OperationContext));
712   opc->c = peer->controller;
713   opc->data = data;
714   opc->type = OP_PEER_INFO;
715   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
716   opc->op =
717       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
718                                         &oprelease_peer_getinfo);
719   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
720                                           opc->op);
721   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
722   return opc->op;
723 }
724
725
726 /**
727  * Change peer configuration.  Must only be called while the
728  * peer is stopped.  Ports and paths cannot be changed this
729  * way.
730  *
731  * @param peer peer to change configuration for
732  * @param cfg new configuration (differences to existing
733  *            configuration only)
734  * @return handle to the operation
735  */
736 struct GNUNET_TESTBED_Operation *
737 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
738                                           const struct
739                                           GNUNET_CONFIGURATION_Handle *cfg)
740 {
741   struct OperationContext *opc;
742   struct PeerReconfigureData *data;
743   size_t csize;
744
745   data = GNUNET_malloc (sizeof (struct PeerReconfigureData));
746   data->peer = peer;
747   data->config = GNUNET_CONFIGURATION_serialize (cfg, &csize);
748   if (NULL == data->config)
749   {
750     GNUNET_free (data);
751     return NULL;
752   }
753   if (csize > UINT16_MAX)
754   {
755     GNUNET_break (0);
756     GNUNET_free (data->config);
757     GNUNET_free (data);
758     return NULL;
759   }
760   data->cfg_size = (uint16_t) csize;
761   opc = GNUNET_malloc (sizeof (struct OperationContext));
762   opc->c = peer->controller;
763   opc->data = data;
764   opc->type = OP_PEER_RECONFIGURE;
765   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
766   opc->op =
767       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_reconfigure,
768                                         &oprelease_peer_reconfigure);
769   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
770                                           opc->op);
771   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
772   return opc->op;
773 }
774
775
776 /**
777  * Destroy the given peer; the peer should have been
778  * stopped first (if it was started).
779  *
780  * @param peer peer to stop
781  * @return handle to the operation
782  */
783 struct GNUNET_TESTBED_Operation *
784 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
785 {
786   struct OperationContext *opc;
787
788   opc = GNUNET_malloc (sizeof (struct OperationContext));
789   opc->data = peer;
790   opc->c = peer->controller;
791   opc->id = GNUNET_TESTBED_get_next_op_id (peer->controller);
792   opc->type = OP_PEER_DESTROY;
793   opc->op =
794       GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
795                                         &oprelease_peer_destroy);
796   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
797                                           opc->op);
798   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
799   return opc->op;
800 }
801
802
803 /**
804  * Manipulate the P2P underlay topology by configuring a link
805  * between two peers.
806  *
807  * @param op_cls closure argument to give with the operation event
808  * @param p1 first peer
809  * @param p2 second peer
810  * @param co option to change
811  * @param ... option-specific values
812  * @return handle to the operation, NULL if configuring the link at this
813  *         time is not allowed
814  */
815 struct GNUNET_TESTBED_Operation *
816 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
817                                         struct GNUNET_TESTBED_Peer *p1,
818                                         struct GNUNET_TESTBED_Peer *p2,
819                                         enum GNUNET_TESTBED_ConnectOption co,
820                                         ...)
821 {
822   GNUNET_break (0);
823   return NULL;
824 }
825
826
827 /**
828  * Both peers must have been started before calling this function.
829  * This function then obtains a HELLO from 'p1', gives it to 'p2'
830  * and asks 'p2' to connect to 'p1'.
831  *
832  * @param op_cls closure argument to give with the operation event
833  * @param cb the callback to call when this operation has finished
834  * @param cb_cls the closure for the above callback
835  * @param p1 first peer
836  * @param p2 second peer
837  * @return handle to the operation, NULL if connecting these two
838  *         peers is fundamentally not possible at this time (peers
839  *         not running or underlay disallows)
840  */
841 struct GNUNET_TESTBED_Operation *
842 GNUNET_TESTBED_overlay_connect (void *op_cls,
843                                 GNUNET_TESTBED_OperationCompletionCallback cb,
844                                 void *cb_cls, struct GNUNET_TESTBED_Peer *p1,
845                                 struct GNUNET_TESTBED_Peer *p2)
846 {
847   struct OperationContext *opc;
848   struct OverlayConnectData *data;
849
850   GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
851   data = GNUNET_malloc (sizeof (struct OverlayConnectData));
852   data->p1 = p1;
853   data->p2 = p2;
854   data->cb = cb;
855   data->cb_cls = cb_cls;
856   opc = GNUNET_malloc (sizeof (struct OperationContext));
857   opc->data = data;
858   opc->c = p1->controller;
859   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
860   opc->type = OP_OVERLAY_CONNECT;
861   opc->op_cls = op_cls;
862   opc->op =
863       GNUNET_TESTBED_operation_create_ (opc, &opstart_overlay_connect,
864                                         &oprelease_overlay_connect);
865   GNUNET_TESTBED_host_queue_oc_ (p1->host, opc->op);
866   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
867   return opc->op;
868 }
869
870
871 /**
872  * Function called when a peer manage service operation is ready
873  *
874  * @param cls the closure from GNUNET_TESTBED_operation_create_()
875  */
876 static void
877 opstart_manage_service (void *cls)
878 {
879   struct OperationContext *opc = cls;
880   struct GNUNET_TESTBED_ManagePeerServiceMessage *msg;
881   struct ManageServiceData *data;
882   
883   GNUNET_assert (NULL != (data = opc->data));  
884   msg = GNUNET_malloc (data->msize);
885   msg->header.size = htons (data->msize);
886   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE);
887   msg->peer_id = htonl (data->peer->unique_id);
888   msg->operation_id = GNUNET_htonll (opc->id);
889   msg->start = (uint8_t) data->start;
890   (void) memcpy (&msg[1], data->service_name, data->msize 
891                  - sizeof (struct GNUNET_TESTBED_ManagePeerServiceMessage));
892   GNUNET_free (data->service_name);
893   data->service_name = NULL;
894   opc->state = OPC_STATE_STARTED;
895   GNUNET_TESTBED_insert_opc_ (opc->c, opc);
896   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
897 }
898
899
900 /**
901  * Callback which will be called when peer manage server operation is released
902  *
903  * @param cls the closure from GNUNET_TESTBED_operation_create_()
904  */
905 static void
906 oprelease_manage_service (void *cls)
907 {
908   struct OperationContext *opc = cls;
909   struct ManageServiceData *data;
910
911   data = opc->data;
912   switch (opc->state)
913   {
914   case OPC_STATE_STARTED:
915     GNUNET_TESTBED_remove_opc_ (opc->c, opc);
916     break;
917   case OPC_STATE_INIT:
918     GNUNET_assert (NULL != data);
919     GNUNET_free (data->service_name);
920     break;
921   case OPC_STATE_FINISHED:
922     break;
923   }
924   GNUNET_free_non_null (data);
925   GNUNET_free (opc);
926 }
927
928
929 /**
930  * Start or stop given service at a peer.  This should not be called to
931  * start/stop the peer's ARM service.  Use GNUNET_TESTBED_peer_start(),
932  * GNUNET_TESTBED_peer_stop() for starting/stopping peer's ARM service.  Success
933  * or failure of the generated operation is signalled through the controller
934  * event callback and/or operation completion callback.
935  *
936  * @param op_cls the closure for the operation
937  * @param peer the peer whose service is to be started/stopped
938  * @param service_name the name of the service
939  * @param cb the operation completion callback
940  * @param cb_cls the closure for the operation completion callback
941  * @param start 1 to start the service; 0 to stop the service
942  * @return an operation handle; NULL upon error (peer not running)
943  */
944 struct GNUNET_TESTBED_Operation *
945 GNUNET_TESTBED_peer_manage_service (void *op_cls,
946                                     struct GNUNET_TESTBED_Peer *peer,
947                                     const char *service_name,
948                                     GNUNET_TESTBED_OperationCompletionCallback cb,
949                                     void *cb_cls,
950                                     unsigned int start)
951 {
952   struct ManageServiceData *data;
953   struct OperationContext *opc;
954   size_t msize;
955
956   GNUNET_assert (PS_STARTED == peer->state); /* peer is not running? */
957   msize = strlen (service_name) + 1;
958   msize += sizeof (struct GNUNET_TESTBED_ManagePeerServiceMessage);
959   if (GNUNET_SERVER_MAX_MESSAGE_SIZE < msize)
960     return NULL;
961   data = GNUNET_malloc (sizeof (struct ManageServiceData));
962   data->cb = cb;
963   data->cb_cls = cb_cls;
964   data->peer = peer;
965   data->service_name = GNUNET_strdup (service_name);
966   data->start = start;
967   data->msize = (uint16_t) msize;
968   opc = GNUNET_malloc (sizeof (struct OperationContext));
969   opc->data = data;
970   opc->c = peer->controller;
971   opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
972   opc->type = OP_MANAGE_SERVICE;
973   opc->op_cls = op_cls;
974   opc->op =
975       GNUNET_TESTBED_operation_create_ (opc, &opstart_manage_service,
976                                         &oprelease_manage_service);
977   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
978                                           opc->op);
979   GNUNET_TESTBED_operation_begin_wait_ (opc->op);
980   return opc->op;
981 }
982
983
984
985 /* end of testbed_api_peers.c */