- verboser log, faster start
[oweals/gnunet.git] / src / testbed / testbed_api_testbed.c
index 3177f8df8664868d48f04fe7b94c4d4b0d7dd860..e9f7e4cbc7390602101885bfcf490f33cbb87765 100644 (file)
@@ -91,6 +91,11 @@ enum State
    */
   RC_LINKED,
 
+  /**
+   * Peers are created
+   */
+  RC_PEERS_CREATED,
+
   /**
    * The testbed run is ready and the master callback can be called now. At this
    * time the peers are all started and if a topology is provided in the
@@ -260,12 +265,6 @@ struct RunContext
    */
   unsigned int num_peers;
 
-  /**
-   * counter to count overlay connect attempts. This counter includes both
-   * successful and failed overlay connects
-   */
-  unsigned int oc_count;
-
   /**
    * Expected overlay connects. Should be zero if no topology is relavant
    */
@@ -356,6 +355,7 @@ peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
   if (rc->peer_count < rc->num_peers)
     return;
   DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
+  rc->state = RC_PEERS_CREATED;
   GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
 }
 
@@ -560,6 +560,27 @@ call_master (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+/**
+ * Callbacks of this type are called when topology configuration is completed
+ *
+ * @param cls the operation closure given to
+ *          GNUNET_TESTBED_overlay_configure_topology_va() and
+ *          GNUNET_TESTBED_overlay_configure() calls
+ * @param nsuccess the number of successful overlay connects
+ * @param nfailures the number of overlay connects which failed
+ */
+static void
+topology_completion_callback (void *cls, unsigned int nsuccess,
+                              unsigned int nfailures)
+{
+  struct RunContext *rc = cls;
+
+  rc->state = RC_READY;
+  GNUNET_SCHEDULER_add_continuation (&call_master, rc,
+                                     GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+}
+
+
 /**
  * Function to create peers
  *
@@ -635,27 +656,6 @@ event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
       return;
     }
   }
-  if (NULL != rc->topology_operation)
-  {
-    switch (event->type)
-    {
-    case GNUNET_TESTBED_ET_OPERATION_FINISHED:
-    case GNUNET_TESTBED_ET_CONNECT:
-      rc->oc_count++;
-      break;
-    default:
-      GNUNET_break (0);
-      shutdown_now (rc);
-      return;
-    }
-    if (rc->oc_count == rc->num_oc)
-    {
-      rc->state = RC_READY;
-      GNUNET_SCHEDULER_add_continuation (&call_master, rc,
-                                         GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-    }
-    goto call_cc;
-  }
   for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
   {
     if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
@@ -675,7 +675,7 @@ event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
     return;
   switch (rc->state)
   {
-  case RC_LINKED:
+  case RC_PEERS_CREATED:
   case RC_READY:
     rc->state = RC_PEERS_STOPPED;
     DEBUG ("Peers stopped in %s\n", prof_time (rc));
@@ -729,6 +729,8 @@ call_cc:
       rc->topology_operation =
           GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
                                                      rc->peers, &rc->num_oc,
+                                                     &topology_completion_callback,
+                                                     rc,
                                                      rc->topology,
                                                      rc->random_links,
                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
@@ -739,6 +741,8 @@ call_cc:
       rc->topology_operation =
           GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
                                                      rc->peers, &rc->num_oc,
+                                                     &topology_completion_callback,
+                                                     rc,
                                                      rc->topology,
                                                      rc->topo_file,
                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
@@ -747,6 +751,8 @@ call_cc:
       rc->topology_operation =
           GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
                                                      rc->peers, &rc->num_oc,
+                                                     &topology_completion_callback,
+                                                     rc,
                                                      rc->topology,
                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
     if (NULL == rc->topology_operation)
@@ -830,8 +836,9 @@ register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     return;
   }
   rc->reg_handle =
-      GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts++],
+      GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
                                     host_registration_completion, rc);
+  rc->reg_hosts++;
 }
 
 
@@ -859,6 +866,8 @@ controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed startup failed\n");
       return;
     default:
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "Controller crash detected. Shutting down.\n");
       rc->cproc = NULL;
       shutdown_now (rc);
       return;
@@ -877,6 +886,7 @@ controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
                                          rc);
   if (0 < rc->num_hosts)
   {
+    rc->reg_hosts = 0;
     rc->register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, rc);
     return;
   }
@@ -938,6 +948,7 @@ host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
                    int status)
 {
   struct RunContext *rc = cls;
+  struct GNUNET_TESTBED_Host **old_hosts;
   unsigned int nhost;
 
   for (nhost = 0; nhost < rc->num_hosts; nhost++)
@@ -966,7 +977,14 @@ host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
   rc->h = rc->hosts[0];
   rc->num_hosts--;
   if (0 < rc->num_hosts)
-    rc->hosts = &rc->hosts[1];
+  {
+    old_hosts = rc->hosts;
+    rc->hosts =
+        GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
+    memcpy (rc->hosts, &old_hosts[1],
+            (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
+    GNUNET_free (old_hosts);
+  }
   else
   {
     GNUNET_free (rc->hosts);