GNUnet  0.11.x
gnunet-service-messenger_operation_store.c
Go to the documentation of this file.
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  */
27 
30 
31 void
33 {
34  GNUNET_assert((store) && (room));
35 
36  store->room = room;
38 }
39 
40 static int
41 iterate_destroy_operations (void *cls, const struct GNUNET_HashCode *key, void *value)
42 {
44 
46 
47  return GNUNET_YES;
48 }
49 
50 void
52 {
53  GNUNET_assert(store);
54 
57 }
58 
59 static int
60 callback_scan_for_operations (void *cls, const char *filename)
61 {
62  struct GNUNET_MESSENGER_OperationStore *store = cls;
63 
64  if (GNUNET_YES == GNUNET_DISK_file_test (filename))
65  {
66  char *path;
67 
68  GNUNET_asprintf (&path, "%s%c", filename, DIR_SEPARATOR);
69 
70  struct GNUNET_MESSENGER_Operation *op = load_operation(store, path);
71 
73  store->operations,
74  &(op->hash), op,
76  {
78  }
79 
80  GNUNET_free(path);
81  }
82 
83  return GNUNET_OK;
84 }
85 
86 void
88  const char *directory)
89 {
90  GNUNET_assert ((store) && (directory));
91 
94 }
95 
96 static int
97 iterate_save_operations (void *cls, const struct GNUNET_HashCode *key, void *value)
98 {
99  const char *save_dir = cls;
100 
102 
103  if (!op)
104  return GNUNET_YES;
105 
106  char *op_dir;
107  GNUNET_asprintf (&op_dir, "%s%s.cfg", save_dir, GNUNET_h2s(key));
108  save_operation(op, op_dir);
109 
110  GNUNET_free(op_dir);
111  return GNUNET_YES;
112 }
113 
114 void
116  const char *directory)
117 {
118  GNUNET_assert ((store) && (directory));
119 
120  char* save_dir;
121  GNUNET_asprintf (&save_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR);
122 
123  if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
124  (GNUNET_OK == GNUNET_DISK_directory_create (save_dir)))
126 
127  GNUNET_free(save_dir);
128 }
129 
132  const struct GNUNET_HashCode *hash)
133 {
134  GNUNET_assert((store) && (hash));
135 
137 
138  if (!op)
140 
141  return op->type;
142 }
143 
144 int
146  const struct GNUNET_HashCode *hash,
149 {
150  GNUNET_assert((store) && (hash));
151 
153 
154  if (op)
155  goto use_op;
156 
157  op = create_operation(hash);
158 
160  {
161  destroy_operation(op);
162 
163  return GNUNET_SYSERR;
164  }
165 
166 use_op:
167  if ((op->type != GNUNET_MESSENGER_OP_UNKNOWN) &&
168  (type == GNUNET_MESSENGER_OP_DELETE))
169  stop_operation (op);
170 
171  return start_operation(op, type, store, delay);
172 }
173 
174 void
176  const struct GNUNET_HashCode *hash)
177 {
178  GNUNET_assert((store) && (hash));
179 
181 
182  if (!op)
183  return;
184 
185  stop_operation(op);
186 
188  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Canceled operation could not be removed: %s\n",
189  GNUNET_h2s(hash));
190 
191  destroy_operation(op);
192 }
193 
194 extern void
196 
197 extern void
198 callback_room_merge (struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_HashCode *hash);
199 
200 void
203  const struct GNUNET_HashCode *hash)
204 {
205  GNUNET_assert((store) && (hash));
206 
207  struct GNUNET_HashCode op_hash;
208  GNUNET_memcpy(&op_hash, hash, sizeof(op_hash));
209  cancel_store_operation (store, &op_hash);
210 
211  struct GNUNET_MESSENGER_SrvRoom *room = store->room;
212 
213  switch (type)
214  {
216  break;
218  callback_room_deletion (room, &op_hash);
219  break;
221  callback_room_merge (room, &op_hash);
222  break;
223  default:
224  break;
225  }
226 }
enum GNUNET_MESSENGER_OperationType type
int start_operation(struct GNUNET_MESSENGER_Operation *op, enum GNUNET_MESSENGER_OperationType type, struct GNUNET_MESSENGER_OperationStore *store, struct GNUNET_TIME_Relative delay)
Starts an inactive operation with a given delay in a specific operation store.
void callback_store_operation(struct GNUNET_MESSENGER_OperationStore *store, enum GNUNET_MESSENGER_OperationType type, const struct GNUNET_HashCode *hash)
void save_operation(const struct GNUNET_MESSENGER_Operation *op, const char *path)
Saves data from an operation into a configuration file at a selected path which can be load to restor...
GNUnet MESSENGER service.
struct GNUNET_CONTAINER_MultiHashMap * operations
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void destroy_operation(struct GNUNET_MESSENGER_Operation *op)
Destroys an operation and frees its memory fully.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:794
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
struct GNUNET_MESSENGER_Operation * create_operation(const struct GNUNET_HashCode *hash)
Creates and allocates a new operation under a given hash.
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
int stop_operation(struct GNUNET_MESSENGER_Operation *op)
Stops an active operation and resets its type to be GNUNET_MESSENGER_OP_UNKNOWN.
static char * value
Value of the record to add/remove.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
struct GNUNET_MESSENGER_Operation * load_operation(struct GNUNET_MESSENGER_OperationStore *store, const char *path)
Loads data from a configuration file at a selected path into a new allocated and created operation fo...
struct GNUNET_MESSENGER_OperationStore * store
static char * filename
int GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:476
void clear_operation_store(struct GNUNET_MESSENGER_OperationStore *store)
Clears an operation store, stops all operations and deallocates its memory.
A 512-bit hashcode.
enum GNUNET_MESSENGER_OperationType get_store_operation_type(const struct GNUNET_MESSENGER_OperationStore *store, const struct GNUNET_HashCode *hash)
Returns the type of the active operation under a given hash in a specific operation store...
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define DIR_SEPARATOR
Definition: platform.h:164
struct GNUNET_HashCode key
The key used in the DHT.
GNUnet MESSENGER service.
static struct GNUNET_TIME_Relative delay
When should dkg communication start?
static int iterate_destroy_operations(void *cls, const struct GNUNET_HashCode *key, void *value)
int GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
void init_operation_store(struct GNUNET_MESSENGER_OperationStore *store, struct GNUNET_MESSENGER_SrvRoom *room)
Initializes an operation store as fully empty with a given room.
void callback_room_merge(struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_HashCode *hash)
int use_store_operation(struct GNUNET_MESSENGER_OperationStore *store, const struct GNUNET_HashCode *hash, enum GNUNET_MESSENGER_OperationType type, struct GNUNET_TIME_Relative delay)
Tries to use an operation under a given hash in a specific operation store.
void cancel_store_operation(struct GNUNET_MESSENGER_OperationStore *store, const struct GNUNET_HashCode *hash)
Stops any active operation under a given hash in a specific operation store.
void save_operation_store(const struct GNUNET_MESSENGER_OperationStore *store, const char *directory)
Saves operations from an operation store into a directory.
static int iterate_save_operations(void *cls, const struct GNUNET_HashCode *key, void *value)
#define GNUNET_log(kind,...)
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
void load_operation_store(struct GNUNET_MESSENGER_OperationStore *store, const char *directory)
Loads operations from a directory into an operation store.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:144
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:404
void callback_room_deletion(struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_HashCode *hash)
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:437
static int callback_scan_for_operations(void *cls, const char *filename)
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.