- stop using message types of payload on service side
[oweals/gnunet.git] / src / mesh / test_mesh_small.c
index a7ecaecdbe8d7ff0259d8f10a5615edeb3c3614a..efe939876f7960dcc0703181dd40c0052672192f 100644 (file)
@@ -54,6 +54,7 @@
 #define SPEED_ACK 4
 #define SPEED_MIN 5
 #define SPEED_NOBUF 6
+#define P2P_SIGNAL 10
 
 /**
  * Which test are we running?
@@ -233,7 +234,6 @@ show_end_data (void)
 static void
 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  fprintf(stderr, "b");
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
 }
@@ -251,7 +251,6 @@ disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   long line = (long) cls;
   unsigned int i;
 
-  fprintf(stderr, "a");
   for (i = 0; i < 3; i++)
     if (NULL != t_op[i])
     {
@@ -286,6 +285,11 @@ disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+/**
+ * Abort test: schedule disconnect and shutdown immediately
+ * 
+ * @param line Line in the code the abort is requested from (__LINE__).
+ */
 void
 abort_test (long line)
 {
@@ -293,9 +297,9 @@ abort_test (long line)
   {
     GNUNET_SCHEDULER_cancel (disconnect_task);
   }
-    disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
-                                                    &disconnect_mesh_peers,
-                                                    (void *) line);
+  disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
+                                                  &disconnect_mesh_peers,
+                                                  (void *) line);
 }
 
 /**
@@ -336,7 +340,7 @@ data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   else
   {
     tunnel = t;
-    destination = p_id[1];
+    destination = p_id[2];
   }
   th = GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
                                           GNUNET_TIME_UNIT_FOREVER_REL,
@@ -385,7 +389,7 @@ tmt_rdy (void *cls, size_t size, void *buf)
   if (size < size_payload || NULL == buf)
   {
     GNUNET_break (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "size %u, buf %p, data_sent %u, data_received %u\n",
                 size,
                 buf,
@@ -450,18 +454,18 @@ data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
 
   switch (client)
   {
-  case 1L:
+  case 0L:
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
     peers_responded++;
     if (test == MULTICAST && peers_responded < 2)
       return GNUNET_OK;
     break;
-  case 2L:
   case 3L:
+  case 4L:
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 "Leaf client %li got a message.\n",
                 client);
-    client = 2L;
+    client = 4L;
     break;
   default:
     GNUNET_assert (0);
@@ -472,11 +476,11 @@ data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
   if (SPEED == test && GNUNET_YES == test_backwards)
   {
-    expected_target_client = 1L;
+    expected_target_client = 0L;
   }
   else
   {
-    expected_target_client = 2L;
+    expected_target_client = 4L;
   }
 
   if (GNUNET_NO == initialized)
@@ -485,13 +489,13 @@ data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
     start_time = GNUNET_TIME_absolute_get ();
     if (SPEED == test)
     {
-      GNUNET_assert (2L == client);
+      GNUNET_assert (4L == client);
       GNUNET_SCHEDULER_add_now (&data_task, NULL);
       return GNUNET_OK;
     }
   }
 
-  if (client == expected_target_client) // Normally 2 or 3
+  if (client == expected_target_client) // Normally 3 or 4
   {
     data_received++;
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
@@ -510,7 +514,7 @@ data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
         return GNUNET_OK;
     }
   }
-  else // Normally 1
+  else // Normally 0
   {
     if (test == SPEED_ACK || test == SPEED)
     {
@@ -527,8 +531,16 @@ data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
         return GNUNET_OK;
       show_end_data();
     }
-    GNUNET_MESH_tunnel_destroy (t);
-    t = NULL;
+    if (test == P2P_SIGNAL)
+    {
+      GNUNET_MESH_tunnel_destroy (incoming_t);
+      incoming_t = NULL;
+    }
+    else
+    {
+      GNUNET_MESH_tunnel_destroy (t);
+      t = NULL;
+    }
   }
 
   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
@@ -573,7 +585,7 @@ incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
               GNUNET_i2s (initiator), (long) cls);
   ok++;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
-  if ((long) cls == 2L)
+  if ((long) cls == 4L)
     incoming_t = tunnel;
   else if ((long) cls == 3L)
     incoming_t2 = tunnel;
@@ -612,7 +624,7 @@ tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Incoming tunnel disconnected at peer %d\n",
               i);
-  if (2L == i)
+  if (4L == i)
   {
     ok++;
     incoming_t = NULL;
@@ -653,6 +665,16 @@ dh (void *cls, const struct GNUNET_PeerIdentity *peer)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "peer %s disconnected\n",
               GNUNET_i2s (peer));
+  if (P2P_SIGNAL == test)
+  {
+    ok ++;
+    if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+    {
+      GNUNET_SCHEDULER_cancel (disconnect_task);
+    }
+    disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
+                                                (void *) __LINE__);
+  }
   return;
 }
 
@@ -673,16 +695,16 @@ ch (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct GNUNET_PeerIdentity *dest;
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "peer %s connected\n", GNUNET_i2s (peer));
+              "%ld peer %s connected\n", i, GNUNET_i2s (peer));
 
-  if (0 == memcmp (p_id[1], peer, sizeof (struct GNUNET_PeerIdentity)) &&
-      i == 1L)
+  if (0 == memcmp (p_id[2], peer, sizeof (struct GNUNET_PeerIdentity)) &&
+      i == 0L)
   {
     ok++;
   }
   if (test == MULTICAST &&
-      0 == memcmp (p_id[2], peer, sizeof (struct GNUNET_PeerIdentity)) &&
-      i == 1L)
+      0 == memcmp (p_id[1], peer, sizeof (struct GNUNET_PeerIdentity)) &&
+      i == 0L)
   {
     ok++;
   }
@@ -690,10 +712,11 @@ ch (void *cls, const struct GNUNET_PeerIdentity *peer,
   switch (test)
   {
     case UNICAST:
+    case P2P_SIGNAL:
     case SPEED:
     case SPEED_ACK:
       // incoming_t is NULL unless we send a relevant data packet
-      dest = p_id[1];
+      dest = p_id[2];
       break;
     case MULTICAST:
       peers_in_tunnel++;
@@ -747,13 +770,13 @@ do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
-  GNUNET_MESH_peer_request_connect_add (t, p_id[1]);
+  GNUNET_MESH_peer_request_connect_add (t, p_id[2]);
 
   if (test == MULTICAST)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "add peer 3\n");
-    GNUNET_MESH_peer_request_connect_add (t, p_id[2]);
+    GNUNET_MESH_peer_request_connect_add (t, p_id[1]);
   }
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -761,10 +784,10 @@ do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
   {
     GNUNET_SCHEDULER_cancel (disconnect_task);
-    disconnect_task =
-        GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_mesh_peers,
-                                      (void *) __LINE__);
   }
+  disconnect_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+                                                  &disconnect_mesh_peers,
+                                                  (void *) __LINE__);
 }
 
 /**
@@ -785,7 +808,6 @@ pi_cb (void *cls,
   long i = (long) cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
-  t_op[i] = NULL;
   if (NULL == pinfo || NULL != emsg)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
@@ -793,9 +815,11 @@ pi_cb (void *cls,
     return;
   }
   p_id[i] = pinfo->result.id;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
   p_ids++;
   if ((MULTICAST == test && p_ids < 3) || p_ids < 2)
     return;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                             &do_test, NULL);
 }
@@ -822,7 +846,7 @@ tmain (void *cls,
   peers_running = num_peers;
   h1 = meshes[0];
   h2 = meshes[num_peers - 1];
-  t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 1L);
+  t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 0L);
   if (SPEED_MIN == test)
   {
     GNUNET_MESH_tunnel_speed_min(t);
@@ -842,15 +866,19 @@ tmain (void *cls,
   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
                                                  GNUNET_TESTBED_PIT_IDENTITY,
                                                  &pi_cb, (void *) 0L);
-  t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
+  t_op[2] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
                                                  GNUNET_TESTBED_PIT_IDENTITY,
-                                                 &pi_cb, (void *) 1L);
+                                                 &pi_cb, (void *) 2L);
   if (MULTICAST == test)
   {
     h3 = meshes[num_peers - 2];
-    t_op[2] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 2],
+    t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 2],
                                                    GNUNET_TESTBED_PIT_IDENTITY,
-                                                   &pi_cb, (void *) 2L);
+                                                   &pi_cb, (void *) 1L);
+  }
+  else
+  {
+    t_op[1] = NULL;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
 }
@@ -881,6 +909,13 @@ main (int argc, char *argv[])
     test_name = "multicast";
     ok_goal = 10;
   }
+  else if (strstr (argv[0], "test_mesh_small_signal") != NULL)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
+    test = P2P_SIGNAL;
+    test_name = "signal";
+    ok_goal = 5;
+  }
   else if (strstr (argv[0], "test_mesh_small_speed_ack") != NULL)
   {
    /* Each peer is supposed to generate the following callbacks:
@@ -945,7 +980,7 @@ main (int argc, char *argv[])
   }
 
   p_ids = 0;
-  GNUNET_MESH_TEST_run (test_name,
+  GNUNET_MESH_TEST_run ("test_mesh_small",
                         "test_mesh_small.conf",
                         5,
                         &tmain,