doxygen documentation
[oweals/gnunet.git] / src / testing / testing_group.c
index 734383b20a1cb77e60aba90e98e9be911f8f02db..61e8675b1f2a307ca6664d5fb8706ee60ab09c8e 100644 (file)
@@ -348,16 +348,6 @@ struct TopologyIterateContext
 
 struct StatsIterateContext
 {
-  /**
-   * Handle to the statistics service.
-   */
-  struct GNUNET_STATISTICS_Handle *stats_handle;
-
-  /**
-   * Handle for getting statistics.
-   */
-  struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
-
   /**
    * Continuation to call once all stats information has been retrieved.
    */
@@ -395,6 +385,21 @@ struct CoreContext
   struct GNUNET_TESTING_Daemon *daemon;
 };
 
+struct StatsCoreContext
+{
+  void *iter_context;
+  struct GNUNET_TESTING_Daemon *daemon;
+  /**
+   * Handle to the statistics service.
+   */
+  struct GNUNET_STATISTICS_Handle *stats_handle;
+
+  /**
+   * Handle for getting statistics.
+   */
+  struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
+};
+
 /**
  * Handle to a group of GNUnet peers.
  */
@@ -465,6 +470,7 @@ struct GNUNET_TESTING_PeerGroup
 struct UpdateContext
 {
   struct GNUNET_CONFIGURATION_Handle *ret;
+  const struct GNUNET_CONFIGURATION_Handle *orig;
   const char *hostname;
   unsigned int nport;
   unsigned int upnum;
@@ -683,24 +689,34 @@ update_config (void *cls,
   unsigned int ival;
   char cval[12];
   char uval[128];
+  char *single_variable;
 
   if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
     {
-      if (ival != 0)
+      GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
+      if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
        {
          GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
          value = cval;
        }
+
+      GNUNET_free(single_variable);
     }
 
   if (0 == strcmp (option, "UNIXPATH"))
     {
-      GNUNET_snprintf (uval, 
-                      sizeof (uval),
-                      "/tmp/test-service-%s-%u", 
-                      section,
-                      ctx->upnum++);
-      value = uval;
+      GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
+      if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
+        {
+          GNUNET_snprintf (uval,
+                           sizeof (uval),
+                           "/tmp/test-service-%s-%u",
+                           section,
+                           ctx->upnum++);
+          value = uval;
+        }
+      GNUNET_free(single_variable);
+
     }
 
   if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
@@ -742,6 +758,7 @@ make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
   uc.upnum = *upnum;
   uc.ret = GNUNET_CONFIGURATION_create ();
   uc.hostname = hostname;
+  uc.orig = cfg;
 
   GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
   if (uc.nport >= HIGH_PORT)
@@ -1301,6 +1318,7 @@ create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_Connecti
   unsigned int distance;
   double probability, random, percentage;
   unsigned int smallWorldConnections;
+  unsigned int small_world_it;
   char *p_string;
   int connect_attempts;
   square = floor (sqrt (pg->total));
@@ -1321,6 +1339,13 @@ create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_Connecti
                    "TESTING");
       GNUNET_free (p_string);
     }
+  if (percentage < 0.0)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
+                  "PERCENTAGE", "TESTING", percentage);
+      percentage = 0.5;
+    }
   probability = 0.5; /* FIXME: default percentage? */
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
                                                         "TESTING",
@@ -1387,7 +1412,9 @@ create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_Connecti
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
 #endif
   smallWorldConnections = 0;
