generalize send API for transport tests
authorChristian Grothoff <christian@grothoff.org>
Thu, 21 Jul 2016 23:10:54 +0000 (23:10 +0000)
committerChristian Grothoff <christian@grothoff.org>
Thu, 21 Jul 2016 23:10:54 +0000 (23:10 +0000)
src/transport/plugin_transport_http_client.c
src/transport/test_transport_api.c
src/transport/test_transport_api_limited_sockets.c
src/transport/test_transport_api_manipulation_send_tcp.c
src/transport/test_transport_api_reliability.c
src/transport/transport-testing-send.c
src/transport/transport-testing.h

index 4e0cde998810ae912345bd10ab22d2ba31a9e901..ceed94af8b2d700e7ddd7bdcde231364bd593e57 100644 (file)
@@ -1061,8 +1061,10 @@ client_send_cb (void *stream,
          "Session %p/request %p: nothing to send, suspending\n",
          s,
          s->put.easyhandle);
-    s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
-        &client_put_disconnect, s);
+    s->put_disconnect_task
+      = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
+                                     &client_put_disconnect,
+                                     s);
     s->put.state = H_PAUSED;
     return CURL_READFUNC_PAUSE;
   }
@@ -1071,7 +1073,9 @@ client_send_cb (void *stream,
   /* calculate how much fits in buffer */
   len = GNUNET_MIN (msg->size - msg->pos,
                     size * nmemb);
-  GNUNET_memcpy (stream, &msg->buf[msg->pos], len);
+  GNUNET_memcpy (stream,
+                &msg->buf[msg->pos],
+                len);
   msg->pos += len;
   if (msg->pos == msg->size)
   {
index 4da4f253324e178f4dc71837a7f82c603721f4e9..d4ae1cf53b27f136680e0ffdceb02ced8fb2d5fb 100644 (file)
@@ -84,9 +84,12 @@ static int
 test (char *argv[],
       int bi_directional)
 {
+  struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
+    .num_messages = 1
+  };
   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
     .connect_continuation = &GNUNET_TRANSPORT_TESTING_large_send,
-    .connect_continuation_cls = &my_ccc,
+    .connect_continuation_cls = &sc,
     .config_file = "test_transport_api_data.conf",
     .rec = &notify_receive,
     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
@@ -96,6 +99,7 @@ test (char *argv[],
   };
 
   ccc = &my_ccc;
+  sc.ccc = ccc;
   if (GNUNET_OK !=
       GNUNET_TRANSPORT_TESTING_main (2,
                                      &GNUNET_TRANSPORT_TESTING_connect_check,
index 25e8550c07626d38436c02d96707c6a26811afe7..1b82ade70945b1e1954d935f8fd0e984c3217e6b 100644 (file)
  */
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
 
-/**
- * How long until we give up on transmitting the message?
- */
-#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
-
-#define MTYPE 12345
-
 #define MAX_FILES 50
 
 
 #if HAVE_SETRLIMIT
 
-static struct GNUNET_TRANSPORT_TransmitHandle *th;
-
 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
 
 
@@ -62,8 +53,10 @@ notify_receive (void *cls,
               "Received message of type %d from peer %s!\n",
               ntohs (message->type),
              GNUNET_i2s (sender));
-  if ((MTYPE == ntohs (message->type)) &&
-      (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
+  if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE ==
+       ntohs (message->type)) &&
+       (sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) ==
+       ntohs (message->size)) )
   {
     ccc->global_ret = GNUNET_OK;
   }
@@ -75,83 +68,27 @@ notify_receive (void *cls,
 }
 
 
-static size_t
-notify_ready (void *cls, size_t size, void *buf)
-{
-  struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
-  struct GNUNET_MessageHeader *hdr;
-
-  th = NULL;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Transmitting message with %u bytes to peer %s\n",
-              (unsigned int) sizeof (struct GNUNET_MessageHeader),
-              GNUNET_i2s (&p->id));
-  GNUNET_assert (size >= 256);
-  if (buf != NULL)
-  {
-    hdr = buf;
-    hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
-    hdr->type = htons (MTYPE);
-  }
-  return sizeof (struct GNUNET_MessageHeader);
-}
-
-
-static void
-notify_disconnect (void *cls,
-                   struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
-                   const struct GNUNET_PeerIdentity *other)
-{
-  GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
-                                           me,
-                                           other);
-  if (NULL != th)
-  {
-    GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
-    th = NULL;
-  }
-}
-
-
-static void
-custom_shutdown (void *cls)
-{
-  if (NULL != th)
-  {
-    GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
-    th = NULL;
-  }
-}
-
-
-static void
-sendtask (void *cls)
-{
-  th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[0]->th,
-                                              &ccc->p[1]->id,
-                                              256,
-                                              TIMEOUT,
-                                               &notify_ready,
-                                              ccc->p[0]);
-}
-
-
 int
 main (int argc, char *argv[])
 {
+  struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
+    .num_messages = 1
+  };
   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
