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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Client disconnected, cleaning up %u messages in NC queue\n",
173 nc->clients = pos->next;
175 prev->next = pos->next;
176 while (NULL != (pml = pos->pending_head))
178 pos->pending_head = pml->next;
181 GNUNET_SERVER_client_drop (client);
184 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
192 * Create a new notification context.
194 * @param server server for which this function creates the context
195 * @param queue_length maximum number of messages to keep in
196 * the notification queue; optional messages are dropped
197 * it the queue gets longer than this number of messages
198 * @return handle to the notification context
200 struct GNUNET_SERVER_NotificationContext *
201 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
202 unsigned int queue_length)
204 struct GNUNET_SERVER_NotificationContext *ret;
206 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
207 ret->server = server;
208 ret->queue_length = queue_length;
209 GNUNET_SERVER_disconnect_notify (server,
210 &handle_client_disconnect,
217 * Destroy the context, force disconnect for all clients.
219 * @param nc context to destroy.
222 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
224 struct ClientList *pos;
225 struct PendingMessageList *pml;
227 while (NULL != (pos = nc->clients))
229 nc->clients = pos->next;
230 GNUNET_SERVER_client_drop (pos->client);
231 GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
232 while (NULL != (pml = pos->pending_head))
234 pos->pending_head = pml->next;
239 if (nc->server != NULL)
240 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
241 &handle_client_disconnect,
248 * Add a client to the notification context.
250 * @param nc context to modify
251 * @param client client to add
254 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
255 struct GNUNET_SERVER_Client *client)
257 struct ClientList *cl;
259 cl = GNUNET_malloc (sizeof (struct ClientList));
260 cl->next = nc->clients;
263 GNUNET_SERVER_client_keep (client);
269 * Function called to notify a client about the socket begin ready to
270 * queue more data. "buf" will be NULL and "size" zero if the socket
271 * was closed for writing in the meantime.
273 * @param cls the 'struct ClientList *'
274 * @param size number of bytes available in buf
275 * @param buf where the callee should write the message
276 * @return number of bytes written to buf
279 transmit_message (void *cls,
283 struct ClientList *cl = cls;
285 struct PendingMessageList *pml;
292 /* 'cl' should be freed via disconnect notification shortly */
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Failed to transmit message from NC queue to client\n");
300 while (cl->pending_head != NULL)
302 pml = cl->pending_head;
303 msize = ntohs (pml->msg->size);
306 cl->pending_head = pml->next;
307 if (pml->next == NULL)
308 cl->pending_tail = NULL;
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Copying message of type %u and size %u from pending queue to transmission buffer\n",
312 ntohs (pml->msg->type),
315 memcpy (&cbuf[ret], pml->msg, msize);
321 if (cl->pending_head != NULL)
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Have %u messages left in NC queue, will try transmission again\n",
328 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
329 ntohs (cl->pending_head->msg->size),
330 GNUNET_TIME_UNIT_FOREVER_REL,
335 GNUNET_assert (cl->num_pending == 0);
341 * Send a message to a particular client.
343 * @param nc context to modify
344 * @param client client to transmit to
345 * @param msg message to send
346 * @param can_drop can this message be dropped due to queue length limitations
349 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
350 struct ClientList *client,
351 const struct GNUNET_MessageHeader *msg,
354 struct PendingMessageList *pml;
357 if ( (client->num_pending > nc->queue_length) &&
358 (GNUNET_YES == can_drop) )
360 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
361 "Dropping message of type %u and size %u due to full queue (%u entries)\n",
364 (unsigned int) nc->queue_length);
367 if (client->num_pending > nc->queue_length)
369 /* FIXME: consider checking for other messages in the
370 queue that are 'droppable' */
372 client->num_pending++;
373 size = ntohs (msg->size);
374 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
375 pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
376 pml->can_drop = can_drop;
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
382 (unsigned int) nc->queue_length);
384 memcpy (&pml[1], msg, size);
386 if (client->pending_tail != NULL)
387 client->pending_tail->next = pml;
389 client->pending_head = pml;
390 client->pending_tail = pml;
391 if (client->th == NULL)
392 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
393 ntohs (client->pending_head->msg->size),
394 GNUNET_TIME_UNIT_FOREVER_REL,
401 * Send a message to a particular client; must have
402 * already been added to the notification context.
404 * @param nc context to modify
405 * @param client client to transmit to
406 * @param msg message to send
407 * @param can_drop can this message be dropped due to queue length limitations
410 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
411 struct GNUNET_SERVER_Client *client,
412 const struct GNUNET_MessageHeader *msg,
415 struct ClientList *pos;
420 if (pos->client == client)
424 GNUNET_assert (pos != NULL);
425 do_unicast (nc, pos, msg, can_drop);
430 * Send a message to all clients of this context.
432 * @param nc context to modify
433 * @param msg message to send
434 * @param can_drop can this message be dropped due to queue length limitations
437 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
438 const struct GNUNET_MessageHeader *msg,
441 struct ClientList *pos;
446 do_unicast (nc, pos, msg, can_drop);
452 /* end of server_nc.c */