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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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);
150 /* No break we continue flow */
152 GNUNET_free (((struct PeerCreateData *) opc->data)->peer);
153 GNUNET_free (opc->data);
156 case OPC_STATE_FINISHED:
164 * Function called when a peer destroy operation is ready
166 * @param cls the closure from GNUNET_TESTBED_operation_create_()
169 opstart_peer_destroy (void *cls)
171 struct OperationContext *opc = cls;
172 struct GNUNET_TESTBED_Peer *peer = opc->data;
173 struct GNUNET_TESTBED_PeerDestroyMessage *msg;
174 struct GNUNET_MQ_Envelope *env;
176 GNUNET_assert (OP_PEER_DESTROY == opc->type);
177 GNUNET_assert (NULL != peer);
178 opc->state = OPC_STATE_STARTED;
179 env = GNUNET_MQ_msg (msg,
180 GNUNET_MESSAGE_TYPE_TESTBED_DESTROY_PEER);
181 msg->peer_id = htonl (peer->unique_id);
182 msg->operation_id = GNUNET_htonll (opc->id);
183 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
184 GNUNET_MQ_send (peer->controller->mq,
190 * Callback which will be called when peer_create type operation is released
192 * @param cls the closure from GNUNET_TESTBED_operation_create_()
195 oprelease_peer_destroy (void *cls)
197 struct OperationContext *opc = cls;
201 case OPC_STATE_STARTED:
202 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
204 /* no break; continue */
208 case OPC_STATE_FINISHED:
216 * Function called when a peer start operation is ready
218 * @param cls the closure from GNUNET_TESTBED_operation_create_()
221 opstart_peer_start (void *cls)
223 struct OperationContext *opc = cls;
224 struct GNUNET_TESTBED_PeerStartMessage *msg;
225 struct GNUNET_MQ_Envelope *env;
226 struct PeerEventData *data;
227 struct GNUNET_TESTBED_Peer *peer;
229 GNUNET_assert (OP_PEER_START == opc->type);
230 GNUNET_assert (NULL != (data = opc->data));
231 GNUNET_assert (NULL != (peer = data->peer));
232 GNUNET_assert ((TESTBED_PS_CREATED == peer->state) || (TESTBED_PS_STOPPED ==
234 opc->state = OPC_STATE_STARTED;
235 env = GNUNET_MQ_msg (msg,
236 GNUNET_MESSAGE_TYPE_TESTBED_START_PEER);
237 msg->peer_id = htonl (peer->unique_id);
238 msg->operation_id = GNUNET_htonll (opc->id);
239 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
240 GNUNET_MQ_send (peer->controller->mq,
246 * Callback which will be called when peer start type operation is released
248 * @param cls the closure from GNUNET_TESTBED_operation_create_()
251 oprelease_peer_start (void *cls)
253 struct OperationContext *opc = cls;
257 case OPC_STATE_STARTED:
258 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
260 /* no break; continue */
262 GNUNET_free (opc->data);
265 case OPC_STATE_FINISHED:
273 * Function called when a peer stop operation is ready
275 * @param cls the closure from GNUNET_TESTBED_operation_create_()
278 opstart_peer_stop (void *cls)
280 struct OperationContext *opc = cls;
281 struct GNUNET_TESTBED_PeerStopMessage *msg;
282 struct PeerEventData *data;
283 struct GNUNET_TESTBED_Peer *peer;
284 struct GNUNET_MQ_Envelope *env;
286 GNUNET_assert (NULL != (data = opc->data));
287 GNUNET_assert (NULL != (peer = data->peer));
288 GNUNET_assert (TESTBED_PS_STARTED == peer->state);
289 opc->state = OPC_STATE_STARTED;
290 env = GNUNET_MQ_msg (msg,
291 GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER);
292 msg->peer_id = htonl (peer->unique_id);
293 msg->operation_id = GNUNET_htonll (opc->id);
294 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
295 GNUNET_MQ_send (peer->controller->mq,
301 * Callback which will be called when peer stop type operation is released
303 * @param cls the closure from GNUNET_TESTBED_operation_create_()
306 oprelease_peer_stop (void *cls)
308 struct OperationContext *opc = cls;
312 case OPC_STATE_STARTED:
313 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
315 /* no break; continue */
317 GNUNET_free (opc->data);
320 case OPC_STATE_FINISHED:
328 * Generate PeerGetConfigurationMessage
330 * @param peer_id the id of the peer whose information we have to get
331 * @param operation_id the ip of the operation that should be represented in the
333 * @return the PeerGetConfigurationMessage
335 struct GNUNET_TESTBED_PeerGetConfigurationMessage *
336 GNUNET_TESTBED_generate_peergetconfig_msg_ (uint32_t peer_id,
337 uint64_t operation_id)
339 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
342 GNUNET_malloc (sizeof
343 (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
345 htons (sizeof(struct GNUNET_TESTBED_PeerGetConfigurationMessage));
346 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_INFORMATION);
347 msg->peer_id = htonl (peer_id);
348 msg->operation_id = GNUNET_htonll (operation_id);
354 * Function called when a peer get information operation is ready
356 * @param cls the closure from GNUNET_TESTBED_operation_create_()
359 opstart_peer_getinfo (void *cls)
361 struct OperationContext *opc = cls;
362 struct PeerInfoData *data = opc->data;
363 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
365 GNUNET_assert (NULL != data);
366 opc->state = OPC_STATE_STARTED;
368 GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id,
370 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
371 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
376 * Callback which will be called when peer stop type operation is released
378 * @param cls the closure from GNUNET_TESTBED_operation_create_()
381 oprelease_peer_getinfo (void *cls)
383 struct OperationContext *opc = cls;
384 struct GNUNET_TESTBED_PeerInformation *data;
388 case OPC_STATE_STARTED:
389 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
391 /* no break; continue */
393 GNUNET_free (opc->data);
396 case OPC_STATE_FINISHED:
398 GNUNET_assert (NULL != data);
401 case GNUNET_TESTBED_PIT_CONFIGURATION:
402 if (NULL != data->result.cfg)
403 GNUNET_CONFIGURATION_destroy (data->result.cfg);
406 case GNUNET_TESTBED_PIT_IDENTITY:
407 GNUNET_free (data->result.id);
411 GNUNET_assert (0); /* We should never reach here */
421 * Function called when a overlay connect operation is ready
423 * @param cls the closure from GNUNET_TESTBED_operation_create_()
426 opstart_overlay_connect (void *cls)
428 struct OperationContext *opc = cls;
429 struct GNUNET_MQ_Envelope *env;
430 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
431 struct OverlayConnectData *data;
433 opc->state = OPC_STATE_STARTED;
435 GNUNET_assert (NULL != data);
436 env = GNUNET_MQ_msg (msg,
437 GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT);
438 msg->peer1 = htonl (data->p1->unique_id);
439 msg->peer2 = htonl (data->p2->unique_id);
440 msg->operation_id = GNUNET_htonll (opc->id);
441 msg->peer2_host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->p2->host));
442 GNUNET_TESTBED_insert_opc_ (opc->c,
444 GNUNET_MQ_send (opc->c->mq,
450 * Callback which will be called when overlay connect operation is released
452 * @param cls the closure from GNUNET_TESTBED_operation_create_()
455 oprelease_overlay_connect (void *cls)
457 struct OperationContext *opc = cls;
458 struct OverlayConnectData *data;
466 case OPC_STATE_STARTED:
467 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
470 case OPC_STATE_FINISHED:
479 * Function called when a peer reconfigure operation is ready
481 * @param cls the closure from GNUNET_TESTBED_operation_create_()
484 opstart_peer_reconfigure (void *cls)
486 struct OperationContext *opc = cls;
487 struct PeerReconfigureData *data = opc->data;
488 struct GNUNET_MQ_Envelope *env;
489 struct GNUNET_TESTBED_PeerReconfigureMessage *msg;
493 opc->state = OPC_STATE_STARTED;
494 GNUNET_assert (NULL != data);
495 xc_size = GNUNET_TESTBED_compress_config_ (data->config,
498 GNUNET_free (data->config);
500 GNUNET_assert (xc_size < UINT16_MAX - sizeof(*msg));
501 env = GNUNET_MQ_msg_extra (msg,
503 GNUNET_MESSAGE_TYPE_TESTBED_RECONFIGURE_PEER);
504 msg->peer_id = htonl (data->peer->unique_id);
505 msg->operation_id = GNUNET_htonll (opc->id);
506 msg->config_size = htons (data->cfg_size);
507 GNUNET_memcpy (&msg[1],
510 GNUNET_free (xconfig);
513 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
514 GNUNET_MQ_send (opc->c->mq,
520 * Callback which will be called when a peer reconfigure operation is released
522 * @param cls the closure from GNUNET_TESTBED_operation_create_()
525 oprelease_peer_reconfigure (void *cls)
527 struct OperationContext *opc = cls;
528 struct PeerReconfigureData *data = opc->data;
533 GNUNET_free (data->config);
537 case OPC_STATE_STARTED:
538 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
541 case OPC_STATE_FINISHED:
549 * Lookup a peer by ID.
551 * @param id global peer ID assigned to the peer
552 * @return handle to the host, NULL on error
554 struct GNUNET_TESTBED_Peer *
555 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
563 * Create the given peer at the specified host using the given
564 * controller. If the given controller is not running on the target
565 * host, it should find or create a controller at the target host and
566 * delegate creating the peer. Explicit delegation paths can be setup
567 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
568 * path exists, a direct link with a subordinate controller is setup
569 * for the first delegated peer to a particular host; the subordinate
570 * controller is then destroyed once the last peer that was delegated
571 * to the remote host is stopped.
573 * Creating the peer only creates the handle to manipulate and further
574 * configure the peer; use "GNUNET_TESTBED_peer_start" and
575 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
578 * Note that the given configuration will be adjusted by the
579 * controller to avoid port/path conflicts with other peers.
580 * The "final" configuration can be obtained using
581 * 'GNUNET_TESTBED_peer_get_information'.
583 * @param controller controller process to use
584 * @param host host to run the peer on; cannot be NULL
585 * @param cfg Template configuration to use for the peer. Should exist until
586 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
587 * @param cb the callback to call when the peer has been created
588 * @param cls the closure to the above callback
589 * @return the operation handle
591 struct GNUNET_TESTBED_Operation *
592 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
593 struct GNUNET_TESTBED_Host *host,
594 const struct GNUNET_CONFIGURATION_Handle *cfg,
595 GNUNET_TESTBED_PeerCreateCallback cb, void *cls)
597 struct GNUNET_TESTBED_Peer *peer;
598 struct PeerCreateData *data;
599 struct OperationContext *opc;
600 static uint32_t id_gen;
602 peer = GNUNET_new (struct GNUNET_TESTBED_Peer);
603 peer->controller = controller;
605 peer->unique_id = id_gen++;
606 peer->state = TESTBED_PS_INVALID;
607 data = GNUNET_new (struct PeerCreateData);
613 opc = GNUNET_new (struct OperationContext);
616 opc->id = GNUNET_TESTBED_get_next_op_id (controller);
617 opc->type = OP_PEER_CREATE;
619 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
620 &oprelease_peer_create);
621 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_parallel_operations,
623 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
629 * Start the given peer.
631 * @param op_cls the closure for this operation; will be set in
632 * event->details.operation_finished.op_cls when this operation fails.
633 * @param peer peer to start
634 * @param pcc function to call upon completion
635 * @param pcc_cls closure for 'pcc'
636 * @return handle to the operation
638 struct GNUNET_TESTBED_Operation *
639 GNUNET_TESTBED_peer_start (void *op_cls, struct GNUNET_TESTBED_Peer *peer,
640 GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
642 struct OperationContext *opc;
643 struct PeerEventData *data;
645 data = GNUNET_new (struct PeerEventData);
648 data->pcc_cls = pcc_cls;
649 opc = GNUNET_new (struct OperationContext);
650 opc->c = peer->controller;
652 opc->op_cls = op_cls;
653 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
654 opc->type = OP_PEER_START;
656 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
657 &oprelease_peer_start);
658 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
660 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
666 * Stop the given peer. The handle remains valid (use
667 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
668 * state of the peer).
670 * @param op_cls the closure for this operation; will be set in the event
672 * @param peer peer to stop
673 * @param pcc function to call upon completion
674 * @param pcc_cls closure for 'pcc'
675 * @return handle to the operation
677 struct GNUNET_TESTBED_Operation *
678 GNUNET_TESTBED_peer_stop (void *op_cls,
679 struct GNUNET_TESTBED_Peer *peer,
680 GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
682 struct OperationContext *opc;
683 struct PeerEventData *data;
685 data = GNUNET_new (struct PeerEventData);
688 data->pcc_cls = pcc_cls;
689 opc = GNUNET_new (struct OperationContext);
690 opc->c = peer->controller;
692 opc->op_cls = op_cls;
693 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
694 opc->type = OP_PEER_STOP;
696 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
697 &oprelease_peer_stop);
698 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
700 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
706 * Request information about a peer. The controller callback will not be called
707 * with event type GNUNET_TESTBED_ET_OPERATION_FINISHED when result for this
708 * operation is available. Instead, the GNUNET_TESTBED_PeerInfoCallback() will
710 * The peer information in the callback is valid until the operation is canceled.
712 * @param peer peer to request information about
713 * @param pit desired information
714 * @param cb the convenience callback to be called when results for this
715 * operation are available
716 * @param cb_cls the closure for the above callback
717 * @return handle to the operation
719 struct GNUNET_TESTBED_Operation *
720 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
721 enum GNUNET_TESTBED_PeerInformationType
722 pit, GNUNET_TESTBED_PeerInfoCallback cb,
725 struct OperationContext *opc;
726 struct PeerInfoData *data;
728 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
729 GNUNET_assert (NULL != cb);
730 data = GNUNET_new (struct PeerInfoData);
734 data->cb_cls = cb_cls;
735 opc = GNUNET_new (struct OperationContext);
736 opc->c = peer->controller;
738 opc->type = OP_PEER_INFO;
739 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
741 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
742 &oprelease_peer_getinfo);
743 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
745 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
751 * Change peer configuration. Must only be called while the
752 * peer is stopped. Ports and paths cannot be changed this
755 * @param peer peer to change configuration for
756 * @param cfg new configuration (differences to existing
757 * configuration only)
758 * @return handle to the operation
760 struct GNUNET_TESTBED_Operation *
761 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
763 GNUNET_CONFIGURATION_Handle *cfg)
765 struct OperationContext *opc;
766 struct PeerReconfigureData *data;
769 data = GNUNET_new (struct PeerReconfigureData);
771 data->config = GNUNET_CONFIGURATION_serialize (cfg, &csize);
772 if (NULL == data->config)
777 if (csize > UINT16_MAX)
780 GNUNET_free (data->config);
784 data->cfg_size = (uint16_t) csize;
785 opc = GNUNET_new (struct OperationContext);
786 opc->c = peer->controller;
788 opc->type = OP_PEER_RECONFIGURE;
789 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
791 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_reconfigure,
792 &oprelease_peer_reconfigure);
793 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
795 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
801 * Destroy the given peer; the peer should have been
802 * stopped first (if it was started).
804 * @param peer peer to stop
805 * @return handle to the operation
807 struct GNUNET_TESTBED_Operation *
808 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
810 struct OperationContext *opc;
812 opc = GNUNET_new (struct OperationContext);
814 opc->c = peer->controller;
815 opc->id = GNUNET_TESTBED_get_next_op_id (peer->controller);
816 opc->type = OP_PEER_DESTROY;
818 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
819 &oprelease_peer_destroy);
820 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
822 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
828 * Manipulate the P2P underlay topology by configuring a link
831 * @param op_cls closure argument to give with the operation event
832 * @param p1 first peer
833 * @param p2 second peer
834 * @param co option to change
835 * @param ... option-specific values
836 * @return handle to the operation, NULL if configuring the link at this
837 * time is not allowed
839 struct GNUNET_TESTBED_Operation *
840 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
841 struct GNUNET_TESTBED_Peer *p1,
842 struct GNUNET_TESTBED_Peer *p2,
843 enum GNUNET_TESTBED_ConnectOption co,
852 * Both peers must have been started before calling this function.
853 * This function then obtains a HELLO from 'p1', gives it to 'p2'
854 * and asks 'p2' to connect to 'p1'.
856 * @param op_cls closure argument to give with the operation event
857 * @param cb the callback to call when this operation has finished
858 * @param cb_cls the closure for the above callback
859 * @param p1 first peer
860 * @param p2 second peer
861 * @return handle to the operation, NULL if connecting these two
862 * peers is fundamentally not possible at this time (peers
863 * not running or underlay disallows)
865 struct GNUNET_TESTBED_Operation *
866 GNUNET_TESTBED_overlay_connect (void *op_cls,
867 GNUNET_TESTBED_OperationCompletionCallback cb,
868 void *cb_cls, struct GNUNET_TESTBED_Peer *p1,
869 struct GNUNET_TESTBED_Peer *p2)
871 struct OperationContext *opc;
872 struct OverlayConnectData *data;
874 GNUNET_assert ((TESTBED_PS_STARTED == p1->state) && (TESTBED_PS_STARTED ==
876 data = GNUNET_new (struct OverlayConnectData);
880 data->cb_cls = cb_cls;
881 opc = GNUNET_new (struct OperationContext);
883 opc->c = p1->controller;
884 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
885 opc->type = OP_OVERLAY_CONNECT;
886 opc->op_cls = op_cls;
888 GNUNET_TESTBED_operation_create_ (opc, &opstart_overlay_connect,
889 &oprelease_overlay_connect);
890 GNUNET_TESTBED_host_queue_oc_ (p1->host, opc->op);
891 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
897 * Function called when a peer manage service operation is ready
899 * @param cls the closure from GNUNET_TESTBED_operation_create_()
902 opstart_manage_service (void *cls)
904 struct OperationContext *opc = cls;
905 struct ManageServiceData *data = opc->data;
906 struct GNUNET_MQ_Envelope *env;
907 struct GNUNET_TESTBED_ManagePeerServiceMessage *msg;
910 GNUNET_assert (NULL != data);
911 xlen = data->msize - sizeof(struct GNUNET_TESTBED_ManagePeerServiceMessage);
912 env = GNUNET_MQ_msg_extra (msg,
914 GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE);
915 msg->peer_id = htonl (data->peer->unique_id);
916 msg->operation_id = GNUNET_htonll (opc->id);
917 msg->start = (uint8_t) data->start;
918 GNUNET_memcpy (&msg[1],
921 GNUNET_free (data->service_name);
922 data->service_name = NULL;
923 opc->state = OPC_STATE_STARTED;
924 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
925 GNUNET_MQ_send (opc->c->mq,
931 * Callback which will be called when peer manage server operation is released
933 * @param cls the closure from GNUNET_TESTBED_operation_create_()
936 oprelease_manage_service (void *cls)
938 struct OperationContext *opc = cls;
939 struct ManageServiceData *data;
944 case OPC_STATE_STARTED:
945 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
949 GNUNET_assert (NULL != data);
950 GNUNET_free (data->service_name);
953 case OPC_STATE_FINISHED:
956 GNUNET_free_non_null (data);
962 * Start or stop given service at a peer. This should not be called to
963 * start/stop the peer's ARM service. Use GNUNET_TESTBED_peer_start(),
964 * GNUNET_TESTBED_peer_stop() for starting/stopping peer's ARM service. Success
965 * or failure of the generated operation is signalled through the controller
966 * event callback and/or operation completion callback.
968 * @param op_cls the closure for the operation
969 * @param peer the peer whose service is to be started/stopped
970 * @param service_name the name of the service
971 * @param cb the operation completion callback
972 * @param cb_cls the closure for the operation completion callback
973 * @param start 1 to start the service; 0 to stop the service
974 * @return an operation handle; NULL upon error (peer not running)
976 struct GNUNET_TESTBED_Operation *
977 GNUNET_TESTBED_peer_manage_service (void *op_cls,
978 struct GNUNET_TESTBED_Peer *peer,
979 const char *service_name,
980 GNUNET_TESTBED_OperationCompletionCallback
985 struct ManageServiceData *data;
986 struct OperationContext *opc;
989 GNUNET_assert (TESTBED_PS_STARTED == peer->state); /* peer is not running? */
990 msize = strlen (service_name) + 1;
991 msize += sizeof(struct GNUNET_TESTBED_ManagePeerServiceMessage);
992 if (GNUNET_MAX_MESSAGE_SIZE < msize)
994 data = GNUNET_new (struct ManageServiceData);
996 data->cb_cls = cb_cls;
998 data->service_name = GNUNET_strdup (service_name);
1000 data->msize = (uint16_t) msize;
1001 opc = GNUNET_new (struct OperationContext);
1003 opc->c = peer->controller;
1004 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
1005 opc->type = OP_MANAGE_SERVICE;
1006 opc->op_cls = op_cls;
1008 GNUNET_TESTBED_operation_create_ (opc, &opstart_manage_service,
1009 &oprelease_manage_service);
1010 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
1012 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
1018 /* end of testbed_api_peers.c */