-    .connect_continuation = &sendtask,
+    .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
+    .connect_continuation_cls = &sc,
     .config_file = "test_transport_api_data.conf",
     .rec = &notify_receive,
     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
-    .nd = &notify_disconnect,
-    .shutdown_task = &custom_shutdown,
-    .timeout = TIMEOUT
+    .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
+    .timeout = TIMEOUT,
+    .global_ret = GNUNET_SYSERR
   };
   struct rlimit r_file_old;
   struct rlimit r_file_new;
   int res;
-  
+
+  sc.ccc = &my_ccc;
   res = getrlimit (RLIMIT_NOFILE,
                   &r_file_old);
   r_file_new.rlim_cur = MAX_FILES;
@@ -183,8 +120,9 @@ main (int argc, char *argv[])
 int
 main (int argc, char *argv[])
 {
-  fprintf (stderr, "Cannot run test on this system\n");
-  return 0;
+  fprintf (stderr,
+          "Cannot run test on this system\n");
+  return 77;
 }
 
 #endif
index 902cac6c2ad2f3a95feeb376abaa1c3500918f01..7e73f0b9c9ee8e65916240798205a81c2bb8abc6 100644 (file)
  */
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 
-/**
- * How long until we give up on transmitting the message?
- */
-#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
-
 #define TEST_MESSAGE_SIZE 2600
 
-#define TEST_MESSAGE_TYPE 12345
-
 
 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
 
-static struct GNUNET_TRANSPORT_TransmitHandle *th;
-
-static struct GNUNET_SCHEDULER_Task *send_task;
-
 static int messages_recv;
 
 static struct GNUNET_TIME_Absolute start_normal;
@@ -63,26 +52,49 @@ static struct GNUNET_TIME_Absolute start_delayed;
 static struct GNUNET_TIME_Relative dur_delayed;
 
 
+static size_t
+get_size (unsigned int cnt_down)
+{
+  return TEST_MESSAGE_SIZE;
+}
+
+
 static void
-custom_shutdown (void *cls)
+sendtask (void *cls)
 {
-  if (NULL != th)
+  struct GNUNET_TIME_Relative delay;
+  struct GNUNET_ATS_Properties prop;
+  struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
+    .num_messages = 1,
+    .get_size_cb = &get_size,
+    .ccc = ccc
+  };
+
+  if (0 == messages_recv)
   {
-    GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
-    th = NULL;
+    start_normal = GNUNET_TIME_absolute_get ();
   }
-  if (NULL != send_task)
+  if (1 == messages_recv)
   {
-    GNUNET_SCHEDULER_cancel (send_task);
-    send_task = NULL;
+    memset (&prop, 0, sizeof (prop));
+    delay = GNUNET_TIME_UNIT_SECONDS;
+    GNUNET_TRANSPORT_set_traffic_metric (ccc->p[1]->th,
+                                         &ccc->p[0]->id,
+                                         &prop,
+                                         GNUNET_TIME_UNIT_ZERO,
+                                         delay);
+    prop.distance = 10;
+    GNUNET_TRANSPORT_set_traffic_metric (ccc->p[0]->th,
+                                         &ccc->p[1]->id,
+                                         &prop,
+                                         delay,
+                                         delay);
+    start_delayed = GNUNET_TIME_absolute_get();
   }
+  GNUNET_TRANSPORT_TESTING_simple_send (&sc);
 }
 
 
