-dce
[oweals/gnunet.git] / src / util / server_nc.c
index 66c69123992c0f0375b3a6aeb7d1c4dcf24199e1..6e0181ecbfa5d1e05b0ef0e1229595353b3f4e24 100644 (file)
@@ -21,7 +21,7 @@
 /**
  * @file util/server_nc.c
  * @brief convenience functions for transmission of
- *        a notification stream 
+ *        a notification stream
  * @author Christian Grothoff
  */
 
@@ -33,8 +33,8 @@
 #include "gnunet_server_lib.h"
 #include "gnunet_time_lib.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
-#define DEBUG_SERVER_NC GNUNET_NO
 
 /**
  * Entry in list of messages pending to be transmitted.
@@ -44,12 +44,12 @@ struct PendingMessageList
 
   /**
    * This is a doubly-linked list.
-   */ 
+   */
   struct PendingMessageList *next;
 
   /**
    * This is a doubly-linked list.
-   */ 
+   */
   struct PendingMessageList *prev;
 
   /**
@@ -60,7 +60,7 @@ struct PendingMessageList
 
   /**
    * Can this message be dropped?
-   */ 
+   */
   int can_drop;
 
 };
@@ -73,12 +73,17 @@ struct ClientList
 {
 
   /**
-   * This is a linked list.
-   */ 
+   * This is a doubly linked list.
+   */
   struct ClientList *next;
 
   /**
-   * Overall context this client belongs to. 
+   * This is a doubly linked list.
+   */
+  struct ClientList *prev;
+
+  /**
+   * Overall context this client belongs to.
    */
   struct GNUNET_SERVER_NotificationContext *nc;
 
@@ -90,16 +95,16 @@ struct ClientList
   /**
    * Handle for pending transmission request to the client (or NULL).
    */
-  struct GNUNET_CONNECTION_TransmitHandle *th;
+  struct GNUNET_SERVER_TransmitHandle *th;
 
   /**
    * Head of linked list of requests queued for transmission.
-   */ 
+   */
   struct PendingMessageList *pending_head;
 
   /**
    * Tail of linked list of requests queued for transmission.
-   */ 
+   */
   struct PendingMessageList *pending_tail;
 
   /**
@@ -127,9 +132,14 @@ struct GNUNET_SERVER_NotificationContext
   struct GNUNET_SERVER_Handle *server;
 
   /**
-   * List of clients receiving notifications.
+   * Head of list of clients receiving notifications.
+   */
+  struct ClientList *clients_head;
+
+  /**
+   * Tail of list of clients receiving notifications.
    */
-  struct ClientList *clients;
+  struct ClientList *clients_tail;
 
   /**
    * Maximum number of optional messages to queue per client.
@@ -146,52 +156,41 @@ struct GNUNET_SERVER_NotificationContext
  * @param client handle of client that disconnected
  */
 static void
-handle_client_disconnect (void *cls,
-                         struct GNUNET_SERVER_Client *client)
+handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
 {
   struct GNUNET_SERVER_NotificationContext *nc = cls;
   struct ClientList *pos;
-  struct ClientList *prev;
   struct PendingMessageList *pml;
 
-  if (client == NULL)
-    {
-      nc->server = NULL;
-      return;
-    }
-  prev = NULL;
-  pos = nc->clients;
-  while (NULL != pos)
-    {
-      if (pos->client == client)
-       break;
-      prev = pos;
-      pos = pos->next;
-    }
-  if (pos == NULL)
+  if (NULL == client)
+  {
+    nc->server = 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
-    prev->next = pos->next;
+  }
+  for (pos = nc->clients_head; NULL != pos; pos = pos->next)
+    if (pos->client == client)
+      break;
+  if (NULL == pos)
+    return;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Client disconnected, cleaning up %u messages in NC queue\n",
+       pos->num_pending);
+  GNUNET_CONTAINER_DLL_remove (nc->clients_head,
+                              nc->clients_tail,
+                              pos);
   while (NULL != (pml = pos->pending_head))
-    {
-      GNUNET_CONTAINER_DLL_remove (pos->pending_head,
-                                  pos->pending_tail,
-                                  pml);
-      GNUNET_free (pml);
-    }
+  {
+    GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
+    GNUNET_free (pml);
+    pos->num_pending--;
+  }
+  if (NULL != pos->th)
+  {
+    GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
+    pos->th = NULL;
+  }
   GNUNET_SERVER_client_drop (client);
-  if (pos->th != NULL)
-    {
-      GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
-      pos->th = NULL;
-    }
+  GNUNET_assert (0 == pos->num_pending);
   GNUNET_free (pos);
 }
 
