- log output
[oweals/gnunet.git] / src / mesh / test_mesh_single.c
index ec91bf2245096bfee3ff6321b7d9218cc143e312..6fc22362c579e7f0137086c3150f2e55a5d6c814 100644 (file)
@@ -19,8 +19,8 @@
 */
 
 /**
- * @file mesh/test_mesh_local.c
- * @brief test mesh local: test of mesh channels with just one peer
+ * @file mesh/test_mesh_single.c
+ * @brief test mesh single: test of mesh channels with just one client
  * @author Bartlomiej Polot
  */
 
@@ -41,7 +41,7 @@ static struct GNUNET_MESH_Channel *ch1;
 
 static struct GNUNET_MESH_Channel *ch2;
 
-static int result = GNUNET_OK;
+static int result;
 
 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
 
@@ -75,6 +75,11 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (NULL != mesh)
   {
     GNUNET_MESH_disconnect (mesh);
+    mesh = NULL;
+  }
+  else
+  {
+    GNUNET_break (0);
   }
 }
 
@@ -97,6 +102,16 @@ do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+static void
+finish (void)
+{
+  if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
+    GNUNET_SCHEDULER_cancel (shutdown_task);
+  shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                                &do_shutdown, NULL);
+}
+
+
 /**
  * Function is called whenever a message is received.
  *
@@ -126,17 +141,15 @@ data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
       my_channel = ch2;
     GNUNET_MESH_notify_transmit_ready (my_channel, GNUNET_NO,
                                        GNUNET_TIME_UNIT_FOREVER_REL,
-                                       sizeof (struct GNUNET_MessageHeader) + DATA_SIZE,
+                                       sizeof (struct GNUNET_MessageHeader)
+                                       + DATA_SIZE,
                                        &do_send, NULL);
     GNUNET_MESH_receive_done (channel);
     return GNUNET_OK;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
-    GNUNET_SCHEDULER_cancel (shutdown_task);
-  shutdown_task =
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown,
-                                  NULL);
-  GNUNET_MESH_receive_done (channel);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All data OK. Destroying channel.\n");
+  GNUNET_MESH_channel_destroy (ch1);
+  ch1 = NULL;
   return GNUNET_OK;
 }
 
@@ -149,12 +162,14 @@ data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
  * @param channel new handle to the channel
  * @param initiator peer that started the channel
  * @param port port number
- * @return initial channel context for the channel (can be NULL -- that's not an error)
+ * @param options channel option flags
+ * @return initial channel context for the channel
+ *         (can be NULL -- that's not an error)
  */
 static void *
 inbound_channel (void *cls, struct GNUNET_MESH_Channel *channel,
                 const struct GNUNET_PeerIdentity *initiator,
-                uint32_t port)
+                uint32_t port, enum GNUNET_MESH_ChannelOption options)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "received incoming channel on port %u\n",
@@ -174,7 +189,7 @@ inbound_channel (void *cls, struct GNUNET_MESH_Channel *channel,
  *                   with the channel is stored
  */
 static void
-inbound_end (void *cls, const struct GNUNET_MESH_Channel *channel,
+channel_end (void *cls, const struct GNUNET_MESH_Channel *channel,
              void *channel_ctx)
 {
   long id = (long) cls;
@@ -182,6 +197,13 @@ inbound_end (void *cls, const struct GNUNET_MESH_Channel *channel,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "incoming channel closed at peer %ld\n",
               id);
+  if (REPETITIONS == repetition && channel == ch2)
+  {
+    ch2 = NULL;
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "everything fine! finishing!\n");
+    result = GNUNET_OK;
+    finish ();
+  }
 }
 
 
@@ -231,14 +253,18 @@ static void
 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_PeerIdentity id;
+  size_t size = sizeof (struct GNUNET_MessageHeader) + DATA_SIZE;
+
+  if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
+    return;
 
   GNUNET_TESTING_peer_get_identity (me, &id);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
-  ch1 = GNUNET_MESH_channel_create (mesh, NULL, &id, 1, GNUNET_YES, GNUNET_NO);
+  ch1 = GNUNET_MESH_channel_create (mesh, NULL, &id, 1,
+                                    GNUNET_MESH_OPTION_DEFAULT);
   GNUNET_MESH_notify_transmit_ready (ch1, GNUNET_NO,
                                      GNUNET_TIME_UNIT_FOREVER_REL,
-                                     sizeof (struct GNUNET_MessageHeader) + DATA_SIZE,
-                                     &do_send, NULL);
+                                     size, &do_send, NULL);
 }
 
 
@@ -264,7 +290,7 @@ run (void *cls,
   mesh = GNUNET_MESH_connect (cfg,       /* configuration */
                               (void *) 1L,     /* cls */
                               &inbound_channel,   /* inbound new hndlr */
-                              &inbound_end,      /* inbound end hndlr */
+                              &channel_end,      /* inbound end hndlr */
                               handlers1, /* traffic handlers */
                               ports);     /* ports offered */
 
@@ -288,6 +314,7 @@ run (void *cls,
 int
 main (int argc, char *argv[])
 {
+  result = GNUNET_NO;
   if (0 != GNUNET_TESTING_peer_run ("test-mesh-local",
                                     "test_mesh.conf",
                                     &run, NULL))
@@ -299,4 +326,4 @@ main (int argc, char *argv[])
   return (result == GNUNET_OK) ? 0 : 1;
 }
 
-/* end of test_mesh_local_1.c */
+/* end of test_mesh_single.c */