2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/testbed_api_testbed.c
23 * @brief high-level testbed management
24 * @author Christian Grothoff
25 * @author Sree Harsha Totakura
29 #include "gnunet_testbed_service.h"
30 #include "testbed_api_peers.h"
31 #include "testbed_api_hosts.h"
32 #include "testbed_api_topology.h"
35 * Generic loggins shorthand
37 #define LOG(kind,...) \
38 GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
47 * The testbed operation handle
49 struct GNUNET_TESTBED_Operation *op;
52 * Context information for GNUNET_TESTBED_run()
54 struct RunContext *rc;
62 * The next pointer for DLL
64 struct DLLOperation *next;
67 * The prev pointer for DLL
69 struct DLLOperation *prev;
74 * States of RunContext
84 * Controllers on given hosts started and linked
89 * The testbed run is ready and the master callback can be called now. At this
90 * time the peers are all started and if a topology is provided in the
91 * configuration the topology would have been attempted
101 * Peers are destroyed
113 * The controller handle
115 struct GNUNET_TESTBED_Controller *c;
118 * The configuration of the controller. This is based on the cfg given to the
119 * function GNUNET_TESTBED_run(). We also use this config as a template while
122 struct GNUNET_CONFIGURATION_Handle *cfg;
125 * Handle to the host on which the controller runs
127 struct GNUNET_TESTBED_Host *h;
130 * The handle to the controller process
132 struct GNUNET_TESTBED_ControllerProc *cproc;
135 * The callback to use as controller callback
137 GNUNET_TESTBED_ControllerCallback cc;
140 * The pointer to the controller callback
145 * TestMaster callback to call when testbed initialization is done
147 GNUNET_TESTBED_TestMaster test_master;
150 * The closure for the TestMaster callback
152 void *test_master_cls;
155 * The head element of DLL operations
157 struct DLLOperation *dll_op_head;
160 * The tail element of DLL operations
162 struct DLLOperation *dll_op_tail;
165 * An array of hosts loaded from the hostkeys file
167 struct GNUNET_TESTBED_Host **hosts;
170 * The handle for whether a host is habitable or not
172 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
175 * Array of peers which we create
177 struct GNUNET_TESTBED_Peer **peers;
180 * The topology generation operation. Will be null if no topology is set in
183 struct GNUNET_TESTBED_Operation *topology_operation;
186 * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
191 * Host registration handle
193 struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
196 * Host registration task
198 GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
201 * Task to be run while shutting down
203 GNUNET_SCHEDULER_TaskIdentifier shutdown_run_task;
206 * The event mask for the controller
211 * State of this context
216 * The topology which has to be achieved with the peers started in this context
218 enum GNUNET_TESTBED_TopologyOption topology;
221 * Number of hosts in the given host file
223 unsigned int num_hosts;
226 * Number of registered hosts. Also used as a counter while checking
227 * habitabillity of hosts
229 unsigned int reg_hosts;
232 * Current peer count for an operation; Set this to 0 and increment for each
233 * successful operation on a peer
235 unsigned int peer_count;
238 * number of peers to start
240 unsigned int num_peers;
243 * counter to count overlay connect attempts. This counter includes both
244 * successful and failed overlay connects
246 unsigned int oc_count;
249 * Expected overlay connects. Should be zero if no topology is relavant
254 * Number of random links to established
256 unsigned int random_links;
262 * Task for starting peers
264 * @param cls the RunHandle
265 * @param tc the task context from scheduler
268 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
270 struct RunContext *rc = cls;
271 struct DLLOperation *dll_op;
274 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting Peers\n");
275 for (peer = 0; peer < rc->num_peers; peer++)
277 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
278 dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
279 dll_op->cls = rc->peers[peer];
280 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
287 * Functions of this signature are called when a peer has been successfully
290 * @param cls the closure from GNUNET_TESTBED_peer_create()
291 * @param peer the handle for the created peer; NULL on any error during
293 * @param emsg NULL if peer is not NULL; else MAY contain the error description
296 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
298 struct DLLOperation *dll_op = cls;
299 struct RunContext *rc;
301 GNUNET_assert (NULL != dll_op);
303 GNUNET_assert (NULL != rc);
304 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
305 GNUNET_TESTBED_operation_done (dll_op->op);
306 GNUNET_free (dll_op);
310 LOG (GNUNET_ERROR_TYPE_WARNING, "Error while creating a peer: %s\n",
312 /* FIXME: GNUNET_TESTBED_shutdown_run()? */
315 rc->peers[rc->peer_count] = peer;
317 if (rc->peer_count < rc->num_peers)
319 LOG (GNUNET_ERROR_TYPE_DEBUG, "Required peers created successfully\n");
320 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
325 * Assuming all peers have been destroyed cleanup run handle
327 * @param cls the run handle
328 * @param tc the task context from scheduler
331 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
333 struct RunContext *rc = cls;
334 struct DLLOperation *dll_op;
337 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
338 GNUNET_assert (NULL == rc->reg_handle);
339 GNUNET_assert (NULL == rc->peers);
340 GNUNET_assert (NULL == rc->hc_handles);
341 GNUNET_assert (RC_PEERS_DESTROYED == rc->state);
342 if (NULL != rc->dll_op_head)
343 { /* cancel our pending operations */
344 while (NULL != (dll_op = rc->dll_op_head))
346 GNUNET_TESTBED_operation_done (dll_op->op);
347 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
348 GNUNET_free (dll_op);
352 GNUNET_TESTBED_controller_disconnect (rc->c);
353 if (NULL != rc->cproc)
354 GNUNET_TESTBED_controller_stop (rc->cproc);
356 GNUNET_TESTBED_host_destroy (rc->h);
357 for (hid = 0; hid < rc->num_hosts; hid++)
358 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
359 GNUNET_free_non_null (rc->hosts);
361 GNUNET_CONFIGURATION_destroy (rc->cfg);
362 GNUNET_free_non_null (rc->topo_file);
368 * Stops the testbed run and releases any used resources
370 * @param cls the tesbed run handle
371 * @param tc the task context from scheduler
374 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
378 * Function to shutdown now
380 * @param rc the RunContext
383 shutdown_now (struct RunContext *rc)
385 if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task)
386 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
387 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
392 * Stops the testbed run and releases any used resources
394 * @param cls the tesbed run handle
395 * @param tc the task context from scheduler
398 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
400 struct RunContext *rc = cls;
401 struct DLLOperation *dll_op;
402 int all_peers_destroyed;
406 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task);
407 rc->shutdown_run_task = GNUNET_SCHEDULER_NO_TASK;
408 if (NULL != rc->hc_handles)
410 for (nhost = 0; nhost < rc->num_hosts; nhost++)
411 if (NULL != rc->hc_handles[nhost])
412 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
413 GNUNET_free (rc->hc_handles);
414 rc->hc_handles = NULL;
416 /* Stop register hosts task if it is running */
417 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
419 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
420 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
422 if (NULL != rc->reg_handle)
424 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
425 rc->reg_handle = NULL;
429 if (NULL != rc->peers)
431 if (NULL != rc->topology_operation)
433 GNUNET_TESTBED_operation_done (rc->topology_operation);
434 rc->topology_operation = NULL;
436 if (RC_INIT == rc->state)
437 rc->state = RC_READY; /* Even though we haven't called the master callback */
439 /* Check if some peers are stopped */
440 for (peer = 0; peer < rc->num_peers; peer++)
442 if (NULL == rc->peers[peer])
444 if (PS_STOPPED != rc->peers[peer]->state)
447 if (peer == rc->num_peers)
449 /* All peers are stopped */
450 rc->state = RC_PEERS_STOPPED;
451 all_peers_destroyed = GNUNET_YES;
452 for (peer = 0; peer < rc->num_peers; peer++)
454 if (NULL == rc->peers[peer])
456 all_peers_destroyed = GNUNET_NO;
457 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
458 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer]);
459 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
462 if (all_peers_destroyed == GNUNET_NO)
465 /* Some peers are stopped */
466 for (peer = 0; peer < rc->num_peers; peer++)
468 if ((NULL == rc->peers[peer]) || (PS_STARTED != rc->peers[peer]->state))
473 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
474 dll_op->op = GNUNET_TESTBED_peer_stop (rc->peers[peer], NULL, NULL);
475 dll_op->cls = rc->peers[peer];
476 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
479 if (rc->peer_count != rc->num_peers)
481 GNUNET_free (rc->peers);
485 rc->state = RC_PEERS_DESTROYED; /* No peers are present so we consider the
486 * state where all peers are destroyed */
487 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
492 * Task to call master task
494 * @param cls the run context
495 * @param tc the task context
498 call_master (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
500 struct RunContext *rc = cls;
502 if (NULL != rc->topology_operation)
504 GNUNET_TESTBED_operation_done (rc->topology_operation);
505 rc->topology_operation = NULL;
507 if (NULL != rc->test_master)
508 rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers);
513 * Function to create peers
515 * @param rc the RunContext
518 create_peers (struct RunContext *rc)
520 struct DLLOperation *dll_op;
524 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
525 GNUNET_assert (NULL != rc->c);
527 for (peer = 0; peer < rc->num_peers; peer++)
529 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
532 GNUNET_TESTBED_peer_create (rc->c,
534 ? rc->h : rc->hosts[peer % rc->num_hosts],
536 peer_create_cb, dll_op);
537 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
543 * Signature of the event handler function called by the
544 * respective event controller.
547 * @param event information about the event
550 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
552 struct RunContext *rc = cls;
553 struct DLLOperation *dll_op;
554 unsigned int peer_id;
556 if (RC_INIT == rc->state)
560 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
561 dll_op = event->details.operation_finished.op_cls;
562 if (NULL != event->details.operation_finished.emsg)
564 LOG (GNUNET_ERROR_TYPE_ERROR,
565 _("Linking controllers failed. Exiting"));
570 GNUNET_assert (event->details.operation_finished.operation == dll_op->op);
571 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
572 GNUNET_TESTBED_operation_done (dll_op->op);
573 GNUNET_free (dll_op);
574 if (rc->reg_hosts == rc->num_hosts)
576 rc->state = RC_LINKED;
586 if (NULL != rc->topology_operation)
590 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
591 case GNUNET_TESTBED_ET_CONNECT:
599 if (rc->oc_count == rc->num_oc)
601 rc->state = RC_READY;
602 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
603 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
607 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
609 if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
610 (event->details.operation_finished.operation == dll_op->op))
612 if ((GNUNET_TESTBED_ET_PEER_STOP == event->type) &&
613 (event->details.peer_stop.peer == dll_op->cls))
618 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
619 GNUNET_TESTBED_operation_done (dll_op->op);
620 GNUNET_free (dll_op);
622 if (rc->peer_count < rc->num_peers)
628 rc->state = RC_PEERS_STOPPED;
630 for (peer_id = 0; peer_id < rc->num_peers; peer_id++)
632 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
633 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer_id]);
634 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
638 case RC_PEERS_STOPPED:
639 rc->state = RC_PEERS_DESTROYED;
640 GNUNET_free (rc->peers);
642 LOG (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully destroyed\n");
643 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
651 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
652 rc->cc (rc->cc_cls, event);
653 if (GNUNET_TESTBED_ET_PEER_START != event->type)
655 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
656 if ((NULL != dll_op->cls) &&
657 (event->details.peer_start.peer == dll_op->cls))
659 if (NULL == dll_op) /* Not our operation */
661 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
662 GNUNET_TESTBED_operation_done (dll_op->op);
663 GNUNET_free (dll_op);
665 if (rc->peer_count < rc->num_peers)
667 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers started successfully\n");
668 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
670 if ( (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology)
671 || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology)
672 || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
674 rc->topology_operation =
675 GNUNET_TESTBED_overlay_configure_topology (NULL,
681 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
683 else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
685 GNUNET_assert (NULL != rc->topo_file);
686 rc->topology_operation =
687 GNUNET_TESTBED_overlay_configure_topology (NULL,
693 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
696 rc->topology_operation =
697 GNUNET_TESTBED_overlay_configure_topology (NULL,
702 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
703 if (NULL == rc->topology_operation)
704 LOG (GNUNET_ERROR_TYPE_WARNING,
705 "Not generating topology. Check number of peers\n");
709 rc->state = RC_READY;
710 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
711 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
716 * Task to register all hosts available in the global host list
718 * @param cls the RunContext
719 * @param tc the scheduler task context
722 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
726 * Callback which will be called to after a host registration succeeded or failed
728 * @param cls the closure
729 * @param emsg the error message; NULL if host registration is successful
732 host_registration_completion (void *cls, const char *emsg)
734 struct RunContext *rc = cls;
736 rc->reg_handle = NULL;
739 LOG (GNUNET_ERROR_TYPE_WARNING,
740 _("Host registration failed for a host. Error: %s\n"), emsg);
744 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
749 * Task to register all hosts available in the global host list
751 * @param cls RunContext
752 * @param tc the scheduler task context
755 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
757 struct RunContext *rc = cls;
758 struct DLLOperation *dll_op;
761 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
762 if (rc->reg_hosts == rc->num_hosts)
764 LOG (GNUNET_ERROR_TYPE_DEBUG,
765 "All hosts successfully registered\n");
767 for (slave = 0; slave < rc->num_hosts; slave++)
769 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
771 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
777 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
782 rc->reg_handle = GNUNET_TESTBED_register_host (rc->c,
783 rc->hosts[rc->reg_hosts++],
784 host_registration_completion,
790 * Callback to signal successfull startup of the controller process
792 * @param cls the closure from GNUNET_TESTBED_controller_start()
793 * @param cfg the configuration with which the controller has been started;
794 * NULL if status is not GNUNET_OK
795 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
796 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
799 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
802 struct RunContext *rc = cls;
805 if (status != GNUNET_OK)
810 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed startup failed\n");
818 GNUNET_CONFIGURATION_destroy (rc->cfg);
819 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
820 event_mask = rc->event_mask;
821 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
822 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
823 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
824 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
825 event_mask |= GNUNET_TESTBED_ET_CONNECT;
827 GNUNET_TESTBED_controller_connect (rc->cfg, rc->h, event_mask, &event_cb, rc);
828 if (0 < rc->num_hosts)
830 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
833 rc->state = RC_LINKED;
839 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
840 * inform whether the given host is habitable or not. The Handle returned by
841 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
844 * @param host the host whose status is being reported; will be NULL if the host
845 * given to GNUNET_TESTBED_is_host_habitable() is NULL
846 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
849 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host, int status)
851 struct RunContext *rc = cls;
854 for (nhost = 0; nhost < rc->num_hosts; nhost++)
856 if (host == rc->hosts[nhost])
859 GNUNET_assert (nhost != rc->num_hosts);
860 rc->hc_handles[nhost] = NULL;
861 if (GNUNET_NO == status)
863 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
864 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
865 GNUNET_TESTBED_host_get_hostname (host));
867 LOG (GNUNET_ERROR_TYPE_ERROR, _("Testbed cannot be started on localhost\n"));
872 if (rc->reg_hosts < rc->num_hosts)
874 GNUNET_free (rc->hc_handles);
875 rc->hc_handles = NULL;
876 rc->h = rc->hosts[0];
878 if (0 < rc->num_hosts)
879 rc->hosts = &rc->hosts[1];
882 GNUNET_free (rc->hosts);
885 /* FIXME: If we are starting controller on different host 127.0.0.1 may not ab
888 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, rc->cfg,
889 &controller_status_cb, rc);
890 if (NULL == rc->cproc)
892 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
899 * Convenience method for running a testbed with
900 * a single call. Underlay and overlay topology
901 * are configured using the "UNDERLAY" and "OVERLAY"
902 * options in the "[testbed]" section of the configuration\
903 * (with possible options given in "UNDERLAY_XXX" and/or
906 * The testbed is to be terminated using a call to
907 * "GNUNET_SCHEDULER_shutdown".
909 * @param host_filename name of the file with the 'hosts', NULL
910 * to run everything on 'localhost'
911 * @param cfg configuration to use (for testbed, controller and peers)
912 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
913 * @param event_mask bit mask with set of events to call 'cc' for;
914 * or-ed values of "1LL" shifted by the
915 * respective 'enum GNUNET_TESTBED_EventType'
916 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
917 * @param cc controller callback to invoke on events; This callback is called
918 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
919 * set in the event_mask as this is the only way get access to the
920 * handle of each peer
921 * @param cc_cls closure for cc
922 * @param test_master this callback will be called once the test is ready
923 * @param test_master_cls closure for 'test_master'.
926 GNUNET_TESTBED_run (const char *host_filename,
927 const struct GNUNET_CONFIGURATION_Handle *cfg,
928 unsigned int num_peers, uint64_t event_mask,
929 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
930 GNUNET_TESTBED_TestMaster test_master,
931 void *test_master_cls)
933 struct RunContext *rc;
935 unsigned long long random_links;
939 GNUNET_assert (num_peers > 0);
940 rc = GNUNET_malloc (sizeof (struct RunContext));
941 if (NULL != host_filename)
944 GNUNET_TESTBED_hosts_load_from_file (host_filename, &rc->hosts);
945 if (0 == rc->num_hosts)
947 LOG (GNUNET_ERROR_TYPE_WARNING,
948 _("No hosts loaded. Need at least one host\n"));
953 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, 0);
954 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
955 rc->num_peers = num_peers;
956 rc->event_mask = event_mask;
959 rc->test_master = test_master;
960 rc->test_master_cls = test_master_cls;
962 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
963 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
967 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology,
970 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
971 "testbed", "OVERLAY_TOPLOGY",
972 _("Specified topology must be supported by testbed"));
974 GNUNET_free (topology);
976 switch (rc->topology)
978 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
979 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
980 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
981 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
982 "OVERLAY_RANDOM_LINKS",
985 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
986 option to be set to the number of random links to be established */
987 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
988 "testbed", "OVERLAY_RANDOM_LINKS");
991 if (random_links > UINT32_MAX)
993 GNUNET_break (0); /* Too big number */
996 rc->random_links = (unsigned int) random_links;
998 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
999 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1000 "OVERLAY_TOPOLOGY_FILE",
1003 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1004 "testbed", "OVERLAY_TOPOLOGY_FILE");
1012 if (NULL != host_filename)
1014 rc->hc_handles = GNUNET_malloc (sizeof (struct
1015 GNUNET_TESTBED_HostHabitableCheckHandle *)
1017 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1019 if (NULL == (rc->hc_handles[nhost] =
1020 GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1025 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1026 if (NULL != rc->hc_handles[nhost])
1027 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
1028 GNUNET_free (rc->hc_handles);
1029 rc->hc_handles = NULL;
1036 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, rc->cfg,
1037 &controller_status_cb, rc);
1038 rc->shutdown_run_task =
1039 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1045 GNUNET_TESTBED_host_destroy (rc->h);
1046 if (NULL != rc->hosts)
1048 for (hid = 0; hid < rc->num_hosts; hid++)
1049 if (NULL != rc->hosts[hid])
1050 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1051 GNUNET_free (rc->hosts);
1057 /* end of testbed_api_testbed.c */