clique topology optimization, progress meter for connecting
[oweals/gnunet.git] / src / testing / test_testing_topology.c
index 36ccad6c957614cebc0c3e5fcf15116c996cff66..b328873f23d90393c4267a31e302164cca867c19 100644 (file)
@@ -28,6 +28,8 @@
 
 #define VERBOSE GNUNET_NO
 
+#define PROGRESS_BARS GNUNET_YES
+
 #define DELAY_FOR_LOGGING GNUNET_NO
 
 /**
@@ -52,6 +54,10 @@ static int ok;
 
 static unsigned long long num_peers;
 
+struct GNUNET_TIME_Relative connect_timeout;
+
+static unsigned long long connect_attempts;
+
 static unsigned int topology_connections;
 
 static unsigned int total_connections;
@@ -142,6 +148,12 @@ struct TestMessageContext
   /* Identifier for this message, so we don't disconnect other peers! */
   uint32_t uid;
 
+  /* Has peer1 been notified already of a connection to peer2? */
+  int peer1notified;
+
+  /* Has the core of peer2 been connected already? */
+  int peer2connected;
+
   /* Task for disconnecting cores, allow task to be cancelled on shutdown */
   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
 
@@ -273,6 +285,7 @@ disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   total_server_connections -= 2;
 }
 
+#if DO_STATS
 static void
 stats_finished (void *cls, int result)
 {
@@ -300,6 +313,7 @@ stats_print (void *cls,
               GNUNET_i2s (peer), subsystem, name, value);
   return GNUNET_OK;
 }
+#endif
 
 static void
 topology_cb (void *cls,
@@ -325,9 +339,11 @@ topology_cb (void *cls,
         {
           fprintf (outfile, "}\n");
           fclose (outfile);
+#if DO_STATS
           GNUNET_TESTING_get_statistics (pg, &stats_finished, &stats_print,
                                          NULL);
-          //GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
+#endif
+          GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
         }
     }
 }
@@ -345,7 +361,7 @@ process_mtype (void *cls,
   if (pos->uid != ntohl (msg->uid))
     return GNUNET_OK;
 
-#if VERBOSE
+#if PROGRESS_BARS
   if ((total_messages_received) % modnum == 0)
     {
       if (total_messages_received == 0)
@@ -364,6 +380,7 @@ process_mtype (void *cls,
 #endif
 
   total_messages_received++;
+
 #if VERBOSE > 1
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received message from `%4s', type %d.\n", GNUNET_i2s (peer),
@@ -375,7 +392,7 @@ process_mtype (void *cls,
 
   if (total_messages_received == expected_messages)
     {
-#if VERBOSE
+#if PROGRESS_BARS
       fprintf (stdout, "100%%]\n");
 #endif
       GNUNET_SCHEDULER_cancel (die_task);
@@ -491,33 +508,89 @@ init_notify_peer2 (void *cls,
 {
   struct TestMessageContext *pos = cls;
 
+  total_server_connections++;
+
+  pos->peer2connected = GNUNET_YES;
+  if (pos->peer1notified == GNUNET_YES) /* Peer 1 has been notified of connection to peer 2 */
+    {
 #if VERBOSE > 1
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Core connection to `%4s' established, scheduling message send\n",
-              GNUNET_i2s (my_identity));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Scheduling message send to peer `%s' from peer `%s' (init_notify_peer2)\n",
+                  GNUNET_i2s (my_identity), GNUNET_h2s(&pos->peer1->id.hashPubKey));
 #endif
-  total_server_connections++;
+      if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
+                                                     0,
+                                                     TIMEOUT,
+                                                     &pos->peer2->id,
+                                                     sizeof (struct
+                                                             GNUNET_TestMessage),
+                                                     &transmit_ready, pos))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
+                      GNUNET_i2s (&pos->peer2->id));
+          transmit_ready_failed++;
+        }
+      else
+        {
+          transmit_ready_scheduled++;
+        }
+    }
+}
 
