Don't pass NULL to destroy_route
[oweals/gnunet.git] / src / cadet / test_cadet_single.c
index 3780c745ce6b2f6b5fe488cbb5200bf680afd7da..b45b0af5dfb8472e08ff3d3a7c8de591c635fbe9 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -43,12 +43,16 @@ static struct GNUNET_CADET_Channel *ch2;
 
 static int result;
 
-static GNUNET_SCHEDULER_TaskIdentifier abort_task;
+static struct GNUNET_SCHEDULER_Task *abort_task;
 
-static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
+static struct GNUNET_SCHEDULER_Task *connect_task;
 
 static unsigned int repetition;
 
+static struct GNUNET_CADET_TransmitHandle *nth;
+
+static struct GNUNET_CADET_Port *port;
+
 
 /* forward declaration */
 static size_t
@@ -59,19 +63,37 @@ do_send (void *cls, size_t size, void *buf);
  * Shutdown nicely
  */
 static void
-do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_shutdown (void *cls)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
-  if (GNUNET_SCHEDULER_NO_TASK != abort_task)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "shutdown\n");
+  if (NULL != port)
+  {
+    GNUNET_CADET_close_port (port);
+    port = NULL;
+  }
+  if (NULL != nth)
+  {
+    GNUNET_CADET_notify_transmit_ready_cancel (nth);
+    nth = NULL;
+  }
+  if (NULL != abort_task)
   {
     GNUNET_SCHEDULER_cancel (abort_task);
+    abort_task = NULL;
+  }
+  if (NULL != connect_task)
+  {
+    GNUNET_SCHEDULER_cancel (connect_task);
+    connect_task = NULL;
   }
   if (NULL != ch1)
   {
     GNUNET_CADET_channel_destroy (ch1);
+    ch1 = NULL;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Disconnect clients\n");
   if (NULL != cadet)
   {
     GNUNET_CADET_disconnect (cadet);
@@ -88,27 +110,12 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Something went wrong and timed out. Kill everything and set error flag
  */
 static void
-do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_abort (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
   result = GNUNET_SYSERR;
-  abort_task = GNUNET_SCHEDULER_NO_TASK;
-  if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
-  {
-    GNUNET_SCHEDULER_cancel (shutdown_task);
-    shutdown_task = GNUNET_SCHEDULER_NO_TASK;
-  }
-  do_shutdown (cls, 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);
+  abort_task = NULL;
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
@@ -119,35 +126,38 @@ finish (void)
  * @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)
+ * @return #GNUNET_OK to keep the connection open,
+ *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
-data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
+data_callback (void *cls,
+               struct GNUNET_CADET_Channel *channel,
                void **channel_ctx,
                const struct GNUNET_MessageHeader *message)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Data callback! Repetition %u/%u\n",
               repetition, REPETITIONS);
-  repetition = repetition + 1;
+  repetition++;
   if (repetition < REPETITIONS)
   {
     struct GNUNET_CADET_Channel *my_channel;
-    if (repetition % 2 == 0)
+    if (0 == repetition % 2)
       my_channel = ch1;
     else
       my_channel = ch2;
-    GNUNET_CADET_notify_transmit_ready (my_channel, GNUNET_NO,
-                                       GNUNET_TIME_UNIT_FOREVER_REL,
-                                       sizeof (struct GNUNET_MessageHeader)
-                                       + DATA_SIZE,
-                                       &do_send, NULL);
+    nth = GNUNET_CADET_notify_transmit_ready (my_channel,
+                                              GNUNET_NO,
+                                              GNUNET_TIME_UNIT_FOREVER_REL,
+                                              sizeof (struct GNUNET_MessageHeader)
+                                              + DATA_SIZE,
+                                              &do_send, NULL);
     GNUNET_CADET_receive_done (channel);
     return GNUNET_OK;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All data OK. Destroying channel.\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "All data OK. Destroying channel.\n");
+  GNUNET_assert (NULL == nth);
   GNUNET_CADET_channel_destroy (ch1);
   ch1 = NULL;
   return GNUNET_OK;
@@ -167,13 +177,15 @@ data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
  *         (can be NULL -- that's not an error)
  */
 static void *
-inbound_channel (void *cls, struct GNUNET_CADET_Channel *channel,
-                const struct GNUNET_PeerIdentity *initiator,
-                uint32_t port, enum GNUNET_CADET_ChannelOption options)
+inbound_channel (void *cls,
+                 struct GNUNET_CADET_Channel *channel,
+                 const struct GNUNET_PeerIdentity *initiator,
+                 const struct GNUNET_HashCode *port,
+                 enum GNUNET_CADET_ChannelOption options)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "received incoming channel on port %u\n",
-              port);
+              "received incoming channel on port %s\n",
+              GNUNET_h2s (port));
   ch2 = channel;
   return NULL;
 }
