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