-static void
-sendtask (void *cls);
-
-
 static void
 notify_receive (void *cls,
                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
@@ -102,8 +114,8 @@ notify_receive (void *cls,
     GNUNET_free (ps);
   }
 
-  if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
-      (TEST_MESSAGE_SIZE == ntohs (message->size)))
+  if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->type)) &&
+       (TEST_MESSAGE_SIZE == ntohs (message->size)) )
   {
     if (GNUNET_NO == ccc->global_ret)
       ccc->global_ret = GNUNET_OK;
@@ -125,8 +137,8 @@ notify_receive (void *cls,
                 messages_recv,
                 GNUNET_STRINGS_relative_time_to_string (dur_normal,
                                                        GNUNET_YES));
-    send_task = GNUNET_SCHEDULER_add_now (&sendtask,
-                                          NULL);
+    GNUNET_SCHEDULER_add_now (&sendtask,
+                             NULL);
   }
   if (1 == messages_recv)
   {
@@ -152,124 +164,18 @@ notify_receive (void *cls,
 }
 
 
-static size_t
-notify_ready (void *cls, size_t size, void *buf)
-{
-  struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
-  struct GNUNET_MessageHeader *hdr;
-
-  th = NULL;
-  if (NULL == buf)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Timeout occurred while waiting for transmit_ready\n");
-    GNUNET_SCHEDULER_shutdown ();
-    ccc->global_ret = 42;
-    return 0;
-  }
-
-  GNUNET_assert (size >= TEST_MESSAGE_SIZE);
-  memset (buf, '\0', TEST_MESSAGE_SIZE);
-  hdr = buf;
-  hdr->size = htons (TEST_MESSAGE_SIZE);
-  hdr->type = htons (TEST_MESSAGE_TYPE);
-
-  {
-    char *ps = GNUNET_strdup (GNUNET_i2s (&ccc->p[1]->id));
-
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Peer %u (`%s') sending message with type %u and size %u bytes to peer %u (`%s')\n",
-                ccc->p[1]->no,
-                ps,
-                ntohs (hdr->type),
-                ntohs (hdr->size),
-                p->no,
-                GNUNET_i2s (&p->id));
-    GNUNET_free (ps);
-  }
-
-  return TEST_MESSAGE_SIZE;
-}
-
-
-static void
-sendtask (void *cls)
-{
-  struct GNUNET_TIME_Relative delay;
-  struct GNUNET_ATS_Properties prop;
-
-  if (NULL == send_task)
-    ccc->global_ret = GNUNET_NO; /* first call! */
-  send_task = NULL;
-  {
-    char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
-
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Sending message from peer %u (`%s') -> peer %u (`%s') !\n",
-                ccc->p[1]->no,
-                GNUNET_i2s (&ccc->p[1]->id),
-                ccc->p[0]->no,
-                receiver_s);
-    GNUNET_free (receiver_s);
-  }
-
-  if (0 == messages_recv)
-  {
-    start_normal = GNUNET_TIME_absolute_get ();
-  }
-  if (1 == messages_recv)
-  {
-    memset (&prop, 0, sizeof (prop));
-    delay = GNUNET_TIME_UNIT_SECONDS;
-    GNUNET_TRANSPORT_set_traffic_metric (ccc->p[1]->th,
-                                         &ccc->p[0]->id,
-                                         &prop,
-                                         GNUNET_TIME_UNIT_ZERO,
-                                         delay);
-    prop.distance = 10;
-    GNUNET_TRANSPORT_set_traffic_metric (ccc->p[0]->th,
-                                         &ccc->p[1]->id,
-                                         &prop,
-                                         delay,
-                                         delay);
-    start_delayed = GNUNET_TIME_absolute_get();
-  }
-  th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
-                                               &ccc->p[0]->id,
-                                               TEST_MESSAGE_SIZE,
-                                               TIMEOUT_TRANSMIT,
-                                               &notify_ready,
-                                               ccc->p[0]);
-}
-
-
-static void
-notify_disconnect (void *cls,
-                   struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
-                   const struct GNUNET_PeerIdentity *other)
-{
-  GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
-                                           me,
-                                           other);
-  if (NULL != th)
-  {
-    GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
-    th = NULL;
-  }
-}
-
-
 int
-main (int argc, char *argv[])
+main (int argc,
+      char *argv[])
 {
   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
     .connect_continuation = &sendtask,
     .config_file = "test_transport_api_data.conf",
     .rec = &notify_receive,
     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
-    .nd = &notify_disconnect,
-    .shutdown_task = &custom_shutdown,
-    .timeout = TIMEOUT
+    .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
+    .timeout = TIMEOUT,
+    .global_ret = GNUNET_NO
   };
 
   ccc = &my_ccc;