@@ -189,21 +201,28 @@ inbound_channel (void *cls, struct GNUNET_CADET_Channel *channel,
  *                   with the channel is stored
  */
 static void
-channel_end (void *cls, const struct GNUNET_CADET_Channel *channel,
+channel_end (void *cls,
+             const struct GNUNET_CADET_Channel *channel,
              void *channel_ctx)
 {
   long id = (long) cls;
 
+  nth = NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "incoming channel closed at peer %ld\n",
               id);
-  if (REPETITIONS == repetition && channel == ch2)
+  if ( (REPETITIONS == repetition) &&
+       (channel == ch2) )
   {
-    ch2 = NULL;
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "everything fine! finishing!\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "everything fine! finishing!\n");
     result = GNUNET_OK;
-    finish ();
+    GNUNET_SCHEDULER_shutdown ();
   }
+  if (channel == ch2)
+    ch2 = NULL;
+  if (channel == ch1)
+    ch1 = NULL;
 }
 
 
@@ -222,7 +241,6 @@ static struct GNUNET_CADET_MessageHandler handlers1[] = {
  * @param cls Closure (unused).
  * @param size Buffer size.
  * @param buf Buffer to fill.
- *
  * @return size of test packet.
  */
 static size_t
@@ -230,13 +248,14 @@ do_send (void *cls, size_t size, void *buf)
 {
   struct GNUNET_MessageHeader *m = buf;
 
+  nth = NULL;
   if (NULL == buf)
   {
     GNUNET_break (0);
     result = GNUNET_SYSERR;
     return 0;
   }
-  m->size = htons (sizeof (struct GNUNET_MessageHeader));
+  m->size = htons (sizeof (struct GNUNET_MessageHeader) + DATA_SIZE);
   m->type = htons (1);
   memset (&m[1], 0, DATA_SIZE);
   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader) + DATA_SIZE);
@@ -247,24 +266,24 @@ do_send (void *cls, size_t size, void *buf)
  * 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)
+do_connect (void *cls)
 {
   struct GNUNET_PeerIdentity id;
   size_t size = sizeof (struct GNUNET_MessageHeader) + DATA_SIZE;
 
-  if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
-    return;
-
+  connect_task = NULL;
   GNUNET_TESTING_peer_get_identity (me, &id);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
-  ch1 = GNUNET_CADET_channel_create (cadet, NULL, &id, 1,
-                                    GNUNET_CADET_OPTION_DEFAULT);
-  GNUNET_CADET_notify_transmit_ready (ch1, GNUNET_NO,
-                                     GNUNET_TIME_UNIT_FOREVER_REL,
-                                     size, &do_send, NULL);
+  ch1 = GNUNET_CADET_channel_create (cadet, NULL, &id, GC_u2h (1),
+                                     GNUNET_CADET_OPTION_DEFAULT);
+  nth = GNUNET_CADET_notify_transmit_ready (ch1,
+                                            GNUNET_NO,
+                                            GNUNET_TIME_UNIT_FOREVER_REL,
+                                            size,
+                                            &do_send,
+                                            NULL);
 }
 
 
@@ -280,31 +299,33 @@ run (void *cls,
      const struct GNUNET_CONFIGURATION_Handle *cfg,
      struct GNUNET_TESTING_Peer *peer)
 {
-  static uint32_t ports[] = {1, 0};
-
   me = peer;
+  GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
   abort_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
                                     (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
                                     NULL);
   cadet = GNUNET_CADET_connect (cfg,       /* configuration */
                               (void *) 1L,     /* cls */
-                              &inbound_channel,   /* inbound new hndlr */
                               &channel_end,      /* inbound end hndlr */
-                              handlers1, /* traffic handlers */
-                              ports);     /* ports offered */
+                              handlers1); /* traffic handlers */
+  port = GNUNET_CADET_open_port (cadet,
+                                 GC_u2h (1),
+                                 &inbound_channel,
+                                 (void *) 1L);
+
 
   if (NULL == cadet)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to cadet :(\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Couldn't connect to cadet :(\n");
     result = GNUNET_SYSERR;
     return;
   }
-  else
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO CADET :D\n");
-  }
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_connect, NULL);
+  connect_task
+    = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                   &do_connect,
+                                   NULL);
 }
 
 
@@ -312,17 +333,21 @@ run (void *cls,
  * Main
  */
 int
-main (int argc, char *argv[])
+main (int argc,
+      char *argv[])
 {
   result = GNUNET_NO;
   if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
                                     "test_cadet.conf",
                                     &run, NULL))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "run failed\n");
     return 2;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Final result: %d\n",
+              result);
   return (result == GNUNET_OK) ? 0 : 1;
 }