aboutsummaryrefslogtreecommitdiff
path: root/src/service/messenger/gnunet-service-messenger_operation.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/messenger/gnunet-service-messenger_operation.c')
-rw-r--r--src/service/messenger/gnunet-service-messenger_operation.c239
1 files changed, 239 insertions, 0 deletions
diff --git a/src/service/messenger/gnunet-service-messenger_operation.c b/src/service/messenger/gnunet-service-messenger_operation.c
new file mode 100644
index 000000000..f13db7de0
--- /dev/null
+++ b/src/service/messenger/gnunet-service-messenger_operation.c
@@ -0,0 +1,239 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021, 2023 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/**
21 * @author Tobias Frisch
22 * @file src/messenger/gnunet-service-messenger_operation.c
23 * @brief GNUnet MESSENGER service
24 */
25
26#include "platform.h"
27#include "gnunet-service-messenger_operation.h"
28
29#include "gnunet-service-messenger_operation_store.h"
30
31struct GNUNET_MESSENGER_Operation*
32create_operation (const struct GNUNET_HashCode *hash)
33{
34 GNUNET_assert (hash);
35
36 struct GNUNET_MESSENGER_Operation *op = GNUNET_new (struct
37 GNUNET_MESSENGER_Operation);
38
39 op->type = GNUNET_MESSENGER_OP_UNKNOWN;
40 GNUNET_memcpy (&(op->hash), hash, sizeof(*hash));
41 op->timestamp = GNUNET_TIME_absolute_get_zero_ ();
42 op->store = NULL;
43 op->task = NULL;
44
45 return op;
46}
47
48
49void
50destroy_operation (struct GNUNET_MESSENGER_Operation *op)
51{
52 GNUNET_assert (op);
53
54 if (op->task)
55 GNUNET_SCHEDULER_cancel (op->task);
56
57 GNUNET_free (op);
58}
59
60
61static void
62callback_operation (void *cls);
63
64struct GNUNET_MESSENGER_Operation*
65load_operation (struct GNUNET_MESSENGER_OperationStore *store,
66 const char *path)
67{
68 GNUNET_assert ((store) && (path));
69
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load operation configuration: %s\n",
71 path);
72
73 struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();
74 struct GNUNET_MESSENGER_Operation *op = NULL;
75
76 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, path))
77 goto destroy_config;
78
79 struct GNUNET_HashCode hash;
80
81 if (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation", "hash",
82 &hash, sizeof(hash)))
83 goto destroy_config;
84
85 op = create_operation (&hash);
86
87 unsigned long long type_number;
88 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "operation",
89 "type", &type_number))
90 switch (type_number)
91 {
92 case GNUNET_MESSENGER_OP_REQUEST:
93 op->type = GNUNET_MESSENGER_OP_REQUEST;
94 break;
95 case GNUNET_MESSENGER_OP_DELETE:
96 op->type = GNUNET_MESSENGER_OP_DELETE;
97 break;
98 case GNUNET_MESSENGER_OP_MERGE:
99 op->type = GNUNET_MESSENGER_OP_MERGE;
100 break;
101 default:
102 break;
103 }
104
105 if ((GNUNET_MESSENGER_OP_UNKNOWN == op->type) ||
106 (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation",
107 "timestamp",
108 &(op->timestamp),
109 sizeof(op->timestamp))))
110 {
111 destroy_operation (op);
112 op = NULL;
113 goto destroy_config;
114 }
115
116 const struct GNUNET_TIME_Relative delay = GNUNET_TIME_absolute_get_remaining (
117 op->timestamp);
118
119 op->task = GNUNET_SCHEDULER_add_delayed_with_priority (
120 delay,
121 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
122 callback_operation,
123 op
124 );
125
126 op->store = store;
127
128destroy_config:
129 GNUNET_CONFIGURATION_destroy (cfg);
130
131 return op;
132}
133
134
135void
136save_operation (const struct GNUNET_MESSENGER_Operation *op,
137 const char *path)
138{
139 GNUNET_assert ((path) && (op));
140
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Save operation configuration: %s\n",
142 path);
143
144 struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();
145
146 char *hash_data;
147 hash_data = GNUNET_STRINGS_data_to_string_alloc (&(op->hash),
148 sizeof(op->hash));
149
150 if (hash_data)
151 {
152 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "hash", hash_data);
153
154 GNUNET_free (hash_data);
155 }
156
157 GNUNET_CONFIGURATION_set_value_number (cfg, "operation", "type", op->type);
158
159 char *timestamp_data;
160 timestamp_data = GNUNET_STRINGS_data_to_string_alloc (&(op->timestamp),
161 sizeof(op->timestamp));
162
163 if (timestamp_data)
164 {
165 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "timestamp",
166 timestamp_data);
167
168 GNUNET_free (timestamp_data);
169 }
170
171 GNUNET_CONFIGURATION_write (cfg, path);
172 GNUNET_CONFIGURATION_destroy (cfg);
173}
174
175
176extern void
177callback_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
178 enum GNUNET_MESSENGER_OperationType type,
179 const struct GNUNET_HashCode *hash);
180
181static void
182callback_operation (void *cls)
183{
184 struct GNUNET_MESSENGER_Operation *op = cls;
185
186 op->task = NULL;
187
188 callback_store_operation (op->store, op->type, &(op->hash));
189}
190
191
192int
193start_operation (struct GNUNET_MESSENGER_Operation *op,
194 enum GNUNET_MESSENGER_OperationType type,
195 struct GNUNET_MESSENGER_OperationStore *store,
196 struct GNUNET_TIME_Relative delay)
197{
198 GNUNET_assert ((op) && (store));
199
200 if (op->task)
201 return GNUNET_SYSERR;
202
203 const struct GNUNET_TIME_Absolute timestamp = GNUNET_TIME_absolute_add (
204 GNUNET_TIME_absolute_get (),
205 delay
206 );
207
208 op->task = GNUNET_SCHEDULER_add_delayed_with_priority (
209 delay,
210 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
211 callback_operation,
212 op
213 );
214
215 op->type = type;
216 op->timestamp = timestamp;
217 op->store = store;
218
219 return GNUNET_OK;
220}
221
222
223int
224stop_operation (struct GNUNET_MESSENGER_Operation *op)
225{
226 GNUNET_assert (op);
227
228 if (! op->task)
229 return GNUNET_SYSERR;
230
231 GNUNET_SCHEDULER_cancel (op->task);
232 op->task = NULL;
233
234 op->type = GNUNET_MESSENGER_OP_UNKNOWN;
235 op->timestamp = GNUNET_TIME_absolute_get_zero_ ();
236 op->store = NULL;
237
238 return GNUNET_OK;
239}