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 struct PendingMessageList
39 struct PendingMessageList *next;
41 const struct GNUNET_MessageHeader *msg;
51 struct ClientList *next;
53 struct GNUNET_SERVER_NotificationContext *nc;
55 struct GNUNET_SERVER_Client *client;
57 struct GNUNET_CONNECTION_TransmitHandle *th;
59 struct PendingMessageList *pending_head;
61 struct PendingMessageList *pending_tail;
63 unsigned int num_pending;
69 * The notification context is the key datastructure for a conveniance
70 * API used for transmission of notifications to the client until the
71 * client disconnects (or the notification context is destroyed, in
72 * which case we disconnect these clients). Essentially, all
73 * (notification) messages are queued up until the client is able to
76 struct GNUNET_SERVER_NotificationContext
79 struct GNUNET_SERVER_Handle *server;
81 struct ClientList *clients;
83 unsigned int queue_length;
89 handle_client_disconnect (void *cls,
90 struct GNUNET_SERVER_Client *client)
92 struct GNUNET_SERVER_NotificationContext *nc = cls;
93 struct ClientList *pos;
94 struct ClientList *prev;
95 struct PendingMessageList *pml;
101 if (pos->client == client)
109 nc->clients = pos->next;
111 prev->next = pos->next;
112 while (NULL != (pml = pos->pending_head))
114 pos->pending_head = pml->next;
122 * Create a new notification context.
124 * @param server server for which this function creates the context
125 * @param queue_length maximum number of messages to keep in
126 * the notification queue; optional messages are dropped
127 * it the queue gets longer than this number of messages
128 * @return handle to the notification context
130 struct GNUNET_SERVER_NotificationContext *
131 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
132 unsigned int queue_length)
134 struct GNUNET_SERVER_NotificationContext *ret;
136 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
137 ret->server = server;
138 ret->queue_length = queue_length;
139 GNUNET_SERVER_disconnect_notify (server,
140 &handle_client_disconnect,
147 * Destroy the context, force disconnect for all clients.
149 * @param nc context to destroy.
152 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
154 struct ClientList *pos;
155 struct PendingMessageList *pml;
157 while (NULL != (pos = nc->clients))
159 nc->clients = pos->next;
160 GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
161 GNUNET_SERVER_client_drop (pos->client);
162 while (NULL != (pml = pos->pending_head))
164 pos->pending_head = pml->next;
169 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
170 &handle_client_disconnect,
177 * Add a client to the notification context.
179 * @param nc context to modify
180 * @param client client to add
183 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
184 struct GNUNET_SERVER_Client *client)
186 struct ClientList *cl;
188 cl = GNUNET_malloc (sizeof (struct ClientList));
189 cl->next = nc->clients;
197 * Function called to notify a client about the socket begin ready to
198 * queue more data. "buf" will be NULL and "size" zero if the socket
199 * was closed for writing in the meantime.
201 * @param cls the 'struct ClientList *'
202 * @param size number of bytes available in buf
203 * @param buf where the callee should write the message
204 * @return number of bytes written to buf
207 transmit_message (void *cls,
211 struct ClientList *cl = cls;
213 struct PendingMessageList *pml;
220 /* 'cl' should be freed via disconnect notification shortly */
224 while (cl->pending_head != NULL)
226 pml = cl->pending_head;
227 cl->pending_head = pml->next;
228 if (pml->next == NULL)
229 cl->pending_tail = NULL;
230 msize = ntohs (pml->msg->size);
233 memcpy (&cbuf[ret], pml->msg, msize);
238 if (cl->pending_head != NULL)
239 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
240 ntohs (cl->pending_head->msg->size),
241 GNUNET_TIME_UNIT_FOREVER_REL,
249 * Send a message to a particular client.
251 * @param nc context to modify
252 * @param client client to transmit to
253 * @param msg message to send
254 * @param can_drop can this message be dropped due to queue length limitations
257 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
258 struct ClientList *client,
259 const struct GNUNET_MessageHeader *msg,
262 struct PendingMessageList *pml;
265 if ( (client->num_pending > nc->queue_length) &&
266 (GNUNET_YES == can_drop) )
268 if (client->num_pending > nc->queue_length)
270 /* FIXME: consider checking for other messages in the
271 queue that are 'droppable' */
273 size = ntohs (msg->size);
274 pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
275 pml->msg = (const struct GNUNET_MessageHeader*) &pml[1];
276 pml->can_drop = can_drop;
277 memcpy (&pml[1], msg, size);
279 if (client->pending_tail != NULL)
280 client->pending_tail->next = pml;
282 client->pending_head = pml;
283 client->pending_tail = pml;
284 if (client->th == NULL)
285 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
286 ntohs (client->pending_head->msg->size),
287 GNUNET_TIME_UNIT_FOREVER_REL,
294 * Send a message to a particular client; must have
295 * already been added to the notification context.
297 * @param nc context to modify
298 * @param client client to transmit to
299 * @param msg message to send
300 * @param can_drop can this message be dropped due to queue length limitations
303 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
304 struct GNUNET_SERVER_Client *client,
305 const struct GNUNET_MessageHeader *msg,
308 struct ClientList *pos;
313 if (pos->client == client)
317 GNUNET_assert (pos != NULL);
318 do_unicast (nc, pos, msg, can_drop);
323 * Send a message to all clients of this context.
325 * @param nc context to modify
326 * @param msg message to send
327 * @param can_drop can this message be dropped due to queue length limitations
330 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
331 const struct GNUNET_MessageHeader *msg,
334 struct ClientList *pos;
339 do_unicast (nc, pos, msg, can_drop);
345 /* end of server_nc.c */