Merge branch 'master' of gnunet.org:gnunet
[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
19 /**
20  * @file util/nc.c
21  * @brief convenience functions for transmission of
22  *        messages to multiple clients
23  * @author Christian Grothoff
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-nc", __VA_ARGS__)
30
31
32 /**
33  * Lists of subscribers we manage for notifications.
34  */
35 struct SubscriberList
36 {
37
38   /**
39    * This is a doubly linked list.
40    */
41   struct SubscriberList *next;
42
43   /**
44    * This is a doubly linked list.
45    */
46   struct SubscriberList *prev;
47
48   /**
49    * Overall context this subscriber belongs to.
50    */
51   struct GNUNET_NotificationContext *nc;
52
53   /**
54    * Handle where we registered with @e mq to be told about
55    * the MQ's destruction.
56    */
57   struct GNUNET_MQ_DestroyNotificationHandle *mq_nh;
58   
59   /**
60    * Message queue for the subscriber.
61    */
62   struct GNUNET_MQ_Handle *mq;
63
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   /**
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 /**
97  * Subscriber has disconnected, clean up.
98  *
99  * @param cls our `struct SubscriberList *`
100  */
101 static void
102 handle_mq_destroy (void *cls)
103 {
104   struct SubscriberList *pos = cls; 
105   struct GNUNET_NotificationContext *nc = pos->nc;
106
107   GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
108                                nc->subscribers_tail,
109                                pos);
110   GNUNET_free (pos);
111 }
112
113
114 /**
115  * Create a new notification context.
116  *
117  * @param queue_length maximum number of messages to keep in
118  *        the notification queue; optional messages are dropped
119  *        if the queue gets longer than this number of messages
120  * @return handle to the notification context
121  */
122 struct GNUNET_NotificationContext *
123 GNUNET_notification_context_create (unsigned int queue_length)
124 {
125   struct GNUNET_NotificationContext *nc;
126
127   nc = GNUNET_new (struct GNUNET_NotificationContext);
128   nc->queue_length = queue_length;
129   return nc;
130 }
131
132
133 /**
134  * Destroy the context, force disconnect for all subscribers.
135  *
136  * @param nc context to destroy.
137  */
138 void
139 GNUNET_notification_context_destroy (struct GNUNET_NotificationContext *nc)
140 {
141   struct SubscriberList *pos;
142
143   while (NULL != (pos = nc->subscribers_head))
144   {
145     GNUNET_CONTAINER_DLL_remove (nc->subscribers_head,
146                                  nc->subscribers_tail,
147                                  pos);
148     GNUNET_MQ_destroy_notify_cancel (pos->mq_nh);
149     GNUNET_free (pos);
150   }
151   GNUNET_free (nc);
152 }
153
154
155 /**
156  * Add a subscriber to the notification context.
157  *
158  * @param nc context to modify
159  * @param mq message queue add
160  */
161 void
162 GNUNET_notification_context_add (struct GNUNET_NotificationContext *nc,
163                                  struct GNUNET_MQ_Handle *mq)
164 {
165   struct SubscriberList *cl;
166
167   for (cl = nc->subscribers_head; NULL != cl; cl = cl->next)
168     if (cl->mq == mq)
169       return; /* 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  */
189 void
190 GNUNET_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  */
215 unsigned int
216 GNUNET_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 /* end of nc.c */