@@ -207,16 +206,14 @@ handle_client_disconnect (void *cls,
  */
 struct GNUNET_SERVER_NotificationContext *
 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
-                                          unsigned int queue_length)
+                                           unsigned int queue_length)
 {
   struct GNUNET_SERVER_NotificationContext *ret;
 
   ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
   ret->server = server;
   ret->queue_length = queue_length;
-  GNUNET_SERVER_disconnect_notify (server,
-                                  &handle_client_disconnect,
-                                  ret);
+  GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, ret);
   return ret;
 }
 
@@ -227,29 +224,36 @@ GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
  * @param nc context to destroy.
  */
 void
-GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
+GNUNET_SERVER_notification_context_destroy (struct
+                                            GNUNET_SERVER_NotificationContext
+                                            *nc)
 {
   struct ClientList *pos;
   struct PendingMessageList *pml;
 
-  while (NULL != (pos = nc->clients))
+  while (NULL != (pos = nc->clients_head))
+  {
+    GNUNET_CONTAINER_DLL_remove (nc->clients_head,
+                                nc->clients_tail,
+                                pos);
+    if (NULL != pos->th)
+    {
+      GNUNET_SERVER_notify_transmit_ready_cancel(pos->th);
+      pos->th = NULL;
+    }
+    GNUNET_SERVER_client_drop (pos->client);
+    while (NULL != (pml = pos->pending_head))
     {
-      nc->clients = pos->next;
-      GNUNET_SERVER_client_drop (pos->client); 
-      GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
-      while (NULL != (pml = pos->pending_head))
-       {
-         GNUNET_CONTAINER_DLL_remove (pos->pending_head,
-                                      pos->pending_tail,
-                                      pml);
-         GNUNET_free (pml);
-       }
-      GNUNET_free (pos);
+      GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
+      GNUNET_free (pml);
+      pos->num_pending--;
     }
-  if (nc->server != NULL)
+    GNUNET_assert (0 == pos->num_pending);
+    GNUNET_free (pos);
+  }
+  if (NULL != nc->server)
     GNUNET_SERVER_disconnect_notify_cancel (nc->server,
-                                           &handle_client_disconnect,
-                                           nc);
+                                            &handle_client_disconnect, nc);
   GNUNET_free (nc);
 }
 
@@ -261,17 +265,22 @@ GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationCon
  * @param client client to add
  */
 void
-GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
-                                       struct GNUNET_SERVER_Client *client)
+GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
+                                        *nc,
+                                        struct GNUNET_SERVER_Client *client)
 {
   struct ClientList *cl;
 
+  for (cl = nc->clients_head; NULL != cl; cl = cl->next)
+    if (cl->client == client)
+      return; /* already present */    
   cl = GNUNET_malloc (sizeof (struct ClientList));
-  cl->next = nc->clients;
+  GNUNET_CONTAINER_DLL_insert (nc->clients_head,
+                              nc->clients_tail,
+                              cl);
   cl->nc = nc;
   cl->client = client;
   GNUNET_SERVER_client_keep (client);
-  nc->clients = cl;
 }
 
 
@@ -286,9 +295,7 @@ GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
  * @return number of bytes written to buf
  */
 static size_t
-transmit_message (void *cls,
-                 size_t size,
-                 void *buf)
+transmit_message (void *cls, size_t size, void *buf)
 {
   struct ClientList *cl = cls;
   char *cbuf = buf;
@@ -297,51 +304,43 @@ transmit_message (void *cls,
   size_t ret;
 
   cl->th = NULL;
-  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;
-    }
+  if (NULL == buf)
+  {
+    /* 'cl' should be freed via disconnect notification shortly */
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Failed to transmit message from NC queue to client\n");
+    return 0;
+  }
   ret = 0;
-  while (NULL != (pml = cl->pending_head) )
-    {
-      msize = ntohs (pml->msg->size);
-      if (size < msize)
-       break;
-      GNUNET_CONTAINER_DLL_remove (cl->pending_head,
-                                  cl->pending_tail,
-                                  pml);
-#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;
-      GNUNET_free (pml);
-      cl->num_pending--;
-    }
-  if (pml != NULL)    
-    {
-#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 (pml->msg->size),
-                                                   GNUNET_TIME_UNIT_FOREVER_REL,
-                                                   &transmit_message,
-                                                   cl);
-    }
+  while (NULL != (pml = cl->pending_head))
+  {
+    msize = ntohs (pml->msg->size);
+    if (size < msize)
+      break;
+    GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml);
+    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);
+    memcpy (&cbuf[ret], pml->msg, msize);
+    ret += msize;
+    size -= msize;
+    GNUNET_free (pml);
+    cl->num_pending--;
+  }
+  if (NULL != pml)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Have %u messages left in NC queue, will try transmission again\n",
+         cl->num_pending);
+    cl->th =
+        GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size),
+                                             GNUNET_TIME_UNIT_FOREVER_REL,
+                                             &transmit_message, cl);
+  }
   else
