2 This file is part of GNUnet.
3 Copyright (C) 2010 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-server-nc", __VA_ARGS__)
35 * Entry in list of messages pending to be transmitted.
37 struct PendingMessageList
41 * This is a doubly-linked list.
43 struct PendingMessageList *next;
46 * This is a doubly-linked list.
48 struct PendingMessageList *prev;
51 * Message to transmit (allocated at the end of this
52 * struct, do not free)
54 const struct GNUNET_MessageHeader *msg;
57 * Can this message be dropped?
65 * Lists of clients we manage for notifications.
71 * This is a doubly linked list.
73 struct ClientList *next;
76 * This is a doubly linked list.
78 struct ClientList *prev;
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_SERVER_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 * Head of list of clients receiving notifications.
132 struct ClientList *clients_head;
135 * Tail of list of clients receiving notifications.
137 struct ClientList *clients_tail;
140 * Maximum number of optional messages to queue per client.
142 unsigned int queue_length;
148 * Client has disconnected, clean up.
150 * @param cls our `struct GNUNET_SERVER_NotificationContext *`
151 * @param client handle of client that disconnected
154 handle_client_disconnect (void *cls,
155 struct GNUNET_SERVER_Client *client)
157 struct GNUNET_SERVER_NotificationContext *nc = cls;
158 struct ClientList *pos;
159 struct PendingMessageList *pml;
166 for (pos = nc->clients_head; NULL != pos; pos = pos->next)
167 if (pos->client == client)
171 LOG (GNUNET_ERROR_TYPE_DEBUG,
172 "Client disconnected, cleaning up %u messages in NC queue\n",
174 GNUNET_CONTAINER_DLL_remove (nc->clients_head,
177 while (NULL != (pml = pos->pending_head))
179 GNUNET_CONTAINER_DLL_remove (pos->pending_head,
187 GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
190 GNUNET_SERVER_client_drop (client);
191 GNUNET_assert (0 == pos->num_pending);
197 * Create a new notification context.
199 * @param server server for which this function creates the context
200 * @param queue_length maximum number of messages to keep in
201 * the notification queue; optional messages are dropped
202 * if the queue gets longer than this number of messages
203 * @return handle to the notification context
205 struct GNUNET_SERVER_NotificationContext *
206 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
207 unsigned int queue_length)
209 struct GNUNET_SERVER_NotificationContext *ret;
211 ret = GNUNET_new (struct GNUNET_SERVER_NotificationContext);
212 ret->server = server;
213 ret->queue_length = queue_length;
214 GNUNET_SERVER_disconnect_notify (server,
215 &handle_client_disconnect,
222 * Destroy the context, force disconnect for all clients.
224 * @param nc context to destroy.
227 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
229 struct ClientList *pos;
230 struct PendingMessageList *pml;
232 while (NULL != (pos = nc->clients_head))
234 GNUNET_CONTAINER_DLL_remove (nc->clients_head,
239 GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
242 GNUNET_SERVER_client_drop (pos->client);
243 while (NULL != (pml = pos->pending_head))
245 GNUNET_CONTAINER_DLL_remove (pos->pending_head,
251 GNUNET_assert (0 == pos->num_pending);
254 if (NULL != nc->server)
255 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
256 &handle_client_disconnect,
263 * Add a client to the notification context.
265 * @param nc context to modify
266 * @param client client to add
269 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
270 struct GNUNET_SERVER_Client *client)
272 struct ClientList *cl;
274 for (cl = nc->clients_head; NULL != cl; cl = cl->next)
275 if (cl->client == client)
276 return; /* already present */
277 cl = GNUNET_new (struct ClientList);
278 GNUNET_CONTAINER_DLL_insert (nc->clients_head,
283 GNUNET_SERVER_client_keep (client);
288 * Function called to notify a client about the socket begin ready to
289 * queue more data. @a buf will be NULL and @a size zero if the socket
290 * was closed for writing in the meantime.
292 * @param cls the `struct ClientList *`
293 * @param size number of bytes available in @a buf
294 * @param buf where the callee should write the message
295 * @return number of bytes written to buf
298 transmit_message (void *cls,
302 struct ClientList *cl = cls;
304 struct PendingMessageList *pml;
311 /* 'cl' should be freed via disconnect notification shortly */
312 LOG (GNUNET_ERROR_TYPE_DEBUG,
313 "Failed to transmit message from NC queue to client\n");
317 while (NULL != (pml = cl->pending_head))
319 msize = ntohs (pml->msg->size);
322 GNUNET_CONTAINER_DLL_remove (cl->pending_head,
325 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 "Copying message of type %u and size %u from pending queue to transmission buffer\n",
327 ntohs (pml->msg->type),
329 GNUNET_memcpy (&cbuf[ret], pml->msg, msize);
337 LOG (GNUNET_ERROR_TYPE_DEBUG,
338 "Have %u messages left in NC queue, will try transmission again\n",
341 GNUNET_SERVER_notify_transmit_ready (cl->client,
342 ntohs (pml->msg->size),
343 GNUNET_TIME_UNIT_FOREVER_REL,
344 &transmit_message, cl);
348 GNUNET_assert (0 == cl->num_pending);
355 * Send a message to a particular client.
357 * @param nc context to modify
358 * @param client client to transmit to
359 * @param msg message to send
360 * @param can_drop can this message be dropped due to queue length limitations
363 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
364 struct ClientList *client,
365 const struct GNUNET_MessageHeader *msg,
368 struct PendingMessageList *pml;
371 if ( (client->num_pending > nc->queue_length) &&
372 (GNUNET_YES == can_drop) )
374 LOG (GNUNET_ERROR_TYPE_INFO,
375 "Dropping message of type %u and size %u due to full queue (%u entries)\n",
376 ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
379 if (client->num_pending > nc->queue_length)
381 /* FIXME: consider checking for other messages in the
382 * queue that are 'droppable' */
384 client->num_pending++;
385 size = ntohs (msg->size);
386 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
387 pml->msg = (const struct GNUNET_MessageHeader *) &pml[1];
388 pml->can_drop = can_drop;
389 LOG (GNUNET_ERROR_TYPE_DEBUG,
390 "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
393 (unsigned int) nc->queue_length);
394 GNUNET_memcpy (&pml[1], msg, size);
396 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head,
397 client->pending_tail,
399 if (NULL == client->th)
401 GNUNET_SERVER_notify_transmit_ready (client->client,
402 ntohs (client->pending_head->
404 GNUNET_TIME_UNIT_FOREVER_REL,
405 &transmit_message, client);
410 * Send a message to a particular client; must have
411 * already been added to the notification context.
413 * @param nc context to modify
414 * @param client client to transmit to
415 * @param msg message to send
416 * @param can_drop can this message be dropped due to queue length limitations
419 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
420 struct GNUNET_SERVER_Client *client,
421 const struct GNUNET_MessageHeader *msg,
424 struct ClientList *pos;
426 for (pos = nc->clients_head; NULL != pos; pos = pos->next)
427 if (pos->client == client)
429 GNUNET_assert (NULL != pos);
430 do_unicast (nc, pos, msg, can_drop);
435 * Send a message to all clients of this context.
437 * @param nc context to modify
438 * @param msg message to send
439 * @param can_drop can this message be dropped due to queue length limitations
442 GNUNET_SERVER_notification_context_broadcast (struct
443 GNUNET_SERVER_NotificationContext *nc,
444 const struct GNUNET_MessageHeader *msg,
447 struct ClientList *pos;
449 for (pos = nc->clients_head; NULL != pos; pos = pos->next)
450 do_unicast (nc, pos, msg, can_drop);
455 * Return active number of subscribers in this context.
457 * @param nc context to query
458 * @return number of current subscribers
461 GNUNET_SERVER_notification_context_get_size (struct GNUNET_SERVER_NotificationContext *nc)
464 struct ClientList *pos;
467 for (pos = nc->clients_head; NULL != pos; pos = pos->next)
472 /* end of server_nc.c */