GNUnet 0.21.2
messenger_api_queue_messages.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2023--2024 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 */
27
30
31void
33{
34 GNUNET_assert (messages);
35
36 messages->head = NULL;
37 messages->tail = NULL;
38}
39
40
41void
43{
44 GNUNET_assert (messages);
45
46 while (messages->head)
47 {
48 struct GNUNET_MESSENGER_QueueMessage *element = messages->head;
49
50 GNUNET_CONTAINER_DLL_remove (messages->head, messages->tail, element);
51
52 if (element->message)
53 destroy_message (element->message);
54
55 if (element->transcript)
56 destroy_message (element->transcript);
57
58 GNUNET_free (element);
59 }
60
61 messages->head = NULL;
62 messages->tail = NULL;
63}
64
65
66void
68 const struct GNUNET_CRYPTO_PrivateKey *sender,
71{
72 GNUNET_assert ((messages) && (sender) && (message));
73
74 struct GNUNET_MESSENGER_QueueMessage *element = GNUNET_new (struct
76
77 if (! element)
78 return;
79
81
82 element->message = message;
83 element->transcript = transcript;
84
85 GNUNET_memcpy (&(element->sender), sender, sizeof (element->sender));
86
87 if (! element->message)
88 {
89 if (element->transcript)
90 destroy_message (element->transcript);
91
92 GNUNET_free (element);
93 return;
94 }
95
97 GNUNET_CONTAINER_DLL_insert (messages->head, messages->tail, element);
98 else if (GNUNET_MESSENGER_KIND_SUBSCRIBE == kind)
99 {
100 struct GNUNET_MESSENGER_QueueMessage *other = messages->head;
101
102 while (other)
103 {
105 break;
106
107 other = other->next;
108 }
109
110 GNUNET_CONTAINER_DLL_insert_before (messages->head, messages->tail, other, element);
111 }
112 else
113 GNUNET_CONTAINER_DLL_insert_tail (messages->head, messages->tail, element);
114}
115
116
119 struct GNUNET_CRYPTO_PrivateKey *sender,
120 struct GNUNET_MESSENGER_Message **transcript)
121{
122 GNUNET_assert (messages);
123
124 struct GNUNET_MESSENGER_QueueMessage *element = messages->head;
125
126 if (! element)
127 {
128 if (transcript)
129 *transcript = NULL;
130
131 return NULL;
132 }
133
134 struct GNUNET_MESSENGER_Message *message = element->message;
135
136 if (transcript)
137 *transcript = element->transcript;
138 else if (element->transcript)
139 destroy_message (element->transcript);
140
141 GNUNET_CONTAINER_DLL_remove (messages->head, messages->tail, element);
142
143 if (sender)
144 GNUNET_memcpy (sender, &(element->sender), sizeof (*sender));
145
146 GNUNET_free (element);
147 return message;
148}
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_before(head, tail, other, element)
Insert an element into a DLL before the given other element.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MESSENGER_MessageKind
Enum for the different supported kinds of messages.
@ GNUNET_MESSENGER_KIND_SUBSCRIBE
The subscribe kind.
@ GNUNET_MESSENGER_KIND_TALK
The talk kind.
@ GNUNET_MESSENGER_KIND_JOIN
The join kind.
void destroy_message(struct GNUNET_MESSENGER_Message *message)
Destroys a message and frees its memory fully.
struct GNUNET_MESSENGER_Message * dequeue_from_messages(struct GNUNET_MESSENGER_QueueMessages *messages, struct GNUNET_CRYPTO_PrivateKey *sender, struct GNUNET_MESSENGER_Message **transcript)
Remove the message from the front of the queue and returns it.
void enqueue_to_messages(struct GNUNET_MESSENGER_QueueMessages *messages, const struct GNUNET_CRYPTO_PrivateKey *sender, struct GNUNET_MESSENGER_Message *message, struct GNUNET_MESSENGER_Message *transcript)
Adds a specific message to the end or the beginning of the queue depending on its priority.
void init_queue_messages(struct GNUNET_MESSENGER_QueueMessages *messages)
Initializes queue of messages as empty queue.
void clear_queue_messages(struct GNUNET_MESSENGER_QueueMessages *messages)
Clears the queue of messages.
A private key for an identity as per LSD0001.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_Message * message
struct GNUNET_CRYPTO_PrivateKey sender
struct GNUNET_MESSENGER_Message * transcript
struct GNUNET_MESSENGER_QueueMessage * next
struct GNUNET_MESSENGER_QueueMessage * tail
struct GNUNET_MESSENGER_QueueMessage * head