1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
This file is part of GNUnet.
Copyright (C) 2016 GNUnet e.V.
GNUnet is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* Asynchronous operations; register callbacks for operations and call them when a response arrives.
*
* @author Gabor X Toth
*/
#include <inttypes.h>
#include "platform.h"
#include "gnunet_util_lib.h"
#define LOG(kind,...) GNUNET_log_from (kind, "util-op", __VA_ARGS__)
struct OperationListItem
{
struct OperationListItem *prev;
struct OperationListItem *next;
/**
* Operation ID.
*/
uint64_t op_id;
/**
* Continuation to invoke with the result of an operation.
*/
GNUNET_ResultCallback result_cb;
/**
* Closure for @a result_cb.
*/
void *cls;
/**
* User context.
*/
void *ctx;
};
/**
* Operations handle.
*/
struct GNUNET_OP_Handle
{
/**
* First operation in the linked list.
*/
struct OperationListItem *op_head;
/**
* Last operation in the linked list.
*/
struct OperationListItem *op_tail;
/**
* Last operation ID used.
*/
uint64_t last_op_id;
};
/**
* Create new operations handle.
*/
struct GNUNET_OP_Handle *
GNUNET_OP_create ()
{
return GNUNET_new (struct GNUNET_OP_Handle);
}
/**
* Destroy operations handle.
*/
void
GNUNET_OP_destroy (struct GNUNET_OP_Handle *h)
{
GNUNET_free (h);
}
/**
* Get a unique operation ID to distinguish between asynchronous requests.
*
* @param h
* Operations handle.
*
* @return Operation ID to use.
*/
uint64_t
GNUNET_OP_get_next_id (struct GNUNET_OP_Handle *h)
{
return ++h->last_op_id;
}
/**
* Find operation by ID.
*
* @param h
* Operations handle.
* @param op_id
* Operation ID to look up.
*
* @return Operation, or NULL if not found.
*/
static struct OperationListItem *
op_find (struct GNUNET_OP_Handle *h,
uint64_t op_id)
{
struct OperationListItem *op;
for (op = h->op_head; NULL != op; op = op->next)
if (op->op_id == op_id)
return op;
return NULL;
}
/**
* Find operation by ID.
*
* @param h
* Operations handle.
* @param op_id
* Operation ID to look up.
* @param[out] result_cb
* If an operation was found, its result callback is returned here.
* @param[out] cls
* If an operation was found, its closure is returned here.
* @param[out] ctx
* If an operation was found, its user context is returned here.
*
* @return #GNUNET_YES if an operation was found,
* #GNUNET_NO if not found.
*/
int
GNUNET_OP_get (struct GNUNET_OP_Handle *h,
uint64_t op_id,
GNUNET_ResultCallback *result_cb,
void **cls,
void **ctx)
{
struct OperationListItem *op = op_find (h, op_id);
if (NULL != op)
{
if (NULL != result_cb)
*result_cb = op->result_cb;
if (NULL != cls)
*cls = op->cls;
if (NULL != ctx)
*ctx = op->ctx;
return GNUNET_YES;
}
return GNUNET_NO;
}
/**
* Add a new operation.
*
* @param h
* Operations handle.
* @param result_cb
* Function to call with the result of the operation.
* @param cls
* Closure for @a result_cb.
* @param ctx
* User context.
*
* @return ID of the new operation.
*/
uint64_t
GNUNET_OP_add (struct GNUNET_OP_Handle *h,
GNUNET_ResultCallback result_cb,
void *cls,
void *ctx)
{
struct OperationListItem *op;
op = GNUNET_new (struct OperationListItem);
op->op_id = GNUNET_OP_get_next_id (h);
op->result_cb = result_cb;
op->cls = cls;
op->ctx = ctx;
GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
h->op_tail,
op);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%p Added operation #%" PRIu64 "\n",
h, op->op_id);
return op->op_id;
}
/**
* Remove an operation, and call its result callback (unless it was cancelled).
*
*
* @param h
* Operations handle.
* @param op_id
* Operation ID.
* @param result_code
* Result of the operation.
* @param data
* Data result of the operation.
* @param data_size
* Size of @a data.
* @param[out] ctx
* User context.
* @param cancel
* Is the operation cancelled?
* #GNUNET_NO Not cancelled, result callback is called.
* #GNUNET_YES Cancelled, result callback is not called.
*
* @return #GNUNET_YES if the operation was found and removed,
* #GNUNET_NO if the operation was not found.
*/
static int
op_result (struct GNUNET_OP_Handle *h,
uint64_t op_id,
int64_t result_code,
const void *data,
uint16_t data_size,
void **ctx,
uint8_t cancel)
{
if (0 == op_id)
return GNUNET_NO;
struct OperationListItem *op = op_find (h, op_id);
if (NULL == op)
{
LOG (GNUNET_ERROR_TYPE_WARNING,
"Could not find operation #%" PRIu64 "\n", op_id);
return GNUNET_NO;
}
if (NULL != ctx)
*ctx = op->ctx;
GNUNET_CONTAINER_DLL_remove (h->op_head,
h->op_tail,
op);
if ( (GNUNET_YES != cancel) &&
(NULL != op->result_cb) )
op->result_cb (op->cls,
result_code, data,
data_size);
GNUNET_free (op);
return GNUNET_YES;
}
/**
* Call the result callback of an operation and remove it.
*
* @param h
* Operations handle.
* @param op_id
* Operation ID.
* @param result_code
* Result of the operation.
* @param data
* Data result of the operation.
* @param data_size
* Size of @a data.
* @param[out] ctx
* User context.
*
* @return #GNUNET_YES if the operation was found and removed,
* #GNUNET_NO if the operation was not found.
*/
int
GNUNET_OP_result (struct GNUNET_OP_Handle *h,
uint64_t op_id,
int64_t result_code,
const void *data,
uint16_t data_size,
void **ctx)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%p Received result for operation #%" PRIu64 ": %" PRId64 " (size: %u)\n",
h, op_id, result_code, data_size);
return op_result (h, op_id, result_code, data, data_size, ctx, GNUNET_NO);
}
/**
* Remove / cancel an operation.
*
* @param h
* Operations handle.
* @param op_id
* Operation ID.
*
* @return #GNUNET_YES if the operation was found and removed,
* #GNUNET_NO if the operation was not found.
*/
int
GNUNET_OP_remove (struct GNUNET_OP_Handle *h,
uint64_t op_id)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%p Cancelling operation #%" PRIu64 "\n",
h, op_id);
return op_result (h, op_id, 0, NULL, 0, NULL, GNUNET_YES);
}
|