-  for (i = 0; i < (int) (natLog * percentage); i++)
+  small_world_it = (unsigned int)(natLog * percentage);
+  GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
+  for (i = 0; i < small_world_it; i++)
     {
       for (j = 0; j < pg->total; j++)
         {
@@ -2242,7 +2269,6 @@ GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
   int unblacklisted_connections;
 
   GNUNET_assert (pg->notify_connection != NULL);
-  ret = GNUNET_OK;
 
   switch (topology)
     {
@@ -2934,7 +2960,7 @@ static int internal_stats_callback (void *cls,
                                     uint64_t value,
                                     int is_persistent)
 {
-  struct CoreContext *core_context = cls;
+  struct StatsCoreContext *core_context = cls;
   struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
 
   return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
@@ -2949,7 +2975,7 @@ static int internal_stats_callback (void *cls,
  */
 static void internal_stats_cont (void *cls, int success)
 {
-  struct CoreContext *core_context = cls;
+  struct StatsCoreContext *core_context = cls;
   struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
 
   stats_context->connected--;
@@ -2958,10 +2984,12 @@ static void internal_stats_cont (void *cls, int success)
   if (stats_context->completed == stats_context->total)
     {
       stats_context->cont(stats_context->cls, GNUNET_YES);
-      if (stats_context->stats_handle != NULL)
-        GNUNET_STATISTICS_destroy(stats_context->stats_handle, GNUNET_NO);
       GNUNET_free(stats_context);
     }
+
+  if (core_context->stats_handle != NULL)
+    GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
+
   GNUNET_free(core_context);
 }
 
@@ -2972,7 +3000,7 @@ static void internal_stats_cont (void *cls, int success)
 static void
 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct CoreContext *core_context = cls;
+  struct StatsCoreContext *core_context = cls;
   struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
 
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
@@ -2994,16 +3022,16 @@ schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
 #endif
 
       stats_context->connected++;
-      stats_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
-      if (stats_context->stats_handle == NULL)
+      core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
+      if (core_context->stats_handle == NULL)
         {
           internal_stats_cont (core_context, GNUNET_NO);
           return;
         }
 
-      stats_context->stats_get_handle = GNUNET_STATISTICS_get(stats_context->stats_handle, NULL, NULL, GNUNET_TIME_relative_get_forever(), &internal_stats_cont, &internal_stats_callback, core_context);
-      if (stats_context->stats_get_handle == NULL)
-         internal_stats_cont (core_context, GNUNET_NO);
+      core_context->stats_get_handle = GNUNET_STATISTICS_get(core_context->stats_handle, NULL, NULL, GNUNET_TIME_relative_get_forever(), &internal_stats_cont, &internal_stats_callback, core_context);
+      if (core_context->stats_get_handle == NULL)
+        internal_stats_cont (core_context, GNUNET_NO);
 
     }
 }
@@ -3019,7 +3047,7 @@ GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
                                GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
 {
   struct StatsIterateContext *stats_context;
-  struct CoreContext *core_ctx;
+  struct StatsCoreContext *core_ctx;
   unsigned int i;
   unsigned int total_count;
 
@@ -3034,7 +3062,7 @@ GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
       if (pg->peers[i].daemon->running == GNUNET_YES)
         {
           /* Allocate one core context per core we need to connect to */
-          core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
+          core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
           core_ctx->daemon = pg->peers[i].daemon;
           /* Set back pointer to topology iteration context */
           core_ctx->iter_context = stats_context;
@@ -3524,6 +3552,29 @@ GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int pos
     return NULL;
 }
 
+/*
+ * Get a daemon by peer identity, so callers can
+ * retrieve the daemon without knowing it's offset.
+ *
+ * @param pg the peer group to retrieve the daemon from
+ * @param peer_id the peer identity of the daemon to retrieve
+ *
+ * @return the daemon on success, or NULL if no such peer identity is found
+ */
+struct GNUNET_TESTING_Daemon *
+GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
+{
+  unsigned int i;
+
+  for (i = 0; i < pg->total; i ++)
+    {
+      if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
+        return pg->peers[i].daemon;
+    }
+
+  return NULL;
+}
+
 /**
  * Prototype of a function that will be called when a
  * particular operation was completed the testing library.
@@ -3790,22 +3841,26 @@ GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
     }
   }
 
+  GNUNET_assert(running >= voff);
   for (i = 0; i < voff; i++)
   {
 #if DEBUG_CHURN
     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
 #endif
+    GNUNET_assert(running_arr != NULL);
     GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
                                timeout, 
                                &churn_stop_callback, churn_ctx, 
                                GNUNET_NO, GNUNET_YES);
   }
 
+  GNUNET_assert(stopped >= von);
   for (i = 0; i < von; i++)
     {
 #if DEBUG_CHURN
       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
 #endif
+      GNUNET_assert(stopped_arr != NULL);
       GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon, 
                                          timeout, &churn_start_callback, churn_ctx);
   }
@@ -3957,9 +4012,10 @@ static void
 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
 {
   struct PeerShutdownContext *peer_shutdown_ctx = cls;
-  struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
+  struct ShutdownContext *shutdown_ctx;
 
   GNUNET_assert(peer_shutdown_ctx != NULL);
+  shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
   GNUNET_assert(shutdown_ctx != NULL);
 
   if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)