-consistently use struct GNUNET_HashCode
[oweals/gnunet.git] / src / dht / test_dht_multipeer.c
index 8de16b363235d41cb4057fe26d1a3ca4e572ec38..656b4ac06ac59e4688f09564fe49a587418df327 100644 (file)
@@ -27,9 +27,6 @@
 #include "gnunet_core_service.h"
 #include "gnunet_dht_service.h"
 
-/* DEFINES */
-#define VERBOSE GNUNET_EXTRA_LOGGING
-
 /* Timeout for entire testcase */
 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)
 
@@ -206,6 +203,11 @@ static enum GNUNET_DHT_RouteOption route_option;
  */
 static GNUNET_SCHEDULER_TaskIdentifier die_task;
 
+/**
+ * Task handle to use to schedule test shutdown
+ */
+GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
+
 /**
  * Global return value (0 for success, anything else for failure)
  */
@@ -220,16 +222,31 @@ shutdown_callback (void *cls, const char *emsg)
 {
   if (emsg != NULL)
   {
-    fprintf (stderr, "Failed to shutdown testing topology: %s\n", emsg);
+    FPRINTF (stderr, "Failed to shutdown testing topology: %s\n", emsg);
     if (ok == 0)
       ok = 2;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown callback completed.\n");
 }
 
 static void
 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
+  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) == 0)
+  {
+    if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
+    {
+      GNUNET_SCHEDULER_cancel(shutdown_task);
+      shutdown_task = GNUNET_SCHEDULER_NO_TASK;
+    }
+  }
+  else
+  {
+    shutdown_task = GNUNET_SCHEDULER_NO_TASK ;
+  }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown requested.\n");
+  if (NULL != pg)
+    GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
   pg = NULL;
 }
 
