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);
180 * Create a new notification context.
182 * @param server server for which this function creates the context
183 * @param queue_length maximum number of messages to keep in
184 * the notification queue; optional messages are dropped
185 * it the queue gets longer than this number of messages
186 * @return handle to the notification context
188 struct GNUNET_SERVER_NotificationContext *
189 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
190 unsigned int queue_length)
192 struct GNUNET_SERVER_NotificationContext *ret;
194 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
195 ret->server = server;
196 ret->queue_length = queue_length;
197 GNUNET_SERVER_disconnect_notify (server,
198 &handle_client_disconnect,
205 * Destroy the context, force disconnect for all clients.
207 * @param nc context to destroy.
210 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
212 struct ClientList *pos;
213 struct PendingMessageList *pml;
215 while (NULL != (pos = nc->clients))
217 nc->clients = pos->next;
218 GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
219 GNUNET_SERVER_client_drop (pos->client);
220 while (NULL != (pml = pos->pending_head))
222 pos->pending_head = pml->next;
227 if (nc->server != NULL)
228 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
229 &handle_client_disconnect,
236 * Add a client to the notification context.
238 * @param nc context to modify
239 * @param client client to add
242 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
243 struct GNUNET_SERVER_Client *client)
245 struct ClientList *cl;
247 cl = GNUNET_malloc (sizeof (struct ClientList));
248 cl->next = nc->clients;
251 GNUNET_SERVER_client_keep (client);
257 * Function called to notify a client about the socket begin ready to
258 * queue more data. "buf" will be NULL and "size" zero if the socket
259 * was closed for writing in the meantime.
261 * @param cls the 'struct ClientList *'
262 * @param size number of bytes available in buf
263 * @param buf where the callee should write the message
264 * @return number of bytes written to buf
267 transmit_message (void *cls,
271 struct ClientList *cl = cls;
273 struct PendingMessageList *pml;
280 /* 'cl' should be freed via disconnect notification shortly */
284 while (cl->pending_head != NULL)
286 pml = cl->pending_head;
287 cl->pending_head = pml->next;
288 if (pml->next == NULL)
289 cl->pending_tail = NULL;
290 msize = ntohs (pml->msg->size);
293 memcpy (&cbuf[ret], pml->msg, msize);
299 if (cl->pending_head != NULL)
300 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
301 ntohs (cl->pending_head->msg->size),
302 GNUNET_TIME_UNIT_FOREVER_REL,
310 * Send a message to a particular client.
312 * @param nc context to modify
313 * @param client client to transmit to
314 * @param msg message to send
315 * @param can_drop can this message be dropped due to queue length limitations
318 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
319 struct ClientList *client,
320 const struct GNUNET_MessageHeader *msg,
323 struct PendingMessageList *pml;
326 if ( (client->num_pending > nc->queue_length) &&
327 (GNUNET_YES == can_drop) )
329 if (client->num_pending > nc->queue_length)
331 /* FIXME: consider checking for other messages in the
332 queue that are 'droppable' */
334 client->num_pending++;
335 size = ntohs (msg->size);
336 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
337 pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
338 pml->can_drop = can_drop;
339 memcpy (&pml[1], msg, size);
341 if (client->pending_tail != NULL)
342 client->pending_tail->next = pml;
344 client->pending_head = pml;
345 client->pending_tail = pml;
346 if (client->th == NULL)
347 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
348 ntohs (client->pending_head->msg->size),
349 GNUNET_TIME_UNIT_FOREVER_REL,
356 * Send a message to a particular client; must have
357 * already been added to the notification context.
359 * @param nc context to modify
360 * @param client client to transmit to
361 * @param msg message to send
362 * @param can_drop can this message be dropped due to queue length limitations
365 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
366 struct GNUNET_SERVER_Client *client,
367 const struct GNUNET_MessageHeader *msg,
370 struct ClientList *pos;
375 if (pos->client == client)
379 GNUNET_assert (pos != NULL);
380 do_unicast (nc, pos, msg, can_drop);
385 * Send a message to all clients of this context.
387 * @param nc context to modify
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_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
393 const struct GNUNET_MessageHeader *msg,
396 struct ClientList *pos;
401 do_unicast (nc, pos, msg, can_drop);
407 /* end of server_nc.c */