- doc
[oweals/gnunet.git] / src / mesh / test_mesh_single.c
index 4c5d0a6597a92707257ee40ac2d983e38d3d31a9..6fc22362c579e7f0137086c3150f2e55a5d6c814 100644 (file)
@@ -19,8 +19,8 @@
 */
 
 /**
- * @file mesh/test_mesh_local.c
- * @brief test mesh local: test of mesh tunnels 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
  */
 
@@ -37,11 +37,11 @@ struct GNUNET_TESTING_Peer *me;
 
 static struct GNUNET_MESH_Handle *mesh;
 
-static struct GNUNET_MESH_Tunnel *t1;
+static struct GNUNET_MESH_Channel *ch1;
 
-static struct GNUNET_MESH_Tunnel *t2;
+static struct GNUNET_MESH_Channel *ch2;
 
-static int result = GNUNET_OK;
+static int result;
 
 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
 
@@ -66,15 +66,20 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     GNUNET_SCHEDULER_cancel (abort_task);
   }
-  if (NULL != t1)
+  if (NULL != ch1)
   {
-    GNUNET_MESH_tunnel_destroy (t1);
+    GNUNET_MESH_channel_destroy (ch1);
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
   if (NULL != mesh)
   {
     GNUNET_MESH_disconnect (mesh);
+    mesh = NULL;
+  }
+  else
+  {
+    GNUNET_break (0);
   }
 }
 
@@ -97,89 +102,108 @@ 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.
  *
  * @param cls closure (set from GNUNET_MESH_connect)
- * @param tunnel connection to the other end
- * @param tunnel_ctx place to store local state associated with the tunnel
+ * @param channel connection to the other end
+ * @param channel_ctx place to store local state associated with the channel
  * @param message the actual message
- * 
+ *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
-data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
+data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
+               void **channel_ctx,
                const struct GNUNET_MessageHeader *message)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data callback! Repetition %u\n", repetition);
-  printf ("rep %u\n", repetition);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Data callback! Repetition %u/%u\n",
+              repetition, REPETITIONS);
   repetition = repetition + 1;
   if (repetition < REPETITIONS)
   {
-    struct GNUNET_MESH_Tunnel *my_tunnel;
+    struct GNUNET_MESH_Channel *my_channel;
     if (repetition % 2 == 0)
-      my_tunnel = t1;
+      my_channel = ch1;
     else
-      my_tunnel = t2;
-    GNUNET_MESH_notify_transmit_ready (my_tunnel, GNUNET_NO,
+      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 (tunnel);
+    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 (tunnel);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All data OK. Destroying channel.\n");
+  GNUNET_MESH_channel_destroy (ch1);
+  ch1 = NULL;
   return GNUNET_OK;
 }
 
 
 /**
- * Method called whenever another peer has added us to a tunnel
+ * Method called whenever another peer has added us to a channel
  * the other peer initiated.
  *
  * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
+ * @param channel new handle to the channel
+ * @param initiator peer that started the channel
  * @param port port number
- * @return initial tunnel context for the tunnel (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_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
+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 tunnel on port %u\n",
+              "received incoming channel on port %u\n",
               port);
-  t2 = tunnel;
+  ch2 = channel;
   return NULL;
 }
 
 
 /**
- * Function called whenever an inbound tunnel is destroyed.  Should clean up
+ * Function called whenever an inbound channel is destroyed.  Should clean up
  * any associated state.
  *
  * @param cls closure (set from GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
+ * @param channel connection to the other end (henceforth invalid)
+ * @param channel_ctx place where local state associated
+ *                   with the channel is stored
  */
 static void
-inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
-             void *tunnel_ctx)
+channel_end (void *cls, const struct GNUNET_MESH_Channel *channel,
+             void *channel_ctx)
 {
   long id = (long) cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "incoming tunnel closed at peer %ld\n",
+              "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 ();
+  }
 }
 
 
@@ -194,11 +218,11 @@ static struct GNUNET_MESH_MessageHandler handlers1[] = {
 
 /**
  * Data send callback: fillbuffer with test packet.
- * 
+ *
  * @param cls Closure (unused).
  * @param size Buffer size.
  * @param buf Buffer to fill.
- * 
+ *
  * @return size of test packet.
  */
 static size_t
@@ -221,7 +245,7 @@ do_send (void *cls, size_t size, void *buf)
 
 /**
  * Connect to other client and send data
- * 
+ *
  * @param cls Closue (unused).
  * @param tc TaskContext.
  */
@@ -229,26 +253,30 @@ 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");
-  t1 = GNUNET_MESH_tunnel_create (mesh, NULL, &id, 1, GNUNET_YES, GNUNET_NO);
-  GNUNET_MESH_notify_transmit_ready (t1, 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);
 }
 
 
 /**
  * Initialize framework and start test
- * 
+ *
  * @param cls Closure (unused).
  * @param cfg Configuration handle.
  * @param peer Testing peer handle.
  */
 static void
-run (void *cls, 
+run (void *cls,
      const struct GNUNET_CONFIGURATION_Handle *cfg,
      struct GNUNET_TESTING_Peer *peer)
 {
@@ -261,8 +289,8 @@ run (void *cls,
                                     NULL);
   mesh = GNUNET_MESH_connect (cfg,       /* configuration */
                               (void *) 1L,     /* cls */
-                              &inbound_tunnel,   /* inbound new hndlr */
-                              &inbound_end,      /* inbound end hndlr */
+                              &inbound_channel,   /* inbound new hndlr */
+                              &channel_end,      /* inbound end hndlr */
                               handlers1, /* traffic handlers */
                               ports);     /* ports offered */
 
@@ -286,9 +314,10 @@ 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))
+                                    &run, NULL))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
     return 2;
@@ -297,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 */