-  if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
-                                                 0,
-                                                 TIMEOUT,
-                                                 &pos->peer2->id,
-                                                 sizeof (struct
-                                                         GNUNET_TestMessage),
-                                                 &transmit_ready, pos))
+/**
+ * Method called whenever a given peer connects.
+ *
+ * @param cls closure
+ * @param peer peer identity this notification is about
+ * @param atsi performance data for the connection
+ */
+static void connect_notify_peers (void *cls,
+                                  const struct
+                                  GNUNET_PeerIdentity *peer,
+                                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
+{
+  struct TestMessageContext *pos = cls;
+
+  if (0 == memcmp(peer, &pos->peer2->id, sizeof(struct GNUNET_PeerIdentity)))
     {
+      pos->peer1notified = GNUNET_YES;
+#if VERBOSE > 1
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
-                  GNUNET_i2s (&pos->peer2->id));
-      transmit_ready_failed++;
+                "Peer `%s' notified of connection to peer `%s'\n",
+                GNUNET_i2s (&pos->peer1->id), GNUNET_h2s(&peer->hashPubKey));
+#endif
     }
   else
+    return;
+
+  if (pos->peer2connected == GNUNET_YES) /* Already connected and notified of connection, send message! */
     {
-      transmit_ready_scheduled++;
+#if VERBOSE > 1
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Scheduling message send to peer `%s' from peer `%s' (init_notify_peer2)\n",
+                GNUNET_i2s (&pos->peer2->id), GNUNET_h2s(&pos->peer1->id.hashPubKey));
+#endif
+      if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
+                                                     0,
+                                                     TIMEOUT,
+                                                     &pos->peer2->id,
+                                                     sizeof (struct
+                                                             GNUNET_TestMessage),
+                                                     &transmit_ready, pos))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
+                      GNUNET_i2s (&pos->peer2->id));
+          transmit_ready_failed++;
+        }
+      else
+        {
+          transmit_ready_scheduled++;
+        }
     }
 }
 
-
 static void
 init_notify_peer1 (void *cls,
                    struct GNUNET_CORE_Handle *server,
@@ -585,7 +658,7 @@ send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                           1,
                                           pos,
                                           &init_notify_peer1,
-                                          NULL, NULL,
+                                          &connect_notify_peers, NULL,
                                           NULL,
                                           NULL,
                                           GNUNET_NO, NULL, GNUNET_NO,
@@ -620,7 +693,7 @@ topology_callback (void *cls,
   struct TestMessageContext *temp_context;
   if (emsg == NULL)
     {
-#if VERBOSE
+#if PROGRESS_BARS
       if ((total_connections) % modnum == 0)
         {
           if (total_connections == 0)
@@ -639,7 +712,7 @@ topology_callback (void *cls,
 #endif
       total_connections++;
 #if VERBOSE > 1
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "connected peer %s to peer %s\n",
                   first_daemon->shortname, second_daemon->shortname);
 #endif
       temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
@@ -667,7 +740,7 @@ topology_callback (void *cls,
 
   if (total_connections == expected_connections)
     {
-#if VERBOSE
+#if PROGRESS_BARS
       fprintf (stdout, "100%%]\n");
 #endif
 #if VERBOSE
@@ -677,6 +750,10 @@ topology_callback (void *cls,
 #endif
       modnum = expected_messages / 4;
       dotnum = (expected_messages / 50) + 1;
+      if (modnum == 0)
+        modnum = 1;
+      if (dotnum == 0)
+        dotnum = 1;
       GNUNET_SCHEDULER_cancel (die_task);
       die_task = GNUNET_SCHEDULER_NO_TASK;
 #if DELAY_FOR_LOGGING
@@ -693,7 +770,7 @@ topology_callback (void *cls,
       GNUNET_SCHEDULER_add_delayed (settle_time, &send_test_messages,
                                     test_messages);
 #endif
-#if VERBOSE
+#if PROGRESS_BARS
       fprintf (stdout, "Test message progress: [");
 #endif
 
@@ -747,27 +824,36 @@ connect_topology ()
         GNUNET_TESTING_connect_topology (pg, connection_topology,
                                          connect_topology_option,
                                          connect_topology_option_modifier,
+                                         connect_timeout,
+                                         connect_attempts,
                                          &topology_creation_finished, NULL);
-#if VERBOSE > 1
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+#if PROGRESS_BARS
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                   "Have %d expected connections\n", expected_connections);
 #endif
     }
 
   GNUNET_SCHEDULER_cancel (die_task);
-  if (expected_connections == GNUNET_SYSERR)
+  if (expected_connections < 1)
     {
       die_task =
         GNUNET_SCHEDULER_add_now (&end_badly,
                                   "from connect topology (bad return)");
+      return;
     }
 
-  die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
+  die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                           (GNUNET_TIME_UNIT_SECONDS,
+                                            SECONDS_PER_PEER_START * num_peers),
                                            &end_badly,
                                            "from connect topology (timeout)");
   modnum = expected_connections / 4;
   dotnum = (expected_connections / 50) + 1;
-#if VERBOSE
+  if (modnum == 0)
+    modnum = 1;
+  if (dotnum == 0)
+    dotnum = 1;
+#if PROGRESS_BARS
   fprintf (stdout, "Peer connection progress: [");
 #endif
 }
@@ -780,7 +866,7 @@ create_topology ()
       (pg, topology, blacklist_topology,
        blacklist_transports) != GNUNET_SYSERR)
     {
-#if VERBOSE
+#if PROGRESS_BARS
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Topology set up, now starting peers!\n");
       fprintf (stdout, "Daemon start progress [");
@@ -795,7 +881,9 @@ create_topology ()
                                   "from create topology (bad return)");
     }
   GNUNET_SCHEDULER_cancel (die_task);
-  die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
+  die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                           (GNUNET_TIME_UNIT_SECONDS,
+                                            SECONDS_PER_PEER_START * num_peers),
                                            &end_badly,
                                            "from continue startup (timeout)");
 }
