GNUnet  0.11.x
gnunet-service-messenger_operation.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 
29 
32 {
34 
36 
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
48 {
50 
51  if (op->task)
53 
54  GNUNET_free(op);
55 }
56 
57 static void
58 callback_operation (void *cls);
59 
62  const char *path)
63 {
64  GNUNET_assert((store) && (path));
65 
67  struct GNUNET_MESSENGER_Operation* op = NULL;
68 
70  goto destroy_config;
71 
72  struct GNUNET_HashCode hash;
73 
74  if (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation", "hash", &hash, sizeof(hash)))
75  goto destroy_config;
76 
77  op = create_operation(&hash);
78 
79  unsigned long long type_number;
80  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, "operation", "type", &type_number))
81  switch (type_number)
82  {
85  break;
88  break;
91  break;
92  default:
93  break;
94  }
95 
96  if ((GNUNET_MESSENGER_OP_UNKNOWN == op->type) ||
97  (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation", "timestamp", &(op->timestamp), sizeof(op->timestamp))))
98  {
100  op = NULL;
101  goto destroy_config;
102  }
103 
105 
107  delay,
110  op
111  );
112 
113  op->store = store;
114 
115 destroy_config:
117 
118  return op;
119 }
120 
121 void
123  const char *path)
124 {
125  GNUNET_assert((path) && (op));
126 
128 
129  char *hash_data;
130  hash_data = GNUNET_STRINGS_data_to_string_alloc (&(op->hash), sizeof(op->hash));
131 
132  if (hash_data)
133  {
134  GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "hash", hash_data);
135 
136  GNUNET_free(hash_data);
137  }
138 
139  GNUNET_CONFIGURATION_set_value_number(cfg, "operation", "type", op->type);
140 
141  char *timestamp_data;
142  timestamp_data = GNUNET_STRINGS_data_to_string_alloc (&(op->timestamp), sizeof(op->timestamp));
143 
144  if (timestamp_data)
145  {
146  GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "timestamp", timestamp_data);
147 
148  GNUNET_free(timestamp_data);
149  }
150 
153 }
154 
155 extern void
158  const struct GNUNET_HashCode *hash);
159 
160 static void
162 {
163  struct GNUNET_MESSENGER_Operation *op = cls;
164 
165  op->task = NULL;
166 
167  callback_store_operation (op->store, op->type, &(op->hash));
168 }
169 
170 int
175 {
176  GNUNET_assert((op) && (store));
177 
178  if (op->task)
179  return GNUNET_SYSERR;
180 
181  const struct GNUNET_TIME_Absolute timestamp = GNUNET_TIME_absolute_add(
183  delay
184  );
185 
187  delay,
190  op
191  );
192 
193  op->type = type;
194  op->timestamp = timestamp;
195  op->store = store;
196 
197  return GNUNET_OK;
198 }
199 
200 int
202 {
203  GNUNET_assert(op);
204 
205  if (!op->task)
206  return GNUNET_SYSERR;
207 
209  op->task = NULL;
210 
212  op->timestamp = GNUNET_TIME_absolute_get_zero_();
213  op->store = NULL;
214 
215  return GNUNET_OK;
216 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:144
static struct GNUNET_TIME_Relative delay
When should dkg communication start?
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)
int stop_operation(struct GNUNET_MESSENGER_Operation *op)
Stops an active operation and resets its type to be GNUNET_MESSENGER_OP_UNKNOWN.
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...
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.
struct GNUNET_MESSENGER_Operation * create_operation(const struct GNUNET_HashCode *hash)
Creates and allocates a new operation under a given hash.
GNUnet MESSENGER service.
@ GNUNET_SCHEDULER_PRIORITY_BACKGROUND
Run as background job (higher than idle, lower than default).
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
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.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
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.
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_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
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:1199
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:750
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero_(void)
Return absolute time of 0ms.
Definition: time.c:109
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:232
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:86
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:277
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.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model