Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / server_nc.c
index 61775c6a1df5f7fbeaa3cbeb72e333f826303dc7..3bab691e9b904550615dd0457adc5ab1e5296c4b 100644 (file)
@@ -33,6 +33,8 @@
 #include "gnunet_time_lib.h"
 
 
+#define DEBUG_SERVER_NC GNUNET_NO
+
 /**
  * Entry in list of messages pending to be transmitted.
  */
@@ -162,6 +164,11 @@ handle_client_disconnect (void *cls,
     }
   if (pos == NULL)
     return;
+#if DEBUG_SERVER_NC
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Client disconnected, cleaning up %u messages in NC queue\n",
+             pos->num_pending);
+#endif
   if (prev == NULL)
     nc->clients = pos->next;
   else
@@ -220,8 +227,8 @@ GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationCon
   while (NULL != (pos = nc->clients))
     {
       nc->clients = pos->next;
-      GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
       GNUNET_SERVER_client_drop (pos->client); 
+      GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
       while (NULL != (pml = pos->pending_head))
        {
          pos->pending_head = pml->next;
@@ -283,18 +290,28 @@ transmit_message (void *cls,
   if (buf == NULL)
     {
       /* 'cl' should be freed via disconnect notification shortly */
+#if DEBUG_SERVER_NC
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Failed to transmit message from NC queue to client\n");
+#endif
       return 0;
     }
   ret = 0;
   while (cl->pending_head != NULL)
     {
       pml = cl->pending_head;
-      cl->pending_head = pml->next;
-      if (pml->next == NULL)
-       cl->pending_tail = NULL;
       msize = ntohs (pml->msg->size);
       if (size < msize)
        break;
+      cl->pending_head = pml->next;
+      if (pml->next == NULL)
+       cl->pending_tail = NULL;
+#if DEBUG_SERVER_NC
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Copying message of type %u and size %u from pending queue to transmission buffer\n",
+                 ntohs (pml->msg->type),
+                 msize);
+#endif
       memcpy (&cbuf[ret], pml->msg, msize);
       ret += msize;
       size -= msize;
@@ -302,11 +319,18 @@ transmit_message (void *cls,
       cl->num_pending--;
     }
   if (cl->pending_head != NULL)    
-    cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
-                                                 ntohs (cl->pending_head->msg->size),
-                                                 GNUNET_TIME_UNIT_FOREVER_REL,
-                                                 &transmit_message,
-                                                 cl);
+    {
+#if DEBUG_SERVER_NC
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Have %u messages left in NC queue, will try transmission again\n",
+                 cl->num_pending);
+#endif
+      cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
+                                                   ntohs (cl->pending_head->msg->size),
+                                                   GNUNET_TIME_UNIT_FOREVER_REL,
+                                                   &transmit_message,
+                                                   cl);
+    }
   return ret;
 }
 
@@ -330,7 +354,14 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
 
   if ( (client->num_pending > nc->queue_length) &&
        (GNUNET_YES == can_drop) )
-    return; /* drop! */
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                 "Dropping message of type %u and size %u due to full queue (%u entries)\n",
+                 ntohs (msg->type),
+                 ntohs (msg->size),
+                 (unsigned int) nc->queue_length);
+      return; /* drop! */
+    }
   if (client->num_pending > nc->queue_length)
     {
       /* FIXME: consider checking for other messages in the
@@ -340,7 +371,14 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
   size = ntohs (msg->size);
   pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
   pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
-  pml->can_drop = can_drop;
+  pml->can_drop = can_drop; 
+#if DEBUG_SERVER_NC
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
+             ntohs (msg->type),
+             ntohs (msg->size),
+             (unsigned int) nc->queue_length);
+#endif
   memcpy (&pml[1], msg, size);
   /* append */
   if (client->pending_tail != NULL)