@@ -818,7 +906,7 @@ peers_started_callback (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
               (num_peers - peers_left) + 1, num_peers);
 #endif
-#if VERBOSE
+#if PROGRESS_BARS
   if ((num_peers - peers_left) % modnum == 0)
     {
       if (num_peers - peers_left == 0)
@@ -838,7 +926,7 @@ peers_started_callback (void *cls,
   peers_left--;
   if (peers_left == 0)
     {
-#if VERBOSE
+#if PROGRESS_BARS
       fprintf (stdout, "100%%]\n");
 #endif
 #if VERBOSE
@@ -891,7 +979,7 @@ hostkey_callback (void *cls,
               num_peers - peers_left, num_peers, GNUNET_i2s (id));
 #endif
 
-#if VERBOSE
+#if PROGRESS_BARS
   if ((num_peers - peers_left) % modnum == 0)
     {
       if (num_peers - peers_left == 0)
@@ -911,7 +999,7 @@ hostkey_callback (void *cls,
   peers_left--;
   if (peers_left == 0)
     {
-#if VERBOSE
+#if PROGRESS_BARS
       fprintf (stdout, "100%%]\n");
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "All %d hostkeys created, now creating topology!\n",
@@ -939,6 +1027,7 @@ run (void *cls,
   char *connect_topology_option_str;
   char *connect_topology_option_modifier_string;
   unsigned long long temp_settle;
+  unsigned long long max_outstanding_connections;
   ok = 1;
 
   dotOutFile = fopen (dotOutFileName, "w");
@@ -1044,6 +1133,34 @@ run (void *cls,
     settle_time =
       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_settle);
 
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_timeout",
+                                             &temp_settle))
+    connect_timeout =
+      GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_settle);
+  else
+    {
+      GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_timeout");
+      return;
+    }
+
+
+  if (GNUNET_OK !=
+        GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_attempts",
+                                               &connect_attempts))
+    {
+      GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_attempts");
+      return;
+    }
+
+  if (GNUNET_OK !=
+        GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "max_outstanding_connections",
+                                               &max_outstanding_connections))
+    {
+      GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "max_outstanding_connections");
+      return;
+    }
+
   if (GNUNET_SYSERR ==
       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
                                              &num_peers))
@@ -1054,7 +1171,11 @@ run (void *cls,
   peers_left = num_peers;
   modnum = num_peers / 4;
   dotnum = (num_peers / 50) + 1;
-#if VERBOSE
+  if (modnum == 0)
+    modnum = 1;
+  if (dotnum == 0)
+    dotnum = 1;
+#if PROGRESS_BARS
   fprintf (stdout, "Hostkey generation progress: [");
 #endif
   /* Set up a task to end testing if peer start fails */
@@ -1067,6 +1188,8 @@ run (void *cls,
 
   GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
   pg = GNUNET_TESTING_daemons_start (cfg,
+                                     peers_left,
+                                     max_outstanding_connections,
                                      peers_left,
                                      GNUNET_TIME_relative_multiply
                                      (GNUNET_TIME_UNIT_SECONDS,
@@ -1117,7 +1240,7 @@ main (int argc, char *argv[])
   char *binary_start_pos;
   char *our_binary_name;
 
-  binary_start_pos = rindex (argv[0], '/');
+  binary_start_pos = strchr (argv[0], '/');
   GNUNET_assert (binary_start_pos != NULL);
   topology_string = strstr (binary_start_pos, "_topology");
   GNUNET_assert (topology_string != NULL);