diff options
Diffstat (limited to 'src/util/nc.c')
-rw-r--r-- | src/util/nc.c | 97 |
1 files changed, 46 insertions, 51 deletions
diff --git a/src/util/nc.c b/src/util/nc.c index 7a3c5877e..ab7ef122d 100644 --- a/src/util/nc.c +++ b/src/util/nc.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file util/nc.c | 22 | * @file util/nc.c |
@@ -28,15 +28,13 @@ | |||
28 | #include "platform.h" | 28 | #include "platform.h" |
29 | #include "gnunet_util_lib.h" | 29 | #include "gnunet_util_lib.h" |
30 | 30 | ||
31 | #define LOG(kind,...) GNUNET_log_from (kind, "util-nc", __VA_ARGS__) | 31 | #define LOG(kind, ...) GNUNET_log_from(kind, "util-nc", __VA_ARGS__) |
32 | 32 | ||
33 | 33 | ||
34 | /** | 34 | /** |
35 | * Lists of subscribers we manage for notifications. | 35 | * Lists of subscribers we manage for notifications. |
36 | */ | 36 | */ |
37 | struct SubscriberList | 37 | struct SubscriberList { |
38 | { | ||
39 | |||
40 | /** | 38 | /** |
41 | * This is a doubly linked list. | 39 | * This is a doubly linked list. |
42 | */ | 40 | */ |
@@ -57,12 +55,11 @@ struct SubscriberList | |||
57 | * the MQ's destruction. | 55 | * the MQ's destruction. |
58 | */ | 56 | */ |
59 | struct GNUNET_MQ_DestroyNotificationHandle *mq_nh; | 57 | struct GNUNET_MQ_DestroyNotificationHandle *mq_nh; |
60 | 58 | ||
61 | /** | 59 | /** |
62 | * Message queue for the subscriber. | 60 | * Message queue for the subscriber. |
63 | */ | 61 | */ |
64 | struct GNUNET_MQ_Handle *mq; | 62 | struct GNUNET_MQ_Handle *mq; |
65 | |||
66 | }; | 63 | }; |
67 | 64 | ||
68 | 65 | ||
@@ -74,9 +71,7 @@ struct SubscriberList | |||
74 | * (notification) messages are queued up until the subscriber is able to | 71 | * (notification) messages are queued up until the subscriber is able to |
75 | * read them. | 72 | * read them. |
76 | */ | 73 | */ |
77 | struct GNUNET_NotificationContext | 74 | struct GNUNET_NotificationContext { |
78 | { | ||
79 | |||
80 | /** | 75 | /** |
81 | * Head of list of subscribers receiving notifications. | 76 | * Head of list of subscribers receiving notifications. |
82 | */ | 77 | */ |
@@ -91,7 +86,6 @@ struct GNUNET_NotificationContext | |||
91 | * Maximum number of optional messages to queue per subscriber. | 86 | * Maximum number of optional messages to queue per subscriber. |
92 | */ | 87 | */ |
93 | unsigned int queue_length; | 88 | unsigned int queue_length; |
94 | |||
95 | }; | 89 | }; |
96 | 90 | ||
97 | 91 | ||
@@ -101,15 +95,15 @@ struct GNUNET_NotificationContext | |||
101 | * @param cls our `struct SubscriberList *` | 95 | * @param cls our `struct SubscriberList *` |
102 | */ | 96 | */ |
103 | static void | 97 | static void |
104 | handle_mq_destroy (void *cls) | 98 | handle_mq_destroy(void *cls) |
105 | { | 99 | { |
106 | struct SubscriberList *pos = cls; | 100 | struct SubscriberList *pos = cls; |
107 | struct GNUNET_NotificationContext *nc = pos->nc; | 101 | struct GNUNET_NotificationContext *nc = pos->nc; |
108 | 102 | ||
109 | GNUNET_CONTAINER_DLL_remove (nc->subscribers_head, | 103 | GNUNET_CONTAINER_DLL_remove(nc->subscribers_head, |
110 | nc->subscribers_tail, | 104 | nc->subscribers_tail, |
111 | pos); | 105 | pos); |
112 | GNUNET_free (pos); | 106 | GNUNET_free(pos); |
113 | } | 107 | } |
114 | 108 | ||
115 | 109 | ||
@@ -122,11 +116,11 @@ handle_mq_destroy (void *cls) | |||
122 | * @return handle to the notification context | 116 | * @return handle to the notification context |
123 | */ | 117 | */ |
124 | struct GNUNET_NotificationContext * | 118 | struct GNUNET_NotificationContext * |
125 | GNUNET_notification_context_create (unsigned int queue_length) | 119 | GNUNET_notification_context_create(unsigned int queue_length) |
126 | { | 120 | { |
127 | struct GNUNET_NotificationContext *nc; | 121 | struct GNUNET_NotificationContext *nc; |
128 | 122 | ||
129 | nc = GNUNET_new (struct GNUNET_NotificationContext); | 123 | nc = GNUNET_new(struct GNUNET_NotificationContext); |
130 | nc->queue_length = queue_length; | 124 | nc->queue_length = queue_length; |
131 | return nc; | 125 | return nc; |
132 | } | 126 | } |
@@ -138,19 +132,19 @@ GNUNET_notification_context_create (unsigned int queue_length) | |||
138 | * @param nc context to destroy. | 132 | * @param nc context to destroy. |
139 | */ | 133 | */ |
140 | void | 134 | void |
141 | GNUNET_notification_context_destroy (struct GNUNET_NotificationContext *nc) | 135 | GNUNET_notification_context_destroy(struct GNUNET_NotificationContext *nc) |
142 | { | 136 | { |
143 | struct SubscriberList *pos; | 137 | struct SubscriberList *pos; |
144 | 138 | ||
145 | while (NULL != (pos = nc->subscribers_head)) | 139 | while (NULL != (pos = nc->subscribers_head)) |
146 | { | 140 | { |
147 | GNUNET_CONTAINER_DLL_remove (nc->subscribers_head, | 141 | GNUNET_CONTAINER_DLL_remove(nc->subscribers_head, |
148 | nc->subscribers_tail, | 142 | nc->subscribers_tail, |
149 | pos); | 143 | pos); |
150 | GNUNET_MQ_destroy_notify_cancel (pos->mq_nh); | 144 | GNUNET_MQ_destroy_notify_cancel(pos->mq_nh); |
151 | GNUNET_free (pos); | 145 | GNUNET_free(pos); |
152 | } | 146 | } |
153 | GNUNET_free (nc); | 147 | GNUNET_free(nc); |
154 | } | 148 | } |
155 | 149 | ||
156 | 150 | ||
@@ -161,23 +155,24 @@ GNUNET_notification_context_destroy (struct GNUNET_NotificationContext *nc) | |||
161 | * @param mq message queue add | 155 | * @param mq message queue add |
162 | */ | 156 | */ |
163 | void | 157 | void |
164 | GNUNET_notification_context_add (struct GNUNET_NotificationContext *nc, | 158 | GNUNET_notification_context_add(struct GNUNET_NotificationContext *nc, |
165 | struct GNUNET_MQ_Handle *mq) | 159 | struct GNUNET_MQ_Handle *mq) |
166 | { | 160 | { |
167 | struct SubscriberList *cl; | 161 | struct SubscriberList *cl; |
168 | 162 | ||
169 | for (cl = nc->subscribers_head; NULL != cl; cl = cl->next) | 163 | for (cl = nc->subscribers_head; NULL != cl; cl = cl->next) |
170 | if (cl->mq == mq) | 164 | if (cl->mq == mq) |
171 | return; /* already present */ | 165 | return; |
172 | cl = GNUNET_new (struct SubscriberList); | 166 | /* already present */ |
173 | GNUNET_CONTAINER_DLL_insert (nc->subscribers_head, | 167 | cl = GNUNET_new(struct SubscriberList); |
174 | nc->subscribers_tail, | 168 | GNUNET_CONTAINER_DLL_insert(nc->subscribers_head, |
175 | cl); | 169 | nc->subscribers_tail, |
170 | cl); | ||
176 | cl->nc = nc; | 171 | cl->nc = nc; |
177 | cl->mq = mq; | 172 | cl->mq = mq; |
178 | cl->mq_nh = GNUNET_MQ_destroy_notify (cl->mq, | 173 | cl->mq_nh = GNUNET_MQ_destroy_notify(cl->mq, |
179 | &handle_mq_destroy, | 174 | &handle_mq_destroy, |
180 | cl); | 175 | cl); |
181 | } | 176 | } |
182 | 177 | ||
183 | 178 | ||
@@ -189,22 +184,22 @@ GNUNET_notification_context_add (struct GNUNET_NotificationContext *nc, | |||
189 | * @param can_drop can this message be dropped due to queue length limitations | 184 | * @param can_drop can this message be dropped due to queue length limitations |
190 | */ | 185 | */ |
191 | void | 186 | void |
192 | GNUNET_notification_context_broadcast (struct GNUNET_NotificationContext *nc, | 187 | GNUNET_notification_context_broadcast(struct GNUNET_NotificationContext *nc, |
193 | const struct GNUNET_MessageHeader *msg, | 188 | const struct GNUNET_MessageHeader *msg, |
194 | int can_drop) | 189 | int can_drop) |
195 | { | 190 | { |
196 | struct SubscriberList *pos; | 191 | struct SubscriberList *pos; |
197 | struct GNUNET_MQ_Envelope *env; | 192 | struct GNUNET_MQ_Envelope *env; |
198 | 193 | ||
199 | for (pos = nc->subscribers_head; NULL != pos; pos = pos->next) | 194 | for (pos = nc->subscribers_head; NULL != pos; pos = pos->next) |
200 | { | 195 | { |
201 | if ( (GNUNET_YES == can_drop) && | 196 | if ((GNUNET_YES == can_drop) && |
202 | (GNUNET_MQ_get_length (pos->mq) > nc->queue_length) ) | 197 | (GNUNET_MQ_get_length(pos->mq) > nc->queue_length)) |
203 | continue; | 198 | continue; |
204 | env = GNUNET_MQ_msg_copy (msg); | 199 | env = GNUNET_MQ_msg_copy(msg); |
205 | GNUNET_MQ_send (pos->mq, | 200 | GNUNET_MQ_send(pos->mq, |
206 | env); | 201 | env); |
207 | } | 202 | } |
208 | } | 203 | } |
209 | 204 | ||
210 | 205 | ||
@@ -215,7 +210,7 @@ GNUNET_notification_context_broadcast (struct GNUNET_NotificationContext *nc, | |||
215 | * @return number of current subscribers | 210 | * @return number of current subscribers |
216 | */ | 211 | */ |
217 | unsigned int | 212 | unsigned int |
218 | GNUNET_notification_context_get_size (struct GNUNET_NotificationContext *nc) | 213 | GNUNET_notification_context_get_size(struct GNUNET_NotificationContext *nc) |
219 | { | 214 | { |
220 | unsigned int num; | 215 | unsigned int num; |
221 | struct SubscriberList *pos; | 216 | struct SubscriberList *pos; |