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