- doc
[oweals/gnunet.git] / src / mesh / test_mesh_small.c
index 6735c98092863e03bbeca640c2204185a2de9e43..20b5c27175b40e70ae5f695175fa065e2fe69922 100644 (file)
@@ -173,10 +173,7 @@ static struct GNUNET_MESH_Channel *incoming_ch;
 static struct GNUNET_TIME_Absolute start_time;
 
 static struct GNUNET_TESTBED_Peer **testbed_peers;
-static struct GNUNET_STATISTICS_Handle *stats;
-static struct GNUNET_STATISTICS_GetHandle *stats_get;
 static struct GNUNET_TESTBED_Operation *stats_op;
-static unsigned int stats_peer;
 static unsigned int ka_sent;
 static unsigned int ka_received;
 
@@ -255,8 +252,6 @@ disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     GNUNET_SCHEDULER_cancel (shutdown_handle);
   }
-  if (NULL != stats_get)
-    GNUNET_STATISTICS_get_cancel (stats_get);
   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
 }
 
@@ -520,84 +515,26 @@ data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
 }
 
 
-/**
- * Adapter function called to establish a connection to the statistics service.
- *
- * @param cls closure
- * @param cfg configuration of the peer to connect to; will be available until
- *          GNUNET_TESTBED_operation_done() is called on the operation returned
- *          from GNUNET_TESTBED_service_connect()
- * @return service handle to return in 'op_result', NULL on error
- */
-static void *
-stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
-{
-  return GNUNET_STATISTICS_create ("<test_mesh>", cfg);
-}
-
-
-/**
- * Adapter function called to destroy a connection to
- * statistics service.
- *
- * @param cls Closure (unused).
- * @param op_result service handle returned from the connect adapter
- */
-static void
-stats_da (void *cls, void *op_result)
-{
-  GNUNET_assert (op_result == stats);
-  GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
-  stats = NULL;
-}
-
-
-/**
- * Function called by testbed once we are connected to stats
- * service. Get the statistics of interest.
- *
- * @param cls Closure (unused).
- * @param op connect operation handle
- * @param ca_result handle to stats service
- * @param emsg error message on failure
- */
-static void
-stats_connect_cb (void *cls,
-                  struct GNUNET_TESTBED_Operation *op,
-                  void *ca_result,
-                  const char *emsg);
-
 /**
  * Stats callback. Finish the stats testbed operation and when all stats have
  * been iterated, shutdown the test.
  *
  * @param cls closure
- * @param success GNUNET_OK if statistics were
- *        successfully obtained, GNUNET_SYSERR if not.
+ * @param op the operation that has been finished
+ * @param emsg error message in case the operation has failed; will be NULL if
+ *          operation has executed successfully.
  */
 static void
-stats_cont (void *cls, int success)
+stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stats_cont for peer %u\n", cls);
   GNUNET_TESTBED_operation_done (stats_op);
-  stats_get = NULL;
-  if (NULL == cls)
-  {
-    stats_op = GNUNET_TESTBED_service_connect (NULL,
-                                               testbed_peers[4],
-                                               "statistics",
-                                               &stats_connect_cb,
-                                               (void *)4,
-                                               &stats_ca,
-                                               &stats_da,
-                                               (void *)4);
-  }
-  else
-  {
-    if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
-      GNUNET_SCHEDULER_cancel (disconnect_task);
-    disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
-                                                (void *) __LINE__);
-  }
+
+  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+    GNUNET_SCHEDULER_cancel (disconnect_task);
+  disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
+                                              (void *) __LINE__);
+
 }
 
 
@@ -605,6 +542,7 @@ stats_cont (void *cls, int success)
  * Process statistic values.
  *
  * @param cls closure
+ * @param peer the peer the statistic belong to
  * @param subsystem name of subsystem that created the statistic
  * @param name the name of the datum
  * @param value the current value
@@ -612,55 +550,30 @@ stats_cont (void *cls, int success)
  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
  */
 static int
-stats_iterator (void *cls, const char *subsystem, const char *name,
+stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
+                const char *subsystem, const char *name,
                 uint64_t value, int is_persistent)
 {
-  if (0 == strncmp("# keepalives sent", name,
-                   strlen("# keepalives sent"))
-      && 0 == stats_peer)
+  static const char *s_sent = "# keepalives sent";
+  static const char *s_recv = "# keepalives received";
+  uint32_t i;
+
+  i = GNUNET_TESTBED_get_index (peer);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %u - %s [%s]: %llu\n",
+              i, subsystem, name, value);
+  if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
     ka_sent = value;
 
-  if (0 == strncmp("# keepalives received", name,
-                   strlen ("# keepalives received"))
-      && 4 == stats_peer)
-    ka_received = value;
-
-  return GNUNET_OK;
-}
-
-
-/**
- * Function called by testbed once we are connected to stats
- * service. Get the statistics of interest.
- *
- * @param cls Closure (unused).
- * @param op connect operation handle
- * @param ca_result handle to stats service
- * @param emsg error message on failure
- */
-static void
-stats_connect_cb (void *cls,
-                  struct GNUNET_TESTBED_Operation *op,
-                  void *ca_result,
-                  const char *emsg)
-{
-  if (NULL == ca_result || NULL != emsg)
+  if (0 == strncmp(s_recv, name, strlen (s_recv)) && 4 == i)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed to connect to statistics service: %s\n", emsg);
-    return;
+    ka_received = value;
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, " sent: %u, received: %u\n",
+                ka_sent, ka_received);
+    if (ka_sent < 2 || ka_sent > ka_received + 1)
+      ok--;
   }
 
-  stats = ca_result;
-
-  stats_get = GNUNET_STATISTICS_get (stats, "mesh", NULL,
-                                      GNUNET_TIME_UNIT_FOREVER_REL,
-                                      &stats_cont, &stats_iterator, cls);
-  if (NULL == stats_get)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Could not get statistics of peer %u!\n", cls);
-  }
+  return GNUNET_OK;
 }
 
 
@@ -676,15 +589,12 @@ check_keepalives (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
     return;
 
+  disconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "check keepalives\n");
   GNUNET_MESH_channel_destroy (ch);
-  stats_op = GNUNET_TESTBED_service_connect (NULL,
-                                             testbed_peers[0],
-                                             "statistics",
-                                             &stats_connect_cb,
-                                             NULL,
-                                             &stats_ca,
-                                             &stats_da,
-                                             NULL);
+  stats_op = GNUNET_TESTBED_get_statistics (5, testbed_peers,
+                                            "mesh", NULL,
+                                            stats_iterator, stats_cont, NULL);
 }
 
 
@@ -923,7 +833,7 @@ int
 main (int argc, char *argv[])
 {
   initialized = GNUNET_NO;
-  uint32_t ports[2];
+  static uint32_t ports[2];
   const char *config_file;
 
   GNUNET_log_setup ("test", "DEBUG", NULL);