index e1ce4da18ddd893d5873db3b4b1f035c7b96c56d..24ad4b81e8f5f00f079a5516f2bf9600f75a57ee 100644 (file)
  */
 #define TOTAL_MSGS (1024 * 3 * FACTOR)
 
-/**
- * Message type of test messages
- */
-#define MTYPE 12345
-
 /**
  * Testcase timeout
  */
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90 * FACTOR)
-
-/**
- * How long until we give up on transmitting the message?
- */
-#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60 * FACTOR)
-
-
-GNUNET_NETWORK_STRUCT_BEGIN
-
-/**
- * Struct for the test message
- */
-struct TestMessage
-{
-  struct GNUNET_MessageHeader header;
-  uint32_t num GNUNET_PACKED;
-};
-GNUNET_NETWORK_STRUCT_END
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 450 * FACTOR)
 
 
 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
 
-static struct GNUNET_TRANSPORT_TransmitHandle *th;
-
 /**
  * Total amount of bytes sent
  */
@@ -88,20 +63,10 @@ static unsigned long long total_bytes;
  */
 static struct GNUNET_TIME_Absolute start_time;
 
-/**
- * No. of message currently scheduled to be send
- */
-static int msg_scheduled;
-
-/**
- * No. of last message sent
- */
-static int msg_sent;
-
 /**
  * No. of last message received
  */
-static int msg_recv;
+static unsigned int msg_recv;
 
 /**
  * Bitmap storing which messages were received
@@ -109,20 +74,41 @@ static int msg_recv;
 static char bitmap[TOTAL_MSGS / 8];
 
 
-static unsigned int
+/**
+ * Get the desired message size for message number @a iter.
+ */
+static size_t
 get_size (unsigned int iter)
 {
-  unsigned int ret;
+  size_t ret;
 
   ret = (iter * iter * iter);
-
 #ifndef LINUX
   /* FreeBSD/OSX etc. Unix DGRAMs do not work
    * with large messages */
   if (0 == strcmp ("unix", test_plugin))
-    return sizeof (struct TestMessage) + (ret % 1024);
+    ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 1024);
 #endif
-  return sizeof (struct TestMessage) + (ret % 60000);
+  ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 60000);
+  return ret;
+}
+
+
+/**
+ * Implementation of the callback for obtaining the
+ * size of messages for transmission.  Counts the total
+ * number of bytes sent as a side-effect.
+ *
+ * @param cnt_down count down from `TOTAL_MSGS - 1`
+ * @return message size of the message
+ */
+static size_t
+get_size_cnt (unsigned int cnt_down)
+{
+  size_t ret = get_size (TOTAL_MSGS - 1 - cnt_down);
+
+  total_bytes += ret;
+  return ret;
 }
 
 
@@ -181,14 +167,8 @@ custom_shutdown (void *cls)
 {
   unsigned long long delta;
   unsigned long long rate;
-  unsigned int i;
   int ok;
 
-  if (NULL != th)
-  {
-    GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
-    th = NULL;
-  }
   /* Calculcate statistics   */
   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
   rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
@@ -197,6 +177,7 @@ custom_shutdown (void *cls)
            rate);
   {
     char *value_name;
+
     GNUNET_asprintf (&value_name,
                      "unreliable_%s",
                      ccc->test_plugin);
@@ -208,7 +189,7 @@ custom_shutdown (void *cls)
   }
 
   ok = 0;
