- rename TOPOLOGY_FILE to OVERLAY_TOPOLOGY_FILE and remove GNUNET_TESTBED_create...
[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 /**
42  * DLL of operations
43  */
44 struct DLLOperation
45 {
46   /**
47    * The testbed operation handle
48    */
49   struct GNUNET_TESTBED_Operation *op;
50
51   /**
52    * Context information for GNUNET_TESTBED_run()
53    */
54   struct RunContext *rc;
55
56   /**
57    * Closure
58    */
59   void *cls;
60
61   /**
62    * The next pointer for DLL
63    */
64   struct DLLOperation *next;
65
66   /**
67    * The prev pointer for DLL
68    */
69   struct DLLOperation *prev;
70 };
71
72
73 /**
74  * States of RunContext
75  */
76 enum State
77 {
78   /**
79    * Initial state
80    */
81   RC_INIT = 0,
82
83   /**
84    * Controllers on given hosts started and linked
85    */
86   RC_LINKED,
87
88   /**
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
92    */
93   RC_READY,
94
95   /**
96    * Peers are stopped
97    */
98   RC_PEERS_STOPPED,
99
100   /**
101    * Peers are destroyed
102    */
103   RC_PEERS_DESTROYED
104 };
105
106
107 /**
108  * Testbed Run Handle
109  */
110 struct RunContext
111 {
112   /**
113    * The controller handle
114    */
115   struct GNUNET_TESTBED_Controller *c;
116
117   /**
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
120    * for peers
121    */
122   struct GNUNET_CONFIGURATION_Handle *cfg;
123
124   /**
125    * Handle to the host on which the controller runs
126    */
127   struct GNUNET_TESTBED_Host *h;
128
129   /**
130    * The handle to the controller process
131    */
132   struct GNUNET_TESTBED_ControllerProc *cproc;
133
134   /**
135    * The callback to use as controller callback
136    */
137   GNUNET_TESTBED_ControllerCallback cc;
138
139   /**
140    * The pointer to the controller callback
141    */
142   void *cc_cls;
143
144   /**
145    * TestMaster callback to call when testbed initialization is done
146    */
147   GNUNET_TESTBED_TestMaster test_master;
148   
149   /**
150    * The closure for the TestMaster callback
151    */
152   void *test_master_cls;
153
154   /**
155    * The head element of DLL operations
156    */
157   struct DLLOperation *dll_op_head;
158
159   /**
160    * The tail element of DLL operations
161    */
162   struct DLLOperation *dll_op_tail;
163
164   /**
165    * An array of hosts loaded from the hostkeys file
166    */
167   struct GNUNET_TESTBED_Host **hosts;
168
169   /**
170    * The handle for whether a host is habitable or not
171    */
172   struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
173
174   /**
175    * Array of peers which we create
176    */
177   struct GNUNET_TESTBED_Peer **peers;
178
179   /**
180    * The topology generation operation. Will be null if no topology is set in
181    * the configuration
182    */
183   struct GNUNET_TESTBED_Operation *topology_operation;
184
185   /**
186    * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
187    */
188   char *topo_file;
189
190   /**
191    * Host registration handle
192    */
193   struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
194
195   /**
196    * Host registration task
197    */
198   GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
199
200   /**
201    * Task to be run while shutting down
202    */
203   GNUNET_SCHEDULER_TaskIdentifier shutdown_run_task;
204
205   /**
206    * The event mask for the controller
207    */
208   uint64_t event_mask;
209
210   /**
211    * State of this context
212    */
213   enum State state;
214
215   /**
216    * The topology which has to be achieved with the peers started in this context
217    */
218   enum GNUNET_TESTBED_TopologyOption topology;
219
220   /**
221    * Number of hosts in the given host file
222    */
223   unsigned int num_hosts;
224
225   /**
226    * Number of registered hosts. Also used as a counter while checking
227    * habitabillity of hosts
228    */
229   unsigned int reg_hosts;
230
231   /**
232    * Current peer count for an operation; Set this to 0 and increment for each
233    * successful operation on a peer
234    */
235   unsigned int peer_count;
236
237   /**
238    * number of peers to start
239    */
240   unsigned int num_peers;
241
242   /**
243    * counter to count overlay connect attempts. This counter includes both
244    * successful and failed overlay connects
245    */
246   unsigned int oc_count;
247
248   /**
249    * Expected overlay connects. Should be zero if no topology is relavant
250    */
251   unsigned int num_oc;
252
253   /**
254    * Number of random links to established
255    */
256   unsigned int random_links;
257   
258 };
259
260
261 /**
262  * Task for starting peers
263  *
264  * @param cls the RunHandle
265  * @param tc the task context from scheduler
266  */
267 static void
268 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
269 {
270   struct RunContext *rc = cls;
271   struct DLLOperation *dll_op;
272   unsigned int peer;
273
274   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting Peers\n");
275   for (peer = 0; peer < rc->num_peers; peer++)
276   {
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);
281   }
282   rc->peer_count = 0;
283 }
284
285
286 /**
287  * Functions of this signature are called when a peer has been successfully
288  * created
289  *
290  * @param cls the closure from GNUNET_TESTBED_peer_create()
291  * @param peer the handle for the created peer; NULL on any error during
292  *          creation
293  * @param emsg NULL if peer is not NULL; else MAY contain the error description
294  */
295 static void
296 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
297 {
298   struct DLLOperation *dll_op = cls;
299   struct RunContext *rc;
300
301   GNUNET_assert (NULL != dll_op);
302   rc = dll_op->rc;
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);
307   if (NULL == peer)
308   {
309     if (NULL != emsg)
310       LOG (GNUNET_ERROR_TYPE_WARNING, "Error while creating a peer: %s\n",
311            emsg);
312     /* FIXME: GNUNET_TESTBED_shutdown_run()? */
313     return;
314   }
315   rc->peers[rc->peer_count] = peer;
316   rc->peer_count++;
317   if (rc->peer_count < rc->num_peers)
318     return;
319   LOG (GNUNET_ERROR_TYPE_DEBUG, "Required peers created successfully\n");
320   GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
321 }
322
323
324 /**
325  * Assuming all peers have been destroyed cleanup run handle
326  *
327  * @param cls the run handle
328  * @param tc the task context from scheduler
329  */
330 static void
331 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
332 {
333   struct RunContext *rc = cls;
334   struct DLLOperation *dll_op;
335   unsigned int hid;
336
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->c)
343     GNUNET_TESTBED_controller_disconnect (rc->c);
344   if (NULL != rc->cproc)
345     GNUNET_TESTBED_controller_stop (rc->cproc);
346   if (NULL != rc->h)
347     GNUNET_TESTBED_host_destroy (rc->h);
348   for (hid = 0; hid < rc->num_hosts; hid++)
349         GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
350   GNUNET_free_non_null (rc->hosts);
351   if (NULL != rc->dll_op_head)
352   {
353     LOG (GNUNET_ERROR_TYPE_WARNING,
354          _("Some operations are still pending. Cancelling them\n"));
355     while (NULL != (dll_op = rc->dll_op_head))
356     {
357       GNUNET_TESTBED_operation_done (dll_op->op);
358       GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
359       GNUNET_free (dll_op);
360     }
361   }
362   if (NULL != rc->cfg)
363     GNUNET_CONFIGURATION_destroy (rc->cfg);
364   GNUNET_free_non_null (rc->topo_file);
365   GNUNET_free (rc);
366 }
367
368
369 /**
370  * Stops the testbed run and releases any used resources
371  *
372  * @param cls the tesbed run handle
373  * @param tc the task context from scheduler
374  */
375 static void
376 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
377
378
379 /**
380  * Function to shutdown now
381  *
382  * @param rc the RunContext
383  */
384 static void
385 shutdown_now (struct RunContext *rc)
386 {
387   if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task)
388     GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
389   rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
390 }
391
392
393 /**
394  * Stops the testbed run and releases any used resources
395  *
396  * @param cls the tesbed run handle
397  * @param tc the task context from scheduler
398  */
399 static void
400 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
401 {
402   struct RunContext *rc = cls;
403   struct DLLOperation *dll_op;
404   unsigned int peer;
405   unsigned int nhost;
406
407   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task);
408   rc->shutdown_run_task = GNUNET_SCHEDULER_NO_TASK;
409   if (NULL != rc->hc_handles)
410   {
411     for (nhost = 0; nhost < rc->num_hosts; nhost++)
412       if (NULL != rc->hc_handles[nhost])
413         GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
414     GNUNET_free (rc->hc_handles);
415     rc->hc_handles = NULL;
416   }
417   /* Stop register hosts task if it is running */
418   if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
419   {
420     GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
421     rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
422   }
423   if (NULL != rc->reg_handle)
424   {
425     GNUNET_TESTBED_cancel_registration (rc->reg_handle);
426     rc->reg_handle = NULL;
427   }
428   if (NULL != rc->c)
429   {
430     if (NULL != rc->peers)
431     {
432       if (NULL != rc->topology_operation)
433       {
434         GNUNET_TESTBED_operation_done (rc->topology_operation);
435         rc->topology_operation = NULL;
436       }
437       if (RC_INIT == rc->state)
438         rc->state = RC_READY;   /* Even though we haven't called the master callback */
439       rc->peer_count = 0;
440       /* Check if some peers are stopped */
441       for (peer = 0; peer < rc->num_peers; peer++)
442       {
443         if (PS_STOPPED != rc->peers[peer]->state)
444           break;
445       }
446       if (peer == rc->num_peers)
447       {
448         /* All peers are stopped */
449         rc->state = RC_PEERS_STOPPED;
450         for (peer = 0; peer < rc->num_peers; peer++)
451         {
452           dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
453           dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer]);
454           GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
455                                             dll_op);
456         }
457         return;
458       }
459       /* Some peers are stopped */
460       for (peer = 0; peer < rc->num_peers; peer++)
461       {
462         if (PS_STARTED != rc->peers[peer]->state)
463         {
464           rc->peer_count++;
465           continue;
466         }
467         dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
468         dll_op->op = GNUNET_TESTBED_peer_stop (rc->peers[peer], NULL, NULL);
469         dll_op->cls = rc->peers[peer];
470         GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
471                                           dll_op);
472       }
473       if (rc->peer_count != rc->num_peers)
474         return;
475     }
476   }
477   rc->state = RC_PEERS_DESTROYED;       /* No peers are present so we consider the
478                                          * state where all peers are destroyed  */
479   GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
480 }
481
482
483 /**
484  * Task to call master task
485  *
486  * @param cls the run context
487  * @param tc the task context
488  */
489 static void
490 call_master (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
491 {
492   struct RunContext *rc = cls;
493   
494   if (NULL != rc->topology_operation)
495   {
496     GNUNET_TESTBED_operation_done (rc->topology_operation);
497     rc->topology_operation = NULL;
498   }
499   if (NULL != rc->test_master)
500     rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers);
501 }
502
503
504 /**
505  * Function to create peers
506  *
507  * @param rc the RunContext
508  */
509 static void
510 create_peers (struct RunContext *rc)
511 {
512   struct DLLOperation *dll_op;
513   unsigned int peer;
514
515   rc->peers =
516       GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
517   GNUNET_assert (NULL != rc->c);
518   rc->peer_count = 0;
519   for (peer = 0; peer < rc->num_peers; peer++)
520   {
521     dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
522     dll_op->rc = rc;
523     dll_op->op =
524         GNUNET_TESTBED_peer_create (rc->c, 
525                                     (0 == rc->num_hosts)
526                                     ? rc->h : rc->hosts[peer % rc->num_hosts],
527                                     rc->cfg,
528                                     peer_create_cb, dll_op);
529     GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
530   }
531 }
532
533
534 /**
535  * Signature of the event handler function called by the
536  * respective event controller.
537  *
538  * @param cls closure
539  * @param event information about the event
540  */
541 static void
542 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
543 {
544   struct RunContext *rc = cls;
545   struct DLLOperation *dll_op;
546   unsigned int peer_id;
547
548   if (RC_INIT == rc->state)
549   {
550     switch (event->type)
551     {
552     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
553       dll_op = event->details.operation_finished.op_cls;
554       if (NULL != event->details.operation_finished.emsg)
555       {
556         LOG (GNUNET_ERROR_TYPE_ERROR,
557              _("Linking controllers failed. Exiting"));
558         shutdown_now (rc);
559       }
560       else
561         rc->reg_hosts++;
562       GNUNET_assert (event->details.operation_finished.operation == dll_op->op);
563       GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
564       GNUNET_TESTBED_operation_done (dll_op->op);
565       GNUNET_free (dll_op);
566       if (rc->reg_hosts == rc->num_hosts)
567       {
568         rc->state = RC_LINKED;
569         create_peers (rc);
570       }
571       return;
572     default:
573       GNUNET_break (0);
574       shutdown_now (rc);
575       return;
576     }
577   }
578   if (NULL != rc->topology_operation)
579   {
580     switch (event->type)
581     {
582     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
583     case GNUNET_TESTBED_ET_CONNECT:
584       rc->oc_count++;
585       break;
586     default:
587       GNUNET_break (0);
588       shutdown_now (rc);
589       return;
590     }
591     if (rc->oc_count == rc->num_oc)
592     {
593       rc->state = RC_READY;
594       GNUNET_SCHEDULER_add_continuation (&call_master, rc,
595                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
596     }
597     return;
598   }
599   if ((RC_LINKED < rc->state) &&
600       ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) ||
601        (GNUNET_TESTBED_ET_PEER_STOP == event->type)))
602   {
603     for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
604     {
605       if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
606           (event->details.operation_finished.operation == dll_op->op))
607         break;
608       if ((GNUNET_TESTBED_ET_PEER_STOP == event->type) &&
609           (event->details.peer_stop.peer == dll_op->cls))
610         break;
611     }
612     if (NULL == dll_op)
613       goto call_cc;
614     GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
615     GNUNET_TESTBED_operation_done (dll_op->op);
616     GNUNET_free (dll_op);
617     rc->peer_count++;
618     if (rc->peer_count < rc->num_peers)
619       return;
620     switch (rc->state)
621     {
622     case RC_READY:
623       rc->state = RC_PEERS_STOPPED;
624       rc->peer_count = 0;
625       for (peer_id = 0; peer_id < rc->num_peers; peer_id++)
626       {
627         dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
628         dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer_id]);
629         GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
630                                           dll_op);
631       }
632       break;
633     case RC_PEERS_STOPPED:
634       rc->state = RC_PEERS_DESTROYED;
635       GNUNET_free (rc->peers);
636       rc->peers = NULL;
637       LOG (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully destroyed\n");
638       GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
639       break;
640     default:
641       GNUNET_assert (0);
642     }
643     return;
644   }
645
646 call_cc:
647   if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
648     rc->cc (rc->cc_cls, event);
649   if (GNUNET_TESTBED_ET_PEER_START != event->type)
650     return;
651   for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
652     if ((NULL != dll_op->cls) &&
653         (event->details.peer_start.peer == dll_op->cls))
654       break;
655   GNUNET_assert (NULL != dll_op);
656   GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
657   GNUNET_TESTBED_operation_done (dll_op->op);
658   GNUNET_free (dll_op);
659   rc->peer_count++;
660   if (rc->peer_count < rc->num_peers)
661     return;
662   LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers started successfully\n");
663   if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
664   {
665     if ( (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology)
666          || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology)
667          || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
668     {
669       rc->topology_operation =
670           GNUNET_TESTBED_overlay_configure_topology (NULL,
671                                                      rc->num_peers,
672                                                      rc->peers,
673                                                      &rc->num_oc,
674                                                      rc->topology,
675                                                      rc->random_links,
676                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
677     }
678     else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
679     {
680       GNUNET_assert (NULL != rc->topo_file);
681       rc->topology_operation =
682           GNUNET_TESTBED_overlay_configure_topology (NULL,
683                                                      rc->num_peers,
684                                                      rc->peers,
685                                                      &rc->num_oc,
686                                                      rc->topology,
687                                                      rc->topo_file,
688                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
689     }
690     else
691       rc->topology_operation =
692           GNUNET_TESTBED_overlay_configure_topology (NULL,
693                                                      rc->num_peers,
694                                                      rc->peers,
695                                                      &rc->num_oc,
696                                                      rc->topology,
697                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
698     if (NULL == rc->topology_operation)
699       LOG (GNUNET_ERROR_TYPE_WARNING,
700            "Not generating topology. Check number of peers\n");
701     else
702       return;
703   }
704   rc->state = RC_READY;
705   GNUNET_SCHEDULER_add_continuation (&call_master, rc,
706                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
707 }
708
709
710 /**
711  * Task to register all hosts available in the global host list
712  *
713  * @param cls the RunContext
714  * @param tc the scheduler task context
715  */
716 static void
717 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
718
719
720 /**
721  * Callback which will be called to after a host registration succeeded or failed
722  *
723  * @param cls the closure
724  * @param emsg the error message; NULL if host registration is successful
725  */
726 static void
727 host_registration_completion (void *cls, const char *emsg)
728 {
729   struct RunContext *rc = cls;
730
731   rc->reg_handle = NULL;
732   if (NULL != emsg)
733   {
734     LOG (GNUNET_ERROR_TYPE_WARNING,
735          _("Host registration failed for a host. Error: %s\n"), emsg);
736     shutdown_now (rc);
737     return;
738   }
739   rc->register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, rc);
740 }
741
742
743 /**
744  * Task to register all hosts available in the global host list
745  *
746  * @param cls RunContext
747  * @param tc the scheduler task context
748  */
749 static void
750 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
751 {
752   struct RunContext *rc = cls;
753   struct DLLOperation *dll_op;
754   unsigned int slave;
755
756   rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
757   if (rc->reg_hosts == rc->num_hosts)
758   {
759     LOG (GNUNET_ERROR_TYPE_DEBUG,
760          "All hosts successfully registered\n");
761     /* Start slaves */
762     for (slave = 0; slave < rc->num_hosts; slave++)
763     {
764       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
765       dll_op->rc = rc;
766       dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
767                                                    rc->c,
768                                                    rc->hosts[slave],
769                                                    rc->h,
770                                                    rc->cfg,
771                                                    GNUNET_YES);
772       GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
773     }
774     rc->reg_hosts = 0;
775     return;
776   }
777   rc->reg_handle = GNUNET_TESTBED_register_host (rc->c,
778                                                  rc->hosts[rc->reg_hosts++],
779                                                  host_registration_completion,
780                                                  rc);
781 }
782
783
784 /**
785  * Callback to signal successfull startup of the controller process
786  *
787  * @param cls the closure from GNUNET_TESTBED_controller_start()
788  * @param cfg the configuration with which the controller has been started;
789  *          NULL if status is not GNUNET_OK
790  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
791  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
792  */
793 static void
794 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
795                       int status)
796 {
797   struct RunContext *rc = cls;
798   uint64_t event_mask;
799
800   if (status != GNUNET_OK)
801   {
802     switch (rc->state)
803     {
804     case RC_INIT:
805       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed startup failed\n");
806       return;
807     default:
808       shutdown_now (rc);
809       return;
810     }
811   }
812   GNUNET_CONFIGURATION_destroy (rc->cfg);
813   rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
814   event_mask = rc->event_mask;
815   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
816   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
817   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
818   if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
819     event_mask |= GNUNET_TESTBED_ET_CONNECT;
820   rc->c =
821       GNUNET_TESTBED_controller_connect (rc->cfg, rc->h, event_mask, &event_cb, rc);
822   if (0 < rc->num_hosts)
823   {
824     rc->register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, rc);
825     return;
826   }
827   rc->state = RC_LINKED;
828   create_peers (rc);
829 }
830
831
832 /**
833  * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
834  * inform whether the given host is habitable or not. The Handle returned by
835  * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
836  *
837  * @param cls NULL
838  * @param host the host whose status is being reported; will be NULL if the host
839  *          given to GNUNET_TESTBED_is_host_habitable() is NULL
840  * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
841  */
842 static void 
843 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host, int status)
844 {
845   struct RunContext *rc = cls;
846   unsigned int nhost;
847   
848   for (nhost = 0; nhost < rc->num_hosts; nhost++)
849   {
850     if (host == rc->hosts[nhost])
851       break;
852   }
853   GNUNET_assert (nhost != rc->num_hosts);
854   rc->hc_handles[nhost] = NULL;
855   if (GNUNET_NO == status)
856   {
857     if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
858       LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
859            GNUNET_TESTBED_host_get_hostname (host));
860     else
861       LOG (GNUNET_ERROR_TYPE_ERROR, _("Testbed cannot be started on localhost\n"));
862     shutdown_now (rc);
863     return;
864   }
865   rc->reg_hosts++;
866   if (rc->reg_hosts < rc->num_hosts)
867     return;
868   GNUNET_free (rc->hc_handles);
869   rc->hc_handles = NULL;
870   rc->h = rc->hosts[0];
871   rc->num_hosts--;
872   if (0 < rc->num_hosts)
873     rc->hosts = &rc->hosts[1];
874   else
875   {
876     GNUNET_free (rc->hosts);
877     rc->hosts = NULL;
878   }
879   /* FIXME: If we are starting controller on different host 127.0.0.1 may not ab
880   correct */
881   rc->cproc =
882       GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, rc->cfg,
883                                        &controller_status_cb, rc);
884   if (NULL == rc->cproc)
885   {
886     LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
887     shutdown_now (rc);
888   }
889 }
890
891
892 /**
893  * Convenience method for running a testbed with
894  * a single call.  Underlay and overlay topology
895  * are configured using the "UNDERLAY" and "OVERLAY"
896  * options in the "[testbed]" section of the configuration\
897  * (with possible options given in "UNDERLAY_XXX" and/or
898  * "OVERLAY_XXX").
899  *
900  * The testbed is to be terminated using a call to
901  * "GNUNET_SCHEDULER_shutdown".
902  *
903  * @param host_filename name of the file with the 'hosts', NULL
904  *        to run everything on 'localhost'
905  * @param cfg configuration to use (for testbed, controller and peers)
906  * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
907  * @param event_mask bit mask with set of events to call 'cc' for;
908  *                   or-ed values of "1LL" shifted by the
909  *                   respective 'enum GNUNET_TESTBED_EventType'
910  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
911  * @param cc controller callback to invoke on events; This callback is called
912  *          for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
913  *          set in the event_mask as this is the only way get access to the
914  *          handle of each peer
915  * @param cc_cls closure for cc
916  * @param test_master this callback will be called once the test is ready
917  * @param test_master_cls closure for 'test_master'.
918  */
919 void
920 GNUNET_TESTBED_run (const char *host_filename,
921                     const struct GNUNET_CONFIGURATION_Handle *cfg,
922                     unsigned int num_peers, uint64_t event_mask,
923                     GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
924                     GNUNET_TESTBED_TestMaster test_master,
925                     void *test_master_cls)
926 {
927   struct RunContext *rc;
928   char *topology;
929   unsigned long long random_links;
930   unsigned int hid;
931   unsigned int nhost;
932   
933   GNUNET_assert (num_peers > 0);
934   rc = GNUNET_malloc (sizeof (struct RunContext));
935   if (NULL != host_filename)
936   {
937     rc->num_hosts =
938         GNUNET_TESTBED_hosts_load_from_file (host_filename, &rc->hosts);
939     if (0 == rc->num_hosts)
940     {
941       LOG (GNUNET_ERROR_TYPE_WARNING,
942            _("No hosts loaded. Need at least one host\n"));
943       goto error_cleanup;
944     }
945   }
946   else
947     rc->h = GNUNET_TESTBED_host_create (NULL, NULL, 0);
948   rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
949   rc->num_peers = num_peers;
950   rc->event_mask = event_mask;
951   rc->cc = cc;
952   rc->cc_cls = cc_cls;
953   rc->test_master = test_master;
954   rc->test_master_cls = test_master_cls;
955   rc->state = RC_INIT;
956   rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;  
957   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
958                                                           "OVERLAY_TOPOLOGY",
959                                                           &topology))
960   {
961     if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology,
962                                                     topology))
963     {
964       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
965                                  "testbed", "OVERLAY_TOPLOGY",
966                                  _("Specified topology must be supported by testbed"));
967     }
968     GNUNET_free (topology);
969   }
970   switch (rc->topology)
971   {
972   case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
973   case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
974   case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
975     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
976                                                             "OVERLAY_RANDOM_LINKS",
977                                                             &random_links))
978     {
979       /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
980          option to be set to the number of random links to be established  */
981       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
982                                  "testbed", "OVERLAY_RANDOM_LINKS");
983       goto error_cleanup;
984     }
985     if (random_links > UINT32_MAX)
986     {
987       GNUNET_break (0);       /* Too big number */
988       goto error_cleanup;
989     }
990     rc->random_links = (unsigned int) random_links;
991     break;
992   case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
993     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
994                                                             "OVERLAY_TOPOLOGY_FILE",
995                                                             &rc->topo_file))
996     {
997       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
998                                  "testbed", "OVERLAY_TOPOLOGY_FILE");
999       goto error_cleanup;
1000     }
1001     break;
1002   default:   
1003     /* Do nothing */
1004     break;
1005   }
1006   if (NULL != host_filename)
1007   {
1008     rc->hc_handles = GNUNET_malloc (sizeof (struct
1009                                             GNUNET_TESTBED_HostHabitableCheckHandle *) 
1010                                     * rc->num_hosts);
1011     for (nhost = 0; nhost < rc->num_hosts; nhost++) 
1012     {    
1013       if (NULL == (rc->hc_handles[nhost] = 
1014                    GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1015                                                      &host_habitable_cb,
1016                                                      rc)))
1017       {
1018         GNUNET_break (0);
1019         for (nhost = 0; nhost < rc->num_hosts; nhost++)
1020           if (NULL != rc->hc_handles[nhost])
1021             GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
1022         GNUNET_free (rc->hc_handles);
1023         rc->hc_handles = NULL;
1024         goto error_cleanup;
1025       }
1026     }
1027   }
1028   else
1029     rc->cproc =
1030       GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, rc->cfg,
1031                                        &controller_status_cb, rc);
1032   rc->shutdown_run_task =
1033       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1034                                     &shutdown_run, rc);
1035   return;
1036
1037  error_cleanup:
1038   if (NULL != rc->h)
1039     GNUNET_TESTBED_host_destroy (rc->h);
1040   if (NULL != rc->hosts)
1041   {
1042     for (hid = 0; hid < rc->num_hosts; hid++)
1043       if (NULL != rc->hosts[hid])
1044         GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1045     GNUNET_free (rc->hosts);
1046   }
1047   GNUNET_free (rc);
1048 }
1049
1050
1051 /* end of testbed_api_testbed.c */