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