-indentation
[oweals/gnunet.git] / src / testbed / testbed_logger_api.c
index 56290221860ab645d9c0b82084a237305d4fc7a7..dab9d660038d3afbb3bb59b84ad531857007fb87 100644 (file)
  */
 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
 
+/**
+ * The size of the buffer we fill before sending out the message
+ */
+#define BUFFER_SIZE GNUNET_SERVER_MAX_MESSAGE_SIZE
 
 /**
  * The message queue for sending messages to the controller service
@@ -277,7 +281,7 @@ queue_message (struct GNUNET_TESTBED_LOGGER_Handle *h,
 
   type = ntohs (msg->type);
   size = ntohs (msg->size);
-  mq = GNUNET_malloc (sizeof (struct MessageQueue));
+  mq = GNUNET_new (struct MessageQueue);
   mq->msg = msg;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Queueing message of type %u, size %u for sending\n", type,
@@ -333,7 +337,7 @@ GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
   client = GNUNET_CLIENT_connect ("testbed-logger", cfg);
   if (NULL == client)
     return NULL;
-  h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_LOGGER_Handle));
+  h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
   h->client = client;
   return h;
 }
@@ -348,15 +352,21 @@ void
 GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
 {
   struct MessageQueue *mq;
+  unsigned int lost;
 
   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
+  lost = 0;
   while (NULL != (mq = h->mq_head))
   {
     GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
     GNUNET_free (mq->msg);
     GNUNET_free (mq);
+    lost++;
   }
+  if (0 != lost)
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Cleaning up %u unsent logger message[s]\n",
+         lost);
   GNUNET_CLIENT_disconnect (h->client);
   GNUNET_free (h);
 }
@@ -379,22 +389,24 @@ GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
 
   GNUNET_assert (0 != size);
   GNUNET_assert (NULL != data);
-  GNUNET_assert (size < (GNUNET_SERVER_MAX_MESSAGE_SIZE
-                         - sizeof (struct GNUNET_MessageHeader)));
+  GNUNET_assert (size <= (BUFFER_SIZE - sizeof (struct GNUNET_MessageHeader)));
   fit_size = sizeof (struct GNUNET_MessageHeader) + h->bs + size;
-  if ( GNUNET_SERVER_MAX_MESSAGE_SIZE < fit_size )
+  if ( BUFFER_SIZE < fit_size )
     dispatch_buffer (h);
   if (NULL == h->buf)
   {
     h->buf = GNUNET_malloc (size);
     h->bs = size;
     memcpy (h->buf, data, size);
-    return;
+    goto dispatch_ready;
   }
   h->buf = GNUNET_realloc (h->buf, h->bs + size);
   memcpy (h->buf + h->bs, data, size);
   h->bs += size;
-  return;
+
+ dispatch_ready:
+  if (BUFFER_SIZE == fit_size)
+    dispatch_buffer (h);
 }