2 This file is part of GNUnet.
3 Copyright (C) 2010, 2016 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @brief convenience functions for transmission of
24 * messages to multiple clients
25 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-nc", __VA_ARGS__)
35 * Lists of subscribers we manage for notifications.
41 * This is a doubly linked list.
43 struct SubscriberList *next;
46 * This is a doubly linked list.
48 struct SubscriberList *prev;
51 * Overall context this subscriber belongs to.
53 struct GNUNET_NotificationContext *nc;
56 * Handle where we registered with @e mq to be told about
57 * the MQ's destruction.
59 struct GNUNET_MQ_DestroyNotificationHandle *mq_nh;
62 * Message queue for the subscriber.
64 struct GNUNET_MQ_Handle *mq;
70 * The notification context is the key datastructure for a convenience
71 * API used for transmission of notifications to the subscriber until the
72 * subscriber disconnects (or the notification context is destroyed, in
73 * which case we disconnect these subscribers). Essentially, all
74 * (notification) messages are queued up until the subscriber is able to
77 struct GNUNET_NotificationContext
81 * Head of list of subscribers receiving notifications.
83 struct SubscriberList *subscribers_head;
86 * Tail of list of subscribers receiving notifications.
88 struct SubscriberList *subscribers_tail;
91 * Maximum number of optional messages to queue per subscriber.
93 unsigned int queue_length;
99 * Subscriber has disconnected, clean up.
101 * @param cls our `struct SubscriberList *`
104 handle_mq_destroy (void *cls)
106 struct SubscriberList *pos = cls;
107 struct GNUNET_NotificationContext *nc = pos->nc;
109 GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
110 nc->subscribers_tail,
117 * Create a new notification context.
119 * @param queue_length maximum number of messages to keep in
120 * the notification queue; optional messages are dropped
121 * if the queue gets longer than this number of messages
122 * @return handle to the notification context
124 struct GNUNET_NotificationContext *
125 GNUNET_notification_context_create (unsigned int queue_length)
127 struct GNUNET_NotificationContext *nc;
129 nc = GNUNET_new (struct GNUNET_NotificationContext);
130 nc->queue_length = queue_length;
136 * Destroy the context, force disconnect for all subscribers.
138 * @param nc context to destroy.
141 GNUNET_notification_context_destroy (struct GNUNET_NotificationContext *nc)
143 struct SubscriberList *pos;
145 while (NULL != (pos = nc->subscribers_head))
147 GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
148 nc->subscribers_tail,
150 GNUNET_MQ_destroy_notify_cancel (pos->mq_nh);
158 * Add a subscriber to the notification context.
160 * @param nc context to modify
161 * @param mq message queue add
164 GNUNET_notification_context_add (struct GNUNET_NotificationContext *nc,
165 struct GNUNET_MQ_Handle *mq)
167 struct SubscriberList *cl;
169 for (cl = nc->subscribers_head; NULL != cl; cl = cl->next)
171 return; /* already present */
172 cl = GNUNET_new (struct SubscriberList);
173 GNUNET_CONTAINER_DLL_insert (nc->subscribers_head,
174 nc->subscribers_tail,
178 cl->mq_nh = GNUNET_MQ_destroy_notify (cl->mq,
185 * Send a message to all subscribers of this context.
187 * @param nc context to modify
188 * @param msg message to send
189 * @param can_drop can this message be dropped due to queue length limitations
192 GNUNET_notification_context_broadcast (struct GNUNET_NotificationContext *nc,
193 const struct GNUNET_MessageHeader *msg,
196 struct SubscriberList *pos;
197 struct GNUNET_MQ_Envelope *env;
199 for (pos = nc->subscribers_head; NULL != pos; pos = pos->next)
201 if ( (GNUNET_YES == can_drop) &&
202 (GNUNET_MQ_get_length (pos->mq) > nc->queue_length) )
204 env = GNUNET_MQ_msg_copy (msg);
205 GNUNET_MQ_send (pos->mq,
212 * Return active number of subscribers in this context.
214 * @param nc context to query
215 * @return number of current subscribers
218 GNUNET_notification_context_get_size (struct GNUNET_NotificationContext *nc)
221 struct SubscriberList *pos;
224 for (pos = nc->subscribers_head; NULL != pos; pos = pos->next)