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