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_Client *client;
55 struct GNUNET_CONNECTION_TransmitHandle *th;
57 struct PendingMessageList *pending;
59 unsigned int num_pending;
65 * The notification context is the key datastructure for a conveniance
66 * API used for transmission of notifications to the client until the
67 * client disconnects (or the notification context is destroyed, in
68 * which case we disconnect these clients). Essentially, all
69 * (notification) messages are queued up until the client is able to
72 struct GNUNET_SERVER_NotificationContext
75 struct GNUNET_SERVER_Handle *server;
77 struct ClientList *clients;
79 unsigned int queue_length;
85 handle_client_disconnect (void *cls,
86 struct GNUNET_SERVER_Client *client)
88 struct GNUNET_SERVER_NotificationContext *nc = cls;
89 struct ClientList *pos;
90 struct ClientList *prev;
91 struct PendingMessageList *pml;
97 if (pos->client == client)
105 nc->clients = pos->next;
107 prev->next = pos->next;
108 while (NULL != (pml = pos->pending))
110 pos->pending = pml->next;
118 * Create a new notification context.
120 * @param server server for which this function creates the context
121 * @param queue_length maximum number of messages to keep in
122 * the notification queue; optional messages are dropped
123 * it the queue gets longer than this number of messages
124 * @return handle to the notification context
126 struct GNUNET_SERVER_NotificationContext *
127 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
128 unsigned int queue_length)
130 struct GNUNET_SERVER_NotificationContext *ret;
132 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
133 ret->server = server;
134 ret->queue_length = queue_length;
135 GNUNET_SERVER_disconnect_notify (server,
136 &handle_client_disconnect,
143 * Destroy the context, force disconnect for all clients.
145 * @param nc context to destroy.
148 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc)
150 struct ClientList *pos;
151 struct PendingMessageList *pml;
153 while (NULL != (pos = nc->clients))
155 nc->clients = pos->next;
156 GNUNET_SERVER_receive_done (pos->client, GNUNET_NO);
157 GNUNET_SERVER_client_drop (pos->client);
158 while (NULL != (pml = pos->pending))
160 pos->pending = pml->next;
165 GNUNET_SERVER_disconnect_notify_cancel (nc->server,
166 &handle_client_disconnect,
173 * Add a client to the notification context.
175 * @param nc context to modify
176 * @param client client to add
179 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
180 struct GNUNET_SERVER_Client *client)
186 * Send a message to a particular client; must have
187 * already been added to the notification context.
189 * @param nc context to modify
190 * @param client client to transmit to
191 * @param msg message to send
192 * @param can_drop can this message be dropped due to queue length limitations
195 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
196 struct GNUNET_SERVER_Client *client,
197 const struct GNUNET_MessageHeader *msg,
204 * Send a message to all clients of this context.
206 * @param nc context to modify
207 * @param msg message to send
208 * @param can_drop can this message be dropped due to queue length limitations
211 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
212 const struct GNUNET_MessageHeader *msg,
218 /* end of server_nc.c */