LRN: Fix automake deps to allow -j* builds again
[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 2, 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_common.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_container_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_time_lib.h"
35
36
37 #define DEBUG_SERVER_NC GNUNET_NO
38
39 /**
40  * Entry in list of messages pending to be transmitted.
41  */
42 struct PendingMessageList
43 {
44
45   /**
46    * This is a doubly-linked list.
47    */ 
48   struct PendingMessageList *next;
49
50   /**
51    * This is a doubly-linked list.
52    */ 
53   struct PendingMessageList *prev;
54
55   /**
56    * Message to transmit (allocated at the end of this
57    * struct, do not free)
58    */
59   const struct GNUNET_MessageHeader *msg;
60
61   /**
62    * Can this message be dropped?
63    */ 
64   int can_drop;
65
66 };
67
68
69 /**
70  * Lists of clients we manage for notifications.
71  */
72 struct ClientList
73 {
74
75   /**
76    * This is a linked list.
77    */ 
78   struct ClientList *next;
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_CONNECTION_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    * List of clients receiving notifications.
131    */
132   struct ClientList *clients;
133
134   /**
135    * Maximum number of optional messages to queue per client.
136    */
137   unsigned int queue_length;
138
139 };
140
141
142 /**
143  * Client has disconnected, clean up.
144  *
145  * @param cls our 'struct GNUNET_SERVER_NotificationContext *'
146  * @param client handle of client that disconnected
147  */
148 static void
149 handle_client_disconnect (void *cls,
150                           struct GNUNET_SERVER_Client *client)
151 {
152   struct GNUNET_SERVER_NotificationContext *nc = cls;
153   struct ClientList *pos;
154   struct ClientList *prev;
155   struct PendingMessageList *pml;
156
157   if (client == NULL)
158     {
159       nc->server = NULL;
160       return;
161     }
162   prev = NULL;
163   pos = nc->clients;
164   while (NULL != pos)
165     {
166       if (pos->client == client)
167         break;
168       prev = pos;
169       pos = pos->next;
170     }
171   if (pos == NULL)
172     return;
173 #if DEBUG_SERVER_NC
174   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175               "Client disconnected, cleaning up %u messages in NC queue\n",
176               pos->num_pending);
177 #endif
178   if (prev == NULL)
179     nc->clients = pos->next;
180   else
181     prev->next = pos->next;
182   while (NULL != (pml = pos->pending_head))
183     {
184       GNUNET_CONTAINER_DLL_remove (pos->pending_head,
185                                    pos->pending_tail,
186                                    pml);
187       GNUNET_free (pml);
188     }
189   if (pos->th != NULL)
190     {
191       GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
192       pos->th = NULL;
193     }
194   GNUNET_SERVER_client_drop (client);
195   GNUNET_free (pos);
196 }
197
198
199 /**
200  * Create a new notification context.
201  *
202  * @param server server for which this function creates the context
203  * @param queue_length maximum number of messages to keep in
204  *        the notification queue; optional messages are dropped
205  *        it the queue gets longer than this number of messages
206  * @return handle to the notification context
207  */
208 struct GNUNET_SERVER_NotificationContext *
209 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
210                                            unsigned int queue_length)
211 {
212   struct GNUNET_SERVER_NotificationContext *ret;
213
214   ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
215   ret->server = server;
216   ret->queue_length = queue_length;
217   GNUNET_SERVER_disconnect_notify (server,
218                                    &handle_client_disconnect,
219                                    ret);
220   return ret;
221 }
222
223
224 /**
225  * Destroy the context, force disconnect for all clients.
226  *
227  * @param nc context to destroy.
228  */
229 void
230 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
231 {
232   struct ClientList *pos;
233   struct PendingMessageList *pml;
234
235   while (NULL != (pos = nc->clients))
236     {
237       nc->clients = pos->next;
238       GNUNET_SERVER_client_drop (pos->client); 
239       while (NULL != (pml = pos->pending_head))
240         {
241           GNUNET_CONTAINER_DLL_remove (pos->pending_head,
242                                        pos->pending_tail,
243                                        pml);
244           GNUNET_free (pml);
245         }
246       GNUNET_free (pos);
247     }
248   if (nc->server != NULL)
249     GNUNET_SERVER_disconnect_notify_cancel (nc->server,
250                                             &handle_client_disconnect,
251                                             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 *nc,
264                                         struct GNUNET_SERVER_Client *client)
265 {
266   struct ClientList *cl;
267
268   cl = GNUNET_malloc (sizeof (struct ClientList));
269   cl->next = nc->clients;
270   cl->nc = nc;
271   cl->client = client;
272   GNUNET_SERVER_client_keep (client);
273   nc->clients = cl;
274 }
275
276
277 /**
278  * Function called to notify a client about the socket begin ready to
279  * queue more data.  "buf" will be NULL and "size" zero if the socket
280  * was closed for writing in the meantime.
281  *
282  * @param cls the 'struct ClientList *'
283  * @param size number of bytes available in buf
284  * @param buf where the callee should write the message
285  * @return number of bytes written to buf
286  */
287 static size_t
288 transmit_message (void *cls,
289                   size_t size,
290                   void *buf)
291 {
292   struct ClientList *cl = cls;
293   char *cbuf = buf;
294   struct PendingMessageList *pml;
295   uint16_t msize;
296   size_t ret;
297
298   cl->th = NULL;
299   if (buf == NULL)
300     {
301       /* 'cl' should be freed via disconnect notification shortly */
302 #if DEBUG_SERVER_NC
303       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304                   "Failed to transmit message from NC queue to client\n");
305 #endif
306       return 0;
307     }
308   ret = 0;
309   while (NULL != (pml = cl->pending_head) )
310     {
311       msize = ntohs (pml->msg->size);
312       if (size < msize)
313         break;
314       GNUNET_CONTAINER_DLL_remove (cl->pending_head,
315                                    cl->pending_tail,
316                                    pml);
317 #if DEBUG_SERVER_NC
318       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319                   "Copying message of type %u and size %u from pending queue to transmission buffer\n",
320                   ntohs (pml->msg->type),
321                   msize);
322 #endif
323       memcpy (&cbuf[ret], pml->msg, msize);
324       ret += msize;
325       size -= msize;
326       GNUNET_free (pml);
327       cl->num_pending--;
328     }
329   if (pml != NULL)    
330     {
331 #if DEBUG_SERVER_NC
332       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333                   "Have %u messages left in NC queue, will try transmission again\n",
334                   cl->num_pending);
335 #endif
336       cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
337                                                     ntohs (pml->msg->size),
338                                                     GNUNET_TIME_UNIT_FOREVER_REL,
339                                                     &transmit_message,
340                                                     cl);
341     }
342   else
343     GNUNET_assert (cl->num_pending == 0);
344   return ret;
345 }
346
347
348 /**
349  * Send a message to a particular client.
350  *
351  * @param nc context to modify
352  * @param client client to transmit to
353  * @param msg message to send
354  * @param can_drop can this message be dropped due to queue length limitations
355  */
356 static void
357 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
358             struct ClientList *client,
359             const struct GNUNET_MessageHeader *msg,
360             int can_drop)
361 {
362   struct PendingMessageList *pml;
363   uint16_t size;
364
365   if ( (client->num_pending > nc->queue_length) &&
366        (GNUNET_YES == can_drop) )
367     {
368       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
369                   "Dropping message of type %u and size %u due to full queue (%u entries)\n",
370                   ntohs (msg->type),
371                   ntohs (msg->size),
372                   (unsigned int) nc->queue_length);
373       return; /* drop! */
374     }
375   if (client->num_pending > nc->queue_length)
376     {
377       /* FIXME: consider checking for other messages in the
378          queue that are 'droppable' */
379     }
380   client->num_pending++;
381   size = ntohs (msg->size);
382   pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
383   pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
384   pml->can_drop = can_drop; 
385 #if DEBUG_SERVER_NC
386   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387               "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
388               ntohs (msg->type),
389               ntohs (msg->size),
390               (unsigned int) nc->queue_length);
391 #endif
392   memcpy (&pml[1], msg, size);
393   /* append */
394   GNUNET_CONTAINER_DLL_insert_tail (client->pending_head,
395                                     client->pending_tail,
396                                     pml);
397   if (client->th == NULL)
398     client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
399                                                       ntohs (client->pending_head->msg->size),
400                                                       GNUNET_TIME_UNIT_FOREVER_REL,
401                                                       &transmit_message,
402                                                       client);
403
404
405
406 /**
407  * Send a message to a particular client; must have
408  * already been added to the notification context.
409  *
410  * @param nc context to modify
411  * @param client client to transmit to
412  * @param msg message to send
413  * @param can_drop can this message be dropped due to queue length limitations
414  */
415 void
416 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
417                                             struct GNUNET_SERVER_Client *client,
418                                             const struct GNUNET_MessageHeader *msg,
419                                             int can_drop)
420 {
421   struct ClientList *pos;
422   
423   pos = nc->clients;
424   while (NULL != pos)
425     {
426       if (pos->client == client)
427         break;
428       pos = pos->next;
429     }
430   GNUNET_assert (pos != NULL);
431   do_unicast (nc, pos, msg, can_drop); 
432 }
433
434
435 /**
436  * Send a message to all clients of this context.
437  *
438  * @param nc context to modify
439  * @param msg message to send
440  * @param can_drop can this message be dropped due to queue length limitations
441  */
442 void
443 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
444                                               const struct GNUNET_MessageHeader *msg,
445                                               int can_drop)
446 {
447   struct ClientList *pos;
448   
449   pos = nc->clients;
450   while (NULL != pos)
451     {
452       do_unicast (nc, pos, msg, can_drop);
453       pos = pos->next;
454     }
455 }
456
457
458 /* end of server_nc.c */