GNUnet 0.21.1
gnunet-service-messenger_operation.c
Go to the documentation of this file.
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 */
27
29
32{
34
37
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
48void
50{
52
53 if (op->task)
55
57}
58
59
60static void
61callback_operation (void *cls);
62
65 const char *path)
66{
67 GNUNET_assert ((store) && (path));
68
69 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load operation configuration: %s\n",
70 path);
71
73 struct GNUNET_MESSENGER_Operation *op = NULL;
74
76 goto destroy_config;
77
78 struct GNUNET_HashCode hash;
79
80 if (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation", "hash",
81 &hash, sizeof(hash)))
82 goto destroy_config;
83
84 op = create_operation (&hash);
85
86 unsigned long long type_number;
88 "type", &type_number))
89 switch (type_number)
90 {
93 break;
96 break;
99 break;
100 default:
101 break;
102 }
103
104 if ((GNUNET_MESSENGER_OP_UNKNOWN == op->type) ||
106 "timestamp",
107 &(op->timestamp),
108 sizeof(op->timestamp))))
109 {
111 op = NULL;
112 goto destroy_config;
113 }
114
116 op->timestamp);
117
119 delay,
122 op
123 );
124
125 op->store = store;
126
127destroy_config:
129
130 return op;
131}
132
133
134void
136 const char *path)
137{
138 GNUNET_assert ((path) && (op));
139
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Save operation configuration: %s\n",
141 path);
142
144
145 char *hash_data;
146 hash_data = GNUNET_STRINGS_data_to_string_alloc (&(op->hash),
147 sizeof(op->hash));
148
149 if (hash_data)
150 {
151 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "hash", hash_data);
152
153 GNUNET_free (hash_data);
154 }
155
156 GNUNET_CONFIGURATION_set_value_number (cfg, "operation", "type", op->type);
157
158 char *timestamp_data;
159 timestamp_data = GNUNET_STRINGS_data_to_string_alloc (&(op->timestamp),
160 sizeof(op->timestamp));
161
162 if (timestamp_data)
163 {
164 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "timestamp",
165 timestamp_data);
166
167 GNUNET_free (timestamp_data);
168 }
169
172}
173
174
175extern void
178 const struct GNUNET_HashCode *hash);
179
180static void
182{
183 struct GNUNET_MESSENGER_Operation *op = cls;
184
185 op->task = NULL;
186
187 callback_store_operation (op->store, op->type, &(op->hash));
188}
189
190
195 struct GNUNET_TIME_Relative delay)
196{
197 GNUNET_assert ((op) && (store));
198
199 if (op->task)
200 return GNUNET_SYSERR;
201
202 const struct GNUNET_TIME_Absolute timestamp = GNUNET_TIME_absolute_add (
204 delay
205 );
206
208 delay,
211 op
212 );
213
214 op->type = type;
215 op->timestamp = timestamp;
216 op->store = store;
217
218 return GNUNET_OK;
219}
220
221
224{
226
227 if (! op->task)
228 return GNUNET_SYSERR;
229
231 op->task = NULL;
232
234 op->timestamp = GNUNET_TIME_absolute_get_zero_ ();
235 op->store = NULL;
236
237 return GNUNET_OK;
238}
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:144
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:109
static uint32_t type
Type string converted to DNS type value.
struct GNUNET_MESSENGER_Operation * create_operation(const struct GNUNET_HashCode *hash)
Creates and allocates a new operation under a given hash.
enum GNUNET_GenericReturnValue stop_operation(struct GNUNET_MESSENGER_Operation *op)
Stops an active operation and resets its type to be GNUNET_MESSENGER_OP_UNKNOWN.
void callback_store_operation(struct GNUNET_MESSENGER_OperationStore *store, enum GNUNET_MESSENGER_OperationType type, const struct GNUNET_HashCode *hash)
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...
enum GNUNET_GenericReturnValue 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 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...
static void callback_operation(void *cls)
void destroy_operation(struct GNUNET_MESSENGER_Operation *op)
Destroys an operation and frees its memory fully.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_data(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, void *buf, size_t buf_size)
Get Crockford32-encoded fixed-size binary data from a configuration.
void GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Set a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
void GNUNET_CONFIGURATION_set_value_number(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long number)
Set a configuration value that should be a number.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Parse a configuration file, add all of the options in the file to the configuration environment.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_SCHEDULER_PRIORITY_BACKGROUND
Run as background job (higher than idle, lower than default).
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:981
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed_with_priority(struct GNUNET_TIME_Relative delay, enum GNUNET_SCHEDULER_Priority priority, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1208
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:764
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero_(void)
Return absolute time of 0ms.
Definition: time.c:142
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:405
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition: time.c:450
A 512-bit hashcode.
struct GNUNET_MESSENGER_OperationStore * store
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.