-  for (i = 0; i < TOTAL_MSGS; i++)
+  for (unsigned int i = 0; i < TOTAL_MSGS; i++)
   {
     if (get_bit (bitmap, i) == 0)
     {
@@ -230,14 +211,13 @@ notify_receive (void *cls,
                 const struct GNUNET_MessageHeader *message)
 {
   static int n;
-
   unsigned int s;
   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
-  const struct TestMessage *hdr;
+  const struct GNUNET_TRANSPORT_TESTING_TestMessage *hdr;
 
-  hdr = (const struct TestMessage *) message;
+  hdr = (const struct GNUNET_TRANSPORT_TESTING_TestMessage *) message;
 
-  if (MTYPE != ntohs (message->type))
+  if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (message->type))
     return;
   msg_recv = ntohl (hdr->num);
   s = get_size (ntohl (hdr->num));
@@ -255,19 +235,24 @@ notify_receive (void *cls,
     return;
   }
 
-  memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
-  if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
+  memset (cbuf,
+         ntohl (hdr->num),
+         s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage));
+  if (0 !=
+      memcmp (cbuf,
+             &hdr[1],
+             s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Expected message %u with bits %u, but body did not match\n",
                 ntohl (hdr->num),
-                (unsigned char) n);
+                (unsigned char) ntohl (hdr->num));
     ccc->global_ret = GNUNET_SYSERR;
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
 #if VERBOSE
-  if (ntohl (hdr->num) % 5 == 0)
+  if (0 == ntohl (hdr->num) % 5)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Got message %u of size %u\n",
@@ -290,141 +275,34 @@ notify_receive (void *cls,
   if (n == TOTAL_MSGS)
   {
     /* end testcase with success */
+    ccc->global_ret = GNUNET_OK;
     GNUNET_SCHEDULER_shutdown ();
   }
 }
 
 
-static size_t
-notify_ready (void *cls,
-              size_t size,
-              void *buf)
-{
-  static int n;
-  char *cbuf = buf;
-  struct TestMessage hdr;
-  unsigned int s;
-  unsigned int ret;
-
-  th = NULL;
-  if (NULL == buf)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
-                msg_scheduled,
-                TOTAL_MSGS);
-    GNUNET_SCHEDULER_shutdown ();
-    ccc->global_ret = 42;
-    return 0;
-  }
-  ret = 0;
-  s = get_size (n);
-  GNUNET_assert (size >= s);
-  GNUNET_assert (buf != NULL);
-  GNUNET_assert (n < TOTAL_MSGS);
-  cbuf = buf;
-  do
-  {
-    GNUNET_assert (n < TOTAL_MSGS);
-    hdr.header.size = htons (s);
-    hdr.header.type = htons (MTYPE);
-    hdr.num = htonl (n);
-    msg_sent = n;
-    GNUNET_memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
-    ret += sizeof (struct TestMessage);
-    memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
-    ret += s - sizeof (struct TestMessage);
-
-#if VERBOSE
-    if (0 == n % 5000)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Sending message %u of size %u\n",
-                  n,
-                  s);
-    }
-#endif
-    n++;
-    s = get_size (n);
-    if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
-      break;                    /* sometimes pack buffer full, sometimes not */
-  }
-  while ((size - ret >= s) && (n < TOTAL_MSGS));
-  if (n < TOTAL_MSGS)
-  {
-    th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
-                                                 &ccc->p[0]->id,
-                                                 s,
-                                                 TIMEOUT_TRANSMIT,
-                                                 &notify_ready,
-                                                 NULL);
-    msg_scheduled = n;
-  }
-  else
-  {
-    FPRINTF (stderr,
-             "%s",
-             "\n");
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "All messages scheduled to be sent\n");
-  }
-  if (0 == n % 5000)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Returning total message block of size %u\n",
-                ret);
-  }
-  total_bytes += ret;
-  return ret;
-}
-
-
-static void
-sendtask (void *cls)
-{
-  start_time = GNUNET_TIME_absolute_get ();
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Starting to send %u messages\n",
-              TOTAL_MSGS);
-  th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
-                                               &ccc->p[0]->id,
-                                               get_size (0),
-                                               TIMEOUT_TRANSMIT,
-                                               &notify_ready,
-                                               NULL);
-}
-
-
-static void
-notify_disconnect (void *cls,
-                   struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
-                   const struct GNUNET_PeerIdentity *other)
-{
-  GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
-                                           me,
-                                           other);
-  if (NULL != th)
-  {
-    GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
-    th = NULL;
-  }
-}
-
-
 int
 main (int argc, char *argv[])
 {
+  struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
+    .num_messages = TOTAL_MSGS,
+    .get_size_cb = &get_size_cnt
+  };
   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
-    .connect_continuation = &sendtask,
+    .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
+    .connect_continuation_cls = &sc,
     .config_file = "test_transport_api_data.conf",
     .rec = &notify_receive,
     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
-    .nd = &notify_disconnect,
+    .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
     .shutdown_task = &custom_shutdown,
