clean up for configs
[oweals/gnunet.git] / src / mesh / test_mesh_local.c
index 25ccf7b2e03c07d7ce1605f310c608b974d78ae9..932f01a2ef4d4198b861923da74f93582403da3c 100644 (file)
@@ -20,7 +20,7 @@
 
 /**
  * @file mesh/test_mesh_local.c
- * @brief test mesh local: test of mesh tunnels with just one peer
+ * @brief test mesh local: test of mesh channels with just one peer
  * @author Bartlomiej Polot
  */
 
@@ -28,7 +28,7 @@
 #include "gnunet_util_lib.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_testing_lib.h"
-#include "gnunet_mesh_service_enc.h"
+#include "gnunet_mesh_service.h"
 
 struct GNUNET_TESTING_Peer *me;
 
@@ -40,10 +40,24 @@ static struct GNUNET_MESH_Channel *ch;
 
 static int result = GNUNET_OK;
 
+static int got_data = GNUNET_NO;
+
 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
 
 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
 
+static struct GNUNET_MESH_TransmitHandle *mth;
+
+
+/**
+ * Connect to other client and send data
+ *
+ * @param cls Closue (unused).
+ * @param tc TaskContext.
+ */
+static void
+do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
 
 /**
  * Shutdown nicely
@@ -98,7 +112,7 @@ do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @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)
  */
@@ -108,6 +122,7 @@ data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
                const struct GNUNET_MessageHeader *message)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data callback! Shutting down.\n");
+  got_data = GNUNET_YES;
   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
     GNUNET_SCHEDULER_cancel (shutdown_task);
   shutdown_task =
@@ -126,18 +141,19 @@ 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
+ * @param options channel options
  * @return initial channel context for the channel
  *         (can be NULL -- that's not an error)
  */
 static void *
-inbound_tunnel (void *cls, struct GNUNET_MESH_Channel *channel,
+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)
 {
   long id = (long) cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "received incoming tunnel on peer %d, port %u\n",
+              "received incoming channel on peer %d, port %u\n",
               id, port);
   if (id != 2L)
   {
@@ -150,7 +166,7 @@ inbound_tunnel (void *cls, struct GNUNET_MESH_Channel *channel,
 
 
 /**
- * Function called whenever an inbound channel is destroyed.  Should clean up
+ * Function called whenever an channel is destroyed.  Should clean up
  * any associated state.
  *
  * @param cls closure (set from GNUNET_MESH_connect)
@@ -159,7 +175,7 @@ inbound_tunnel (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;
@@ -167,6 +183,18 @@ 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 (NULL != mth)
+  {
+    GNUNET_MESH_notify_transmit_ready_cancel (mth);
+    mth = NULL;
+  }
+  if (GNUNET_NO == got_data)
+  {
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
+                                  GNUNET_TIME_UNIT_SECONDS,
+                                  2),
+                                  &do_connect, NULL);
+  }
 }
 
 
@@ -190,11 +218,11 @@ static struct GNUNET_MESH_MessageHandler handlers2[] = {
 
 /**
  * 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
@@ -202,6 +230,7 @@ do_send (void *cls, size_t size, void *buf)
 {
   struct GNUNET_MessageHeader *m = buf;
 
+  mth = NULL;
   if (NULL == buf)
   {
     GNUNET_break (0);
@@ -216,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.
  */
@@ -228,23 +257,23 @@ do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_TESTING_peer_get_identity (me, &id);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
   ch = GNUNET_MESH_channel_create (mesh_peer_1, NULL, &id, 1,
-                                  GNUNET_YES, GNUNET_NO);
-  GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
-                                     GNUNET_TIME_UNIT_FOREVER_REL,
-                                     sizeof (struct GNUNET_MessageHeader),
-                                     &do_send, NULL);
+                                   GNUNET_MESH_OPTION_DEFAULT);
+  mth = GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           sizeof (struct GNUNET_MessageHeader),
+                                           &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)
 {
@@ -256,16 +285,16 @@ run (void *cls,
                                     (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
                                     NULL);
   mesh_peer_1 = GNUNET_MESH_connect (cfg,       /* configuration */
-                                     (void *) 1L,     /* cls */
-                                     &inbound_tunnel,   /* inbound new hndlr */
-                                     &inbound_end,      /* inbound end hndlr */
+                                     (void *) 1L,       /* cls */
+                                     NULL,              /* inbound new hndlr */
+                                     &channel_end,      /* channel end hndlr */
                                      handlers1, /* traffic handlers */
                                      NULL);     /* ports offered */
 
   mesh_peer_2 = GNUNET_MESH_connect (cfg,       /* configuration */
                                      (void *) 2L,     /* cls */
-                                     &inbound_tunnel,   /* inbound new hndlr */
-                                     &inbound_end,      /* inbound end hndlr */
+                                     &inbound_channel,   /* inbound new hndlr */
+                                     &channel_end,      /* channel end hndlr */
                                      handlers2, /* traffic handlers */
                                      ports);     /* ports offered */
   if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
@@ -278,7 +307,10 @@ run (void *cls,
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO MESH :D\n");
   }
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_connect, NULL);
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
+                                  GNUNET_TIME_UNIT_SECONDS,
+                                  2),
+                                &do_connect, NULL);
 }