Update plibc header
[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, struct GNUNET_SERVER_Client *client)
155 {
156   struct GNUNET_SERVER_NotificationContext *nc = cls;
157   struct ClientList *pos;
158   struct PendingMessageList *pml;
159
160   if (NULL == client)
161   {
162     nc->server = NULL;
163     return;
164   }
165   for (pos = nc->clients_head; NULL != pos; pos = pos->next)
166     if (pos->client == client)
167       break;
168   if (NULL == pos)
169     return;
170   LOG (GNUNET_ERROR_TYPE_DEBUG,
171        "Client disconnected, cleaning up %u messages in NC queue\n",
172        pos->num_pending);
173   GNUNET_CONTAINER_DLL_remove (nc->clients_head,
174                                nc->clients_tail,
175                                pos);
176   while (NULL != (pml = pos->pending_head))
177   {
178     GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
179     GNUNET_free (pml);
180     pos->num_pending--;
181   }
182   if (NULL != pos->th)
183   {
184     GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
185     pos->th = NULL;
186   }
187   GNUNET_SERVER_client_drop (client);
188   GNUNET_assert (0 == pos->num_pending);
189   GNUNET_free (pos);
190 }
191
192
193 /**
194  * Create a new notification context.
195  *
196  * @param server server for which this function creates the context
197  * @param queue_length maximum number of messages to keep in
198  *        the notification queue; optional messages are dropped
199  *        if the queue gets longer than this number of messages
200  * @return handle to the notification context
201  */
202 struct GNUNET_SERVER_NotificationContext *
203 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
204                                            unsigned int queue_length)
205 {
206   struct GNUNET_SERVER_NotificationContext *ret;
207
208   ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
209   ret->server = server;
210   ret->queue_length = queue_length;
211   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, ret);
212   return ret;
213 }
214
215
216 /**
217  * Destroy the context, force disconnect for all clients.
218  *
219  * @param nc context to destroy.
220  */
221 void
222 GNUNET_SERVER_notification_context_destroy (struct
223                                             GNUNET_SERVER_NotificationContext
224                                             *nc)
225 {
226   struct ClientList *pos;
227   struct PendingMessageList *pml;
228
229   while (NULL != (pos = nc->clients_head))
230   {
231     GNUNET_CONTAINER_DLL_remove (nc->clients_head,
232                                  nc->clients_tail,
233                                  pos);
234     if (NULL != pos->th)
235     {
236       GNUNET_SERVER_notify_transmit_ready_cancel(pos->th);
237       pos->th = NULL;
238     }
239     GNUNET_SERVER_client_drop (pos->client);
240     while (NULL != (pml = pos->pending_head))
241     {
242       GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
243       GNUNET_free (pml);
244       pos->num_pending--;
245     }
246     GNUNET_assert (0 == pos->num_pending);
247     GNUNET_free (pos);
248   }
249   if (NULL != nc->server)
250     GNUNET_SERVER_disconnect_notify_cancel (nc->server,
251                                             &handle_client_disconnect, nc);
252   GNUNET_free (nc);
253 }
254
255
256 /**
257  * Add a client to the notification context.
258  *
259  * @param nc context to modify
260  * @param client client to add
261  */
262 void
263 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
264                                         *nc,
265                                         struct GNUNET_SERVER_Client *client)
266 {
267   struct ClientList *cl;
268
269   for (cl = nc->clients_head; NULL != cl; cl = cl->next)
270     if (cl->client == client)
271       return; /* already present */
272   cl = GNUNET_malloc (sizeof (struct ClientList));
273   GNUNET_CONTAINER_DLL_insert (nc->clients_head,
274                                nc->clients_tail,
275                                cl);
276   cl->nc = nc;
277   cl->client = client;
278   GNUNET_SERVER_client_keep (client);
279 }
280
281
282 /**
283  * Function called to notify a client about the socket begin ready to
284  * queue more data.  "buf" will be NULL and "size" zero if the socket
285  * was closed for writing in the meantime.
286  *
287  * @param cls the 'struct ClientList *'
288  * @param size number of bytes available in buf
289  * @param buf where the callee should write the message
290  * @return number of bytes written to buf
291  */
292 static size_t
293 transmit_message (void *cls, size_t size, void *buf)
294 {
295   struct ClientList *cl = cls;
296   char *cbuf = buf;
297   struct PendingMessageList *pml;
298   uint16_t msize;
299   size_t ret;
300
301   cl->th = NULL;
302   if (NULL == buf)
303   {
304     /* 'cl' should be freed via disconnect notification shortly */
305     LOG (GNUNET_ERROR_TYPE_DEBUG,
306          "Failed to transmit message from NC queue to client\n");
307     return 0;
308   }
309   ret = 0;
310   while (NULL != (pml = cl->pending_head))
311   {
312     msize = ntohs (pml->msg->size);
313     if (size < msize)
314       break;
315     GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml);
316     LOG (GNUNET_ERROR_TYPE_DEBUG,
317          "Copying message of type %u and size %u from pending queue to transmission buffer\n",
318          ntohs (pml->msg->type), msize);
319     memcpy (&cbuf[ret], pml->msg, msize);
320     ret += msize;
321     size -= msize;
322     GNUNET_free (pml);
323     cl->num_pending--;
324   }
325   if (NULL != pml)
326   {
327     LOG (GNUNET_ERROR_TYPE_DEBUG,
328          "Have %u messages left in NC queue, will try transmission again\n",
329          cl->num_pending);
330     cl->th =
331         GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size),
332                                              GNUNET_TIME_UNIT_FOREVER_REL,
333                                              &transmit_message, cl);
334   }
335   else
336   {
337     GNUNET_assert (0 == cl->num_pending);
338   }
339   return ret;
340 }
341
342
343 /**
344  * Send a message to a particular client.
345  *
346  * @param nc context to modify
347  * @param client client to transmit to
348  * @param msg message to send
349  * @param can_drop can this message be dropped due to queue length limitations
350  */
351 static void
352 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
353             struct ClientList *client, const struct GNUNET_MessageHeader *msg,
354             int can_drop)
355 {
356   struct PendingMessageList *pml;
357   uint16_t size;
358
359   if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_drop))
360   {
361     LOG (GNUNET_ERROR_TYPE_INFO,
362          "Dropping message of type %u and size %u due to full queue (%u entries)\n",
363          ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
364     return;                     /* drop! */
365   }
366   if (client->num_pending > nc->queue_length)
367   {
368     /* FIXME: consider checking for other messages in the
369      * queue that are 'droppable' */
370   }
371   client->num_pending++;
372   size = ntohs (msg->size);
373   pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
374   pml->msg = (const struct GNUNET_MessageHeader *) &pml[1];
375   pml->can_drop = can_drop;
376   LOG (GNUNET_ERROR_TYPE_DEBUG,
377        "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
378        ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
379   memcpy (&pml[1], msg, size);
380   /* append */
381   GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
382                                     pml);
383   if (NULL == client->th)
384     client->th =
385         GNUNET_SERVER_notify_transmit_ready (client->client,
386                                              ntohs (client->pending_head->
387                                                     msg->size),
388                                              GNUNET_TIME_UNIT_FOREVER_REL,
389                                              &transmit_message, client);
390 }
391
392
393 /**
394  * Send a message to a particular client; must have
395  * already been added to the notification context.
396  *
397  * @param nc context to modify
398  * @param client client to transmit to
399  * @param msg message to send
400  * @param can_drop can this message be dropped due to queue length limitations
401  */
402 void
403 GNUNET_SERVER_notification_context_unicast (struct
404                                             GNUNET_SERVER_NotificationContext
405                                             *nc,
406                                             struct GNUNET_SERVER_Client *client,
407                                             const struct GNUNET_MessageHeader
408                                             *msg, int can_drop)
409 {
410   struct ClientList *pos;
411
412   for (pos = nc->clients_head; NULL != pos; pos = pos->next)
413     if (pos->client == client)
414       break;
415   GNUNET_assert (NULL != pos);
416   do_unicast (nc, pos, msg, can_drop);
417 }
418
419
420 /**
421  * Send a message to all clients of this context.
422  *
423  * @param nc context to modify
424  * @param msg message to send
425  * @param can_drop can this message be dropped due to queue length limitations
426  */
427 void
428 GNUNET_SERVER_notification_context_broadcast (struct
429                                               GNUNET_SERVER_NotificationContext
430                                               *nc,
431                                               const struct GNUNET_MessageHeader
432                                               *msg, int can_drop)
433 {
434   struct ClientList *pos;
435
436   for (pos = nc->clients_head; NULL != pos; pos = pos->next)
437     do_unicast (nc, pos, msg, can_drop);
438 }
439
440
441 /* end of server_nc.c */