GNUnet  0.10.x
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 <inttypes.h>
29 
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 
33 #define LOG(kind,...) GNUNET_log_from (kind, "util-op", __VA_ARGS__)
34 
36 {
39 
43  uint64_t op_id;
44 
49 
53  void *cls;
54 
58  void *ctx;
59 };
60 
61 
67 {
72 
77 
81  uint64_t last_op_id;
82 };
83 
84 
88 struct GNUNET_OP_Handle *
90 {
91  return GNUNET_new (struct GNUNET_OP_Handle);
92 }
93 
94 
98 void
100 {
101  GNUNET_free (h);
102 }
103 
104 
113 uint64_t
115 {
116  return ++h->last_op_id;
117 }
118 
119 
130 static struct OperationListItem *
132  uint64_t op_id)
133 {
134  struct OperationListItem *op;
135 
136  for (op = h->op_head; NULL != op; op = op->next)
137  if (op->op_id == op_id)
138  return op;
139  return NULL;
140 }
141 
142 
160 int
162  uint64_t op_id,
164  void **cls,
165  void **ctx)
166 {
167  struct OperationListItem *op = op_find (h, op_id);
168  if (NULL != op)
169  {
170  if (NULL != result_cb)
171  *result_cb = op->result_cb;
172  if (NULL != cls)
173  *cls = op->cls;
174  if (NULL != ctx)
175  *ctx = op->ctx;
176  return GNUNET_YES;
177  }
178  return GNUNET_NO;
179 }
180 
181 
196 uint64_t
199  void *cls,
200  void *ctx)
201 {
202  struct OperationListItem *op;
203 
204  op = GNUNET_new (struct OperationListItem);
205  op->op_id = GNUNET_OP_get_next_id (h);
206  op->result_cb = result_cb;
207  op->cls = cls;
208  op->ctx = ctx;
210  h->op_tail,
211  op);
213  "%p Added operation #%" PRIu64 "\n",
214  h, op->op_id);
215  return op->op_id;
216 }
217 
218 
243 static int
245  uint64_t op_id,
246  int64_t result_code,
247  const void *data,
248  uint16_t data_size,
249  void **ctx,
250  uint8_t cancel)
251 {
252  if (0 == op_id)
253  return GNUNET_NO;
254 
255  struct OperationListItem *op = op_find (h, op_id);
256  if (NULL == op)
257  {
259  "Could not find operation #%" PRIu64 "\n", op_id);
260  return GNUNET_NO;
261  }
262 
263  if (NULL != ctx)
264  *ctx = op->ctx;
265 
267  h->op_tail,
268  op);
269 
270  if ( (GNUNET_YES != cancel) &&
271  (NULL != op->result_cb) )
272  op->result_cb (op->cls,
273  result_code, data,
274  data_size);
275  GNUNET_free (op);
276  return GNUNET_YES;
277 }
278 
279 
299 int
301  uint64_t op_id,
302  int64_t result_code,
303  const void *data,
304  uint16_t data_size,
305  void **ctx)
306 {
308  "%p Received result for operation #%" PRIu64 ": %" PRId64 " (size: %u)\n",
309  h, op_id, result_code, data_size);
310  return op_result (h, op_id, result_code, data, data_size, ctx, GNUNET_NO);
311 }
312 
313 
325 int
327  uint64_t op_id)
328 {
330  "%p Cancelling operation #%" PRIu64 "\n",
331  h, op_id);
332  return op_result (h, op_id, 0, NULL, 0, NULL, GNUNET_YES);
333 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct OperationListItem * prev
Definition: op.c:37
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:197
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.
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:244
struct OperationListItem * op_tail
Last operation in the linked list.
Definition: op.c:76
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define LOG(kind,...)
Definition: op.c:33
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
int GNUNET_OP_remove(struct GNUNET_OP_Handle *h, uint64_t op_id)
Remove / cancel an operation.
Definition: op.c:326
void * ctx
User context.
Definition: op.c:58
static struct OperationListItem * op_find(struct GNUNET_OP_Handle *h, uint64_t op_id)
Find operation by ID.
Definition: op.c:131
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:114
void * cls
Closure for result_cb.
Definition: op.c:53
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:300
uint64_t last_op_id
Last operation ID used.
Definition: op.c:81
uint64_t op_id
Operation ID.
Definition: op.c:43
struct GNUNET_OP_Handle * GNUNET_OP_create()
Create new operations handle.
Definition: op.c:89
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
void GNUNET_OP_destroy(struct GNUNET_OP_Handle *h)
Destroy operations handle.
Definition: op.c:99
Operations handle.
Definition: op.c:66
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:161
#define GNUNET_YES
Definition: gnunet_common.h:80
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:139
uint32_t data
The data value.
GNUNET_ResultCallback result_cb
Continuation to invoke with the result of an operation.
Definition: op.c:48
static size_t data_size
Number of bytes in data.
struct OperationListItem * op_head
First operation in the linked list.
Definition: op.c:71
struct OperationListItem * next
Definition: op.c:38
#define GNUNET_free(ptr)
Wrapper around free.