2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file util/server_nc.c
23 * @brief convenience functions for transmission of
24 * a notification stream
25 * @author Christian Grothoff
29 #include "gnunet_common.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_container_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_time_lib.h"
37 #define DEBUG_SERVER_NC GNUNET_NO
40 * Entry in list of messages pending to be transmitted.
42 struct PendingMessageList
46 * This is a doubly-linked list.
48 struct PendingMessageList *next;
51 * This is a doubly-linked list.
53 struct PendingMessageList *prev;
56 * Message to transmit (allocated at the end of this
57 * struct, do not free)
59 const struct GNUNET_MessageHeader *msg;
62 * Can this message be dropped?
70 * Lists of clients we manage for notifications.
76 * This is a linked list.
78 struct ClientList *next;
81 * Overall context this client belongs to.
83 struct GNUNET_SERVER_NotificationContext *nc;
86 * Handle to the client.
88 struct GNUNET_SERVER_Client *client;
91 * Handle for pending transmission request to the client (or NULL).
93 struct GNUNET_CONNECTION_TransmitHandle *th;
96 * Head of linked list of requests queued for transmission.
98 struct PendingMessageList *pending_head;
101 * Tail of linked list of requests queued for transmission.
103 struct PendingMessageList *pending_tail;
106 * Number of messages currently in the list.
108 unsigned int num_pending;
114 * The notification context is the key datastructure for a convenience
115 * API used for transmission of notifications to the client until the
116 * client disconnects (or the notification context is destroyed, in
117 * which case we disconnect these clients). Essentially, all
118 * (notification) messages are queued up until the client is able to
121 struct GNUNET_SERVER_NotificationContext
125 * Server we do notifications for.
127 struct GNUNET_SERVER_Handle *server;
130 * List of clients receiving notifications.
132 struct ClientList *clients;
135 * Maximum number of optional messages to queue per client.
137 unsigned int queue_length;
143 * Client has disconnected, clean up.
145 * @param cls our 'struct GNUNET_SERVER_NotificationContext *'
146 * @param client handle of client that disconnected
149 handle_client_disconnect (void *cls,
150 struct GNUNET_SERVER_Client *client)
152 struct GNUNET_SERVER_NotificationContext *nc = cls;
153 struct ClientList *pos;
154 struct ClientList *prev;
155 struct PendingMessageList *pml;
166 if (pos->client == client)
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Client disconnected, cleaning up %u messages in NC queue\n",
179 nc->clients = pos->next;
181 prev->next = pos->next;
182 while (NULL != (pml = pos->pending_head))
184 GNUNET_CONTAINER_DLL_remove (pos->pending_head,
189 GNUNET_SERVER_client_drop (client);
192 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
200 * Create a new notification context.
202 * @param server server for which this function creates the context
203 * @param queue_length maximum number of messages to keep in
204 * the notification queue; optional messages are dropped
205 * it the queue gets longer than this number of messages
206 * @return handle to the notification context
208 struct GNUNET_SERVER_NotificationContext *
209 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
210 unsigned int queue_length)
212 struct GNUNET_SERVER_NotificationContext *ret;
214 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
215 ret->server = server;
216 ret->queue_length = queue_length;
217 GNUNET_SERVER_disconnect_notify (server,
218 &handle_client_disconnect,
225 * Destroy the context, force disconnect for all clients.
227 * @param nc context to destroy.
230 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
232 struct ClientList *pos;
233 struct PendingMessageList *pml;
235 while (NULL != (pos = nc->clients))
237 nc->clients = pos->next;
238 GNUNET_SERVER_client_drop (pos->client);
239 GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
240 while (NULL != (pml = pos->pending_head))
242 GNUNET_CONTAINER_DLL_remove (pos->pending_head,
249 if (nc->server != NULL)
250 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
251 &handle_client_disconnect,
258 * Add a client to the notification context.
260 * @param nc context to modify
261 * @param client client to add
264 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
265 struct GNUNET_SERVER_Client *client)
267 struct ClientList *cl;
269 cl = GNUNET_malloc (sizeof (struct ClientList));
270 cl->next = nc->clients;
273 GNUNET_SERVER_client_keep (client);
279 * Function called to notify a client about the socket begin ready to
280 * queue more data. "buf" will be NULL and "size" zero if the socket
281 * was closed for writing in the meantime.
283 * @param cls the 'struct ClientList *'
284 * @param size number of bytes available in buf
285 * @param buf where the callee should write the message
286 * @return number of bytes written to buf
289 transmit_message (void *cls,
293 struct ClientList *cl = cls;
295 struct PendingMessageList *pml;
302 /* 'cl' should be freed via disconnect notification shortly */
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "Failed to transmit message from NC queue to client\n");
310 while (NULL != (pml = cl->pending_head) )
312 msize = ntohs (pml->msg->size);
315 GNUNET_CONTAINER_DLL_remove (cl->pending_head,
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Copying message of type %u and size %u from pending queue to transmission buffer\n",
321 ntohs (pml->msg->type),
324 memcpy (&cbuf[ret], pml->msg, msize);
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "Have %u messages left in NC queue, will try transmission again\n",
337 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
338 ntohs (pml->msg->size),
339 GNUNET_TIME_UNIT_FOREVER_REL,
344 GNUNET_assert (cl->num_pending == 0);
350 * Send a message to a particular client.
352 * @param nc context to modify
353 * @param client client to transmit to
354 * @param msg message to send
355 * @param can_drop can this message be dropped due to queue length limitations
358 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
359 struct ClientList *client,
360 const struct GNUNET_MessageHeader *msg,
363 struct PendingMessageList *pml;
366 if ( (client->num_pending > nc->queue_length) &&
367 (GNUNET_YES == can_drop) )
369 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
370 "Dropping message of type %u and size %u due to full queue (%u entries)\n",
373 (unsigned int) nc->queue_length);
376 if (client->num_pending > nc->queue_length)
378 /* FIXME: consider checking for other messages in the
379 queue that are 'droppable' */
381 client->num_pending++;
382 size = ntohs (msg->size);
383 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
384 pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
385 pml->can_drop = can_drop;
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
391 (unsigned int) nc->queue_length);
393 memcpy (&pml[1], msg, size);
395 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head,
396 client->pending_tail,
398 if (client->th == NULL)
399 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
400 ntohs (client->pending_head->msg->size),
401 GNUNET_TIME_UNIT_FOREVER_REL,
408 * Send a message to a particular client; must have
409 * already been added to the notification context.
411 * @param nc context to modify
412 * @param client client to transmit to
413 * @param msg message to send
414 * @param can_drop can this message be dropped due to queue length limitations
417 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
418 struct GNUNET_SERVER_Client *client,
419 const struct GNUNET_MessageHeader *msg,
422 struct ClientList *pos;
427 if (pos->client == client)
431 GNUNET_assert (pos != NULL);
432 do_unicast (nc, pos, msg, can_drop);
437 * Send a message to all clients of this context.
439 * @param nc context to modify
440 * @param msg message to send
441 * @param can_drop can this message be dropped due to queue length limitations
444 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
445 const struct GNUNET_MessageHeader *msg,
448 struct ClientList *pos;
453 do_unicast (nc, pos, msg, can_drop);
459 /* end of server_nc.c */