aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_operation_store.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/gnunet-service-messenger_operation_store.c')
-rw-r--r--src/messenger/gnunet-service-messenger_operation_store.c236
1 files changed, 0 insertions, 236 deletions
diff --git a/src/messenger/gnunet-service-messenger_operation_store.c b/src/messenger/gnunet-service-messenger_operation_store.c
deleted file mode 100644
index 276f0b92b..000000000
--- a/src/messenger/gnunet-service-messenger_operation_store.c
+++ /dev/null
@@ -1,236 +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_store.c
23 * @brief GNUnet MESSENGER service
24 */
25
26#include "gnunet-service-messenger_operation_store.h"
27
28#include "gnunet-service-messenger_operation.h"
29#include "gnunet-service-messenger_room.h"
30
31void
32init_operation_store (struct GNUNET_MESSENGER_OperationStore *store,
33 struct GNUNET_MESSENGER_SrvRoom *room)
34{
35 GNUNET_assert((store) && (room));
36
37 store->room = room;
38 store->operations = GNUNET_CONTAINER_multihashmap_create(8, GNUNET_NO);
39}
40
41static int
42iterate_destroy_operations (void *cls,
43 const struct GNUNET_HashCode *key,
44 void *value)
45{
46 struct GNUNET_MESSENGER_Operation *op = value;
47
48 destroy_operation(op);
49
50 return GNUNET_YES;
51}
52
53void
54clear_operation_store (struct GNUNET_MESSENGER_OperationStore *store)
55{
56 GNUNET_assert(store);
57
58 GNUNET_CONTAINER_multihashmap_iterate (store->operations, iterate_destroy_operations, NULL);
59 GNUNET_CONTAINER_multihashmap_destroy(store->operations);
60}
61
62static int
63callback_scan_for_operations (void *cls,
64 const char *filename)
65{
66 struct GNUNET_MESSENGER_OperationStore *store = cls;
67
68 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
69 return GNUNET_OK;
70
71 if ((strlen(filename) <= 4) || (0 != strcmp(filename + strlen(filename) - 4, ".cfg")))
72 return GNUNET_OK;
73
74 struct GNUNET_MESSENGER_Operation *op = load_operation(store, filename);
75
76 if ((op) && (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
77 store->operations,
78 &(op->hash), op,
79 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)))
80 {
81 destroy_operation(op);
82 }
83
84 return GNUNET_OK;
85}
86
87void
88load_operation_store (struct GNUNET_MESSENGER_OperationStore *store,
89 const char *directory)
90{
91 GNUNET_assert ((store) && (directory));
92
93 char* load_dir;
94 GNUNET_asprintf (&load_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR);
95
96 if (GNUNET_OK == GNUNET_DISK_directory_test (load_dir, GNUNET_YES))
97 GNUNET_DISK_directory_scan (load_dir, callback_scan_for_operations, store);
98
99 GNUNET_free(load_dir);
100}
101
102static int
103iterate_save_operations (void *cls,
104 const struct GNUNET_HashCode *key,
105 void *value)
106{
107 const char *save_dir = cls;
108
109 struct GNUNET_MESSENGER_Operation *op = value;
110
111 if (!op)
112 return GNUNET_YES;
113
114 char *op_dir;
115 GNUNET_asprintf (&op_dir, "%s%s.cfg", save_dir, GNUNET_h2s(key));
116 save_operation(op, op_dir);
117
118 GNUNET_free(op_dir);
119 return GNUNET_YES;
120}
121
122void
123save_operation_store (const struct GNUNET_MESSENGER_OperationStore *store,
124 const char *directory)
125{
126 GNUNET_assert ((store) && (directory));
127
128 char* save_dir;
129 GNUNET_asprintf (&save_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR);
130
131 if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
132 (GNUNET_OK == GNUNET_DISK_directory_create (save_dir)))
133 GNUNET_CONTAINER_multihashmap_iterate (store->operations, iterate_save_operations, save_dir);
134
135 GNUNET_free(save_dir);
136}
137
138enum GNUNET_MESSENGER_OperationType
139get_store_operation_type (const struct GNUNET_MESSENGER_OperationStore *store,
140 const struct GNUNET_HashCode *hash)
141{
142 GNUNET_assert((store) && (hash));
143
144 struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get(store->operations, hash);
145
146 if (!op)
147 return GNUNET_MESSENGER_OP_UNKNOWN;
148
149 return op->type;
150}
151
152int
153use_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
154 const struct GNUNET_HashCode *hash,
155 enum GNUNET_MESSENGER_OperationType type,
156 struct GNUNET_TIME_Relative delay)
157{
158 GNUNET_assert((store) && (hash));
159
160 struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get(store->operations, hash);
161
162 if (op)
163 goto use_op;
164
165 op = create_operation(hash);
166
167 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(store->operations, hash, op, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
168 {
169 destroy_operation(op);
170
171 return GNUNET_SYSERR;
172 }
173
174use_op:
175 if ((op->type != GNUNET_MESSENGER_OP_UNKNOWN) &&
176 (type == GNUNET_MESSENGER_OP_DELETE))
177 stop_operation (op);
178
179 return start_operation(op, type, store, delay);
180}
181
182void
183cancel_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
184 const struct GNUNET_HashCode *hash)
185{
186 GNUNET_assert((store) && (hash));
187
188 struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get(store->operations, hash);
189
190 if (!op)
191 return;
192
193 stop_operation(op);
194
195 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(store->operations, hash, op))
196 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Canceled operation could not be removed: %s\n",
197 GNUNET_h2s(hash));
198
199 destroy_operation(op);
200}
201
202extern void
203callback_room_deletion (struct GNUNET_MESSENGER_SrvRoom *room,
204 const struct GNUNET_HashCode *hash);
205
206extern void
207callback_room_merge (struct GNUNET_MESSENGER_SrvRoom *room,
208 const struct GNUNET_HashCode *hash);
209
210void
211callback_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
212 enum GNUNET_MESSENGER_OperationType type,
213 const struct GNUNET_HashCode *hash)
214{
215 GNUNET_assert((store) && (hash));
216
217 struct GNUNET_HashCode op_hash;
218 GNUNET_memcpy(&op_hash, hash, sizeof(op_hash));
219 cancel_store_operation (store, &op_hash);
220
221 struct GNUNET_MESSENGER_SrvRoom *room = store->room;
222
223 switch (type)
224 {
225 case GNUNET_MESSENGER_OP_REQUEST:
226 break;
227 case GNUNET_MESSENGER_OP_DELETE:
228 callback_room_deletion (room, &op_hash);
229 break;
230 case GNUNET_MESSENGER_OP_MERGE:
231 callback_room_merge (room, &op_hash);
232 break;
233 default:
234 break;
235 }
236}