- log
[oweals/gnunet.git] / src / util / server_nc.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/server_nc.c
23  * @brief convenience functions for transmission of
24  *        a notification stream
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-server-nc", __VA_ARGS__)
32
33
34 /**
35  * Entry in list of messages pending to be transmitted.
36  */
37 struct PendingMessageList
38 {
39
40   /**
41    * This is a doubly-linked list.
42    */
43   struct PendingMessageList *next;
44
45   /**
46    * This is a doubly-linked list.
47    */
48   struct PendingMessageList *prev;
49
50   /**
51    * Message to transmit (allocated at the end of this
52    * struct, do not free)
53    */
54   const struct GNUNET_MessageHeader *msg;
55
56   /**
57    * Can this message be dropped?
58    */
59   int can_drop;
60
61 };
62
63
64 /**
65  * Lists of clients we manage for notifications.
66  */
67 struct ClientList
68 {
69
70   /**
71    * This is a doubly linked list.
72    */
73   struct ClientList *next;
74
75   /**
76    * This is a doubly linked list.
77    */
78   struct ClientList *prev;
79
80   /**
81    * Overall context this client belongs to.
82    */
83   struct GNUNET_SERVER_NotificationContext *nc;
84
85   /**
86    * Handle to the client.
87    */
88   struct GNUNET_SERVER_Client *client;
89
90   /**
91    * Handle for pending transmission request to the client (or NULL).
92    */
93   struct GNUNET_SERVER_TransmitHandle *th;
94
95   /**
96    * Head of linked list of requests queued for transmission.
97    */
98   struct PendingMessageList *pending_head;
99
100   /**
101    * Tail of linked list of requests queued for transmission.
102    */
103   struct PendingMessageList *pending_tail;
104
105   /**
106    * Number of messages currently in the list.
107    */
108   unsigned int num_pending;
109
110 };
111
112
113 /**
114  * The notification context is the key datastructure for a convenience
115  * API used for transmission of notifications to the client until the
116  * client disconnects (or the notification context is destroyed, in
117  * which case we disconnect these clients).  Essentially, all
118  * (notification) messages are queued up until the client is able to
119  * read them.
120  */
121 struct GNUNET_SERVER_NotificationContext
122 {
123
124   /**
125    * Server we do notifications for.
126    */
127   struct GNUNET_SERVER_Handle *server;
128
129   /**
130    * Head of list of clients receiving notifications.
131    */
132   struct ClientList *clients_head;
133
134   /**
135    * Tail of list of clients receiving notifications.
136    */
137   struct ClientList *clients_tail;
138
139   /**
140    * Maximum number of optional messages to queue per client.
141    */
142   unsigned int queue_length;
143
144 };
145
146
147 /**
148  * Client has disconnected, clean up.
149  *
150  * @param cls our `struct GNUNET_SERVER_NotificationContext *`
151  * @param client handle of client that disconnected
152  */
153 static void
154 handle_client_disconnect (void *cls,
155                           struct GNUNET_SERVER_Client *client)
156 {
157   struct GNUNET_SERVER_NotificationContext *nc = cls;
158   struct ClientList *pos;
159   struct PendingMessageList *pml;
160
161   if (NULL == client)
162   {
163     nc->server = NULL;
164     return;
165   }
166   for (pos = nc->clients_head; NULL != pos; pos = pos->next)
167     if (pos->client == client)
168       break;
169   if (NULL == pos)
170     return;
171   LOG (GNUNET_ERROR_TYPE_DEBUG,
172        "Client disconnected, cleaning up %u messages in NC queue\n",
173        pos->num_pending);
174   GNUNET_CONTAINER_DLL_remove (nc->clients_head,
175                                nc->clients_tail,
176                                pos);
177   while (NULL != (pml = pos->pending_head))
178   {
179     GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
180     GNUNET_free (pml);
181     pos->num_pending--;
182   }
183   if (NULL != pos->th)
184   {
185     GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
186     pos->th = NULL;
187   }
188   GNUNET_SERVER_client_drop (client);
189   GNUNET_assert (0 == pos->num_pending);
190   GNUNET_free (pos);
191 }
192
193
194 /**
195  * Create a new notification context.
196  *
197  * @param server server for which this function creates the context
198  * @param queue_length maximum number of messages to keep in
199  *        the notification queue; optional messages are dropped
200  *        if the queue gets longer than this number of messages
201  * @return handle to the notification context
202  */
203 struct GNUNET_SERVER_NotificationContext *
204 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
205                                            unsigned int queue_length)
206 {
207   struct GNUNET_SERVER_NotificationContext *ret;
208
209   ret = GNUNET_new (struct GNUNET_SERVER_NotificationContext);
210   ret->server = server;
211   ret->queue_length = queue_length;
212   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, ret);
213   return ret;
214 }
215
216
217 /**
218  * Destroy the context, force disconnect for all clients.
219  *
220  * @param nc context to destroy.
221  */
222 void
223 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
224 {
225   struct ClientList *pos;
226   struct PendingMessageList *pml;
227
228   while (NULL != (pos = nc->clients_head))
229   {
230     GNUNET_CONTAINER_DLL_remove (nc->clients_head,
231                                  nc->clients_tail,
232                                  pos);
233     if (NULL != pos->th)
234     {
235       GNUNET_SERVER_notify_transmit_ready_cancel(pos->th);
236       pos->th = NULL;
237     }
238     GNUNET_SERVER_client_drop (pos->client);
239     while (NULL != (pml = pos->pending_head))
240     {
241       GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
242       GNUNET_free (pml);
243       pos->num_pending--;
244     }
245     GNUNET_assert (0 == pos->num_pending);
246     GNUNET_free (pos);
247   }
248   if (NULL != nc->server)
249     GNUNET_SERVER_disconnect_notify_cancel (nc->server,
250                                             &handle_client_disconnect, nc);
251   GNUNET_free (nc);
252 }
253
254
255 /**
256  * Add a client to the notification context.
257  *
258  * @param nc context to modify
259  * @param client client to add
260  */
261 void
262 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
263                                         struct GNUNET_SERVER_Client *client)
264 {
265   struct ClientList *cl;
266
267   for (cl = nc->clients_head; NULL != cl; cl = cl->next)
268     if (cl->client == client)
269       return; /* already present */
270   cl = GNUNET_new (struct ClientList);
271   GNUNET_CONTAINER_DLL_insert (nc->clients_head,
272                                nc->clients_tail,
273                                cl);
274   cl->nc = nc;
275   cl->client = client;
276   GNUNET_SERVER_client_keep (client);
277 }
278
279
280 /**
281  * Function called to notify a client about the socket begin ready to
282  * queue more data.  @a buf will be NULL and @a size zero if the socket
283  * was closed for writing in the meantime.
284  *
285  * @param cls the `struct ClientList *`
286  * @param size number of bytes available in @a buf
287  * @param buf where the callee should write the message
288  * @return number of bytes written to buf
289  */
290 static size_t
291 transmit_message (void *cls, size_t size, void *buf)
292 {
293   struct ClientList *cl = cls;
294   char *cbuf = buf;
295   struct PendingMessageList *pml;
296   uint16_t msize;
297   size_t ret;
298
299   cl->th = NULL;
300   if (NULL == buf)
301   {
302     /* 'cl' should be freed via disconnect notification shortly */
303     LOG (GNUNET_ERROR_TYPE_DEBUG,
304          "Failed to transmit message from NC queue to client\n");
305     return 0;
306   }
307   ret = 0;
308   while (NULL != (pml = cl->pending_head))
309   {
310     msize = ntohs (pml->msg->size);
311     if (size < msize)
312       break;
313     GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml);
314     LOG (GNUNET_ERROR_TYPE_DEBUG,
315          "Copying message of type %u and size %u from pending queue to transmission buffer\n",
316          ntohs (pml->msg->type), msize);
317     memcpy (&cbuf[ret], pml->msg, msize);
318     ret += msize;
319     size -= msize;
320     GNUNET_free (pml);
321     cl->num_pending--;
322   }
323   if (NULL != pml)
324   {
325     LOG (GNUNET_ERROR_TYPE_DEBUG,
326          "Have %u messages left in NC queue, will try transmission again\n",
327          cl->num_pending);
328     cl->th =
329         GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size),
330                                              GNUNET_TIME_UNIT_FOREVER_REL,
331                                              &transmit_message, cl);
332   }
333   else
334   {
335     GNUNET_assert (0 == cl->num_pending);
336   }
337   return ret;
338 }
339
340
341 /**
342  * Send a message to a particular client.
343  *
344  * @param nc context to modify
345  * @param client client to transmit to
346  * @param msg message to send
347  * @param can_drop can this message be dropped due to queue length limitations
348  */
349 static void
350 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
351             struct ClientList *client,
352             const struct GNUNET_MessageHeader *msg,
353             int can_drop)
354 {
355   struct PendingMessageList *pml;
356   uint16_t size;
357
358   if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_drop))
359   {
360     LOG (GNUNET_ERROR_TYPE_INFO,
361          "Dropping message of type %u and size %u due to full queue (%u entries)\n",
362          ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
363     return;                     /* drop! */
364   }
365   if (client->num_pending > nc->queue_length)
366   {
367     /* FIXME: consider checking for other messages in the
368      * queue that are 'droppable' */
369   }
370   client->num_pending++;
371   size = ntohs (msg->size);
372   pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
373   pml->msg = (const struct GNUNET_MessageHeader *) &pml[1];
374   pml->can_drop = can_drop;
375   LOG (GNUNET_ERROR_TYPE_DEBUG,
376        "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
377        ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
378   memcpy (&pml[1], msg, size);
379   /* append */
380   GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
381                                     pml);
382   if (NULL == client->th)
383     client->th =
384         GNUNET_SERVER_notify_transmit_ready (client->client,
385                                              ntohs (client->pending_head->
386                                                     msg->size),
387                                              GNUNET_TIME_UNIT_FOREVER_REL,
388                                              &transmit_message, client);
389 }
390
391
392 /**
393  * Send a message to a particular client; must have
394  * already been added to the notification context.
395  *
396  * @param nc context to modify
397  * @param client client to transmit to
398  * @param msg message to send
399  * @param can_drop can this message be dropped due to queue length limitations
400  */
401 void
402 GNUNET_SERVER_notification_context_unicast (struct
403                                             GNUNET_SERVER_NotificationContext *nc,
404                                             struct GNUNET_SERVER_Client *client,
405                                             const struct GNUNET_MessageHeader *msg,
406                                             int can_drop)
407 {
408   struct ClientList *pos;
409
410   for (pos = nc->clients_head; NULL != pos; pos = pos->next)
411     if (pos->client == client)
412       break;
413   GNUNET_assert (NULL != pos);
414   do_unicast (nc, pos, msg, can_drop);
415 }
416
417
418 /**
419  * Send a message to all clients of this context.
420  *
421  * @param nc context to modify
422  * @param msg message to send
423  * @param can_drop can this message be dropped due to queue length limitations
424  */
425 void
426 GNUNET_SERVER_notification_context_broadcast (struct
427                                               GNUNET_SERVER_NotificationContext *nc,
428                                               const struct GNUNET_MessageHeader *msg,
429                                               int can_drop)
430 {
431   struct ClientList *pos;
432
433   for (pos = nc->clients_head; NULL != pos; pos = pos->next)
434     do_unicast (nc, pos, msg, can_drop);
435 }
436
437
438 /* end of server_nc.c */