-    .timeout = TIMEOUT
+    .timeout = TIMEOUT,
+    .global_ret = GNUNET_SYSERR
   };
 
   ccc = &my_ccc;
+  sc.ccc = ccc;
+  start_time = GNUNET_TIME_absolute_get ();
   if (GNUNET_OK !=
       GNUNET_TRANSPORT_TESTING_main (2,
                                      &GNUNET_TRANSPORT_TESTING_connect_check,
index 20d1f2fbc8858645770328eca2bae807d33a740c..60fed23a694ab1a362c1545b07be26e9e8fc4e96 100644 (file)
@@ -58,7 +58,7 @@ notify_ready (void *cls,
   GNUNET_assert (size >= msize);
   if (NULL != buf)
   {
-    memset (buf, '\0', msize);
+    memset (buf, sj->num, msize);
     test = buf;
     test->header.size = htons (msize);
     test->header.type = htons (sj->mtype);
@@ -79,6 +79,9 @@ notify_ready (void *cls,
                 GNUNET_i2s (&receiver->id));
     GNUNET_free (ps);
   }
+  if (NULL != sj->cont)
+    GNUNET_SCHEDULER_add_now (sj->cont,
+                             sj->cont_cls);
   GNUNET_free (sj);
   return msize;
 }
@@ -107,6 +110,8 @@ find_cr (void *cls,
  * @param mtype message type to use
  * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)`
  * @param num unique message number
+ * @param cont continuation to call after transmission
+ * @param cont_cls closure for @a cont
  * @return #GNUNET_OK if message was queued,
  *         #GNUNET_NO if peers are not connected
  *         #GNUNET_SYSERR if @a msize is illegal
@@ -116,7 +121,9 @@ GNUNET_TRANSPORT_TESTING_send (struct GNUNET_TRANSPORT_TESTING_PeerContext *send
                               struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
                               uint16_t mtype,
                               uint16_t msize,
-                              uint32_t num)
+                              uint32_t num,
+                              GNUNET_SCHEDULER_TaskCallback cont,
+                              void *cont_cls)
 {
   struct GNUNET_TRANSPORT_TESTING_Handle *tth = sender->tth;
   struct TRANSPORT_TESTING_SendJob *sj;
@@ -147,6 +154,8 @@ GNUNET_TRANSPORT_TESTING_send (struct GNUNET_TRANSPORT_TESTING_PeerContext *send
   sj->num = num;
   sj->sender = sender;
   sj->receiver = receiver;
+  sj->cont = cont;
+  sj->cont_cls = cont_cls;
   sj->mtype = mtype;
   sj->msize = msize;
   GNUNET_CONTAINER_DLL_insert (tth->sj_head,
@@ -180,10 +189,15 @@ GNUNET_TRANSPORT_TESTING_send (struct GNUNET_TRANSPORT_TESTING_PeerContext *send
  *
  * @param ccc context which should contain at least two peers, the
  *        first two of which should be currently connected
+ * @param size desired message size
+ * @param cont continuation to call after transmission
+ * @param cont_cls closure for @a cont
  */
 static void
 do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
-        uint16_t size)
+        uint16_t size,
+        GNUNET_SCHEDULER_TaskCallback cont,
+        void *cont_cls)
 {
   int ret;
 
@@ -192,7 +206,9 @@ do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
                                       ccc->p[1],
                                       GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
                                       size,
-                                      ccc->send_num_gen++);
+                                      ccc->send_num_gen++,
+                                      cont,
+                                      cont_cls);
   GNUNET_assert (GNUNET_SYSERR != ret);
   if (GNUNET_NO == ret)
   {
@@ -214,10 +230,28 @@ do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
 void
 GNUNET_TRANSPORT_TESTING_simple_send (void *cls)
 {
-  struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
+  struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
+  int done;
+  size_t msize;
 
-  do_send (ccc,
-          sizeof (struct GNUNET_MessageHeader));
+  if (0 < sc->num_messages)
+  {
+    sc->num_messages--;
+    done = (0 == sc->num_messages);
+  }
+  else
+  {
+    done = 0; /* infinite loop */
+  }
+  msize = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage);
+  if (NULL != sc->get_size_cb)
+    msize = sc->get_size_cb (sc->num_messages);
+  /* if this was the last message, call the continuation,
+     otherwise call this function again */
+  do_send (sc->ccc,
+          msize,
+          done ? sc->cont : &GNUNET_TRANSPORT_TESTING_simple_send,
+          done ? sc->cont_cls : sc);
 }
 
 
@@ -232,10 +266,28 @@ GNUNET_TRANSPORT_TESTING_simple_send (void *cls)
 void
 GNUNET_TRANSPORT_TESTING_large_send (void *cls)
 {
-  struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
+  struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
+  int done;
+  size_t msize;
 
-  do_send (ccc,
-          2600);
+  if (0 < sc->num_messages)
+  {
+    sc->num_messages--;
+    done = (0 == sc->num_messages);
+  }
+  else
+  {
+    done = 0; /* infinite loop */
+  }
+  msize = 2600;
+  if (NULL != sc->get_size_cb)
+    msize = sc->get_size_cb (sc->num_messages);
+  /* if this was the last message, call the continuation,
+     otherwise call this function again */
+  do_send (sc->ccc,
+          msize,
+          done ? sc->cont : &GNUNET_TRANSPORT_TESTING_large_send,
+          done ? sc->cont_cls : sc);
 }
 
 /* end of transport-testing-send.c */
index 42daa45962143e3c6c96f8e742a85b7b6fe14eb9..dc26950cb63af1fc23e42160624064788e3f23cd 100644 (file)
@@ -255,6 +255,16 @@ struct TRANSPORT_TESTING_SendJob
    */
   struct GNUNET_TRANSPORT_TransmitHandle *th;
 
+  /**
+   * Function to call upon completion.
+   */
+  GNUNET_SCHEDULER_TaskCallback cont;
+
+  /**
+   * Closure for @e cont.
+   */
+  void *cont_cls;
+  
   /**
    * Number of the message.
    */ 
@@ -774,6 +784,8 @@ GNUNET_TRANSPORT_TESTING_main_ (const char *argv0,
  * @param mtype message type to use
  * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)`
  * @param num unique message number
+ * @param cont continuation to call after transmission
+ * @param cont_cls closure for @a cont
  * @return #GNUNET_OK if message was queued,
  *         #GNUNET_NO if peers are not connected
  *         #GNUNET_SYSERR if @a msize is illegal
@@ -783,7 +795,9 @@ GNUNET_TRANSPORT_TESTING_send (struct GNUNET_TRANSPORT_TESTING_PeerContext *send
                               struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
                               uint16_t mtype,
                               uint16_t msize,
-                              uint32_t num);
+                              uint32_t num,
+                              GNUNET_SCHEDULER_TaskCallback cont,
+                              void *cont_cls);
 
 
 /**
@@ -807,11 +821,49 @@ struct GNUNET_TRANSPORT_TESTING_TestMessage
 GNUNET_NETWORK_STRUCT_END
 
 
+/**
+ * Type of the closure argument to pass to
+ * #GNUNET_TRANSPORT_TESTING_simple_send() and
+ * #GNUNET_TRANSPORT_TESTING_large_send().
+ */
+struct GNUNET_TRANSPORT_TESTING_SendClosure
+{
+  /**
+   * Context for the transmission.
+   */
+  struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
+
+  /**
+   * Function that returns the desired message size. Overrides
+   * the message size, can be NULL in which case the message
+   * size is the default.
+   */
+  size_t (*get_size_cb)(unsigned int n);
+  
+  /**
+   * Number of messages to be transmitted in a loop.
+   * Use zero for "forever" (until external shutdown).
+   */
+  unsigned int num_messages;
+  
+  /**
+   * Function to call after all transmissions, can be NULL.
+   */
+  GNUNET_SCHEDULER_TaskCallback cont;
+
+  /**
+   * Closure for @e cont.
+   */
+  void *cont_cls;
+  
+};
+
+
 /**
  * Task that sends a minimalistic test message from the 
  * first peer to the second peer.
  *
- * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
+ * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
  *        which should contain at least two peers, the first two
  *        of which should be currently connected
  */
@@ -829,7 +881,7 @@ GNUNET_TRANSPORT_TESTING_simple_send (void *cls);
  * Task that sends a large test message from the 
  * first peer to the second peer.
  *
- * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
+ * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
  *        which should contain at least two peers, the first two
  *        of which should be currently connected
  */