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,
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 == peer->state));
233 opc->state = OPC_STATE_STARTED;
234 env = GNUNET_MQ_msg(msg,
235 GNUNET_MESSAGE_TYPE_TESTBED_START_PEER);
236 msg->peer_id = htonl(peer->unique_id);
237 msg->operation_id = GNUNET_htonll(opc->id);
238 GNUNET_TESTBED_insert_opc_(opc->c, opc);
239 GNUNET_MQ_send(peer->controller->mq,
245 * Callback which will be called when peer start type operation is released
247 * @param cls the closure from GNUNET_TESTBED_operation_create_()
250 oprelease_peer_start(void *cls)
252 struct OperationContext *opc = cls;
256 case OPC_STATE_STARTED:
257 GNUNET_TESTBED_remove_opc_(opc->c, opc);
259 /* no break; continue */
261 GNUNET_free(opc->data);
264 case OPC_STATE_FINISHED:
272 * Function called when a peer stop operation is ready
274 * @param cls the closure from GNUNET_TESTBED_operation_create_()
277 opstart_peer_stop(void *cls)
279 struct OperationContext *opc = cls;
280 struct GNUNET_TESTBED_PeerStopMessage *msg;
281 struct PeerEventData *data;
282 struct GNUNET_TESTBED_Peer *peer;
283 struct GNUNET_MQ_Envelope *env;
285 GNUNET_assert(NULL != (data = opc->data));
286 GNUNET_assert(NULL != (peer = data->peer));
287 GNUNET_assert(TESTBED_PS_STARTED == peer->state);
288 opc->state = OPC_STATE_STARTED;
289 env = GNUNET_MQ_msg(msg,
290 GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER);
291 msg->peer_id = htonl(peer->unique_id);
292 msg->operation_id = GNUNET_htonll(opc->id);
293 GNUNET_TESTBED_insert_opc_(opc->c, opc);
294 GNUNET_MQ_send(peer->controller->mq,
300 * Callback which will be called when peer stop type operation is released
302 * @param cls the closure from GNUNET_TESTBED_operation_create_()
305 oprelease_peer_stop(void *cls)
307 struct OperationContext *opc = cls;
311 case OPC_STATE_STARTED:
312 GNUNET_TESTBED_remove_opc_(opc->c, opc);
314 /* no break; continue */
316 GNUNET_free(opc->data);
319 case OPC_STATE_FINISHED:
327 * Generate PeerGetConfigurationMessage
329 * @param peer_id the id of the peer whose information we have to get
330 * @param operation_id the ip of the operation that should be represented in the
332 * @return the PeerGetConfigurationMessage
334 struct GNUNET_TESTBED_PeerGetConfigurationMessage *
335 GNUNET_TESTBED_generate_peergetconfig_msg_(uint32_t peer_id,
336 uint64_t operation_id)
338 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
342 (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
344 htons(sizeof(struct GNUNET_TESTBED_PeerGetConfigurationMessage));
345 msg->header.type = htons(GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_INFORMATION);
346 msg->peer_id = htonl(peer_id);
347 msg->operation_id = GNUNET_htonll(operation_id);
353 * Function called when a peer get information operation is ready
355 * @param cls the closure from GNUNET_TESTBED_operation_create_()
358 opstart_peer_getinfo(void *cls)
360 struct OperationContext *opc = cls;
361 struct PeerInfoData *data = opc->data;
362 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
364 GNUNET_assert(NULL != data);
365 opc->state = OPC_STATE_STARTED;
367 GNUNET_TESTBED_generate_peergetconfig_msg_(data->peer->unique_id,
369 GNUNET_TESTBED_insert_opc_(opc->c, opc);
370 GNUNET_TESTBED_queue_message_(opc->c, &msg->header);
375 * Callback which will be called when peer stop type operation is released
377 * @param cls the closure from GNUNET_TESTBED_operation_create_()
380 oprelease_peer_getinfo(void *cls)
382 struct OperationContext *opc = cls;
383 struct GNUNET_TESTBED_PeerInformation *data;
387 case OPC_STATE_STARTED:
388 GNUNET_TESTBED_remove_opc_(opc->c, opc);
390 /* no break; continue */
392 GNUNET_free(opc->data);
395 case OPC_STATE_FINISHED:
397 GNUNET_assert(NULL != data);
400 case GNUNET_TESTBED_PIT_CONFIGURATION:
401 if (NULL != data->result.cfg)
402 GNUNET_CONFIGURATION_destroy(data->result.cfg);
405 case GNUNET_TESTBED_PIT_IDENTITY:
406 GNUNET_free(data->result.id);
410 GNUNET_assert(0); /* We should never reach here */
420 * Function called when a overlay connect operation is ready
422 * @param cls the closure from GNUNET_TESTBED_operation_create_()
425 opstart_overlay_connect(void *cls)
427 struct OperationContext *opc = cls;
428 struct GNUNET_MQ_Envelope *env;
429 struct GNUNET_TESTBED_OverlayConnectMessage *msg;
430 struct OverlayConnectData *data;
432 opc->state = OPC_STATE_STARTED;
434 GNUNET_assert(NULL != data);
435 env = GNUNET_MQ_msg(msg,
436 GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT);
437 msg->peer1 = htonl(data->p1->unique_id);
438 msg->peer2 = htonl(data->p2->unique_id);
439 msg->operation_id = GNUNET_htonll(opc->id);
440 msg->peer2_host_id = htonl(GNUNET_TESTBED_host_get_id_(data->p2->host));
441 GNUNET_TESTBED_insert_opc_(opc->c,
443 GNUNET_MQ_send(opc->c->mq,
449 * Callback which will be called when overlay connect operation is released
451 * @param cls the closure from GNUNET_TESTBED_operation_create_()
454 oprelease_overlay_connect(void *cls)
456 struct OperationContext *opc = cls;
457 struct OverlayConnectData *data;
465 case OPC_STATE_STARTED:
466 GNUNET_TESTBED_remove_opc_(opc->c, opc);
469 case OPC_STATE_FINISHED:
478 * Function called when a peer reconfigure operation is ready
480 * @param cls the closure from GNUNET_TESTBED_operation_create_()
483 opstart_peer_reconfigure(void *cls)
485 struct OperationContext *opc = cls;
486 struct PeerReconfigureData *data = opc->data;
487 struct GNUNET_MQ_Envelope *env;
488 struct GNUNET_TESTBED_PeerReconfigureMessage *msg;
492 opc->state = OPC_STATE_STARTED;
493 GNUNET_assert(NULL != data);
494 xc_size = GNUNET_TESTBED_compress_config_(data->config,
497 GNUNET_free(data->config);
499 GNUNET_assert(xc_size < UINT16_MAX - sizeof(*msg));
500 env = GNUNET_MQ_msg_extra(msg,
502 GNUNET_MESSAGE_TYPE_TESTBED_RECONFIGURE_PEER);
503 msg->peer_id = htonl(data->peer->unique_id);
504 msg->operation_id = GNUNET_htonll(opc->id);
505 msg->config_size = htons(data->cfg_size);
506 GNUNET_memcpy(&msg[1],
509 GNUNET_free(xconfig);
512 GNUNET_TESTBED_insert_opc_(opc->c, opc);
513 GNUNET_MQ_send(opc->c->mq,
519 * Callback which will be called when a peer reconfigure operation is released
521 * @param cls the closure from GNUNET_TESTBED_operation_create_()
524 oprelease_peer_reconfigure(void *cls)
526 struct OperationContext *opc = cls;
527 struct PeerReconfigureData *data = opc->data;
532 GNUNET_free(data->config);
536 case OPC_STATE_STARTED:
537 GNUNET_TESTBED_remove_opc_(opc->c, opc);
540 case OPC_STATE_FINISHED:
548 * Lookup a peer by ID.
550 * @param id global peer ID assigned to the peer
551 * @return handle to the host, NULL on error
553 struct GNUNET_TESTBED_Peer *
554 GNUNET_TESTBED_peer_lookup_by_id_(uint32_t id)
562 * Create the given peer at the specified host using the given
563 * controller. If the given controller is not running on the target
564 * host, it should find or create a controller at the target host and
565 * delegate creating the peer. Explicit delegation paths can be setup
566 * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation
567 * path exists, a direct link with a subordinate controller is setup
568 * for the first delegated peer to a particular host; the subordinate
569 * controller is then destroyed once the last peer that was delegated
570 * to the remote host is stopped.
572 * Creating the peer only creates the handle to manipulate and further
573 * configure the peer; use "GNUNET_TESTBED_peer_start" and
574 * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
577 * Note that the given configuration will be adjusted by the
578 * controller to avoid port/path conflicts with other peers.
579 * The "final" configuration can be obtained using
580 * 'GNUNET_TESTBED_peer_get_information'.
582 * @param controller controller process to use
583 * @param host host to run the peer on; cannot be NULL
584 * @param cfg Template configuration to use for the peer. Should exist until
585 * operation is cancelled or GNUNET_TESTBED_operation_done() is called
586 * @param cb the callback to call when the peer has been created
587 * @param cls the closure to the above callback
588 * @return the operation handle
590 struct GNUNET_TESTBED_Operation *
591 GNUNET_TESTBED_peer_create(struct GNUNET_TESTBED_Controller *controller,
592 struct GNUNET_TESTBED_Host *host,
593 const struct GNUNET_CONFIGURATION_Handle *cfg,
594 GNUNET_TESTBED_PeerCreateCallback cb, void *cls)
596 struct GNUNET_TESTBED_Peer *peer;
597 struct PeerCreateData *data;
598 struct OperationContext *opc;
599 static uint32_t id_gen;
601 peer = GNUNET_new(struct GNUNET_TESTBED_Peer);
602 peer->controller = controller;
604 peer->unique_id = id_gen++;
605 peer->state = TESTBED_PS_INVALID;
606 data = GNUNET_new(struct PeerCreateData);
612 opc = GNUNET_new(struct OperationContext);
615 opc->id = GNUNET_TESTBED_get_next_op_id(controller);
616 opc->type = OP_PEER_CREATE;
618 GNUNET_TESTBED_operation_create_(opc, &opstart_peer_create,
619 &oprelease_peer_create);
620 GNUNET_TESTBED_operation_queue_insert_(controller->opq_parallel_operations,
622 GNUNET_TESTBED_operation_begin_wait_(opc->op);
628 * Start the given peer.
630 * @param op_cls the closure for this operation; will be set in
631 * event->details.operation_finished.op_cls when this operation fails.
632 * @param peer peer to start
633 * @param pcc function to call upon completion
634 * @param pcc_cls closure for 'pcc'
635 * @return handle to the operation
637 struct GNUNET_TESTBED_Operation *
638 GNUNET_TESTBED_peer_start(void *op_cls, struct GNUNET_TESTBED_Peer *peer,
639 GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
641 struct OperationContext *opc;
642 struct PeerEventData *data;
644 data = GNUNET_new(struct PeerEventData);
647 data->pcc_cls = pcc_cls;
648 opc = GNUNET_new(struct OperationContext);
649 opc->c = peer->controller;
651 opc->op_cls = op_cls;
652 opc->id = GNUNET_TESTBED_get_next_op_id(opc->c);
653 opc->type = OP_PEER_START;
655 GNUNET_TESTBED_operation_create_(opc, &opstart_peer_start,
656 &oprelease_peer_start);
657 GNUNET_TESTBED_operation_queue_insert_(opc->c->opq_parallel_operations,
659 GNUNET_TESTBED_operation_begin_wait_(opc->op);
665 * Stop the given peer. The handle remains valid (use
666 * "GNUNET_TESTBED_peer_destroy" to fully clean up the
667 * state of the peer).
669 * @param op_cls the closure for this operation; will be set in the event
671 * @param peer peer to stop
672 * @param pcc function to call upon completion
673 * @param pcc_cls closure for 'pcc'
674 * @return handle to the operation
676 struct GNUNET_TESTBED_Operation *
677 GNUNET_TESTBED_peer_stop(void *op_cls,
678 struct GNUNET_TESTBED_Peer *peer,
679 GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
681 struct OperationContext *opc;
682 struct PeerEventData *data;
684 data = GNUNET_new(struct PeerEventData);
687 data->pcc_cls = pcc_cls;
688 opc = GNUNET_new(struct OperationContext);
689 opc->c = peer->controller;
691 opc->op_cls = op_cls;
692 opc->id = GNUNET_TESTBED_get_next_op_id(opc->c);
693 opc->type = OP_PEER_STOP;
695 GNUNET_TESTBED_operation_create_(opc, &opstart_peer_stop,
696 &oprelease_peer_stop);
697 GNUNET_TESTBED_operation_queue_insert_(opc->c->opq_parallel_operations,
699 GNUNET_TESTBED_operation_begin_wait_(opc->op);
705 * Request information about a peer. The controller callback will not be called
706 * with event type GNUNET_TESTBED_ET_OPERATION_FINISHED when result for this
707 * operation is available. Instead, the GNUNET_TESTBED_PeerInfoCallback() will
709 * The peer information in the callback is valid until the operation is canceled.
711 * @param peer peer to request information about
712 * @param pit desired information
713 * @param cb the convenience callback to be called when results for this
714 * operation are available
715 * @param cb_cls the closure for the above callback
716 * @return handle to the operation
718 struct GNUNET_TESTBED_Operation *
719 GNUNET_TESTBED_peer_get_information(struct GNUNET_TESTBED_Peer *peer,
720 enum GNUNET_TESTBED_PeerInformationType
721 pit, GNUNET_TESTBED_PeerInfoCallback cb,
724 struct OperationContext *opc;
725 struct PeerInfoData *data;
727 GNUNET_assert(GNUNET_TESTBED_PIT_GENERIC != pit);
728 GNUNET_assert(NULL != cb);
729 data = GNUNET_new(struct PeerInfoData);
733 data->cb_cls = cb_cls;
734 opc = GNUNET_new(struct OperationContext);
735 opc->c = peer->controller;
737 opc->type = OP_PEER_INFO;
738 opc->id = GNUNET_TESTBED_get_next_op_id(opc->c);
740 GNUNET_TESTBED_operation_create_(opc, &opstart_peer_getinfo,
741 &oprelease_peer_getinfo);
742 GNUNET_TESTBED_operation_queue_insert_(opc->c->opq_parallel_operations,
744 GNUNET_TESTBED_operation_begin_wait_(opc->op);
750 * Change peer configuration. Must only be called while the
751 * peer is stopped. Ports and paths cannot be changed this
754 * @param peer peer to change configuration for
755 * @param cfg new configuration (differences to existing
756 * configuration only)
757 * @return handle to the operation
759 struct GNUNET_TESTBED_Operation *
760 GNUNET_TESTBED_peer_update_configuration(struct GNUNET_TESTBED_Peer *peer,
762 GNUNET_CONFIGURATION_Handle *cfg)
764 struct OperationContext *opc;
765 struct PeerReconfigureData *data;
768 data = GNUNET_new(struct PeerReconfigureData);
770 data->config = GNUNET_CONFIGURATION_serialize(cfg, &csize);
771 if (NULL == data->config)
776 if (csize > UINT16_MAX)
779 GNUNET_free(data->config);
783 data->cfg_size = (uint16_t)csize;
784 opc = GNUNET_new(struct OperationContext);
785 opc->c = peer->controller;
787 opc->type = OP_PEER_RECONFIGURE;
788 opc->id = GNUNET_TESTBED_get_next_op_id(opc->c);
790 GNUNET_TESTBED_operation_create_(opc, &opstart_peer_reconfigure,
791 &oprelease_peer_reconfigure);
792 GNUNET_TESTBED_operation_queue_insert_(opc->c->opq_parallel_operations,
794 GNUNET_TESTBED_operation_begin_wait_(opc->op);
800 * Destroy the given peer; the peer should have been
801 * stopped first (if it was started).
803 * @param peer peer to stop
804 * @return handle to the operation
806 struct GNUNET_TESTBED_Operation *
807 GNUNET_TESTBED_peer_destroy(struct GNUNET_TESTBED_Peer *peer)
809 struct OperationContext *opc;
811 opc = GNUNET_new(struct OperationContext);
813 opc->c = peer->controller;
814 opc->id = GNUNET_TESTBED_get_next_op_id(peer->controller);
815 opc->type = OP_PEER_DESTROY;
817 GNUNET_TESTBED_operation_create_(opc, &opstart_peer_destroy,
818 &oprelease_peer_destroy);
819 GNUNET_TESTBED_operation_queue_insert_(opc->c->opq_parallel_operations,
821 GNUNET_TESTBED_operation_begin_wait_(opc->op);
827 * Manipulate the P2P underlay topology by configuring a link
830 * @param op_cls closure argument to give with the operation event
831 * @param p1 first peer
832 * @param p2 second peer
833 * @param co option to change
834 * @param ... option-specific values
835 * @return handle to the operation, NULL if configuring the link at this
836 * time is not allowed
838 struct GNUNET_TESTBED_Operation *
839 GNUNET_TESTBED_underlay_configure_link(void *op_cls,
840 struct GNUNET_TESTBED_Peer *p1,
841 struct GNUNET_TESTBED_Peer *p2,
842 enum GNUNET_TESTBED_ConnectOption co,
851 * Both peers must have been started before calling this function.
852 * This function then obtains a HELLO from 'p1', gives it to 'p2'
853 * and asks 'p2' to connect to 'p1'.
855 * @param op_cls closure argument to give with the operation event
856 * @param cb the callback to call when this operation has finished
857 * @param cb_cls the closure for the above callback
858 * @param p1 first peer
859 * @param p2 second peer
860 * @return handle to the operation, NULL if connecting these two
861 * peers is fundamentally not possible at this time (peers
862 * not running or underlay disallows)
864 struct GNUNET_TESTBED_Operation *
865 GNUNET_TESTBED_overlay_connect(void *op_cls,
866 GNUNET_TESTBED_OperationCompletionCallback cb,
867 void *cb_cls, struct GNUNET_TESTBED_Peer *p1,
868 struct GNUNET_TESTBED_Peer *p2)
870 struct OperationContext *opc;
871 struct OverlayConnectData *data;
873 GNUNET_assert((TESTBED_PS_STARTED == p1->state) && (TESTBED_PS_STARTED == p2->state));
874 data = GNUNET_new(struct OverlayConnectData);
878 data->cb_cls = cb_cls;
879 opc = GNUNET_new(struct OperationContext);
881 opc->c = p1->controller;
882 opc->id = GNUNET_TESTBED_get_next_op_id(opc->c);
883 opc->type = OP_OVERLAY_CONNECT;
884 opc->op_cls = op_cls;
886 GNUNET_TESTBED_operation_create_(opc, &opstart_overlay_connect,
887 &oprelease_overlay_connect);
888 GNUNET_TESTBED_host_queue_oc_(p1->host, opc->op);
889 GNUNET_TESTBED_operation_begin_wait_(opc->op);
895 * Function called when a peer manage service operation is ready
897 * @param cls the closure from GNUNET_TESTBED_operation_create_()
900 opstart_manage_service(void *cls)
902 struct OperationContext *opc = cls;
903 struct ManageServiceData *data = opc->data;
904 struct GNUNET_MQ_Envelope *env;
905 struct GNUNET_TESTBED_ManagePeerServiceMessage *msg;
908 GNUNET_assert(NULL != data);
909 xlen = data->msize - sizeof(struct GNUNET_TESTBED_ManagePeerServiceMessage);
910 env = GNUNET_MQ_msg_extra(msg,
912 GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE);
913 msg->peer_id = htonl(data->peer->unique_id);
914 msg->operation_id = GNUNET_htonll(opc->id);
915 msg->start = (uint8_t)data->start;
916 GNUNET_memcpy(&msg[1],
919 GNUNET_free(data->service_name);
920 data->service_name = NULL;
921 opc->state = OPC_STATE_STARTED;
922 GNUNET_TESTBED_insert_opc_(opc->c, opc);
923 GNUNET_MQ_send(opc->c->mq,
929 * Callback which will be called when peer manage server operation is released
931 * @param cls the closure from GNUNET_TESTBED_operation_create_()
934 oprelease_manage_service(void *cls)
936 struct OperationContext *opc = cls;
937 struct ManageServiceData *data;
942 case OPC_STATE_STARTED:
943 GNUNET_TESTBED_remove_opc_(opc->c, opc);
947 GNUNET_assert(NULL != data);
948 GNUNET_free(data->service_name);
951 case OPC_STATE_FINISHED:
954 GNUNET_free_non_null(data);
960 * Start or stop given service at a peer. This should not be called to
961 * start/stop the peer's ARM service. Use GNUNET_TESTBED_peer_start(),
962 * GNUNET_TESTBED_peer_stop() for starting/stopping peer's ARM service. Success
963 * or failure of the generated operation is signalled through the controller
964 * event callback and/or operation completion callback.
966 * @param op_cls the closure for the operation
967 * @param peer the peer whose service is to be started/stopped
968 * @param service_name the name of the service
969 * @param cb the operation completion callback
970 * @param cb_cls the closure for the operation completion callback
971 * @param start 1 to start the service; 0 to stop the service
972 * @return an operation handle; NULL upon error (peer not running)
974 struct GNUNET_TESTBED_Operation *
975 GNUNET_TESTBED_peer_manage_service(void *op_cls,
976 struct GNUNET_TESTBED_Peer *peer,
977 const char *service_name,
978 GNUNET_TESTBED_OperationCompletionCallback cb,
982 struct ManageServiceData *data;
983 struct OperationContext *opc;
986 GNUNET_assert(TESTBED_PS_STARTED == peer->state); /* peer is not running? */
987 msize = strlen(service_name) + 1;
988 msize += sizeof(struct GNUNET_TESTBED_ManagePeerServiceMessage);
989 if (GNUNET_MAX_MESSAGE_SIZE < msize)
991 data = GNUNET_new(struct ManageServiceData);
993 data->cb_cls = cb_cls;
995 data->service_name = GNUNET_strdup(service_name);
997 data->msize = (uint16_t)msize;
998 opc = GNUNET_new(struct OperationContext);
1000 opc->c = peer->controller;
1001 opc->id = GNUNET_TESTBED_get_next_op_id(opc->c);
1002 opc->type = OP_MANAGE_SERVICE;
1003 opc->op_cls = op_cls;
1005 GNUNET_TESTBED_operation_create_(opc, &opstart_manage_service,
1006 &oprelease_manage_service);
1007 GNUNET_TESTBED_operation_queue_insert_(opc->c->opq_parallel_operations,
1009 GNUNET_TESTBED_operation_begin_wait_(opc->op);
1015 /* end of testbed_api_peers.c */