Link libgnunetblockgroup to libgnunetblock
[oweals/gnunet.git] / src / util / server_nc.c
index d6625eefb23d0aa96d8304cdb70246bd83446a14..a95cd7f6df38f963eddd6af529f64db526d35584 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2010 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2010 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -147,11 +147,12 @@ 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;
@@ -175,7 +176,9 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
                               pos);
   while (NULL != (pml = pos->pending_head))
   {
-    GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
+    GNUNET_CONTAINER_DLL_remove (pos->pending_head,
+                                 pos->pending_tail,
+                                 pml);
     GNUNET_free (pml);
     pos->num_pending--;
   }
@@ -205,10 +208,12 @@ 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);
+  GNUNET_SERVER_disconnect_notify (server,
+                                   &handle_client_disconnect,
+                                   ret);
   return ret;
 }
 
@@ -219,9 +224,7 @@ 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;
@@ -233,13 +236,15 @@ GNUNET_SERVER_notification_context_destroy (struct
                                 pos);
     if (NULL != pos->th)
     {
-      GNUNET_SERVER_notify_transmit_ready_cancel(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_CONTAINER_DLL_remove (pos->pending_head,
+                                   pos->pending_tail,
+                                   pml);
       GNUNET_free (pml);
       pos->num_pending--;
     }
@@ -248,7 +253,8 @@ GNUNET_SERVER_notification_context_destroy (struct
   }
   if (NULL != nc->server)
     GNUNET_SERVER_disconnect_notify_cancel (nc->server,
-                                            &handle_client_disconnect, nc);
+                                            &handle_client_disconnect,
+                                            nc);
   GNUNET_free (nc);
 }
 
@@ -260,8 +266,7 @@ 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;
@@ -269,7 +274,7 @@ GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
   for (cl = nc->clients_head; NULL != cl; cl = cl->next)
     if (cl->client == client)
       return; /* already present */
-  cl = GNUNET_malloc (sizeof (struct ClientList));
+  cl = GNUNET_new (struct ClientList);
   GNUNET_CONTAINER_DLL_insert (nc->clients_head,
                               nc->clients_tail,
                               cl);
@@ -281,16 +286,18 @@ GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
 
 /**
  * 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
  */
 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;
@@ -312,11 +319,14 @@ transmit_message (void *cls, size_t size, void *buf)
     msize = ntohs (pml->msg->size);
     if (size < msize)
       break;
-    GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml);
+    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);
+         ntohs (pml->msg->type),
+         msize);
+    GNUNET_memcpy (&cbuf[ret], pml->msg, msize);
     ret += msize;
     size -= msize;
     GNUNET_free (pml);
@@ -328,7 +338,8 @@ transmit_message (void *cls, size_t size, void *buf)
          "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_SERVER_notify_transmit_ready (cl->client,
+                                             ntohs (pml->msg->size),
                                              GNUNET_TIME_UNIT_FOREVER_REL,
                                              &transmit_message, cl);
   }
@@ -350,13 +361,15 @@ 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;
   uint16_t size;
 
-  if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_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",
@@ -375,10 +388,13 @@ do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
   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);
+       ntohs (msg->type),
+       ntohs (msg->size),
+       (unsigned int) nc->queue_length);
+  GNUNET_memcpy (&pml[1], msg, size);
   /* append */
-  GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
+  GNUNET_CONTAINER_DLL_insert_tail (client->pending_head,
+                                    client->pending_tail,
                                     pml);
   if (NULL == client->th)
     client->th =
@@ -400,12 +416,10 @@ 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,
+GNUNET_SERVER_notification_context_unicast (struct 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;
 
@@ -426,10 +440,9 @@ 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;
 
@@ -438,4 +451,22 @@ GNUNET_SERVER_notification_context_broadcast (struct
 }
 
 
+/**
+ * Return active number of subscribers in this context.
+ *
+ * @param nc context to query
+ * @return number of current subscribers
+ */
+unsigned int
+GNUNET_SERVER_notification_context_get_size (struct GNUNET_SERVER_NotificationContext *nc)
+{
+  unsigned int num;
+  struct ClientList *pos;
+
+  num = 0;
+  for (pos = nc->clients_head; NULL != pos; pos = pos->next)
+    num++;
+  return num;
+}
+
 /* end of server_nc.c */