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