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"
37 * Entry in list of messages pending to be transmitted.
39 struct PendingMessageList
43 * This is a linked list.
45 struct PendingMessageList *next;
48 * Message to transmit (allocated at the end of this
49 * struct, do not free)
51 const struct GNUNET_MessageHeader *msg;
54 * Can this message be dropped?
62 * Lists of clients we manage for notifications.
68 * This is a linked list.
70 struct ClientList *next;
73 * Overall context this client belongs to.
75 struct GNUNET_SERVER_NotificationContext *nc;
78 * Handle to the client.
80 struct GNUNET_SERVER_Client *client;
83 * Handle for pending transmission request to the client (or NULL).
85 struct GNUNET_CONNECTION_TransmitHandle *th;
88 * Head of linked list of requests queued for transmission.
90 struct PendingMessageList *pending_head;
93 * Tail of linked list of requests queued for transmission.
95 struct PendingMessageList *pending_tail;
98 * Number of messages currently in the list.
100 unsigned int num_pending;
106 * The notification context is the key datastructure for a conveniance
107 * API used for transmission of notifications to the client until the
108 * client disconnects (or the notification context is destroyed, in
109 * which case we disconnect these clients). Essentially, all
110 * (notification) messages are queued up until the client is able to
113 struct GNUNET_SERVER_NotificationContext
117 * Server we do notifications for.
119 struct GNUNET_SERVER_Handle *server;
122 * List of clients receiving notifications.
124 struct ClientList *clients;
127 * Maximum number of optional messages to queue per client.
129 unsigned int queue_length;
135 * Client has disconnected, clean up.
137 * @param cls our 'struct GNUNET_SERVER_NotificationContext *'
138 * @param client handle of client that disconnected
141 handle_client_disconnect (void *cls,
142 struct GNUNET_SERVER_Client *client)
144 struct GNUNET_SERVER_NotificationContext *nc = cls;
145 struct ClientList *pos;
146 struct ClientList *prev;
147 struct PendingMessageList *pml;
158 if (pos->client == client)
166 nc->clients = pos->next;
168 prev->next = pos->next;
169 while (NULL != (pml = pos->pending_head))
171 pos->pending_head = pml->next;
174 GNUNET_SERVER_client_drop (client);
177 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
185 * Create a new notification context.
187 * @param server server for which this function creates the context
188 * @param queue_length maximum number of messages to keep in
189 * the notification queue; optional messages are dropped
190 * it the queue gets longer than this number of messages
191 * @return handle to the notification context
193 struct GNUNET_SERVER_NotificationContext *
194 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
195 unsigned int queue_length)
197 struct GNUNET_SERVER_NotificationContext *ret;
199 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
200 ret->server = server;
201 ret->queue_length = queue_length;
202 GNUNET_SERVER_disconnect_notify (server,
203 &handle_client_disconnect,
210 * Destroy the context, force disconnect for all clients.
212 * @param nc context to destroy.
215 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
217 struct ClientList *pos;
218 struct PendingMessageList *pml;
220 while (NULL != (pos = nc->clients))
222 nc->clients = pos->next;
223 GNUNET_SERVER_client_drop (pos->client);
224 GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
225 while (NULL != (pml = pos->pending_head))
227 pos->pending_head = pml->next;
232 if (nc->server != NULL)
233 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
234 &handle_client_disconnect,
241 * Add a client to the notification context.
243 * @param nc context to modify
244 * @param client client to add
247 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
248 struct GNUNET_SERVER_Client *client)
250 struct ClientList *cl;
252 cl = GNUNET_malloc (sizeof (struct ClientList));
253 cl->next = nc->clients;
256 GNUNET_SERVER_client_keep (client);
262 * Function called to notify a client about the socket begin ready to
263 * queue more data. "buf" will be NULL and "size" zero if the socket
264 * was closed for writing in the meantime.
266 * @param cls the 'struct ClientList *'
267 * @param size number of bytes available in buf
268 * @param buf where the callee should write the message
269 * @return number of bytes written to buf
272 transmit_message (void *cls,
276 struct ClientList *cl = cls;
278 struct PendingMessageList *pml;
285 /* 'cl' should be freed via disconnect notification shortly */
289 while (cl->pending_head != NULL)
291 pml = cl->pending_head;
292 cl->pending_head = pml->next;
293 if (pml->next == NULL)
294 cl->pending_tail = NULL;
295 msize = ntohs (pml->msg->size);
298 memcpy (&cbuf[ret], pml->msg, msize);
304 if (cl->pending_head != NULL)
305 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
306 ntohs (cl->pending_head->msg->size),
307 GNUNET_TIME_UNIT_FOREVER_REL,
315 * Send a message to a particular client.
317 * @param nc context to modify
318 * @param client client to transmit to
319 * @param msg message to send
320 * @param can_drop can this message be dropped due to queue length limitations
323 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
324 struct ClientList *client,
325 const struct GNUNET_MessageHeader *msg,
328 struct PendingMessageList *pml;
331 if ( (client->num_pending > nc->queue_length) &&
332 (GNUNET_YES == can_drop) )
334 if (client->num_pending > nc->queue_length)
336 /* FIXME: consider checking for other messages in the
337 queue that are 'droppable' */
339 client->num_pending++;
340 size = ntohs (msg->size);
341 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
342 pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
343 pml->can_drop = can_drop;
344 memcpy (&pml[1], msg, size);
346 if (client->pending_tail != NULL)
347 client->pending_tail->next = pml;
349 client->pending_head = pml;
350 client->pending_tail = pml;
351 if (client->th == NULL)
352 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
353 ntohs (client->pending_head->msg->size),
354 GNUNET_TIME_UNIT_FOREVER_REL,
361 * Send a message to a particular client; must have
362 * already been added to the notification context.
364 * @param nc context to modify
365 * @param client client to transmit to
366 * @param msg message to send
367 * @param can_drop can this message be dropped due to queue length limitations
370 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
371 struct GNUNET_SERVER_Client *client,
372 const struct GNUNET_MessageHeader *msg,
375 struct ClientList *pos;
380 if (pos->client == client)
384 GNUNET_assert (pos != NULL);
385 do_unicast (nc, pos, msg, can_drop);
390 * Send a message to all clients of this context.
392 * @param nc context to modify
393 * @param msg message to send
394 * @param can_drop can this message be dropped due to queue length limitations
397 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
398 const struct GNUNET_MessageHeader *msg,
401 struct ClientList *pos;
406 do_unicast (nc, pos, msg, can_drop);
412 /* end of server_nc.c */