GNUnet 0.21.0
op.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2016 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 */
20
28#include "platform.h"
29#include <inttypes.h>
30
31
32#include "gnunet_util_lib.h"
33
34#define LOG(kind, ...) GNUNET_log_from (kind, "util-op", __VA_ARGS__)
35
37{
40
44 uint64_t op_id;
45
50
54 void *cls;
55
59 void *ctx;
60};
61
62
68{
73
78
82 uint64_t last_op_id;
83};
84
85
89struct GNUNET_OP_Handle *
91{
92 return GNUNET_new (struct GNUNET_OP_Handle);
93}
94
95
99void
101{
102 GNUNET_free (h);
103}
104
105
114uint64_t
116{
117 return ++h->last_op_id;
118}
119
120
131static struct OperationListItem *
133 uint64_t op_id)
134{
135 struct OperationListItem *op;
136
137 for (op = h->op_head; NULL != op; op = op->next)
138 if (op->op_id == op_id)
139 return op;
140 return NULL;
141}
142
143
161int
163 uint64_t op_id,
165 void **cls,
166 void **ctx)
167{
168 struct OperationListItem *op = op_find (h, op_id);
169
170 if (NULL != op)
171 {
172 if (NULL != result_cb)
173 *result_cb = op->result_cb;
174 if (NULL != cls)
175 *cls = op->cls;
176 if (NULL != ctx)
177 *ctx = op->ctx;
178 return GNUNET_YES;
179 }
180 return GNUNET_NO;
181}
182
183
198uint64_t
201 void *cls,
202 void *ctx)
203{
204 struct OperationListItem *op;
205
207 op->op_id = GNUNET_OP_get_next_id (h);
208 op->result_cb = result_cb;
209 op->cls = cls;
210 op->ctx = ctx;
212 h->op_tail,
213 op);
215 "%p Added operation #%" PRIu64 "\n",
216 h, op->op_id);
217 return op->op_id;
218}
219
220
245static int
247 uint64_t op_id,
248 int64_t result_code,
249 const void *data,
250 uint16_t data_size,
251 void **ctx,
252 uint8_t cancel)
253{
254 if (0 == op_id)
255 return GNUNET_NO;
256
257 struct OperationListItem *op = op_find (h, op_id);
258 if (NULL == op)
259 {
261 "Could not find operation #%" PRIu64 "\n", op_id);
262 return GNUNET_NO;
263 }
264
265 if (NULL != ctx)
266 *ctx = op->ctx;
267
269 h->op_tail,
270 op);
271
272 if ((GNUNET_YES != cancel) &&
273 (NULL != op->result_cb))
274 op->result_cb (op->cls,
275 result_code, data,
276 data_size);
277 GNUNET_free (op);
278 return GNUNET_YES;
279}
280
281
301int
303 uint64_t op_id,
304 int64_t result_code,
305 const void *data,
306 uint16_t data_size,
307 void **ctx)
308{
310 "%p Received result for operation #%" PRIu64 ": %" PRId64 " (size: %u)\n",
311 h, op_id, result_code, data_size);
312 return op_result (h, op_id, result_code, data, data_size, ctx, GNUNET_NO);
313}
314
315
327int
329 uint64_t op_id)
330{
332 "%p Cancelling operation #%" PRIu64 "\n",
333 h, op_id);
334 return op_result (h, op_id, 0, NULL, 0, NULL, GNUNET_YES);
335}
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:144
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static char * data
The data to insert into the dht.
static struct GNUNET_FS_Handle * ctx
static size_t data_size
Number of bytes in data.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
uint64_t GNUNET_OP_get_next_id(struct GNUNET_OP_Handle *h)
Get a unique operation ID to distinguish between asynchronous requests.
Definition: op.c:115
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)
Call the result callback of an operation and remove it.
Definition: op.c:302
void(* GNUNET_ResultCallback)(void *cls, int64_t result_code, const void *data, uint16_t data_size)
Function called with the result of an asynchronous operation.
int GNUNET_OP_get(struct GNUNET_OP_Handle *h, uint64_t op_id, GNUNET_ResultCallback *result_cb, void **cls, void **ctx)
Find operation by ID.
Definition: op.c:162
int GNUNET_OP_remove(struct GNUNET_OP_Handle *h, uint64_t op_id)
Remove / cancel an operation.
Definition: op.c:328
void GNUNET_OP_destroy(struct GNUNET_OP_Handle *h)
Destroy operations handle.
Definition: op.c:100
uint64_t GNUNET_OP_add(struct GNUNET_OP_Handle *h, GNUNET_ResultCallback result_cb, void *cls, void *ctx)
Add a new operation.
Definition: op.c:199
struct GNUNET_OP_Handle * GNUNET_OP_create()
Create new operations handle.
Definition: op.c:90
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
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)
Remove an operation, and call its result callback (unless it was cancelled).
Definition: op.c:246
#define LOG(kind,...)
Definition: op.c:34
static struct OperationListItem * op_find(struct GNUNET_OP_Handle *h, uint64_t op_id)
Find operation by ID.
Definition: op.c:132
struct GNUNET_ARM_Operation * next
This is a doubly-linked list.
Definition: arm_api.c:45
Operations handle.
Definition: op.c:68
struct OperationListItem * op_tail
Last operation in the linked list.
Definition: op.c:77
struct OperationListItem * op_head
First operation in the linked list.
Definition: op.c:72
uint64_t last_op_id
Last operation ID used.
Definition: op.c:82
void * ctx
User context.
Definition: op.c:59
void * cls
Closure for result_cb.
Definition: op.c:54
struct OperationListItem * next
Definition: op.c:39
struct OperationListItem * prev
Definition: op.c:38
uint64_t op_id
Operation ID.
Definition: op.c:44
GNUNET_ResultCallback result_cb
Continuation to invoke with the result of an operation.
Definition: op.c:49