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.
17 * @file testbed/testbed_api_peers.c
18 * @brief management of the knowledge about peers in this library
19 * (we know the peer ID, its host, pending operations, etc.)
20 * @author Christian Grothoff
21 * @author Sree Harsha Totakura
25 #include "testbed_api_peers.h"
26 #include "testbed_api.h"
28 #include "testbed_api_hosts.h"
29 #include "testbed_api_operations.h"
35 static struct GNUNET_TESTBED_Peer *peer_list_head;
40 static struct GNUNET_TESTBED_Peer *peer_list_tail;
44 * Adds a peer to the peer list
46 * @param peer the peer to add to the peer list
49 GNUNET_TESTBED_peer_register_ (struct GNUNET_TESTBED_Peer *peer)
51 GNUNET_CONTAINER_DLL_insert_tail (peer_list_head, peer_list_tail, peer);
56 * Removes a peer from the peer list
58 * @param peer the peer to remove
61 GNUNET_TESTBED_peer_deregister_ (struct GNUNET_TESTBED_Peer *peer)
63 GNUNET_CONTAINER_DLL_remove (peer_list_head, peer_list_tail, peer);
71 GNUNET_TESTBED_cleanup_peers_ (void)
73 struct GNUNET_TESTBED_Peer *peer;
75 while (NULL != (peer = peer_list_head))
77 GNUNET_TESTBED_peer_deregister_ (peer);
85 * Function to call to start a peer_create type operation once all
86 * queues the operation is part of declare that the
87 * operation can be activated.
89 * @param cls the closure from GNUNET_TESTBED_operation_create_()
92 opstart_peer_create (void *cls)
94 struct OperationContext *opc = cls;
95 struct PeerCreateData *data = opc->data;
96 struct GNUNET_TESTBED_PeerCreateMessage *msg;
97 struct GNUNET_MQ_Envelope *env;
103 GNUNET_assert (OP_PEER_CREATE == opc->type);
104 GNUNET_assert (NULL != data);
105 GNUNET_assert (NULL != data->peer);
106 opc->state = OPC_STATE_STARTED;
107 config = GNUNET_CONFIGURATION_serialize (data->cfg,
109 xc_size = GNUNET_TESTBED_compress_config_ (config,
112 GNUNET_free (config);
113 env = GNUNET_MQ_msg_extra (msg,
115 GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER);
116 msg->operation_id = GNUNET_htonll (opc->id);
117 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->peer->host));
118 msg->peer_id = htonl (data->peer->unique_id);
119 msg->config_size = htons ((uint16_t) c_size);
120 GNUNET_memcpy (&msg[1],
123 GNUNET_MQ_send (opc->c->mq,
125 GNUNET_free (xconfig);
126 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
131 * Callback which will be called when peer_create type operation is released
133 * @param cls the closure from GNUNET_TESTBED_operation_create_()
136 oprelease_peer_create (void *cls)
138 struct OperationContext *opc = cls;
142 case OPC_STATE_STARTED:
143 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
144 /* No break we continue flow */
146 GNUNET_free (((struct PeerCreateData *) opc->data)->peer);
147 GNUNET_free (opc->data);
149 case OPC_STATE_FINISHED:
157 * Function called when a peer destroy operation is ready
159 * @param cls the closure from GNUNET_TESTBED_operation_create_()
162 opstart_peer_destroy (void *cls)
164 struct OperationContext *opc = cls;
165 struct GNUNET_TESTBED_Peer *peer = opc->data;
166 struct GNUNET_TESTBED_PeerDestroyMessage *msg;
167 struct GNUNET_MQ_Envelope *env;
169 GNUNET_assert (OP_PEER_DESTROY == opc->type);
170 GNUNET_assert (NULL != peer);
171 opc->state = OPC_STATE_STARTED;
172 env = GNUNET_MQ_msg (msg,
173 GNUNET_MESSAGE_TYPE_TESTBED_DESTROY_PEER);
174 msg->peer_id = htonl (peer->unique_id);
175 msg->operation_id = GNUNET_htonll (opc->id);
176 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
177 GNUNET_MQ_send (peer->controller->mq,
183 * Callback which will be called when peer_create type operation is released
185 * @param cls the closure from GNUNET_TESTBED_operation_create_()
188 oprelease_peer_destroy (void *cls)
190 struct OperationContext *opc = cls;
194 case OPC_STATE_STARTED:
195 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
196 /* no break; continue */
199 case OPC_STATE_FINISHED:
207 * Function called when a peer start operation is ready
209 * @param cls the closure from GNUNET_TESTBED_operation_create_()
212 opstart_peer_start (void *cls)
214 struct OperationContext *opc = cls;
215 struct GNUNET_TESTBED_PeerStartMessage *msg;
216 struct GNUNET_MQ_Envelope *env;
217 struct PeerEventData *data;
218 struct GNUNET_TESTBED_Peer *peer;
220 GNUNET_assert (OP_PEER_START == opc->type);
221 GNUNET_assert (NULL != (data = opc->data));
222 GNUNET_assert (NULL != (peer = data->peer));
223 GNUNET_assert ((TESTBED_PS_CREATED == peer->state) || (TESTBED_PS_STOPPED == peer->state));
224 opc->state = OPC_STATE_STARTED;
225 env = GNUNET_MQ_msg (msg,
226 GNUNET_MESSAGE_TYPE_TESTBED_START_PEER);
227 msg->peer_id = htonl (peer->unique_id);
228 msg->operation_id = GNUNET_htonll (opc->id);
229 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
230 GNUNET_MQ_send (peer->controller->mq,
236 * Callback which will be called when peer start type operation is released
238 * @param cls the closure from GNUNET_TESTBED_operation_create_()
241 oprelease_peer_start (void *cls)
243 struct OperationContext *opc = cls;
247 case OPC_STATE_STARTED:
248 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
249 /* no break; continue */
251 GNUNET_free (opc->data);
253 case OPC_STATE_FINISHED:
261 * Function called when a peer stop operation is ready
263 * @param cls the closure from GNUNET_TESTBED_operation_create_()
266 opstart_peer_stop (void *cls)
268 struct OperationContext *opc = cls;
269 struct GNUNET_TESTBED_PeerStopMessage *msg;
270 struct PeerEventData *data;
271 struct GNUNET_TESTBED_Peer *peer;
272 struct GNUNET_MQ_Envelope *env;
274 GNUNET_assert (NULL != (data = opc->data));
275 GNUNET_assert (NULL != (peer = data->peer));
276 GNUNET_assert (TESTBED_PS_STARTED == peer->state);
277 opc->state = OPC_STATE_STARTED;
278 env = GNUNET_MQ_msg (msg,
279 GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER);
280 msg->peer_id = htonl (peer->unique_id);
281 msg->operation_id = GNUNET_htonll (opc->id);
282 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
283 GNUNET_MQ_send (peer->controller->mq,
289 * Callback which will be called when peer stop type operation is released
291 * @param cls the closure from GNUNET_TESTBED_operation_create_()
294 oprelease_peer_stop (void *cls)
296 struct OperationContext *opc = cls;
300 case OPC_STATE_STARTED:
301 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
302 /* no break; continue */
304 GNUNET_free (opc->data);
306 case OPC_STATE_FINISHED:
314 * Generate PeerGetConfigurationMessage
316 * @param peer_id the id of the peer whose information we have to get
317 * @param operation_id the ip of the operation that should be represented in the
319 * @return the PeerGetConfigurationMessage
321 struct GNUNET_TESTBED_PeerGetConfigurationMessage *
322 GNUNET_TESTBED_generate_peergetconfig_msg_ (uint32_t peer_id,
323 uint64_t operation_id)
325 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
328 GNUNET_malloc (sizeof
329 (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
331 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
332 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_INFORMATION);
333 msg->peer_id = htonl (peer_id);
334 msg->operation_id = GNUNET_htonll (operation_id);
340 * Function called when a peer get information operation is ready
342 * @param cls the closure from GNUNET_TESTBED_operation_create_()
345 opstart_peer_getinfo (void *cls)
347 struct OperationContext *opc = cls;
348 struct PeerInfoData *data = opc->data;
349 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
351 GNUNET_assert (NULL != data);
352 opc->state = OPC_STATE_STARTED;
354 GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id,
356 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
357 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
362 * Callback which will be called when peer stop type operation is released
364 * @param cls the closure from GNUNET_TESTBED_operation_create_()
367 oprelease_peer_getinfo (void *cls)
369 struct OperationContext *opc = cls;
370 struct GNUNET_TESTBED_PeerInformation *data;
374 case OPC_STATE_STARTED:
375 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
376 /* no break; continue */
378 GNUNET_free (opc->data);
380 case OPC_STATE_FINISHED:
382 GNUNET_assert (NULL != data);
385 case GNUNET_TESTBED_PIT_CONFIGURATION:
386 if (NULL != data->result.cfg)
387 GNUNET_CONFIGURATION_destroy (data->result.cfg);
389 case GNUNET_TESTBED_PIT_IDENTITY:
390 GNUNET_free (data->result.id);
393 GNUNET_assert (0); /* We should never reach here */
403 * Function called when a overlay connect operation is ready
405 * @param cls the closure from GNUNET_TESTBED_operation_create_()
408 opstart_overlay_connect (void *cls)
410 struct OperationContext *opc = cls;
411 struct GNUNET_MQ_Envelope *env;
412 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
413 struct OverlayConnectData *data;
415 opc->state = OPC_STATE_STARTED;
417 GNUNET_assert (NULL != data);
418 env = GNUNET_MQ_msg (msg,
419 GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT);
420 msg->peer1 = htonl (data->p1->unique_id);
421 msg->peer2 = htonl (data->p2->unique_id);
422 msg->operation_id = GNUNET_htonll (opc->id);
423 msg->peer2_host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->p2->host));
424 GNUNET_TESTBED_insert_opc_ (opc->c,
426 GNUNET_MQ_send (opc->c->mq,
432 * Callback which will be called when overlay connect operation is released
434 * @param cls the closure from GNUNET_TESTBED_operation_create_()
437 oprelease_overlay_connect (void *cls)
439 struct OperationContext *opc = cls;
440 struct OverlayConnectData *data;
447 case OPC_STATE_STARTED:
448 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
450 case OPC_STATE_FINISHED:
459 * Function called when a peer reconfigure operation is ready
461 * @param cls the closure from GNUNET_TESTBED_operation_create_()
464 opstart_peer_reconfigure (void *cls)
466 struct OperationContext *opc = cls;
467 struct PeerReconfigureData *data = opc->data;
468 struct GNUNET_MQ_Envelope *env;
469 struct GNUNET_TESTBED_PeerReconfigureMessage *msg;
473 opc->state = OPC_STATE_STARTED;
474 GNUNET_assert (NULL != data);
475 xc_size = GNUNET_TESTBED_compress_config_ (data->config,
478 GNUNET_free (data->config);
480 GNUNET_assert (xc_size < UINT16_MAX - sizeof (*msg));
481 env = GNUNET_MQ_msg_extra (msg,
483 GNUNET_MESSAGE_TYPE_TESTBED_RECONFIGURE_PEER);
484 msg->peer_id = htonl (data->peer->unique_id);
485 msg->operation_id = GNUNET_htonll (opc->id);
486 msg->config_size = htons (data->cfg_size);
487 GNUNET_memcpy (&msg[1],
490 GNUNET_free (xconfig);
493 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
494 GNUNET_MQ_send (opc->c->mq,
500 * Callback which will be called when a peer reconfigure operation is released
502 * @param cls the closure from GNUNET_TESTBED_operation_create_()
505 oprelease_peer_reconfigure (void *cls)
507 struct OperationContext *opc = cls;
508 struct PeerReconfigureData *data = opc->data;
513 GNUNET_free (data->config);
516 case OPC_STATE_STARTED:
517 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
519 case OPC_STATE_FINISHED:
527 * Lookup a peer by ID.
529 * @param id global peer ID assigned to the peer
530 * @return handle to the host, NULL on error
532 struct GNUNET_TESTBED_Peer *
533 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
541 * Create the given peer at the specified host using the given
542 * controller. If the given controller is not running on the target
543 * host, it should find or create a controller at the target host and
544 * delegate creating the peer. Explicit delegation paths can be setup
545 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
546 * path exists, a direct link with a subordinate controller is setup
547 * for the first delegated peer to a particular host; the subordinate
548 * controller is then destroyed once the last peer that was delegated
549 * to the remote host is stopped.
551 * Creating the peer only creates the handle to manipulate and further
552 * configure the peer; use "GNUNET_TESTBED_peer_start" and
553 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
556 * Note that the given configuration will be adjusted by the
557 * controller to avoid port/path conflicts with other peers.
558 * The "final" configuration can be obtained using
559 * 'GNUNET_TESTBED_peer_get_information'.
561 * @param controller controller process to use
562 * @param host host to run the peer on; cannot be NULL
563 * @param cfg Template configuration to use for the peer. Should exist until
564 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
565 * @param cb the callback to call when the peer has been created
566 * @param cls the closure to the above callback
567 * @return the operation handle
569 struct GNUNET_TESTBED_Operation *
570 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
571 struct GNUNET_TESTBED_Host *host,
572 const struct GNUNET_CONFIGURATION_Handle *cfg,
573 GNUNET_TESTBED_PeerCreateCallback cb, void *cls)
576 struct GNUNET_TESTBED_Peer *peer;
577 struct PeerCreateData *data;
578 struct OperationContext *opc;
579 static uint32_t id_gen;
581 peer = GNUNET_new (struct GNUNET_TESTBED_Peer);
582 peer->controller = controller;
584 peer->unique_id = id_gen++;
585 peer->state = TESTBED_PS_INVALID;
586 data = GNUNET_new (struct PeerCreateData);
592 opc = GNUNET_new (struct OperationContext);
595 opc->id = GNUNET_TESTBED_get_next_op_id (controller);
596 opc->type = OP_PEER_CREATE;
598 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
599 &oprelease_peer_create);
600 GNUNET_TESTBED_operation_queue_insert_ (controller->opq_parallel_operations,
602 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
608 * Start the given peer.
610 * @param op_cls the closure for this operation; will be set in
611 * event->details.operation_finished.op_cls when this operation fails.
612 * @param peer peer to start
613 * @param pcc function to call upon completion
614 * @param pcc_cls closure for 'pcc'
615 * @return handle to the operation
617 struct GNUNET_TESTBED_Operation *
618 GNUNET_TESTBED_peer_start (void *op_cls, struct GNUNET_TESTBED_Peer *peer,
619 GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
621 struct OperationContext *opc;
622 struct PeerEventData *data;
624 data = GNUNET_new (struct PeerEventData);
627 data->pcc_cls = pcc_cls;
628 opc = GNUNET_new (struct OperationContext);
629 opc->c = peer->controller;
631 opc->op_cls = op_cls;
632 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
633 opc->type = OP_PEER_START;
635 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
636 &oprelease_peer_start);
637 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
639 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
645 * Stop the given peer. The handle remains valid (use
646 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
647 * state of the peer).
649 * @param op_cls the closure for this operation; will be set in the event
651 * @param peer peer to stop
652 * @param pcc function to call upon completion
653 * @param pcc_cls closure for 'pcc'
654 * @return handle to the operation
656 struct GNUNET_TESTBED_Operation *
657 GNUNET_TESTBED_peer_stop (void *op_cls,
658 struct GNUNET_TESTBED_Peer *peer,
659 GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
661 struct OperationContext *opc;
662 struct PeerEventData *data;
664 data = GNUNET_new (struct PeerEventData);
667 data->pcc_cls = pcc_cls;
668 opc = GNUNET_new (struct OperationContext);
669 opc->c = peer->controller;
671 opc->op_cls = op_cls;
672 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
673 opc->type = OP_PEER_STOP;
675 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
676 &oprelease_peer_stop);
677 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
679 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
685 * Request information about a peer. The controller callback will not be called
686 * with event type GNUNET_TESTBED_ET_OPERATION_FINISHED when result for this
687 * operation is available. Instead, the GNUNET_TESTBED_PeerInfoCallback() will
689 * The peer information in the callback is valid until the operation is canceled.
691 * @param peer peer to request information about
692 * @param pit desired information
693 * @param cb the convenience callback to be called when results for this
694 * operation are available
695 * @param cb_cls the closure for the above callback
696 * @return handle to the operation
698 struct GNUNET_TESTBED_Operation *
699 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
700 enum GNUNET_TESTBED_PeerInformationType
701 pit, GNUNET_TESTBED_PeerInfoCallback cb,
704 struct OperationContext *opc;
705 struct PeerInfoData *data;
707 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
708 GNUNET_assert (NULL != cb);
709 data = GNUNET_new (struct PeerInfoData);
713 data->cb_cls = cb_cls;
714 opc = GNUNET_new (struct OperationContext);
715 opc->c = peer->controller;
717 opc->type = OP_PEER_INFO;
718 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
720 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
721 &oprelease_peer_getinfo);
722 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
724 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
730 * Change peer configuration. Must only be called while the
731 * peer is stopped. Ports and paths cannot be changed this
734 * @param peer peer to change configuration for
735 * @param cfg new configuration (differences to existing
736 * configuration only)
737 * @return handle to the operation
739 struct GNUNET_TESTBED_Operation *
740 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
742 GNUNET_CONFIGURATION_Handle *cfg)
744 struct OperationContext *opc;
745 struct PeerReconfigureData *data;
748 data = GNUNET_new (struct PeerReconfigureData);
750 data->config = GNUNET_CONFIGURATION_serialize (cfg, &csize);
751 if (NULL == data->config)
756 if (csize > UINT16_MAX)
759 GNUNET_free (data->config);
763 data->cfg_size = (uint16_t) csize;
764 opc = GNUNET_new (struct OperationContext);
765 opc->c = peer->controller;
767 opc->type = OP_PEER_RECONFIGURE;
768 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
770 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_reconfigure,
771 &oprelease_peer_reconfigure);
772 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
774 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
780 * Destroy the given peer; the peer should have been
781 * stopped first (if it was started).
783 * @param peer peer to stop
784 * @return handle to the operation
786 struct GNUNET_TESTBED_Operation *
787 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
789 struct OperationContext *opc;
791 opc = GNUNET_new (struct OperationContext);
793 opc->c = peer->controller;
794 opc->id = GNUNET_TESTBED_get_next_op_id (peer->controller);
795 opc->type = OP_PEER_DESTROY;
797 GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
798 &oprelease_peer_destroy);
799 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
801 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
807 * Manipulate the P2P underlay topology by configuring a link
810 * @param op_cls closure argument to give with the operation event
811 * @param p1 first peer
812 * @param p2 second peer
813 * @param co option to change
814 * @param ... option-specific values
815 * @return handle to the operation, NULL if configuring the link at this
816 * time is not allowed
818 struct GNUNET_TESTBED_Operation *
819 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
820 struct GNUNET_TESTBED_Peer *p1,
821 struct GNUNET_TESTBED_Peer *p2,
822 enum GNUNET_TESTBED_ConnectOption co,
831 * Both peers must have been started before calling this function.
832 * This function then obtains a HELLO from 'p1', gives it to 'p2'
833 * and asks 'p2' to connect to 'p1'.
835 * @param op_cls closure argument to give with the operation event
836 * @param cb the callback to call when this operation has finished
837 * @param cb_cls the closure for the above callback
838 * @param p1 first peer
839 * @param p2 second peer
840 * @return handle to the operation, NULL if connecting these two
841 * peers is fundamentally not possible at this time (peers
842 * not running or underlay disallows)
844 struct GNUNET_TESTBED_Operation *
845 GNUNET_TESTBED_overlay_connect (void *op_cls,
846 GNUNET_TESTBED_OperationCompletionCallback cb,
847 void *cb_cls, struct GNUNET_TESTBED_Peer *p1,
848 struct GNUNET_TESTBED_Peer *p2)
850 struct OperationContext *opc;
851 struct OverlayConnectData *data;
853 GNUNET_assert ((TESTBED_PS_STARTED == p1->state) && (TESTBED_PS_STARTED == p2->state));
854 data = GNUNET_new (struct OverlayConnectData);
858 data->cb_cls = cb_cls;
859 opc = GNUNET_new (struct OperationContext);
861 opc->c = p1->controller;
862 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
863 opc->type = OP_OVERLAY_CONNECT;
864 opc->op_cls = op_cls;
866 GNUNET_TESTBED_operation_create_ (opc, &opstart_overlay_connect,
867 &oprelease_overlay_connect);
868 GNUNET_TESTBED_host_queue_oc_ (p1->host, opc->op);
869 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
875 * Function called when a peer manage service operation is ready
877 * @param cls the closure from GNUNET_TESTBED_operation_create_()
880 opstart_manage_service (void *cls)
882 struct OperationContext *opc = cls;
883 struct ManageServiceData *data = opc->data;
884 struct GNUNET_MQ_Envelope *env;
885 struct GNUNET_TESTBED_ManagePeerServiceMessage *msg;
888 GNUNET_assert (NULL != data);
889 xlen = data->msize - sizeof (struct GNUNET_TESTBED_ManagePeerServiceMessage);
890 env = GNUNET_MQ_msg_extra (msg,
892 GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE);
893 msg->peer_id = htonl (data->peer->unique_id);
894 msg->operation_id = GNUNET_htonll (opc->id);
895 msg->start = (uint8_t) data->start;
896 GNUNET_memcpy (&msg[1],
899 GNUNET_free (data->service_name);
900 data->service_name = NULL;
901 opc->state = OPC_STATE_STARTED;
902 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
903 GNUNET_MQ_send (opc->c->mq,
909 * Callback which will be called when peer manage server operation is released
911 * @param cls the closure from GNUNET_TESTBED_operation_create_()
914 oprelease_manage_service (void *cls)
916 struct OperationContext *opc = cls;
917 struct ManageServiceData *data;
922 case OPC_STATE_STARTED:
923 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
926 GNUNET_assert (NULL != data);
927 GNUNET_free (data->service_name);
929 case OPC_STATE_FINISHED:
932 GNUNET_free_non_null (data);
938 * Start or stop given service at a peer. This should not be called to
939 * start/stop the peer's ARM service. Use GNUNET_TESTBED_peer_start(),
940 * GNUNET_TESTBED_peer_stop() for starting/stopping peer's ARM service. Success
941 * or failure of the generated operation is signalled through the controller
942 * event callback and/or operation completion callback.
944 * @param op_cls the closure for the operation
945 * @param peer the peer whose service is to be started/stopped
946 * @param service_name the name of the service
947 * @param cb the operation completion callback
948 * @param cb_cls the closure for the operation completion callback
949 * @param start 1 to start the service; 0 to stop the service
950 * @return an operation handle; NULL upon error (peer not running)
952 struct GNUNET_TESTBED_Operation *
953 GNUNET_TESTBED_peer_manage_service (void *op_cls,
954 struct GNUNET_TESTBED_Peer *peer,
955 const char *service_name,
956 GNUNET_TESTBED_OperationCompletionCallback cb,
960 struct ManageServiceData *data;
961 struct OperationContext *opc;
964 GNUNET_assert (TESTBED_PS_STARTED == peer->state); /* peer is not running? */
965 msize = strlen (service_name) + 1;
966 msize += sizeof (struct GNUNET_TESTBED_ManagePeerServiceMessage);
967 if (GNUNET_MAX_MESSAGE_SIZE < msize)
969 data = GNUNET_new (struct ManageServiceData);
971 data->cb_cls = cb_cls;
973 data->service_name = GNUNET_strdup (service_name);
975 data->msize = (uint16_t) msize;
976 opc = GNUNET_new (struct OperationContext);
978 opc->c = peer->controller;
979 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
980 opc->type = OP_MANAGE_SERVICE;
981 opc->op_cls = op_cls;
983 GNUNET_TESTBED_operation_create_ (opc, &opstart_manage_service,
984 &oprelease_manage_service);
985 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
987 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
993 /* end of testbed_api_peers.c */