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"
36 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
39 #define DEBUG_SERVER_NC GNUNET_EXTRA_LOGGING
42 * Entry in list of messages pending to be transmitted.
44 struct PendingMessageList
48 * This is a doubly-linked list.
50 struct PendingMessageList *next;
53 * This is a doubly-linked list.
55 struct PendingMessageList *prev;
58 * Message to transmit (allocated at the end of this
59 * struct, do not free)
61 const struct GNUNET_MessageHeader *msg;
64 * Can this message be dropped?
72 * Lists of clients we manage for notifications.
78 * This is a linked list.
80 struct ClientList *next;
83 * Overall context this client belongs to.
85 struct GNUNET_SERVER_NotificationContext *nc;
88 * Handle to the client.
90 struct GNUNET_SERVER_Client *client;
93 * Handle for pending transmission request to the client (or NULL).
95 struct GNUNET_CONNECTION_TransmitHandle *th;
98 * Head of linked list of requests queued for transmission.
100 struct PendingMessageList *pending_head;
103 * Tail of linked list of requests queued for transmission.
105 struct PendingMessageList *pending_tail;
108 * Number of messages currently in the list.
110 unsigned int num_pending;
116 * The notification context is the key datastructure for a convenience
117 * API used for transmission of notifications to the client until the
118 * client disconnects (or the notification context is destroyed, in
119 * which case we disconnect these clients). Essentially, all
120 * (notification) messages are queued up until the client is able to
123 struct GNUNET_SERVER_NotificationContext
127 * Server we do notifications for.
129 struct GNUNET_SERVER_Handle *server;
132 * List of clients receiving notifications.
134 struct ClientList *clients;
137 * Maximum number of optional messages to queue per client.
139 unsigned int queue_length;
145 * Client has disconnected, clean up.
147 * @param cls our 'struct GNUNET_SERVER_NotificationContext *'
148 * @param client handle of client that disconnected
151 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
153 struct GNUNET_SERVER_NotificationContext *nc = cls;
154 struct ClientList *pos;
155 struct ClientList *prev;
156 struct PendingMessageList *pml;
167 if (pos->client == client)
175 LOG (GNUNET_ERROR_TYPE_DEBUG,
176 "Client disconnected, cleaning up %u messages in NC queue\n",
180 nc->clients = pos->next;
182 prev->next = pos->next;
183 while (NULL != (pml = pos->pending_head))
185 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
190 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
193 GNUNET_SERVER_client_drop (client);
199 * Create a new notification context.
201 * @param server server for which this function creates the context
202 * @param queue_length maximum number of messages to keep in
203 * the notification queue; optional messages are dropped
204 * it the queue gets longer than this number of messages
205 * @return handle to the notification context
207 struct GNUNET_SERVER_NotificationContext *
208 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
209 unsigned int queue_length)
211 struct GNUNET_SERVER_NotificationContext *ret;
213 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
214 ret->server = server;
215 ret->queue_length = queue_length;
216 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, ret);
222 * Destroy the context, force disconnect for all clients.
224 * @param nc context to destroy.
227 GNUNET_SERVER_notification_context_destroy (struct
228 GNUNET_SERVER_NotificationContext
231 struct ClientList *pos;
232 struct PendingMessageList *pml;
234 while (NULL != (pos = nc->clients))
236 nc->clients = pos->next;
237 GNUNET_SERVER_client_drop (pos->client);
238 while (NULL != (pml = pos->pending_head))
240 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
245 if (nc->server != NULL)
246 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
247 &handle_client_disconnect, nc);
253 * Add a client to the notification context.
255 * @param nc context to modify
256 * @param client client to add
259 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
261 struct GNUNET_SERVER_Client *client)
263 struct ClientList *cl;
265 for (cl = nc->clients; NULL != cl; cl = cl->next)
266 if (cl->client == client)
267 return; /* already present */
268 cl = GNUNET_malloc (sizeof (struct ClientList));
269 cl->next = nc->clients;
272 GNUNET_SERVER_client_keep (client);
278 * Function called to notify a client about the socket begin ready to
279 * queue more data. "buf" will be NULL and "size" zero if the socket
280 * was closed for writing in the meantime.
282 * @param cls the 'struct ClientList *'
283 * @param size number of bytes available in buf
284 * @param buf where the callee should write the message
285 * @return number of bytes written to buf
288 transmit_message (void *cls, size_t size, void *buf)
290 struct ClientList *cl = cls;
292 struct PendingMessageList *pml;
299 /* 'cl' should be freed via disconnect notification shortly */
301 LOG (GNUNET_ERROR_TYPE_DEBUG,
302 "Failed to transmit message from NC queue to client\n");
307 while (NULL != (pml = cl->pending_head))
309 msize = ntohs (pml->msg->size);
312 GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml);
314 LOG (GNUNET_ERROR_TYPE_DEBUG,
315 "Copying message of type %u and size %u from pending queue to transmission buffer\n",
316 ntohs (pml->msg->type), msize);
318 memcpy (&cbuf[ret], pml->msg, msize);
327 LOG (GNUNET_ERROR_TYPE_DEBUG,
328 "Have %u messages left in NC queue, will try transmission again\n",
332 GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size),
333 GNUNET_TIME_UNIT_FOREVER_REL,
334 &transmit_message, cl);
337 GNUNET_assert (cl->num_pending == 0);
343 * Send a message to a particular client.
345 * @param nc context to modify
346 * @param client client to transmit to
347 * @param msg message to send
348 * @param can_drop can this message be dropped due to queue length limitations
351 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
352 struct ClientList *client, const struct GNUNET_MessageHeader *msg,
355 struct PendingMessageList *pml;
358 if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_drop))
360 LOG (GNUNET_ERROR_TYPE_INFO,
361 "Dropping message of type %u and size %u due to full queue (%u entries)\n",
362 ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
365 if (client->num_pending > nc->queue_length)
367 /* FIXME: consider checking for other messages in the
368 * queue that are 'droppable' */
370 client->num_pending++;
371 size = ntohs (msg->size);
372 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
373 pml->msg = (const struct GNUNET_MessageHeader *) &pml[1];
374 pml->can_drop = can_drop;
376 LOG (GNUNET_ERROR_TYPE_DEBUG,
377 "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
378 ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
380 memcpy (&pml[1], msg, size);
382 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
384 if (client->th == NULL)
386 GNUNET_SERVER_notify_transmit_ready (client->client,
387 ntohs (client->pending_head->
389 GNUNET_TIME_UNIT_FOREVER_REL,
390 &transmit_message, client);
395 * Send a message to a particular client; must have
396 * already been added to the notification context.
398 * @param nc context to modify
399 * @param client client to transmit to
400 * @param msg message to send
401 * @param can_drop can this message be dropped due to queue length limitations
404 GNUNET_SERVER_notification_context_unicast (struct
405 GNUNET_SERVER_NotificationContext
407 struct GNUNET_SERVER_Client *client,
408 const struct GNUNET_MessageHeader
411 struct ClientList *pos;
416 if (pos->client == client)
420 GNUNET_assert (pos != NULL);
421 do_unicast (nc, pos, msg, can_drop);
426 * Send a message to all clients of this context.
428 * @param nc context to modify
429 * @param msg message to send
430 * @param can_drop can this message be dropped due to queue length limitations
433 GNUNET_SERVER_notification_context_broadcast (struct
434 GNUNET_SERVER_NotificationContext
436 const struct GNUNET_MessageHeader
439 struct ClientList *pos;
444 do_unicast (nc, pos, msg, can_drop);
450 /* end of server_nc.c */