GNUnet 0.22.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;
49 element = messages->head;
50
51 GNUNET_CONTAINER_DLL_remove (messages->head, messages->tail, element);
52
53 if (element->message)
54 destroy_message (element->message);
55
56 if (element->transcript)
57 destroy_message (element->transcript);
58
59 GNUNET_free (element);
60 }
61
62 messages->head = NULL;
63 messages->tail = NULL;
64}
65
66
67void
69 const struct GNUNET_CRYPTO_PrivateKey *sender,
72{
73 struct GNUNET_MESSENGER_QueueMessage *element;
75
76 GNUNET_assert ((messages) && (sender) && (message));
77
79 if (! element)
80 return;
81
82 kind = message->header.kind;
83
84 element->message = message;
85 element->transcript = transcript;
86
87 GNUNET_memcpy (&(element->sender), sender, sizeof (element->sender));
88
89 if (! element->message)
90 {
91 if (element->transcript)
92 destroy_message (element->transcript);
93
94 GNUNET_free (element);
95 return;
96 }
97
99 GNUNET_CONTAINER_DLL_insert (messages->head, messages->tail, element);
100 else if (GNUNET_MESSENGER_KIND_SUBSCRIBE == kind)
101 {
102 struct GNUNET_MESSENGER_QueueMessage *other;
103
104 other = messages->head;
105 while (other)
106 {
108 break;
109
110 other = other->next;
111 }
112
113 GNUNET_CONTAINER_DLL_insert_before (messages->head, messages->tail, other, element);
114 }
115 else
116 GNUNET_CONTAINER_DLL_insert_tail (messages->head, messages->tail, element);
117}
118
119
122 struct GNUNET_CRYPTO_PrivateKey *sender,
123 struct GNUNET_MESSENGER_Message **transcript)
124{
125 struct GNUNET_MESSENGER_QueueMessage *element;
126 struct GNUNET_MESSENGER_Message *message;
127
128 GNUNET_assert (messages);
129
130 element = messages->head;
131 if (! element)
132 {
133 if (transcript)
134 *transcript = NULL;
135
136 return NULL;
137 }
138
139 message = element->message;
140
141 if (transcript)
142 *transcript = element->transcript;
143 else if (element->transcript)
144 destroy_message (element->transcript);
145
146 GNUNET_CONTAINER_DLL_remove (messages->head, messages->tail, element);
147
148 if (sender)
149 GNUNET_memcpy (sender, &(element->sender), sizeof (*sender));
150
151 GNUNET_free (element);
152 return message;
153}
#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