aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_operation.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/gnunet-service-messenger_operation.c')
-rw-r--r--src/messenger/gnunet-service-messenger_operation.c112
1 files changed, 65 insertions, 47 deletions
diff --git a/src/messenger/gnunet-service-messenger_operation.c b/src/messenger/gnunet-service-messenger_operation.c
index 9c4201fdf..f13db7de0 100644
--- a/src/messenger/gnunet-service-messenger_operation.c
+++ b/src/messenger/gnunet-service-messenger_operation.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2021 GNUnet e.V. 3 Copyright (C) 2021, 2023 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -31,30 +31,33 @@
31struct GNUNET_MESSENGER_Operation* 31struct GNUNET_MESSENGER_Operation*
32create_operation (const struct GNUNET_HashCode *hash) 32create_operation (const struct GNUNET_HashCode *hash)
33{ 33{
34 GNUNET_assert(hash); 34 GNUNET_assert (hash);
35 35
36 struct GNUNET_MESSENGER_Operation *op = GNUNET_new(struct GNUNET_MESSENGER_Operation); 36 struct GNUNET_MESSENGER_Operation *op = GNUNET_new (struct
37 GNUNET_MESSENGER_Operation);
37 38
38 op->type = GNUNET_MESSENGER_OP_UNKNOWN; 39 op->type = GNUNET_MESSENGER_OP_UNKNOWN;
39 GNUNET_memcpy(&(op->hash), hash, sizeof(*hash)); 40 GNUNET_memcpy (&(op->hash), hash, sizeof(*hash));
40 op->timestamp = GNUNET_TIME_absolute_get_zero_(); 41 op->timestamp = GNUNET_TIME_absolute_get_zero_ ();
41 op->store = NULL; 42 op->store = NULL;
42 op->task = NULL; 43 op->task = NULL;
43 44
44 return op; 45 return op;
45} 46}
46 47
48
47void 49void
48destroy_operation (struct GNUNET_MESSENGER_Operation *op) 50destroy_operation (struct GNUNET_MESSENGER_Operation *op)
49{ 51{
50 GNUNET_assert(op); 52 GNUNET_assert (op);
51 53
52 if (op->task) 54 if (op->task)
53 GNUNET_SCHEDULER_cancel(op->task); 55 GNUNET_SCHEDULER_cancel (op->task);
54 56
55 GNUNET_free(op); 57 GNUNET_free (op);
56} 58}
57 59
60
58static void 61static void
59callback_operation (void *cls); 62callback_operation (void *cls);
60 63
@@ -62,25 +65,28 @@ struct GNUNET_MESSENGER_Operation*
62load_operation (struct GNUNET_MESSENGER_OperationStore *store, 65load_operation (struct GNUNET_MESSENGER_OperationStore *store,
63 const char *path) 66 const char *path)
64{ 67{
65 GNUNET_assert((store) && (path)); 68 GNUNET_assert ((store) && (path));
66 69
67 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Load operation configuration: %s\n", path); 70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load operation configuration: %s\n",
71 path);
68 72
69 struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create (); 73 struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();
70 struct GNUNET_MESSENGER_Operation* op = NULL; 74 struct GNUNET_MESSENGER_Operation *op = NULL;
71 75
72 if (GNUNET_OK != GNUNET_CONFIGURATION_parse(cfg, path)) 76 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, path))
73 goto destroy_config; 77 goto destroy_config;
74 78
75 struct GNUNET_HashCode hash; 79 struct GNUNET_HashCode hash;
76 80
77 if (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation", "hash", &hash, sizeof(hash))) 81 if (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation", "hash",
82 &hash, sizeof(hash)))
78 goto destroy_config; 83 goto destroy_config;
79 84
80 op = create_operation(&hash); 85 op = create_operation (&hash);
81 86
82 unsigned long long type_number; 87 unsigned long long type_number;
83 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, "operation", "type", &type_number)) 88 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "operation",
89 "type", &type_number))
84 switch (type_number) 90 switch (type_number)
85 { 91 {
86 case GNUNET_MESSENGER_OP_REQUEST: 92 case GNUNET_MESSENGER_OP_REQUEST:
@@ -97,21 +103,25 @@ load_operation (struct GNUNET_MESSENGER_OperationStore *store,
97 } 103 }
98 104
99 if ((GNUNET_MESSENGER_OP_UNKNOWN == op->type) || 105 if ((GNUNET_MESSENGER_OP_UNKNOWN == op->type) ||
100 (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation", "timestamp", &(op->timestamp), sizeof(op->timestamp)))) 106 (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation",
107 "timestamp",
108 &(op->timestamp),
109 sizeof(op->timestamp))))
101 { 110 {
102 destroy_operation(op); 111 destroy_operation (op);
103 op = NULL; 112 op = NULL;
104 goto destroy_config; 113 goto destroy_config;
105 } 114 }
106 115
107 const struct GNUNET_TIME_Relative delay = GNUNET_TIME_absolute_get_remaining(op->timestamp); 116 const struct GNUNET_TIME_Relative delay = GNUNET_TIME_absolute_get_remaining (
117 op->timestamp);
108 118
109 op->task = GNUNET_SCHEDULER_add_delayed_with_priority( 119 op->task = GNUNET_SCHEDULER_add_delayed_with_priority (
110 delay, 120 delay,
111 GNUNET_SCHEDULER_PRIORITY_BACKGROUND, 121 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
112 callback_operation, 122 callback_operation,
113 op 123 op
114 ); 124 );
115 125
116 op->store = store; 126 op->store = store;
117 127
@@ -121,42 +131,48 @@ destroy_config:
121 return op; 131 return op;
122} 132}
123 133
134
124void 135void
125save_operation (const struct GNUNET_MESSENGER_Operation *op, 136save_operation (const struct GNUNET_MESSENGER_Operation *op,
126 const char *path) 137 const char *path)
127{ 138{
128 GNUNET_assert((path) && (op)); 139 GNUNET_assert ((path) && (op));
129 140
130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Save operation configuration: %s\n", path); 141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Save operation configuration: %s\n",
142 path);
131 143
132 struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create (); 144 struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();
133 145
134 char *hash_data; 146 char *hash_data;
135 hash_data = GNUNET_STRINGS_data_to_string_alloc (&(op->hash), sizeof(op->hash)); 147 hash_data = GNUNET_STRINGS_data_to_string_alloc (&(op->hash),
148 sizeof(op->hash));
136 149
137 if (hash_data) 150 if (hash_data)
138 { 151 {
139 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "hash", hash_data); 152 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "hash", hash_data);
140 153
141 GNUNET_free(hash_data); 154 GNUNET_free (hash_data);
142 } 155 }
143 156
144 GNUNET_CONFIGURATION_set_value_number(cfg, "operation", "type", op->type); 157 GNUNET_CONFIGURATION_set_value_number (cfg, "operation", "type", op->type);
145 158
146 char *timestamp_data; 159 char *timestamp_data;
147 timestamp_data = GNUNET_STRINGS_data_to_string_alloc (&(op->timestamp), sizeof(op->timestamp)); 160 timestamp_data = GNUNET_STRINGS_data_to_string_alloc (&(op->timestamp),
161 sizeof(op->timestamp));
148 162
149 if (timestamp_data) 163 if (timestamp_data)
150 { 164 {
151 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "timestamp", timestamp_data); 165 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "timestamp",
166 timestamp_data);
152 167
153 GNUNET_free(timestamp_data); 168 GNUNET_free (timestamp_data);
154 } 169 }
155 170
156 GNUNET_CONFIGURATION_write (cfg, path); 171 GNUNET_CONFIGURATION_write (cfg, path);
157 GNUNET_CONFIGURATION_destroy (cfg); 172 GNUNET_CONFIGURATION_destroy (cfg);
158} 173}
159 174
175
160extern void 176extern void
161callback_store_operation (struct GNUNET_MESSENGER_OperationStore *store, 177callback_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
162 enum GNUNET_MESSENGER_OperationType type, 178 enum GNUNET_MESSENGER_OperationType type,
@@ -172,28 +188,29 @@ callback_operation (void *cls)
172 callback_store_operation (op->store, op->type, &(op->hash)); 188 callback_store_operation (op->store, op->type, &(op->hash));
173} 189}
174 190
191
175int 192int
176start_operation (struct GNUNET_MESSENGER_Operation *op, 193start_operation (struct GNUNET_MESSENGER_Operation *op,
177 enum GNUNET_MESSENGER_OperationType type, 194 enum GNUNET_MESSENGER_OperationType type,
178 struct GNUNET_MESSENGER_OperationStore *store, 195 struct GNUNET_MESSENGER_OperationStore *store,
179 struct GNUNET_TIME_Relative delay) 196 struct GNUNET_TIME_Relative delay)
180{ 197{
181 GNUNET_assert((op) && (store)); 198 GNUNET_assert ((op) && (store));
182 199
183 if (op->task) 200 if (op->task)
184 return GNUNET_SYSERR; 201 return GNUNET_SYSERR;
185 202
186 const struct GNUNET_TIME_Absolute timestamp = GNUNET_TIME_absolute_add( 203 const struct GNUNET_TIME_Absolute timestamp = GNUNET_TIME_absolute_add (
187 GNUNET_TIME_absolute_get(), 204 GNUNET_TIME_absolute_get (),
188 delay 205 delay
189 ); 206 );
190 207
191 op->task = GNUNET_SCHEDULER_add_delayed_with_priority( 208 op->task = GNUNET_SCHEDULER_add_delayed_with_priority (
192 delay, 209 delay,
193 GNUNET_SCHEDULER_PRIORITY_BACKGROUND, 210 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
194 callback_operation, 211 callback_operation,
195 op 212 op
196 ); 213 );
197 214
198 op->type = type; 215 op->type = type;
199 op->timestamp = timestamp; 216 op->timestamp = timestamp;
@@ -202,19 +219,20 @@ start_operation (struct GNUNET_MESSENGER_Operation *op,
202 return GNUNET_OK; 219 return GNUNET_OK;
203} 220}
204 221
222
205int 223int
206stop_operation (struct GNUNET_MESSENGER_Operation *op) 224stop_operation (struct GNUNET_MESSENGER_Operation *op)
207{ 225{
208 GNUNET_assert(op); 226 GNUNET_assert (op);
209 227
210 if (!op->task) 228 if (! op->task)
211 return GNUNET_SYSERR; 229 return GNUNET_SYSERR;
212 230
213 GNUNET_SCHEDULER_cancel(op->task); 231 GNUNET_SCHEDULER_cancel (op->task);
214 op->task = NULL; 232 op->task = NULL;
215 233
216 op->type = GNUNET_MESSENGER_OP_UNKNOWN; 234 op->type = GNUNET_MESSENGER_OP_UNKNOWN;
217 op->timestamp = GNUNET_TIME_absolute_get_zero_(); 235 op->timestamp = GNUNET_TIME_absolute_get_zero_ ();
218 op->store = NULL; 236 op->store = NULL;
219 237
220 return GNUNET_OK; 238 return GNUNET_OK;