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