aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/nc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/nc.c')
-rw-r--r--src/lib/util/nc.c228
1 files changed, 228 insertions, 0 deletions
diff --git a/src/lib/util/nc.c b/src/lib/util/nc.c
new file mode 100644
index 000000000..2a612917c
--- /dev/null
+++ b/src/lib/util/nc.c
@@ -0,0 +1,228 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2010, 2016 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero 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.
9
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.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file util/nc.c
23 * @brief convenience functions for transmission of
24 * messages to multiple clients
25 * @author Christian Grothoff
26 */
27
28
29#include "platform.h"
30#include "gnunet_util_lib.h"
31
32#define LOG(kind, ...) GNUNET_log_from (kind, "util-nc", __VA_ARGS__)
33
34
35/**
36 * Lists of subscribers we manage for notifications.
37 */
38struct SubscriberList
39{
40 /**
41 * This is a doubly linked list.
42 */
43 struct SubscriberList *next;
44
45 /**
46 * This is a doubly linked list.
47 */
48 struct SubscriberList *prev;
49
50 /**
51 * Overall context this subscriber belongs to.
52 */
53 struct GNUNET_NotificationContext *nc;
54
55 /**
56 * Handle where we registered with @e mq to be told about
57 * the MQ's destruction.
58 */
59 struct GNUNET_MQ_DestroyNotificationHandle *mq_nh;
60
61 /**
62 * Message queue for the subscriber.
63 */
64 struct GNUNET_MQ_Handle *mq;
65};
66
67
68/**
69 * The notification context is the key datastructure for a convenience
70 * API used for transmission of notifications to the subscriber until the
71 * subscriber disconnects (or the notification context is destroyed, in
72 * which case we disconnect these subscribers). Essentially, all
73 * (notification) messages are queued up until the subscriber is able to
74 * read them.
75 */
76struct GNUNET_NotificationContext
77{
78 /**
79 * Head of list of subscribers receiving notifications.
80 */
81 struct SubscriberList *subscribers_head;
82
83 /**
84 * Tail of list of subscribers receiving notifications.
85 */
86 struct SubscriberList *subscribers_tail;
87
88 /**
89 * Maximum number of optional messages to queue per subscriber.
90 */
91 unsigned int queue_length;
92};
93
94
95/**
96 * Subscriber has disconnected, clean up.
97 *
98 * @param cls our `struct SubscriberList *`
99 */
100static void
101handle_mq_destroy (void *cls)
102{
103 struct SubscriberList *pos = cls;
104 struct GNUNET_NotificationContext *nc = pos->nc;
105
106 GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
107 nc->subscribers_tail,
108 pos);
109 GNUNET_free (pos);
110}
111
112
113/**
114 * Create a new notification context.
115 *
116 * @param queue_length maximum number of messages to keep in
117 * the notification queue; optional messages are dropped
118 * if the queue gets longer than this number of messages
119 * @return handle to the notification context
120 */
121struct GNUNET_NotificationContext *
122GNUNET_notification_context_create (unsigned int queue_length)
123{
124 struct GNUNET_NotificationContext *nc;
125
126 nc = GNUNET_new (struct GNUNET_NotificationContext);
127 nc->queue_length = queue_length;
128 return nc;
129}
130
131
132/**
133 * Destroy the context, force disconnect for all subscribers.
134 *
135 * @param nc context to destroy.
136 */
137void
138GNUNET_notification_context_destroy (struct GNUNET_NotificationContext *nc)
139{
140 struct SubscriberList *pos;
141
142 while (NULL != (pos = nc->subscribers_head))
143 {
144 GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
145 nc->subscribers_tail,
146 pos);
147 GNUNET_MQ_destroy_notify_cancel (pos->mq_nh);
148 GNUNET_free (pos);
149 }
150 GNUNET_free (nc);
151}
152
153
154/**
155 * Add a subscriber to the notification context.
156 *
157 * @param nc context to modify
158 * @param mq message queue add
159 */
160void
161GNUNET_notification_context_add (struct GNUNET_NotificationContext *nc,
162 struct GNUNET_MQ_Handle *mq)
163{
164 struct SubscriberList *cl;
165
166 for (cl = nc->subscribers_head; NULL != cl; cl = cl->next)
167 if (cl->mq == mq)
168 return;
169 /* already present */
170 cl = GNUNET_new (struct SubscriberList);
171 GNUNET_CONTAINER_DLL_insert (nc->subscribers_head,
172 nc->subscribers_tail,
173 cl);
174 cl->nc = nc;
175 cl->mq = mq;
176 cl->mq_nh = GNUNET_MQ_destroy_notify (cl->mq,
177 &handle_mq_destroy,
178 cl);
179}
180
181
182/**
183 * Send a message to all subscribers of this context.
184 *
185 * @param nc context to modify
186 * @param msg message to send
187 * @param can_drop can this message be dropped due to queue length limitations
188 */
189void
190GNUNET_notification_context_broadcast (struct GNUNET_NotificationContext *nc,
191 const struct GNUNET_MessageHeader *msg,
192 int can_drop)
193{
194 struct SubscriberList *pos;
195 struct GNUNET_MQ_Envelope *env;
196
197 for (pos = nc->subscribers_head; NULL != pos; pos = pos->next)
198 {
199 if ((GNUNET_YES == can_drop) &&
200 (GNUNET_MQ_get_length (pos->mq) > nc->queue_length))
201 continue;
202 env = GNUNET_MQ_msg_copy (msg);
203 GNUNET_MQ_send (pos->mq,
204 env);
205 }
206}
207
208
209/**
210 * Return active number of subscribers in this context.
211 *
212 * @param nc context to query
213 * @return number of current subscribers
214 */
215unsigned int
216GNUNET_notification_context_get_size (struct GNUNET_NotificationContext *nc)
217{
218 unsigned int num;
219 struct SubscriberList *pos;
220
221 num = 0;
222 for (pos = nc->subscribers_head; NULL != pos; pos = pos->next)
223 num++;
224 return num;
225}
226
227
228/* end of nc.c */