-    GNUNET_assert (cl->num_pending == 0);
+  {
+    GNUNET_assert (0 == cl->num_pending);
+  }
   return ret;
 }
 
@@ -356,52 +355,44 @@ transmit_message (void *cls,
  */
 static void
 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
-           struct ClientList *client,
-           const struct GNUNET_MessageHeader *msg,
-           int can_drop)
+            struct ClientList *client, const struct GNUNET_MessageHeader *msg,
+            int can_drop)
 {
   struct PendingMessageList *pml;
   uint16_t size;
 
-  if ( (client->num_pending > nc->queue_length) &&
-       (GNUNET_YES == can_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) && (GNUNET_YES == can_drop))
+  {
+    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
-        queue that are 'droppable' */
-    }
+  {
+    /* FIXME: consider checking for other messages in the
+     * queue that are 'droppable' */
+  }
   client->num_pending++;
   size = ntohs (msg->size);
   pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
-  pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
-  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
+  pml->msg = (const struct GNUNET_MessageHeader *) &pml[1];
+  pml->can_drop = can_drop;
+  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);
   memcpy (&pml[1], msg, size);
   /* append */
-  GNUNET_CONTAINER_DLL_insert_tail (client->pending_head,
-                                   client->pending_tail,
-                                   pml);
+  GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
+                                    pml);
   if (client->th == NULL)
-    client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
-                                                     ntohs (client->pending_head->msg->size),
-                                                     GNUNET_TIME_UNIT_FOREVER_REL,
-                                                     &transmit_message,
-                                                     client);
-} 
+    client->th =
+        GNUNET_SERVER_notify_transmit_ready (client->client,
+                                             ntohs (client->pending_head->
+                                                    msg->size),
+                                             GNUNET_TIME_UNIT_FOREVER_REL,
+                                             &transmit_message, client);
+}
 
 
 /**
@@ -414,22 +405,20 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
  * @param can_drop can this message be dropped due to queue length limitations
  */
 void
-GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
-                                           struct GNUNET_SERVER_Client *client,
-                                           const struct GNUNET_MessageHeader *msg,
-                                           int can_drop)
+GNUNET_SERVER_notification_context_unicast (struct
+                                            GNUNET_SERVER_NotificationContext
+                                            *nc,
+                                            struct GNUNET_SERVER_Client *client,
+                                            const struct GNUNET_MessageHeader
+                                            *msg, int can_drop)
 {
   struct ClientList *pos;
-  
-  pos = nc->clients;
-  while (NULL != pos)
-    {
-      if (pos->client == client)
-       break;
-      pos = pos->next;
-    }
-  GNUNET_assert (pos != NULL);
-  do_unicast (nc, pos, msg, can_drop); 
+
+  for (pos = nc->clients_head; NULL != pos; pos = pos->next)
+    if (pos->client == client)
+      break;
+  GNUNET_assert (NULL != pos);
+  do_unicast (nc, pos, msg, can_drop);
 }
 
 
@@ -441,18 +430,16 @@ GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationCon
  * @param can_drop can this message be dropped due to queue length limitations
  */
 void
-GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
-                                             const struct GNUNET_MessageHeader *msg,
-                                             int can_drop)
+GNUNET_SERVER_notification_context_broadcast (struct
+                                              GNUNET_SERVER_NotificationContext
+                                              *nc,
+                                              const struct GNUNET_MessageHeader
+                                              *msg, int can_drop)
 {
   struct ClientList *pos;
-  
-  pos = nc->clients;
-  while (NULL != pos)
-    {
-      do_unicast (nc, pos, msg, can_drop);
-      pos = pos->next;
-    }
+
+  for (pos = nc->clients_head; NULL != pos; pos = pos->next)
+    do_unicast (nc, pos, msg, can_drop);
 }