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 it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
18 * @brief convenience functions for transmission of
19 * messages to multiple clients
20 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
26 #define LOG(kind,...) GNUNET_log_from (kind, "util-nc", __VA_ARGS__)
30 * Lists of subscribers we manage for notifications.
36 * This is a doubly linked list.
38 struct SubscriberList *next;
41 * This is a doubly linked list.
43 struct SubscriberList *prev;
46 * Overall context this subscriber belongs to.
48 struct GNUNET_NotificationContext *nc;
51 * Handle where we registered with @e mq to be told about
52 * the MQ's destruction.
54 struct GNUNET_MQ_DestroyNotificationHandle *mq_nh;
57 * Message queue for the subscriber.
59 struct GNUNET_MQ_Handle *mq;
65 * The notification context is the key datastructure for a convenience
66 * API used for transmission of notifications to the subscriber until the
67 * subscriber disconnects (or the notification context is destroyed, in
68 * which case we disconnect these subscribers). Essentially, all
69 * (notification) messages are queued up until the subscriber is able to
72 struct GNUNET_NotificationContext
76 * Head of list of subscribers receiving notifications.
78 struct SubscriberList *subscribers_head;
81 * Tail of list of subscribers receiving notifications.
83 struct SubscriberList *subscribers_tail;
86 * Maximum number of optional messages to queue per subscriber.
88 unsigned int queue_length;
94 * Subscriber has disconnected, clean up.
96 * @param cls our `struct SubscriberList *`
99 handle_mq_destroy (void *cls)
101 struct SubscriberList *pos = cls;
102 struct GNUNET_NotificationContext *nc = pos->nc;
104 GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
105 nc->subscribers_tail,
112 * Create a new notification context.
114 * @param queue_length maximum number of messages to keep in
115 * the notification queue; optional messages are dropped
116 * if the queue gets longer than this number of messages
117 * @return handle to the notification context
119 struct GNUNET_NotificationContext *
120 GNUNET_notification_context_create (unsigned int queue_length)
122 struct GNUNET_NotificationContext *nc;
124 nc = GNUNET_new (struct GNUNET_NotificationContext);
125 nc->queue_length = queue_length;
131 * Destroy the context, force disconnect for all subscribers.
133 * @param nc context to destroy.
136 GNUNET_notification_context_destroy (struct GNUNET_NotificationContext *nc)
138 struct SubscriberList *pos;
140 while (NULL != (pos = nc->subscribers_head))
142 GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
143 nc->subscribers_tail,
145 GNUNET_MQ_destroy_notify_cancel (pos->mq_nh);
153 * Add a subscriber to the notification context.
155 * @param nc context to modify
156 * @param mq message queue add
159 GNUNET_notification_context_add (struct GNUNET_NotificationContext *nc,
160 struct GNUNET_MQ_Handle *mq)
162 struct SubscriberList *cl;
164 for (cl = nc->subscribers_head; NULL != cl; cl = cl->next)
166 return; /* already present */
167 cl = GNUNET_new (struct SubscriberList);
168 GNUNET_CONTAINER_DLL_insert (nc->subscribers_head,
169 nc->subscribers_tail,
173 cl->mq_nh = GNUNET_MQ_destroy_notify (cl->mq,
180 * Send a message to all subscribers of this context.
182 * @param nc context to modify
183 * @param msg message to send
184 * @param can_drop can this message be dropped due to queue length limitations
187 GNUNET_notification_context_broadcast (struct GNUNET_NotificationContext *nc,
188 const struct GNUNET_MessageHeader *msg,
191 struct SubscriberList *pos;
192 struct GNUNET_MQ_Envelope *env;
194 for (pos = nc->subscribers_head; NULL != pos; pos = pos->next)
196 if ( (GNUNET_YES == can_drop) &&
197 (GNUNET_MQ_get_length (pos->mq) > nc->queue_length) )
199 env = GNUNET_MQ_msg_copy (msg);
200 GNUNET_MQ_send (pos->mq,
207 * Return active number of subscribers in this context.
209 * @param nc context to query
210 * @return number of current subscribers
213 GNUNET_notification_context_get_size (struct GNUNET_NotificationContext *nc)
216 struct SubscriberList *pos;
219 for (pos = nc->subscribers_head; NULL != pos; pos = pos->next)