-docu, style fixes
[oweals/gnunet.git] / src / fragmentation / test_fragmentation.c
index 14901cf68f231d7c5341532afe9103c8dffd2f1a..50779c98b9fa16faae62be9c6d40415f2debb170 100644 (file)
 #include "platform.h"
 #include "gnunet_fragmentation_lib.h"
 
-#define VERBOSE GNUNET_EXTRA_LOGGING
-
 #define DETAILS GNUNET_NO
 
 /**
  * Number of messages to transmit (note: each uses ~32k memory!)
  */
-#define NUM_MSGS 5000
+#define NUM_MSGS 500
 
 /**
  * MTU to force on fragmentation (must be > 1k + 12)
@@ -42,7 +40,7 @@
 /**
  * Simulate dropping of 1 out of how many messages? (must be > 1)
  */
-#define DROPRATE 10
+#define DROPRATE 5
 
 static int ret = 1;
 
@@ -62,6 +60,27 @@ static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
 
 static struct GNUNET_FRAGMENT_Context *frags[NUM_MSGS];
 
+static struct GNUNET_SCHEDULER_Task * shutdown_task;
+
+static void
+do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  unsigned int i;
+
+  ret = 0;
+  shutdown_task = NULL;
+  GNUNET_DEFRAGMENT_context_destroy (defrag);
+  defrag = NULL;
+  for (i = 0; i < NUM_MSGS; i++)
+  {
+    if (frags[i] == NULL)
+      continue;
+    GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
+    frags[i] = NULL;
+  }
+}
+
+
 static void
 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
 {
@@ -70,7 +89,7 @@ proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
   const char *buf;
 
 #if DETAILS
-  fprintf (stderr, "!");        /* message complete, good! */
+  FPRINTF (stderr, "%s",  "!");        /* message complete, good! */
 #endif
   buf = (const char *) hdr;
   for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
@@ -78,21 +97,13 @@ proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
   total++;
 #if ! DETAILS
   if (0 == (total % (NUM_MSGS / 100)))
-    fprintf (stderr, ".");
+    FPRINTF (stderr, "%s",  ".");
 #endif
   /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
-  if (total >= NUM_MSGS - (NUM_MSGS / 10))
+  if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
   {
-    ret = 0;
-    GNUNET_DEFRAGMENT_context_destroy (defrag);
-    defrag = NULL;
-    for (i = 0; i < NUM_MSGS; i++)
-    {
-      if (frags[i] == NULL)
-        continue;
-      GNUNET_FRAGMENT_context_destroy (frags[i]);
-      frags[i] = NULL;
-    }
+    if (NULL == shutdown_task)
+      shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
   }
 }
 
@@ -119,9 +130,9 @@ proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
     if (ret == GNUNET_OK)
     {
 #if DETAILS
-      fprintf (stderr, "@");    /* good ACK */
+      FPRINTF (stderr, "%s",  "@");    /* good ACK */
 #endif
-      GNUNET_FRAGMENT_context_destroy (frags[i]);
+      GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
       frags[i] = NULL;
       acks++;
       return;
@@ -129,14 +140,14 @@ proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
     if (ret == GNUNET_NO)
     {
 #if DETAILS
-      fprintf (stderr, "@");    /* good ACK */
+      FPRINTF (stderr, "%s",  "@");    /* good ACK */
 #endif
       acks++;
       return;
     }
   }
 #if DETAILS
-  fprintf (stderr, "_");        /* BAD: ack that nobody feels responsible for... */
+  FPRINTF (stderr, "%s",  "_");        /* BAD: ack that nobody feels responsible for... */
 #endif
 }
 
@@ -158,21 +169,21 @@ proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
   }
   if (NULL == defrag)
   {
-    fprintf (stderr, "E");      /* Error: frag after shutdown!? */
+    FPRINTF (stderr, "%s",  "E");      /* Error: frag after shutdown!? */
     return;
   }
   ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
   if (ret == GNUNET_NO)
   {
 #if DETAILS
-    fprintf (stderr, "?");      /* duplicate fragment */
+    FPRINTF (stderr, "%s",  "?");      /* duplicate fragment */
 #endif
     dups++;
   }
   else if (ret == GNUNET_OK)
   {
 #if DETAILS
-    fprintf (stderr, ".");      /* good fragment */
+    FPRINTF (stderr, "%s",  ".");      /* good fragment */
 #endif
     fragc++;
   }
@@ -202,7 +213,9 @@ run (void *cls, char *const *args, const char *cfgfile,
         htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
     frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
                                                MTU, &trackers[i],
-                                               GNUNET_TIME_UNIT_SECONDS, msg,
+                                               GNUNET_TIME_UNIT_MILLISECONDS,
+                                               GNUNET_TIME_UNIT_SECONDS,
+                                              msg,
                                                &proc_frac, &frags[i]);
   }
 }
@@ -219,29 +232,21 @@ main (int argc, char *argv[])
     "-c",
     "test_fragmentation_data.conf",
     "-L",
-#if VERBOSE
-    "DEBUG",
-#else
     "WARNING",
-#endif
     NULL
   };
   unsigned int i;
 
   GNUNET_log_setup ("test-fragmentation",
-#if VERBOSE
-                    "DEBUG",
-#else
                     "WARNING",
-#endif
                     NULL);
   for (i = 0; i < NUM_MSGS; i++)
-    GNUNET_BANDWIDTH_tracker_init (&trackers[i],
+    GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
                                    GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
                                    100);
   GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
                       &run, NULL);
-  fprintf (stderr,
+  FPRINTF (stderr,
            "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
            fragc, dups, acks, ack_drops);
   return ret;