2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V.
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.
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.
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.
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
30 #include "testbed_api_peers.h"
31 #include "testbed_api.h"
33 #include "testbed_api_hosts.h"
34 #include "testbed_api_operations.h"
40 static struct GNUNET_TESTBED_Peer *peer_list_head;
45 static struct GNUNET_TESTBED_Peer *peer_list_tail;
49 * Adds a peer to the peer list
51 * @param peer the peer to add to the peer list
54 GNUNET_TESTBED_peer_register_ (struct GNUNET_TESTBED_Peer *peer)
56 GNUNET_CONTAINER_DLL_insert_tail (peer_list_head, peer_list_tail, peer);
61 * Removes a peer from the peer list
63 * @param peer the peer to remove
66 GNUNET_TESTBED_peer_deregister_ (struct GNUNET_TESTBED_Peer *peer)
68 GNUNET_CONTAINER_DLL_remove (peer_list_head, peer_list_tail, peer);
76 GNUNET_TESTBED_cleanup_peers_ (void)
78 struct GNUNET_TESTBED_Peer *peer;
80 while (NULL != (peer = peer_list_head))
82 GNUNET_TESTBED_peer_deregister_ (peer);
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.
94 * @param cls the closure from GNUNET_TESTBED_operation_create_()
97 opstart_peer_create (void *cls)
99 struct OperationContext *opc = cls;
100 struct PeerCreateData *data = opc->data;
101 struct GNUNET_TESTBED_PeerCreateMessage *msg;
102 struct GNUNET_MQ_Envelope *env;
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,
114 xc_size = GNUNET_TESTBED_compress_config_ (config,
117 GNUNET_free (config);
118 env = GNUNET_MQ_msg_extra (msg,
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 GNUNET_memcpy (&msg[1],
128 GNUNET_MQ_send (opc->c->mq,
130 GNUNET_free (xconfig);
131 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
136 * Callback which will be called when peer_create type operation is released
138 * @param cls the closure from GNUNET_TESTBED_operation_create_()
141 oprelease_peer_create (void *cls)
143 struct OperationContext *opc = cls;
147 case OPC_STATE_STARTED:
148 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
149 /* No break we continue flow */
151 GNUNET_free (((struct PeerCreateData *) opc->data)->peer);
152 GNUNET_free (opc->data);
154 case OPC_STATE_FINISHED:
162 * Function called when a peer destroy operation is ready
164 * @param cls the closure from GNUNET_TESTBED_operation_create_()
167 opstart_peer_destroy (void *cls)
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;
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,
188 * Callback which will be called when peer_create type operation is released
190 * @param cls the closure from GNUNET_TESTBED_operation_create_()
193 oprelease_peer_destroy (void *cls)
195 struct OperationContext *opc = cls;
199 case OPC_STATE_STARTED:
200 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
201 /* no break; continue */
204 case OPC_STATE_FINISHED:
212 * Function called when a peer start operation is ready
214 * @param cls the closure from GNUNET_TESTBED_operation_create_()
217 opstart_peer_start (void *cls)
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;
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,
241 * Callback which will be called when peer start type operation is released
243 * @param cls the closure from GNUNET_TESTBED_operation_create_()
246 oprelease_peer_start (void *cls)
248 struct OperationContext *opc = cls;
252 case OPC_STATE_STARTED:
253 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
254 /* no break; continue */
256 GNUNET_free (opc->data);
258 case OPC_STATE_FINISHED:
266 * Function called when a peer stop operation is ready
268 * @param cls the closure from GNUNET_TESTBED_operation_create_()
271 opstart_peer_stop (void *cls)
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;
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,
294 * Callback which will be called when peer stop type operation is released
296 * @param cls the closure from GNUNET_TESTBED_operation_create_()
299 oprelease_peer_stop (void *cls)
301 struct OperationContext *opc = cls;
305 case OPC_STATE_STARTED:
306 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
307 /* no break; continue */
309 GNUNET_free (opc->data);
311 case OPC_STATE_FINISHED:
319 * Generate PeerGetConfigurationMessage
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
324 * @return the PeerGetConfigurationMessage
326 struct GNUNET_TESTBED_PeerGetConfigurationMessage *
327 GNUNET_TESTBED_generate_peergetconfig_msg_ (uint32_t peer_id,
328 uint64_t operation_id)
330 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
333 GNUNET_malloc (sizeof
334 (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
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);
345 * Function called when a peer get information operation is ready
347 * @param cls the closure from GNUNET_TESTBED_operation_create_()
350 opstart_peer_getinfo (void *cls)
352 struct OperationContext *opc = cls;
353 struct PeerInfoData *data = opc->data;
354 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
356 GNUNET_assert (NULL != data);
357 opc->state = OPC_STATE_STARTED;
359 GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id,
361 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
362 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
367 * Callback which will be called when peer stop type operation is released
369 * @param cls the closure from GNUNET_TESTBED_operation_create_()
372 oprelease_peer_getinfo (void *cls)
374 struct OperationContext *opc = cls;
375 struct GNUNET_TESTBED_PeerInformation *data;
379 case OPC_STATE_STARTED:
380 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
381 /* no break; continue */
383 GNUNET_free (opc->data);
385 case OPC_STATE_FINISHED:
387 GNUNET_assert (NULL != data);
390 case GNUNET_TESTBED_PIT_CONFIGURATION:
391 if (NULL != data->result.cfg)
392 GNUNET_CONFIGURATION_destroy (data->result.cfg);
394 case GNUNET_TESTBED_PIT_IDENTITY:
395 GNUNET_free (data->result.id);
398 GNUNET_assert (0); /* We should never reach here */
408 * Function called when a overlay connect operation is ready
410 * @param cls the closure from GNUNET_TESTBED_operation_create_()
413 opstart_overlay_connect (void *cls)
415 struct OperationContext *opc = cls;
416 struct GNUNET_MQ_Envelope *env;
417 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
418 struct OverlayConnectData *data;
420 opc->state = OPC_STATE_STARTED;
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,
431 GNUNET_MQ_send (opc->c->mq,
437 * Callback which will be called when overlay connect operation is released
439 * @param cls the closure from GNUNET_TESTBED_operation_create_()
442 oprelease_overlay_connect (void *cls)
444 struct OperationContext *opc = cls;
445 struct OverlayConnectData *data;
452 case OPC_STATE_STARTED:
453 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
455 case OPC_STATE_FINISHED:
464 * Function called when a peer reconfigure operation is ready
466 * @param cls the closure from GNUNET_TESTBED_operation_create_()
469 opstart_peer_reconfigure (void *cls)
471 struct OperationContext *opc = cls;
472 struct PeerReconfigureData *data = opc->data;
473 struct GNUNET_MQ_Envelope *env;
474 struct GNUNET_TESTBED_PeerReconfigureMessage *msg;
478 opc->state = OPC_STATE_STARTED;
479 GNUNET_assert (NULL != data);
480 xc_size = GNUNET_TESTBED_compress_config_ (data->config,
483 GNUNET_free (data->config);
485 GNUNET_assert (xc_size < UINT16_MAX - sizeof (*msg));
486 env = GNUNET_MQ_msg_extra (msg,
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 GNUNET_memcpy (&msg[1],
495 GNUNET_free (xconfig);
498 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
499 GNUNET_MQ_send (opc->c->mq,
505 * Callback which will be called when a peer reconfigure operation is released
507 * @param cls the closure from GNUNET_TESTBED_operation_create_()
510 oprelease_peer_reconfigure (void *cls)
512 struct OperationContext *opc = cls;
513 struct PeerReconfigureData *data = opc->data;
518 GNUNET_free (data->config);
521 case OPC_STATE_STARTED:
522 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
524 case OPC_STATE_FINISHED:
532 * Lookup a peer by ID.
534 * @param id global peer ID assigned to the peer
535 * @return handle to the host, NULL on error
537 struct GNUNET_TESTBED_Peer *
538 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
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.
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
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'.
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
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)
581 struct GNUNET_TESTBED_Peer *peer;
582 struct PeerCreateData *data;
583 struct OperationContext *opc;
584 static uint32_t id_gen;
586 peer = GNUNET_new (struct GNUNET_TESTBED_Peer);
587 peer->controller = controller;
589 peer->unique_id = id_gen++;
590 peer->state = TESTBED_PS_INVALID;
591 data = GNUNET_new (struct PeerCreateData);
597 opc = GNUNET_new (struct OperationContext);
600 opc->id = GNUNET_TESTBED_get_next_op_id (controller);
601 opc->type = OP_PEER_CREATE;
603 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
604 &oprelease_peer_create);
605 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_parallel_operations,
607 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
613 * Start the given peer.
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
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)
626 struct OperationContext *opc;
627 struct PeerEventData *data;
629 data = GNUNET_new (struct PeerEventData);
632 data->pcc_cls = pcc_cls;
633 opc = GNUNET_new (struct OperationContext);
634 opc->c = peer->controller;
636 opc->op_cls = op_cls;
637 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
638 opc->type = OP_PEER_START;
640 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
641 &oprelease_peer_start);
642 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
644 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
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).
654 * @param op_cls the closure for this operation; will be set in the event
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
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)
666 struct OperationContext *opc;
667 struct PeerEventData *data;
669 data = GNUNET_new (struct PeerEventData);
672 data->pcc_cls = pcc_cls;
673 opc = GNUNET_new (struct OperationContext);
674 opc->c = peer->controller;
676 opc->op_cls = op_cls;
677 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
678 opc->type = OP_PEER_STOP;
680 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
681 &oprelease_peer_stop);
682 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
684 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
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
694 * The peer information in the callback is valid until the operation is canceled.
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
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,
709 struct OperationContext *opc;
710 struct PeerInfoData *data;
712 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
713 GNUNET_assert (NULL != cb);
714 data = GNUNET_new (struct PeerInfoData);
718 data->cb_cls = cb_cls;
719 opc = GNUNET_new (struct OperationContext);
720 opc->c = peer->controller;
722 opc->type = OP_PEER_INFO;
723 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
725 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
726 &oprelease_peer_getinfo);
727 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
729 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
735 * Change peer configuration. Must only be called while the
736 * peer is stopped. Ports and paths cannot be changed this
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
744 struct GNUNET_TESTBED_Operation *
745 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
747 GNUNET_CONFIGURATION_Handle *cfg)
749 struct OperationContext *opc;
750 struct PeerReconfigureData *data;
753 data = GNUNET_new (struct PeerReconfigureData);
755 data->config = GNUNET_CONFIGURATION_serialize (cfg, &csize);
756 if (NULL == data->config)
761 if (csize > UINT16_MAX)
764 GNUNET_free (data->config);
768 data->cfg_size = (uint16_t) csize;
769 opc = GNUNET_new (struct OperationContext);
770 opc->c = peer->controller;
772 opc->type = OP_PEER_RECONFIGURE;
773 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
775 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_reconfigure,
776 &oprelease_peer_reconfigure);
777 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
779 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
785 * Destroy the given peer; the peer should have been
786 * stopped first (if it was started).
788 * @param peer peer to stop
789 * @return handle to the operation
791 struct GNUNET_TESTBED_Operation *
792 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
794 struct OperationContext *opc;
796 opc = GNUNET_new (struct OperationContext);
798 opc->c = peer->controller;
799 opc->id = GNUNET_TESTBED_get_next_op_id (peer->controller);
800 opc->type = OP_PEER_DESTROY;
802 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
803 &oprelease_peer_destroy);
804 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
806 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
812 * Manipulate the P2P underlay topology by configuring a link
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
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,
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'.
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)
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)
855 struct OperationContext *opc;
856 struct OverlayConnectData *data;
858 GNUNET_assert ((TESTBED_PS_STARTED == p1->state) && (TESTBED_PS_STARTED == p2->state));
859 data = GNUNET_new (struct OverlayConnectData);
863 data->cb_cls = cb_cls;
864 opc = GNUNET_new (struct OperationContext);
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;
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);
880 * Function called when a peer manage service operation is ready
882 * @param cls the closure from GNUNET_TESTBED_operation_create_()
885 opstart_manage_service (void *cls)
887 struct OperationContext *opc = cls;
888 struct ManageServiceData *data = opc->data;
889 struct GNUNET_MQ_Envelope *env;
890 struct GNUNET_TESTBED_ManagePeerServiceMessage *msg;
893 GNUNET_assert (NULL != data);
894 xlen = data->msize - sizeof (struct GNUNET_TESTBED_ManagePeerServiceMessage);
895 env = GNUNET_MQ_msg_extra (msg,
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 GNUNET_memcpy (&msg[1],
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,
914 * Callback which will be called when peer manage server operation is released
916 * @param cls the closure from GNUNET_TESTBED_operation_create_()
919 oprelease_manage_service (void *cls)
921 struct OperationContext *opc = cls;
922 struct ManageServiceData *data;
927 case OPC_STATE_STARTED:
928 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
931 GNUNET_assert (NULL != data);
932 GNUNET_free (data->service_name);
934 case OPC_STATE_FINISHED:
937 GNUNET_free_non_null (data);
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.
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)
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,
965 struct ManageServiceData *data;
966 struct OperationContext *opc;
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_MAX_MESSAGE_SIZE < msize)
974 data = GNUNET_new (struct ManageServiceData);
976 data->cb_cls = cb_cls;
978 data->service_name = GNUNET_strdup (service_name);
980 data->msize = (uint16_t) msize;
981 opc = GNUNET_new (struct OperationContext);
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;
988 GNUNET_TESTBED_operation_create_ (opc, &opstart_manage_service,
989 &oprelease_manage_service);
990 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
992 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
998 /* end of testbed_api_peers.c */