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_scheduler_lib.h"
32 #include "gnunet_server_lib.h"
33 #include "gnunet_time_lib.h"
36 #define DEBUG_SERVER_NC GNUNET_NO
39 * Entry in list of messages pending to be transmitted.
41 struct PendingMessageList
45 * This is a linked list.
47 struct PendingMessageList *next;
50 * Message to transmit (allocated at the end of this
51 * struct, do not free)
53 const struct GNUNET_MessageHeader *msg;
56 * Can this message be dropped?
64 * Lists of clients we manage for notifications.
70 * This is a linked list.
72 struct ClientList *next;
75 * Overall context this client belongs to.
77 struct GNUNET_SERVER_NotificationContext *nc;
80 * Handle to the client.
82 struct GNUNET_SERVER_Client *client;
85 * Handle for pending transmission request to the client (or NULL).
87 struct GNUNET_CONNECTION_TransmitHandle *th;
90 * Head of linked list of requests queued for transmission.
92 struct PendingMessageList *pending_head;
95 * Tail of linked list of requests queued for transmission.
97 struct PendingMessageList *pending_tail;
100 * Number of messages currently in the list.
102 unsigned int num_pending;
108 * The notification context is the key datastructure for a conveniance
109 * API used for transmission of notifications to the client until the
110 * client disconnects (or the notification context is destroyed, in
111 * which case we disconnect these clients). Essentially, all
112 * (notification) messages are queued up until the client is able to
115 struct GNUNET_SERVER_NotificationContext
119 * Server we do notifications for.
121 struct GNUNET_SERVER_Handle *server;
124 * List of clients receiving notifications.
126 struct ClientList *clients;
129 * Maximum number of optional messages to queue per client.
131 unsigned int queue_length;
137 * Client has disconnected, clean up.
139 * @param cls our 'struct GNUNET_SERVER_NotificationContext *'
140 * @param client handle of client that disconnected
143 handle_client_disconnect (void *cls,
144 struct GNUNET_SERVER_Client *client)
146 struct GNUNET_SERVER_NotificationContext *nc = cls;
147 struct ClientList *pos;
148 struct ClientList *prev;
149 struct PendingMessageList *pml;
160 if (pos->client == client)
168 nc->clients = pos->next;
170 prev->next = pos->next;
171 while (NULL != (pml = pos->pending_head))
173 pos->pending_head = pml->next;
176 GNUNET_SERVER_client_drop (client);
179 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
187 * Create a new notification context.
189 * @param server server for which this function creates the context
190 * @param queue_length maximum number of messages to keep in
191 * the notification queue; optional messages are dropped
192 * it the queue gets longer than this number of messages
193 * @return handle to the notification context
195 struct GNUNET_SERVER_NotificationContext *
196 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
197 unsigned int queue_length)
199 struct GNUNET_SERVER_NotificationContext *ret;
201 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
202 ret->server = server;
203 ret->queue_length = queue_length;
204 GNUNET_SERVER_disconnect_notify (server,
205 &handle_client_disconnect,
212 * Destroy the context, force disconnect for all clients.
214 * @param nc context to destroy.
217 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
219 struct ClientList *pos;
220 struct PendingMessageList *pml;
222 while (NULL != (pos = nc->clients))
224 nc->clients = pos->next;
225 GNUNET_SERVER_client_drop (pos->client);
226 GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
227 while (NULL != (pml = pos->pending_head))
229 pos->pending_head = pml->next;
234 if (nc->server != NULL)
235 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
236 &handle_client_disconnect,
243 * Add a client to the notification context.
245 * @param nc context to modify
246 * @param client client to add
249 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
250 struct GNUNET_SERVER_Client *client)
252 struct ClientList *cl;
254 cl = GNUNET_malloc (sizeof (struct ClientList));
255 cl->next = nc->clients;
258 GNUNET_SERVER_client_keep (client);
264 * Function called to notify a client about the socket begin ready to
265 * queue more data. "buf" will be NULL and "size" zero if the socket
266 * was closed for writing in the meantime.
268 * @param cls the 'struct ClientList *'
269 * @param size number of bytes available in buf
270 * @param buf where the callee should write the message
271 * @return number of bytes written to buf
274 transmit_message (void *cls,
278 struct ClientList *cl = cls;
280 struct PendingMessageList *pml;
287 /* 'cl' should be freed via disconnect notification shortly */
291 while (cl->pending_head != NULL)
293 pml = cl->pending_head;
294 msize = ntohs (pml->msg->size);
297 cl->pending_head = pml->next;
298 if (pml->next == NULL)
299 cl->pending_tail = NULL;
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Copying message of type %u and size %u from pending queue to transmission buffer\n",
303 ntohs (pml->msg->type),
306 memcpy (&cbuf[ret], pml->msg, msize);
312 if (cl->pending_head != NULL)
313 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
314 ntohs (cl->pending_head->msg->size),
315 GNUNET_TIME_UNIT_FOREVER_REL,
323 * Send a message to a particular client.
325 * @param nc context to modify
326 * @param client client to transmit to
327 * @param msg message to send
328 * @param can_drop can this message be dropped due to queue length limitations
331 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
332 struct ClientList *client,
333 const struct GNUNET_MessageHeader *msg,
336 struct PendingMessageList *pml;
339 if ( (client->num_pending > nc->queue_length) &&
340 (GNUNET_YES == can_drop) )
342 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
343 "Dropping message of type %u and size %u due to full queue (%u entries)\n",
346 (unsigned int) nc->queue_length);
349 if (client->num_pending > nc->queue_length)
351 /* FIXME: consider checking for other messages in the
352 queue that are 'droppable' */
354 client->num_pending++;
355 size = ntohs (msg->size);
356 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
357 pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
358 pml->can_drop = can_drop;
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
364 (unsigned int) nc->queue_length);
366 memcpy (&pml[1], msg, size);
368 if (client->pending_tail != NULL)
369 client->pending_tail->next = pml;
371 client->pending_head = pml;
372 client->pending_tail = pml;
373 if (client->th == NULL)
374 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
375 ntohs (client->pending_head->msg->size),
376 GNUNET_TIME_UNIT_FOREVER_REL,
383 * Send a message to a particular client; must have
384 * already been added to the notification context.
386 * @param nc context to modify
387 * @param client client to transmit to
388 * @param msg message to send
389 * @param can_drop can this message be dropped due to queue length limitations
392 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
393 struct GNUNET_SERVER_Client *client,
394 const struct GNUNET_MessageHeader *msg,
397 struct ClientList *pos;
402 if (pos->client == client)
406 GNUNET_assert (pos != NULL);
407 do_unicast (nc, pos, msg, can_drop);
412 * Send a message to all clients of this context.
414 * @param nc context to modify
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_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
420 const struct GNUNET_MessageHeader *msg,
423 struct ClientList *pos;
428 do_unicast (nc, pos, msg, can_drop);
434 /* end of server_nc.c */