-fix time assertion introduce in last patch
[oweals/gnunet.git] / src / util / server_nc.c
index deb1a18c920b84f310bf377e5d50b3a11abd51f6..0074ea9352a370d6b9d01cf1183465a69b2e27b0 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
  */
 
 #include "platform.h"
-#include "gnunet_common.h"
-#include "gnunet_connection_lib.h"
-#include "gnunet_container_lib.h"
-#include "gnunet_scheduler_lib.h"
-#include "gnunet_server_lib.h"
-#include "gnunet_time_lib.h"
+#include "gnunet_util_lib.h"
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-server-nc", __VA_ARGS__)
 
 
 /**
@@ -73,10 +68,15 @@ struct ClientList
 {
 
   /**
-   * This is a linked list.
+   * This is a doubly linked list.
    */
   struct ClientList *next;
 
+  /**
+   * This is a doubly linked list.
+   */
+  struct ClientList *prev;
+
   /**
    * Overall context this client belongs to.
    */
@@ -127,9 +127,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.
@@ -142,51 +147,46 @@ struct GNUNET_SERVER_NotificationContext
 /**
  * Client has disconnected, clean up.
  *
- * @param cls our 'struct GNUNET_SERVER_NotificationContext *'
+ * @param cls our `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)
+  if (NULL == client)
   {
     nc->server = NULL;
     return;
   }
-  prev = NULL;
-  pos = nc->clients;
-  while (NULL != pos)
-  {
+  for (pos = nc->clients_head; NULL != pos; pos = pos->next)
     if (pos->client == client)
       break;
-    prev = pos;
-    pos = pos->next;
-  }
-  if (pos == NULL)
+  if (NULL == pos)
     return;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Client disconnected, cleaning up %u messages in NC queue\n",
        pos->num_pending);
-  if (prev == NULL)
-    nc->clients = pos->next;
-  else
-    prev->next = pos->next;
+  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);
+    pos->num_pending--;
   }
-  if (pos->th != NULL)
+  if (NULL != pos->th)
   {
     GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
     pos->th = NULL;
   }
   GNUNET_SERVER_client_drop (client);
+  GNUNET_assert (0 == pos->num_pending);
   GNUNET_free (pos);
 }
 
@@ -197,7 +197,7 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
  * @param server server for which this function creates the context
  * @param queue_length maximum number of messages to keep in
  *        the notification queue; optional messages are dropped
- *        it the queue gets longer than this number of messages
+ *        if the queue gets longer than this number of messages
  * @return handle to the notification context
  */
 struct GNUNET_SERVER_NotificationContext *
@@ -206,7 +206,7 @@ GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
 {
   struct GNUNET_SERVER_NotificationContext *ret;
 
-  ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
+  ret = GNUNET_new (struct GNUNET_SERVER_NotificationContext);
   ret->server = server;
   ret->queue_length = queue_length;
   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, ret);
@@ -220,25 +220,32 @@ 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))
   {
-    nc->clients = pos->next;
+    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))
     {
       GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
       GNUNET_free (pml);
+      pos->num_pending--;
     }
+    GNUNET_assert (0 == pos->num_pending);
     GNUNET_free (pos);
   }
-  if (nc->server != NULL)
+  if (NULL != nc->server)
     GNUNET_SERVER_disconnect_notify_cancel (nc->server,
                                             &handle_client_disconnect, nc);
   GNUNET_free (nc);
@@ -252,31 +259,31 @@ GNUNET_SERVER_notification_context_destroy (struct
  * @param client client to add
  */
 void
-GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
-                                        *nc,
+GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
                                         struct GNUNET_SERVER_Client *client)
 {
   struct ClientList *cl;
 
-  for (cl = nc->clients; NULL != cl; cl = cl->next)
+  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;
+      return; /* already present */
+  cl = GNUNET_new (struct ClientList);
+  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;
 }
 
 
 /**
  * Function called to notify a client about the socket begin ready to
- * queue more data.  "buf" will be NULL and "size" zero if the socket
+ * queue more data.  @a buf will be NULL and @a size zero if the socket
  * was closed for writing in the meantime.
  *
- * @param cls the 'struct ClientList *'
- * @param size number of bytes available in buf
+ * @param cls the `struct ClientList *`
+ * @param size number of bytes available in @a buf
  * @param buf where the callee should write the message
  * @return number of bytes written to buf
  */
@@ -290,7 +297,7 @@ transmit_message (void *cls, size_t size, void *buf)
   size_t ret;
 
   cl->th = NULL;
-  if (buf == NULL)
+  if (NULL == buf)
   {
     /* 'cl' should be freed via disconnect notification shortly */
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -313,7 +320,7 @@ transmit_message (void *cls, size_t size, void *buf)
     GNUNET_free (pml);
     cl->num_pending--;
   }
-  if (pml != NULL)
+  if (NULL != pml)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Have %u messages left in NC queue, will try transmission again\n",
@@ -324,7 +331,9 @@ transmit_message (void *cls, size_t size, void *buf)
                                              &transmit_message, cl);
   }
   else
-    GNUNET_assert (cl->num_pending == 0);
+  {
+    GNUNET_assert (0 == cl->num_pending);
+  }
   return ret;
 }
 
@@ -339,7 +348,8 @@ transmit_message (void *cls, size_t size, void *buf)
  */
 static void
 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
-            struct ClientList *client, const struct GNUNET_MessageHeader *msg,
+            struct ClientList *client,
+            const struct GNUNET_MessageHeader *msg,
             int can_drop)
 {
   struct PendingMessageList *pml;
@@ -369,7 +379,7 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
   /* append */
   GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
                                     pml);
-  if (client->th == NULL)
+  if (NULL == client->th)
     client->th =
         GNUNET_SERVER_notify_transmit_ready (client->client,
                                              ntohs (client->pending_head->
@@ -390,22 +400,17 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
  */
 void
 GNUNET_SERVER_notification_context_unicast (struct
-                                            GNUNET_SERVER_NotificationContext
-                                            *nc,
+                                            GNUNET_SERVER_NotificationContext *nc,
                                             struct GNUNET_SERVER_Client *client,
-                                            const struct GNUNET_MessageHeader
-                                            *msg, int can_drop)
+                                            const struct GNUNET_MessageHeader *msg,
+                                            int can_drop)
 {
   struct ClientList *pos;
 
-  pos = nc->clients;
-  while (NULL != pos)
-  {
+  for (pos = nc->clients_head; NULL != pos; pos = pos->next)
     if (pos->client == client)
       break;
-    pos = pos->next;
-  }
-  GNUNET_assert (pos != NULL);
+  GNUNET_assert (NULL != pos);
   do_unicast (nc, pos, msg, can_drop);
 }
 
@@ -419,19 +424,14 @@ GNUNET_SERVER_notification_context_unicast (struct
  */
 void
 GNUNET_SERVER_notification_context_broadcast (struct
-                                              GNUNET_SERVER_NotificationContext
-                                              *nc,
-                                              const struct GNUNET_MessageHeader
-                                              *msg, int can_drop)
+                                              GNUNET_SERVER_NotificationContext *nc,
+                                              const struct GNUNET_MessageHeader *msg,
+                                              int can_drop)
 {
   struct ClientList *pos;
 
-  pos = nc->clients;
-  while (NULL != pos)
-  {
+  for (pos = nc->clients_head; NULL != pos; pos = pos->next)
     do_unicast (nc, pos, msg, can_drop);
-    pos = pos->next;
-  }
 }