GNUnet 0.21.1
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 enum GNUNET_GenericReturnValue priority)
72{
73 GNUNET_assert ((messages) && (sender) && (message));
74
75 struct GNUNET_MESSENGER_QueueMessage *element = GNUNET_new (struct
77
78 if (! element)
79 return;
80
81 element->message = message;
82 element->transcript = transcript;
83
84 GNUNET_memcpy (&(element->sender), sender, sizeof (element->sender));
85
86 if (! element->message)
87 {
88 if (element->transcript)
89 destroy_message (element->transcript);
90
91 GNUNET_free (element);
92 return;
93 }
94
95 if (GNUNET_YES == priority)
96 GNUNET_CONTAINER_DLL_insert (messages->head, messages->tail, element);
97 else
98 GNUNET_CONTAINER_DLL_insert_tail (messages->head, messages->tail, element);
99}
100
101
104 struct GNUNET_CRYPTO_PrivateKey *sender,
105 struct GNUNET_MESSENGER_Message **transcript)
106{
107 GNUNET_assert (messages);
108
109 struct GNUNET_MESSENGER_QueueMessage *element = messages->head;
110
111 if (! element)
112 {
113 if (transcript)
114 *transcript = NULL;
115
116 return NULL;
117 }
118
119 struct GNUNET_MESSENGER_Message *message = element->message;
120
121 if (transcript)
122 *transcript = element->transcript;
123 else if (element->transcript)
124 destroy_message (element->transcript);
125
126 GNUNET_CONTAINER_DLL_remove (messages->head, messages->tail, element);
127
128 if (sender)
129 GNUNET_memcpy (sender, &(element->sender), sizeof (*sender));
130
131 GNUNET_free (element);
132 return message;
133}
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#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.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_YES
#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.
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, enum GNUNET_GenericReturnValue priority)
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.
struct GNUNET_MESSENGER_Message * message
struct GNUNET_CRYPTO_PrivateKey sender
struct GNUNET_MESSENGER_Message * transcript
struct GNUNET_MESSENGER_QueueMessage * tail
struct GNUNET_MESSENGER_QueueMessage * head