topology loading from file
[oweals/gnunet.git] / src / testbed / testbed_api_testbed.c
1 /*
2   This file is part of GNUnet
3   (C) 2008--2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file testbed/testbed_api_testbed.c
23  * @brief high-level testbed management
24  * @author Christian Grothoff
25  * @author Sree Harsha Totakura
26  */
27
28 #include "platform.h"
29 #include "gnunet_testbed_service.h"
30 #include "testbed_api_peers.h"
31 #include "testbed_api_hosts.h"
32 #include "testbed_api_topology.h"
33
34 /**
35  * Generic loggins shorthand
36  */
37 #define LOG(kind,...)                                           \
38   GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
39
40 /**
41  * Opaque handle to an abstract operation to be executed by the testing framework.
42  */
43 struct GNUNET_TESTBED_Testbed
44 {
45   /**
46    * The array of hosts
47    */
48   struct GNUNET_TESTBED_Host **hosts;
49
50   /**
51    * The number of hosts in the hosts array
52    */
53   unsigned int num_hosts;
54
55   /**
56    * The controller handle
57    */
58   struct GNUNET_TESTBED_Controller *c;
59 };
60
61
62 /**
63  * DLL of operations
64  */
65 struct DLLOperation
66 {
67   /**
68    * The testbed operation handle
69    */
70   struct GNUNET_TESTBED_Operation *op;
71
72   /**
73    * Context information for GNUNET_TESTBED_run()
74    */
75   struct RunContext *rc;
76
77   /**
78    * Closure
79    */
80   void *cls;
81
82   /**
83    * The next pointer for DLL
84    */
85   struct DLLOperation *next;
86
87   /**
88    * The prev pointer for DLL
89    */
90   struct DLLOperation *prev;
91 };
92
93
94 /**
95  * States of RunContext
96  */
97 enum State
98 {
99   /**
100    * Initial state
101    */
102   RC_INIT = 0,
103
104   /**
105    * The testbed run is ready and the master callback can be called now. At this
106    * time the peers are all started and if a topology is provided in the
107    * configuration the topology would have been attempted
108    */
109   RC_READY,
110
111   /**
112    * Peers are stopped
113    */
114   RC_PEERS_STOPPED,
115
116   /**
117    * Peers are destroyed
118    */
119   RC_PEERS_DESTROYED
120 };
121
122
123 /**
124  * Testbed Run Handle
125  */
126 struct RunContext
127 {
128   /**
129    * The controller handle
130    */
131   struct GNUNET_TESTBED_Controller *c;
132
133   /**
134    * Handle to the host on which the controller runs
135    */
136   struct GNUNET_TESTBED_Host *h;
137
138   /**
139    * The handle to the controller process
140    */
141   struct GNUNET_TESTBED_ControllerProc *cproc;
142
143   /**
144    * The callback to use as controller callback
145    */
146   GNUNET_TESTBED_ControllerCallback cc;
147
148   /**
149    * The pointer to the controller callback
150    */
151   void *cc_cls;
152
153   /**
154    * Master task to call when testbed initialization is done
155    */
156   GNUNET_SCHEDULER_Task master;
157
158   /**
159    * The closure for the master task
160    */
161   void *master_cls;
162
163   /**
164    * The head element of DLL operations
165    */
166   struct DLLOperation *dll_op_head;
167
168   /**
169    * The tail element of DLL operations
170    */
171   struct DLLOperation *dll_op_tail;
172
173   /**
174    * Array of peers which we create
175    */
176   struct GNUNET_TESTBED_Peer **peers;
177
178   /**
179    * The topology generation operation. Will be null if no topology is set in
180    * the configuration
181    */
182   struct GNUNET_TESTBED_Operation *topology_operation;
183
184   /**
185    * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
186    */
187   char *topo_file;
188
189   /**
190    * The event mask for the controller
191    */
192   uint64_t event_mask;
193
194   /**
195    * State of this context
196    */
197   enum State state;
198
199   /**
200    * The topology which has to be achieved with the peers started in this context
201    */
202   enum GNUNET_TESTBED_TopologyOption topology;
203
204   /**
205    * Current peer count for an operation; Set this to 0 and increment for each
206    * successful operation on a peer
207    */
208   unsigned int peer_count;
209
210   /**
211    * number of peers to start
212    */
213   unsigned int num_peers;
214
215   /**
216    * counter to count overlay connect attempts. This counter includes both
217    * successful and failed overlay connects
218    */
219   unsigned int oc_count;
220
221   /**
222    * Expected overlay connects. Should be zero if no topology is relavant
223    */
224   unsigned int num_oc;
225
226   /**
227    * Number of random links to established
228    */
229   unsigned int random_links;
230   
231 };
232
233
234 /**
235  * Task for starting peers
236  *
237  * @param cls the RunHandle
238  * @param tc the task context from scheduler
239  */
240 static void
241 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
242 {
243   struct RunContext *rc = cls;
244   struct DLLOperation *dll_op;
245   unsigned int peer;
246
247   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting Peers\n");
248   for (peer = 0; peer < rc->num_peers; peer++)
249   {
250     dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
251     dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
252     dll_op->cls = rc->peers[peer];
253     GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
254   }
255   rc->peer_count = 0;
256 }
257
258
259 /**
260  * Functions of this signature are called when a peer has been successfully
261  * created
262  *
263  * @param cls the closure from GNUNET_TESTBED_peer_create()
264  * @param peer the handle for the created peer; NULL on any error during
265  *          creation
266  * @param emsg NULL if peer is not NULL; else MAY contain the error description
267  */
268 static void
269 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
270 {
271   struct DLLOperation *dll_op = cls;
272   struct RunContext *rc;
273
274   GNUNET_assert (NULL != dll_op);
275   rc = dll_op->rc;
276   GNUNET_assert (NULL != rc);
277   GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
278   GNUNET_TESTBED_operation_done (dll_op->op);
279   GNUNET_free (dll_op);
280   if (NULL == peer)
281   {
282     if (NULL != emsg)
283       LOG (GNUNET_ERROR_TYPE_WARNING, "Error while creating a peer: %s\n",
284            emsg);
285     /* FIXME: GNUNET_TESTBED_shutdown_run()? */
286     return;
287   }
288   rc->peers[rc->peer_count] = peer;
289   rc->peer_count++;
290   if (rc->peer_count < rc->num_peers)
291     return;
292   LOG (GNUNET_ERROR_TYPE_DEBUG, "Required peers created successfully\n");
293   GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
294 }
295
296
297 /**
298  * Assuming all peers have been destroyed cleanup run handle
299  *
300  * @param cls the run handle
301  * @param tc the task context from scheduler
302  */
303 static void
304 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
305 {
306   struct RunContext *rc = cls;
307   struct DLLOperation *dll_op;
308
309   GNUNET_assert (NULL == rc->peers);
310   GNUNET_assert (RC_PEERS_DESTROYED == rc->state);
311   if (NULL != rc->c)
312     GNUNET_TESTBED_controller_disconnect (rc->c);
313   if (NULL != rc->cproc)
314     GNUNET_TESTBED_controller_stop (rc->cproc);
315   if (NULL != rc->h)
316     GNUNET_TESTBED_host_destroy (rc->h);
317   if (NULL != rc->dll_op_head)
318   {
319     LOG (GNUNET_ERROR_TYPE_WARNING,
320          _("Some operations are still pending. Cancelling them\n"));
321     while (NULL != (dll_op = rc->dll_op_head))
322     {
323       GNUNET_TESTBED_operation_done (dll_op->op);
324       GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
325       GNUNET_free (dll_op);
326     }
327   }
328   GNUNET_free_non_null (rc->topo_file);
329   GNUNET_free (rc);
330 }
331
332
333 /**
334  * Task to call master task
335  *
336  * @param cls the run context
337  * @param tc the task context
338  */
339 static void
340 call_master (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
341 {
342   struct RunContext *rc = cls;
343   
344   if (NULL != rc->topology_operation)
345   {
346     GNUNET_TESTBED_operation_done (rc->topology_operation);
347     rc->topology_operation = NULL;
348   }
349   if (NULL != rc->master)
350     GNUNET_SCHEDULER_add_continuation (rc->master, rc->master_cls,
351                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
352 }
353
354
355 /**
356  * Signature of the event handler function called by the
357  * respective event controller.
358  *
359  * @param cls closure
360  * @param event information about the event
361  */
362 static void
363 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
364 {
365   struct RunContext *rc = cls;
366   struct DLLOperation *dll_op;
367   unsigned int peer_id;
368
369   if (NULL != rc->topology_operation)
370   {
371     switch (event->type)
372     {
373     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
374     case GNUNET_TESTBED_ET_CONNECT:
375       rc->oc_count++;
376       break;
377     default:
378       GNUNET_assert (0);
379     }
380     if (rc->oc_count == rc->num_oc)
381     {
382       rc->state = RC_READY;
383       GNUNET_SCHEDULER_add_continuation (&call_master, rc,
384                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
385     }
386     return;
387   }
388   if ((RC_INIT != rc->state) &&
389       ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) ||
390        (GNUNET_TESTBED_ET_PEER_STOP == event->type)))
391   {
392     for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
393     {
394       if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
395           (event->details.operation_finished.operation == dll_op->op))
396         break;
397       if ((GNUNET_TESTBED_ET_PEER_STOP == event->type) &&
398           (event->details.peer_stop.peer == dll_op->cls))
399         break;
400     }
401     if (NULL == dll_op)
402       goto call_cc;
403     GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
404     GNUNET_TESTBED_operation_done (dll_op->op);
405     GNUNET_free (dll_op);
406     rc->peer_count++;
407     if (rc->peer_count < rc->num_peers)
408       return;
409     switch (rc->state)
410     {
411     case RC_READY:
412       rc->state = RC_PEERS_STOPPED;
413       rc->peer_count = 0;
414       for (peer_id = 0; peer_id < rc->num_peers; peer_id++)
415       {
416         dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
417         dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer_id]);
418         GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
419                                           dll_op);
420       }
421       break;
422     case RC_PEERS_STOPPED:
423       rc->state = RC_PEERS_DESTROYED;
424       GNUNET_free (rc->peers);
425       rc->peers = NULL;
426       LOG (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully destroyed\n");
427       GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
428       break;
429     default:
430       GNUNET_assert (0);
431     }
432     return;
433   }
434
435 call_cc:
436   if ((0 != (rc->event_mask && (1LL << event->type))) && (NULL != rc->cc))
437     rc->cc (rc->cc_cls, event);
438   if (GNUNET_TESTBED_ET_PEER_START != event->type)
439     return;
440   for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
441     if ((NULL != dll_op->cls) &&
442         (event->details.peer_start.peer == dll_op->cls))
443       break;
444   GNUNET_assert (NULL != dll_op);
445   GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
446   GNUNET_TESTBED_operation_done (dll_op->op);
447   GNUNET_free (dll_op);
448   rc->peer_count++;
449   if (rc->peer_count < rc->num_peers)
450     return;
451   LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers started successfully\n");
452   if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
453   {
454     if ( (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology)
455          || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology)
456          || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
457     {
458       rc->topology_operation =
459           GNUNET_TESTBED_overlay_configure_topology (NULL,
460                                                      rc->num_peers,
461                                                      rc->peers,
462                                                      &rc->num_oc,
463                                                      rc->topology,
464                                                      rc->random_links,
465                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
466     }
467     else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
468     {
469       GNUNET_assert (NULL != rc->topo_file);
470       rc->topology_operation =
471           GNUNET_TESTBED_overlay_configure_topology (NULL,
472                                                      rc->num_peers,
473                                                      rc->peers,
474                                                      &rc->num_oc,
475                                                      rc->topology,
476                                                      rc->topo_file,
477                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
478     }
479     else
480       rc->topology_operation =
481           GNUNET_TESTBED_overlay_configure_topology (NULL,
482                                                      rc->num_peers,
483                                                      rc->peers,
484                                                      &rc->num_oc,
485                                                      rc->topology,
486                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
487     if (NULL == rc->topology_operation)
488       LOG (GNUNET_ERROR_TYPE_WARNING,
489            "Not generating topology. Check number of peers\n");
490     else
491       return;
492   }
493   rc->state = RC_READY;
494   GNUNET_SCHEDULER_add_continuation (&call_master, rc,
495                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
496 }
497
498
499
500 /**
501  * Callback to signal successfull startup of the controller process
502  *
503  * @param cls the closure from GNUNET_TESTBED_controller_start()
504  * @param cfg the configuration with which the controller has been started;
505  *          NULL if status is not GNUNET_OK
506  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
507  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
508  */
509 static void
510 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
511                       int status)
512 {
513   struct RunContext *rc = cls;
514   struct DLLOperation *dll_op;
515   uint64_t event_mask;
516   unsigned int peer;
517
518   if (status != GNUNET_OK)
519   {
520     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed startup failed\n");
521     return;
522   }
523   event_mask = rc->event_mask;
524   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
525   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
526   if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
527     event_mask |= GNUNET_TESTBED_ET_CONNECT;
528   rc->c =
529       GNUNET_TESTBED_controller_connect (cfg, rc->h, event_mask, &event_cb, rc);
530   rc->peers =
531       GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
532   GNUNET_assert (NULL != rc->c);
533   rc->peer_count = 0;
534   for (peer = 0; peer < rc->num_peers; peer++)
535   {
536     dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
537     dll_op->rc = rc;
538     dll_op->op =
539         GNUNET_TESTBED_peer_create (rc->c, rc->h, cfg, peer_create_cb, dll_op);
540     GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
541   }
542 }
543
544
545 /**
546  * Stops the testbed run and releases any used resources
547  *
548  * @param cls the tesbed run handle
549  * @param tc the task context from scheduler
550  */
551 static void
552 shutdown_run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
553 {
554   struct RunContext *rc = cls;
555   struct DLLOperation *dll_op;
556   unsigned int peer;
557
558   if (NULL != rc->c)
559   {
560     if (NULL != rc->peers)
561     {
562       if (NULL != rc->topology_operation)
563       {
564         GNUNET_TESTBED_operation_done (rc->topology_operation);
565         rc->topology_operation = NULL;
566       }
567       if (RC_INIT == rc->state)
568         rc->state = RC_READY;   /* Even though we haven't called the master callback */
569       rc->peer_count = 0;
570       /* Check if some peers are stopped */
571       for (peer = 0; peer < rc->num_peers; peer++)
572       {
573         if (PS_STOPPED != rc->peers[peer]->state)
574           break;
575       }
576       if (peer == rc->num_peers)
577       {
578         /* All peers are stopped */
579         rc->state = RC_PEERS_STOPPED;
580         for (peer = 0; peer < rc->num_peers; peer++)
581         {
582           dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
583           dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer]);
584           GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
585                                             dll_op);
586         }
587         return;
588       }
589       /* Some peers are stopped */
590       for (peer = 0; peer < rc->num_peers; peer++)
591       {
592         if (PS_STARTED != rc->peers[peer]->state)
593         {
594           rc->peer_count++;
595           continue;
596         }
597         dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
598         dll_op->op = GNUNET_TESTBED_peer_stop (rc->peers[peer], NULL, NULL);
599         dll_op->cls = rc->peers[peer];
600         GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
601                                           dll_op);
602       }
603       if (rc->peer_count != rc->num_peers)
604         return;
605     }
606   }
607   rc->state = RC_PEERS_DESTROYED;       /* No peers are present so we consider the
608                                          * state where all peers are destroyed  */
609   GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
610 }
611
612
613 /**
614  * Convenience method for running a testbed with
615  * a single call.  Underlay and overlay topology
616  * are configured using the "UNDERLAY" and "OVERLAY"
617  * options in the "[testbed]" section of the configuration\
618  * (with possible options given in "UNDERLAY_XXX" and/or
619  * "OVERLAY_XXX").
620  *
621  * The testbed is to be terminated using a call to
622  * "GNUNET_SCHEDULER_shutdown".
623  *
624  * @param host_filename name of the file with the 'hosts', NULL
625  *        to run everything on 'localhost'
626  * @param cfg configuration to use (for testbed, controller and peers)
627  * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
628  * @param event_mask bit mask with set of events to call 'cc' for;
629  *                   or-ed values of "1LL" shifted by the
630  *                   respective 'enum GNUNET_TESTBED_EventType'
631  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
632  * @param cc controller callback to invoke on events; This callback is called
633  *          for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
634  *          set in the event_mask as this is the only way get access to the
635  *          handle of each peer
636  * @param cc_cls closure for cc
637  * @param master task to run once the testbed is ready
638  * @param master_cls
639  */
640 void
641 GNUNET_TESTBED_run (const char *host_filename,
642                     const struct GNUNET_CONFIGURATION_Handle *cfg,
643                     unsigned int num_peers, uint64_t event_mask,
644                     GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
645                     GNUNET_SCHEDULER_Task master, void *master_cls)
646 {
647   struct RunContext *rc;
648   char *topology;
649   unsigned long long random_links;
650
651   GNUNET_break (NULL == host_filename); /* Currently we do not support host
652                                          * files */
653   GNUNET_assert (NULL != cc);
654   GNUNET_assert (num_peers > 0);
655   host_filename = NULL;
656   rc = GNUNET_malloc (sizeof (struct RunContext));
657   rc->h = GNUNET_TESTBED_host_create (NULL, NULL, 0);
658   GNUNET_assert (NULL != rc->h);
659   rc->cproc =
660       GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, cfg,
661                                        &controller_status_cb, rc);
662   GNUNET_assert (NULL != rc->cproc);
663   rc->num_peers = num_peers;
664   rc->event_mask = event_mask;
665   rc->event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
666   rc->cc = cc;
667   rc->cc_cls = cc_cls;
668   rc->master = master;
669   rc->master_cls = master_cls;
670   rc->state = RC_INIT;
671   rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
672   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
673                                                           "OVERLAY_TOPOLOGY",
674                                                           &topology))
675   {
676     if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology,
677                                                     topology))
678     {
679       LOG (GNUNET_ERROR_TYPE_WARNING,
680            "Unknown topology %s given in configuration\n", topology);
681     }
682     GNUNET_free (topology);
683   }
684   if ( (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology)
685        || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology)
686        || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
687   {
688     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
689                                                             "OVERLAY_RANDOM_LINKS",
690                                                             &random_links))
691     {
692       /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
693          option to be set to the number of random links to be established  */
694       GNUNET_break (0);
695       GNUNET_free (rc);
696       return;
697     }
698     if (random_links > UINT32_MAX)
699     {
700       GNUNET_break (0);       /* Too big number */
701       GNUNET_free (rc);
702       return;
703     }
704     rc->random_links = (unsigned int) random_links;
705   }
706   else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
707   {
708     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
709                                                             "TOPOLOGY_FILE",
710                                                             &rc->topo_file))
711     {
712       /* You need to set TOPOLOGY_FILE option to a topolog file */
713       GNUNET_break (0);
714       GNUNET_free (rc);
715       return;
716     }
717   }
718   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
719                                 &shutdown_run_task, rc);
720 }
721
722
723 /**
724  * Configure and run a testbed using the given
725  * master controller on 'num_hosts' starting
726  * 'num_peers' using the given peer configuration.
727  *
728  * @param controller master controller for the testbed
729  *                   (must not be destroyed until after the
730  *                    testbed is destroyed).
731  * @param num_hosts number of hosts in 'hosts', 0 to only
732  *        use 'localhost'
733  * @param hosts list of hosts to use for the testbed
734  * @param num_peers number of peers to start
735  * @param cfg the configuration to use as a template for peers and also for
736  *         checking the value of testbed helper binary
737  * @param underlay_topology underlay topology to create
738  * @param va topology-specific options
739  * @return handle to the testbed; NULL upon error (error messaage will be printed)
740  */
741 struct GNUNET_TESTBED_Testbed *
742 GNUNET_TESTBED_create_va (struct GNUNET_TESTBED_Controller *controller,
743                           unsigned int num_hosts,
744                           struct GNUNET_TESTBED_Host **hosts,
745                           unsigned int num_peers,
746                           const struct GNUNET_CONFIGURATION_Handle *cfg,
747                           enum GNUNET_TESTBED_TopologyOption underlay_topology,
748                           va_list va)
749 {
750   unsigned int nhost;
751
752   GNUNET_assert (underlay_topology < GNUNET_TESTBED_TOPOLOGY_NONE);
753   if (num_hosts != 0)
754   {
755     for (nhost = 0; nhost < num_hosts; nhost++)
756     {
757       if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost], cfg))
758       {
759         LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
760              GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
761         break;
762       }
763     }
764     if (num_hosts != nhost)
765       return NULL;
766   }
767   /* We need controller callback here to get operation done events while
768      linking hosts */
769   GNUNET_break (0);
770   return NULL;
771 }
772
773
774 /**
775  * Configure and run a testbed using the given
776  * master controller on 'num_hosts' starting
777  * 'num_peers' using the given peer configuration.
778  *
779  * @param controller master controller for the testbed
780  *                   (must not be destroyed until after the
781  *                    testbed is destroyed).
782  * @param num_hosts number of hosts in 'hosts', 0 to only
783  *        use 'localhost'
784  * @param hosts list of hosts to use for the testbed
785  * @param num_peers number of peers to start
786  * @param cfg the configuration to use as a template for peers and also for
787  *         checking the value of testbed helper binary
788  * @param underlay_topology underlay topology to create
789  * @param ... topology-specific options
790  */
791 struct GNUNET_TESTBED_Testbed *
792 GNUNET_TESTBED_create (struct GNUNET_TESTBED_Controller *controller,
793                        unsigned int num_hosts,
794                        struct GNUNET_TESTBED_Host **hosts,
795                        unsigned int num_peers,
796                        const struct GNUNET_CONFIGURATION_Handle *cfg,
797                        enum GNUNET_TESTBED_TopologyOption underlay_topology,
798                        ...)
799 {
800   struct GNUNET_TESTBED_Testbed *testbed;
801   va_list vargs;
802   
803   va_start (vargs, underlay_topology);
804   testbed = GNUNET_TESTBED_create_va (controller, num_hosts, hosts, num_peers,
805                                       cfg, underlay_topology, vargs);
806   va_end (vargs);
807   return testbed;
808 }
809
810
811 /**
812  * Destroy a testbed.  Stops all running peers and then
813  * destroys all peers.  Does NOT destroy the master controller.
814  *
815  * @param testbed testbed to destroy
816  */
817 void
818 GNUNET_TESTBED_destroy (struct GNUNET_TESTBED_Testbed *testbed)
819 {
820   GNUNET_break (0);
821 }
822
823
824 /* end of testbed_api_testbed.c */