error handling
[oweals/gnunet.git] / src / util / nc.c
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 #include "platform.h"
29 #include "gnunet_util_lib.h"
30
31 #define LOG(kind, ...) GNUNET_log_from (kind, "util-nc", __VA_ARGS__)
32
33
34 /**
35  * Lists of subscribers we manage for notifications.
36  */
37 struct SubscriberList
38 {
39   /**
40    * This is a doubly linked list.
41    */
42   struct SubscriberList *next;
43
44   /**
45    * This is a doubly linked list.
46    */
47   struct SubscriberList *prev;
48
49   /**
50    * Overall context this subscriber belongs to.
51    */
52   struct GNUNET_NotificationContext *nc;
53
54   /**
55    * Handle where we registered with @e mq to be told about
56    * the MQ's destruction.
57    */
58   struct GNUNET_MQ_DestroyNotificationHandle *mq_nh;
59
60   /**
61    * Message queue for the subscriber.
62    */
63   struct GNUNET_MQ_Handle *mq;
64 };
65
66
67 /**
68  * The notification context is the key datastructure for a convenience
69  * API used for transmission of notifications to the subscriber until the
70  * subscriber disconnects (or the notification context is destroyed, in
71  * which case we disconnect these subscribers).  Essentially, all
72  * (notification) messages are queued up until the subscriber is able to
73  * read them.
74  */
75 struct GNUNET_NotificationContext
76 {
77   /**
78    * Head of list of subscribers receiving notifications.
79    */
80   struct SubscriberList *subscribers_head;
81
82   /**
83    * Tail of list of subscribers receiving notifications.
84    */
85   struct SubscriberList *subscribers_tail;
86
87   /**
88    * Maximum number of optional messages to queue per subscriber.
89    */
90   unsigned int queue_length;
91 };
92
93
94 /**
95  * Subscriber has disconnected, clean up.
96  *
97  * @param cls our `struct SubscriberList *`
98  */
99 static void
100 handle_mq_destroy (void *cls)
101 {
102   struct SubscriberList *pos = cls;
103   struct GNUNET_NotificationContext *nc = pos->nc;
104
105   GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
106                                nc->subscribers_tail,
107                                pos);
108   GNUNET_free (pos);
109 }
110
111
112 /**
113  * Create a new notification context.
114  *
115  * @param queue_length maximum number of messages to keep in
116  *        the notification queue; optional messages are dropped
117  *        if the queue gets longer than this number of messages
118  * @return handle to the notification context
119  */
120 struct GNUNET_NotificationContext *
121 GNUNET_notification_context_create (unsigned int queue_length)
122 {
123   struct GNUNET_NotificationContext *nc;
124
125   nc = GNUNET_new (struct GNUNET_NotificationContext);
126   nc->queue_length = queue_length;
127   return nc;
128 }
129
130
131 /**
132  * Destroy the context, force disconnect for all subscribers.
133  *
134  * @param nc context to destroy.
135  */
136 void
137 GNUNET_notification_context_destroy (struct GNUNET_NotificationContext *nc)
138 {
139   struct SubscriberList *pos;
140
141   while (NULL != (pos = nc->subscribers_head))
142   {
143     GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
144                                  nc->subscribers_tail,
145                                  pos);
146     GNUNET_MQ_destroy_notify_cancel (pos->mq_nh);
147     GNUNET_free (pos);
148   }
149   GNUNET_free (nc);
150 }
151
152
153 /**
154  * Add a subscriber to the notification context.
155  *
156  * @param nc context to modify
157  * @param mq message queue add
158  */
159 void
160 GNUNET_notification_context_add (struct GNUNET_NotificationContext *nc,
161                                  struct GNUNET_MQ_Handle *mq)
162 {
163   struct SubscriberList *cl;
164
165   for (cl = nc->subscribers_head; NULL != cl; cl = cl->next)
166     if (cl->mq == mq)
167       return;
168   /* already present */
169   cl = GNUNET_new (struct SubscriberList);
170   GNUNET_CONTAINER_DLL_insert (nc->subscribers_head,
171                                nc->subscribers_tail,
172                                cl);
173   cl->nc = nc;
174   cl->mq = mq;
175   cl->mq_nh = GNUNET_MQ_destroy_notify (cl->mq,
176                                         &handle_mq_destroy,
177                                         cl);
178 }
179
180
181 /**
182  * Send a message to all subscribers of this context.
183  *
184  * @param nc context to modify
185  * @param msg message to send
186  * @param can_drop can this message be dropped due to queue length limitations
187  */
188 void
189 GNUNET_notification_context_broadcast (struct GNUNET_NotificationContext *nc,
190                                        const struct GNUNET_MessageHeader *msg,
191                                        int can_drop)
192 {
193   struct SubscriberList *pos;
194   struct GNUNET_MQ_Envelope *env;
195
196   for (pos = nc->subscribers_head; NULL != pos; pos = pos->next)
197   {
198     if ((GNUNET_YES == can_drop) &&
199         (GNUNET_MQ_get_length (pos->mq) > nc->queue_length))
200       continue;
201     env = GNUNET_MQ_msg_copy (msg);
202     GNUNET_MQ_send (pos->mq,
203                     env);
204   }
205 }
206
207
208 /**
209  * Return active number of subscribers in this context.
210  *
211  * @param nc context to query
212  * @return number of current subscribers
213  */
214 unsigned int
215 GNUNET_notification_context_get_size (struct GNUNET_NotificationContext *nc)
216 {
217   unsigned int num;
218   struct SubscriberList *pos;
219
220   num = 0;
221   for (pos = nc->subscribers_head; NULL != pos; pos = pos->next)
222     num++;
223   return num;
224 }
225
226
227 /* end of nc.c */