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;
153 if (pos->client == client)
161 nc->clients = pos->next;
163 prev->next = pos->next;
164 while (NULL != (pml = pos->pending_head))
166 pos->pending_head = pml->next;
169 GNUNET_SERVER_client_drop (client);
175 * Create a new notification context.
177 * @param server server for which this function creates the context
178 * @param queue_length maximum number of messages to keep in
179 * the notification queue; optional messages are dropped
180 * it the queue gets longer than this number of messages
181 * @return handle to the notification context
183 struct GNUNET_SERVER_NotificationContext *
184 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
185 unsigned int queue_length)
187 struct GNUNET_SERVER_NotificationContext *ret;
189 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
190 ret->server = server;
191 ret->queue_length = queue_length;
192 GNUNET_SERVER_disconnect_notify (server,
193 &handle_client_disconnect,
200 * Destroy the context, force disconnect for all clients.
202 * @param nc context to destroy.
205 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
207 struct ClientList *pos;
208 struct PendingMessageList *pml;
210 while (NULL != (pos = nc->clients))
212 nc->clients = pos->next;
213 GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
214 GNUNET_SERVER_client_drop (pos->client);
215 while (NULL != (pml = pos->pending_head))
217 pos->pending_head = pml->next;
222 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
223 &handle_client_disconnect,
230 * Add a client to the notification context.
232 * @param nc context to modify
233 * @param client client to add
236 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
237 struct GNUNET_SERVER_Client *client)
239 struct ClientList *cl;
241 cl = GNUNET_malloc (sizeof (struct ClientList));
242 cl->next = nc->clients;
245 GNUNET_SERVER_client_keep (client);
251 * Function called to notify a client about the socket begin ready to
252 * queue more data. "buf" will be NULL and "size" zero if the socket
253 * was closed for writing in the meantime.
255 * @param cls the 'struct ClientList *'
256 * @param size number of bytes available in buf
257 * @param buf where the callee should write the message
258 * @return number of bytes written to buf
261 transmit_message (void *cls,
265 struct ClientList *cl = cls;
267 struct PendingMessageList *pml;
274 /* 'cl' should be freed via disconnect notification shortly */
278 while (cl->pending_head != NULL)
280 pml = cl->pending_head;
281 cl->pending_head = pml->next;
282 if (pml->next == NULL)
283 cl->pending_tail = NULL;
284 msize = ntohs (pml->msg->size);
287 memcpy (&cbuf[ret], pml->msg, msize);
293 if (cl->pending_head != NULL)
294 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
295 ntohs (cl->pending_head->msg->size),
296 GNUNET_TIME_UNIT_FOREVER_REL,
304 * Send a message to a particular client.
306 * @param nc context to modify
307 * @param client client to transmit to
308 * @param msg message to send
309 * @param can_drop can this message be dropped due to queue length limitations
312 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
313 struct ClientList *client,
314 const struct GNUNET_MessageHeader *msg,
317 struct PendingMessageList *pml;
320 if ( (client->num_pending > nc->queue_length) &&
321 (GNUNET_YES == can_drop) )
323 if (client->num_pending > nc->queue_length)
325 /* FIXME: consider checking for other messages in the
326 queue that are 'droppable' */
328 client->num_pending++;
329 size = ntohs (msg->size);
330 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
331 pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
332 pml->can_drop = can_drop;
333 memcpy (&pml[1], msg, size);
335 if (client->pending_tail != NULL)
336 client->pending_tail->next = pml;
338 client->pending_head = pml;
339 client->pending_tail = pml;
340 if (client->th == NULL)
341 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
342 ntohs (client->pending_head->msg->size),
343 GNUNET_TIME_UNIT_FOREVER_REL,
350 * Send a message to a particular client; must have
351 * already been added to the notification context.
353 * @param nc context to modify
354 * @param client client to transmit to
355 * @param msg message to send
356 * @param can_drop can this message be dropped due to queue length limitations
359 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
360 struct GNUNET_SERVER_Client *client,
361 const struct GNUNET_MessageHeader *msg,
364 struct ClientList *pos;
369 if (pos->client == client)
373 GNUNET_assert (pos != NULL);
374 do_unicast (nc, pos, msg, can_drop);
379 * Send a message to all clients of this context.
381 * @param nc context to modify
382 * @param msg message to send
383 * @param can_drop can this message be dropped due to queue length limitations
386 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
387 const struct GNUNET_MessageHeader *msg,
390 struct ClientList *pos;
395 do_unicast (nc, pos, msg, can_drop);
401 /* end of server_nc.c */