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