GNUnet  0.19.2
nc.c
Go to the documentation of this file.
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 
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 
39 {
44 
49 
54 
60 
65 };
66 
67 
77 {
82 
87 
91  unsigned int queue_length;
92 };
93 
94 
100 static void
101 handle_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 
123 {
125 
127  nc->queue_length = queue_length;
128  return nc;
129 }
130 
131 
137 void
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);
148  GNUNET_free (pos);
149  }
150  GNUNET_free (nc);
151 }
152 
153 
160 void
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,
178  cl);
179 }
180 
181 
189 void
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;
203  GNUNET_MQ_send (pos->mq,
204  env);
205  }
206 }
207 
208 
215 unsigned int
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 */
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static struct GNUNET_PEERINFO_NotifyContext * nc
Iterator context.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
@ GNUNET_YES
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_notification_context_destroy(struct GNUNET_NotificationContext *nc)
Destroy the context, force disconnect for all subscribers.
Definition: nc.c:138
unsigned int GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq)
Obtain the current length of the message queue.
Definition: mq.c:293
struct GNUNET_MQ_DestroyNotificationHandle * GNUNET_MQ_destroy_notify(struct GNUNET_MQ_Handle *mq, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Register function to be called whenever mq is being destroyed.
Definition: mq.c:893
unsigned int GNUNET_notification_context_get_size(struct GNUNET_NotificationContext *nc)
Return active number of subscribers in this context.
Definition: nc.c:216
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:304
void GNUNET_notification_context_broadcast(struct GNUNET_NotificationContext *nc, const struct GNUNET_MessageHeader *msg, int can_drop)
Send a message to all subscribers of this context.
Definition: nc.c:190
void GNUNET_MQ_destroy_notify_cancel(struct GNUNET_MQ_DestroyNotificationHandle *dnh)
Cancel registration from GNUNET_MQ_destroy_notify().
Definition: mq.c:911
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_copy(const struct GNUNET_MessageHeader *hdr)
Create a new envelope by copying an existing message.
Definition: mq.c:533
struct GNUNET_NotificationContext * GNUNET_notification_context_create(unsigned int queue_length)
Create a new notification context.
Definition: nc.c:122
void GNUNET_notification_context_add(struct GNUNET_NotificationContext *nc, struct GNUNET_MQ_Handle *mq)
Add a subscriber to the notification context.
Definition: nc.c:161
static void handle_mq_destroy(void *cls)
Subscriber has disconnected, clean up.
Definition: nc.c:101
Handle we return for callbacks registered to be notified when GNUNET_MQ_destroy() is called on a queu...
Definition: mq.c:654
Handle to a message queue.
Definition: mq.c:87
Header for all communications.
The notification context is the key datastructure for a convenience API used for transmission of noti...
Definition: nc.c:77
struct SubscriberList * subscribers_head
Head of list of subscribers receiving notifications.
Definition: nc.c:81
struct SubscriberList * subscribers_tail
Tail of list of subscribers receiving notifications.
Definition: nc.c:86
unsigned int queue_length
Maximum number of optional messages to queue per subscriber.
Definition: nc.c:91
Lists of subscribers we manage for notifications.
Definition: nc.c:39
struct GNUNET_MQ_Handle * mq
Message queue for the subscriber.
Definition: nc.c:64
struct GNUNET_NotificationContext * nc
Overall context this subscriber belongs to.
Definition: nc.c:53
struct GNUNET_MQ_DestroyNotificationHandle * mq_nh
Handle where we registered with mq to be told about the MQ's destruction.
Definition: nc.c:59
struct SubscriberList * prev
This is a doubly linked list.
Definition: nc.c:48
struct SubscriberList * next
This is a doubly linked list.
Definition: nc.c:43