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