@@ -259,7 +276,7 @@ static struct StatValues stats[] = {
   {"core", "# bytes encrypted", 0},
   {"core", "# type maps received", 0},
   {"core", "# session keys confirmed via PONG", 0},
-  {"core", "# entries in session map", 0},
+  {"core", "# peers connected", 0},
   {"core", "# key exchanges initiated", 0},
   {"core", "# send requests dropped (disconnected)", 0},
   {"core", "# transmissions delayed due to corking", 0},
@@ -311,7 +328,7 @@ print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
   struct StatMaster *sm = cls;
 
   stats[sm->value].total += value;
-  fprintf (stderr, "Peer %2u: %12s/%50s = %12llu\n", sm->daemon, subsystem,
+  FPRINTF (stderr, "Peer %2u: %12s/%50s = %12llu\n", sm->daemon, subsystem,
            name, (unsigned long long) value);
   return GNUNET_OK;
 }
@@ -369,7 +386,7 @@ stat_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     i = 0;
     while (stats[i].name != NULL)
     {
-      fprintf (stderr, "Total  : %12s/%50s = %12llu\n", stats[i].subsystem,
+      FPRINTF (stderr, "Total  : %12s/%50s = %12llu\n", stats[i].subsystem,
                stats[i].name, (unsigned long long) stats[i].total);
       i++;
     }
@@ -439,7 +456,7 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct TestGetContext *test_get;
 
   die_task = GNUNET_SCHEDULER_NO_TASK;
-  fprintf (stderr, "Failing test with error: `%s'!\n", emsg);
+  FPRINTF (stderr, "Failing test with error: `%s'!\n", emsg);
   while (NULL != (test_put = all_puts_head))
   {
     if (test_put->task != GNUNET_SCHEDULER_NO_TASK)
@@ -463,7 +480,7 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   ok = 1;
   /* testing_peergroup will do that in its own end_badly() handler */
-  /*GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);*/
+  /*GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); */
   pg = NULL;
 }
 
@@ -475,7 +492,7 @@ static void
 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct TestGetContext *test_get = cls;
-  GNUNET_HashCode search_key;   /* Key stored under */
+  struct GNUNET_HashCode search_key;   /* Key stored under */
   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
 
   test_get->task = GNUNET_SCHEDULER_NO_TASK;
@@ -484,7 +501,7 @@ get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (test_get->succeeded != GNUNET_YES)
   {
     gets_failed++;
-    fprintf (stderr, "Get from peer %s for key %s failed!\n",
+    FPRINTF (stderr, "Get from peer %s for key %s failed!\n",
              GNUNET_i2s (&test_get->daemon->id), GNUNET_h2s (&search_key));
   }
   GNUNET_assert (test_get->get_handle != NULL);
@@ -500,7 +517,7 @@ get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if ((gets_failed > 10) && (outstanding_gets == 0))
   {
     /* Had more than 10% failures */
-    fprintf (stderr, "%llu gets succeeded, %llu gets failed!\n", gets_completed,
+    FPRINTF (stderr, "%llu gets succeeded, %llu gets failed!\n", gets_completed,
              gets_failed);
     GNUNET_SCHEDULER_cancel (die_task);
     ok = 1;
@@ -510,7 +527,7 @@ get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   if ((gets_completed + gets_failed == num_peers * num_peers) && (outstanding_gets == 0))       /* All gets successful */
   {
-    fprintf (stderr, "%llu gets succeeded, %llu gets failed!\n", gets_completed,
+    FPRINTF (stderr, "%llu gets succeeded, %llu gets failed!\n", gets_completed,
              gets_failed);
     GNUNET_SCHEDULER_cancel (die_task);
     ok = 0;
@@ -531,7 +548,7 @@ get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  */
 static void
 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
-                     const GNUNET_HashCode * key,
+                     const struct GNUNET_HashCode * key,
                      const struct GNUNET_PeerIdentity *get_path,
                      unsigned int get_path_length,
                      const struct GNUNET_PeerIdentity *put_path,
@@ -539,7 +556,7 @@ get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
                      size_t size, const void *data)
 {
   struct TestGetContext *test_get = cls;
-  GNUNET_HashCode search_key;   /* Key stored under */
+  struct GNUNET_HashCode search_key;   /* Key stored under */
   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
 
   memset (original_data, test_get->uid, sizeof (original_data));
@@ -552,27 +569,27 @@ get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
   {
     unsigned int i;
 
-    fprintf (stderr, "PUT (%u) Path: ", test_get->uid);
+    FPRINTF (stderr, "PUT (%u) Path: ", test_get->uid);
     for (i = 0; i < put_path_length; i++)
-      fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&put_path[i]));
-    fprintf (stderr, "\n");
+      FPRINTF (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&put_path[i]));
+    FPRINTF (stderr, "%s",  "\n");
   }
   if (get_path != NULL)
   {
     unsigned int i;
 
-    fprintf (stderr, "GET (%u) Path: ", test_get->uid);
+    FPRINTF (stderr, "GET (%u) Path: ", test_get->uid);
     for (i = 0; i < get_path_length; i++)
-      fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&get_path[i]));
-    fprintf (stderr, "%s%s\n", get_path_length > 0 ? "->" : "",
+      FPRINTF (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&get_path[i]));
+    FPRINTF (stderr, "%s%s\n", get_path_length > 0 ? "->" : "",
              GNUNET_i2s (&test_get->daemon->id));
   }
 #endif
 
-  if ((0 != memcmp (&search_key, key, sizeof (GNUNET_HashCode))) ||
+  if ((0 != memcmp (&search_key, key, sizeof (struct GNUNET_HashCode))) ||
       (0 != memcmp (original_data, data, sizeof (original_data))))
   {
-    fprintf (stderr, "Key or data is not the same as was inserted!\n");
+    FPRINTF (stderr, "%s",  "Key or data is not the same as was inserted!\n");
     return;
   }
   gets_completed++;
@@ -589,7 +606,7 @@ static void
 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct TestGetContext *test_get = cls;
-  GNUNET_HashCode key;          /* Made up key to store data under */
+  struct GNUNET_HashCode key;          /* Made up key to store data under */
   char data[TEST_DATA_SIZE];    /* Made up data to store */
 
   if (outstanding_gets > MAX_OUTSTANDING_GETS)
@@ -604,7 +621,7 @@ do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_assert (test_get->dht_handle != NULL);
   outstanding_gets++;
   test_get->get_handle =
-      GNUNET_DHT_get_start (test_get->dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
+      GNUNET_DHT_get_start (test_get->dht_handle, 
                             GNUNET_BLOCK_TYPE_TEST, &key, 1, route_option, NULL,
                             0, &get_result_iterator, test_get);
   test_get->task =
@@ -638,10 +655,9 @@ start_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   unsigned long long j;
   struct TestGetContext *test_get;
 
-#if VERBOSE
-  fprintf (stderr, "Issuing %llu GETs\n",
-           (unsigned long long) (num_peers * num_peers));
-#endif
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Issuing %llu GETs\n",
+             (unsigned long long) (num_peers * num_peers));
   for (i = 0; i < num_peers; i++)
     for (j = 0; j < num_peers; j++)
     {
@@ -658,13 +674,16 @@ start_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Called when the PUT request has been transmitted to the DHT service.
  */
 static void
-put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+put_finished (void *cls, int success)
 {
   struct TestPutContext *test_put = cls;
 
   outstanding_puts--;
   puts_completed++;
-  GNUNET_SCHEDULER_cancel (test_put->task);
+  if (GNUNET_SCHEDULER_NO_TASK != test_put->task)
+  {
+    GNUNET_SCHEDULER_cancel (test_put->task);
+  }
   test_put->task = GNUNET_SCHEDULER_add_now (&put_disconnect_task, test_put);
   if (puts_completed != num_peers * num_peers)
     return;
@@ -681,7 +700,7 @@ static void
 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct TestPutContext *test_put = cls;
-  GNUNET_HashCode key;          /* Made up key to store data under */
+  struct GNUNET_HashCode key;          /* Made up key to store data under */
   char data[TEST_DATA_SIZE];    /* Made up data to store */
 
   test_put->task = GNUNET_SCHEDULER_NO_TASK;
@@ -696,10 +715,9 @@ do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
   GNUNET_assert (test_put->dht_handle != NULL);
   outstanding_puts++;
-#if VERBOSE > 2
-  fprintf (stderr, "PUT %u at `%s'\n", test_put->uid,
-           GNUNET_i2s (&test_put->daemon->id));
-#endif
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "PUT %u at `%s'\n", test_put->uid,
+             GNUNET_i2s (&test_put->daemon->id));
   GNUNET_DHT_put (test_put->dht_handle, &key, 1, route_option,
                   GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
                   GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
@@ -716,6 +734,11 @@ run_dht_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   unsigned long long i;
   struct TestPutContext *test_put;
 
+  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+  {
+    ok = 1;
+    return;
+  }
 #if PATH_TRACKING
   route_option =
       GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
@@ -725,7 +748,7 @@ run_dht_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   die_task =
       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
                                     "from setup puts/gets");
-  fprintf (stderr, "Issuing %llu PUTs (one per peer)\n",
+  FPRINTF (stderr, "Issuing %llu PUTs (one per peer)\n",
            (unsigned long long) (num_peers * num_peers));
   for (i = 0; i < num_peers * num_peers; i++)
   {
@@ -750,7 +773,7 @@ startup_done (void *cls, const char *emsg)
 {
   if (emsg != NULL)
   {
-    fprintf (stderr, "Failed to setup topology: %s\n", emsg);
+    FPRINTF (stderr, "Failed to setup topology: %s\n", emsg);
     die_task = GNUNET_SCHEDULER_add_now (&end_badly, "topology setup failed");
     return;
   }
@@ -774,12 +797,14 @@ run (void *cls, char *const *args, const char *cfgfile,
     return;
   }
   if (GNUNET_SYSERR ==
-      GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
+      GNUNET_CONFIGURATION_get_value_number (cfg, "testing_old", "num_peers",
                                              &num_peers))
     num_peers = DEFAULT_NUM_PEERS;
   pg = GNUNET_TESTING_peergroup_start (cfg, num_peers, TIMEOUT, NULL,
                                        &startup_done, NULL, NULL);
   GNUNET_assert (NULL != pg);
+  shutdown_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL,
+                                               &do_stop, NULL);
 }
 
 
@@ -792,9 +817,6 @@ check ()
   char *const argv[] = { "test-dht-multipeer",  /* Name to give running binary */
     "-c",
     "test_dht_multipeer_data.conf",     /* Config file to use */
-#if VERBOSE
-    "-L", "DEBUG",
-#endif
     NULL
   };
   struct GNUNET_GETOPT_CommandLineOption options[] = {
@@ -818,13 +840,8 @@ main (int argc, char *argv[])
 {
   int ret;
 
-
   GNUNET_log_setup ("test-dht-multipeer",
-#if VERBOSE
-                    "DEBUG",
-#else
                     "WARNING",
-#endif
                     NULL);
   ret = check ();
   /**