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 
38 
42  uint64_t op_id;
43 
48 
52  void *cls;
53 
57  void *ctx;
58 };
59 
60 
70 
75 
79  uint64_t last_op_id;
80 };
81 
82 
86 struct GNUNET_OP_Handle *
88 {
89  return GNUNET_new(struct GNUNET_OP_Handle);
90 }
91 
92 
96 void
98 {
99  GNUNET_free(h);
100 }
101 
102 
111 uint64_t
113 {
114  return ++h->last_op_id;
115 }
116 
117 
128 static struct OperationListItem *
130  uint64_t op_id)
131 {
132  struct OperationListItem *op;
133 
134  for (op = h->op_head; NULL != op; op = op->next)
135  if (op->op_id == op_id)
136  return op;
137  return NULL;
138 }
139 
140 
158 int
160  uint64_t op_id,
162  void **cls,
163  void **ctx)
164 {
165  struct OperationListItem *op = op_find(h, op_id);
166 
167  if (NULL != op)
168  {
169  if (NULL != result_cb)
170  *result_cb = op->result_cb;
171  if (NULL != cls)
172  *cls = op->cls;
173  if (NULL != ctx)
174  *ctx = op->ctx;
175  return GNUNET_YES;
176  }
177  return GNUNET_NO;
178 }
179 
180 
195 uint64_t
198  void *cls,
199  void *ctx)
200 {
201  struct OperationListItem *op;
202 
203  op = GNUNET_new(struct OperationListItem);
204  op->op_id = GNUNET_OP_get_next_id(h);
205  op->result_cb = result_cb;
206  op->cls = cls;
207  op->ctx = ctx;
209  h->op_tail,
210  op);
212  "%p Added operation #%" PRIu64 "\n",
213  h, op->op_id);
214  return op->op_id;
215 }
216 
217 
242 static int
244  uint64_t op_id,
245  int64_t result_code,
246  const void *data,
247  uint16_t data_size,
248  void **ctx,
249  uint8_t cancel)
250 {
251  if (0 == op_id)
252  return GNUNET_NO;
253 
254  struct OperationListItem *op = op_find(h, op_id);
255  if (NULL == op)
256  {
258  "Could not find operation #%" PRIu64 "\n", op_id);
259  return GNUNET_NO;
260  }
261 
262  if (NULL != ctx)
263  *ctx = op->ctx;
264 
266  h->op_tail,
267  op);
268 
269  if ((GNUNET_YES != cancel) &&
270  (NULL != op->result_cb))
271  op->result_cb(op->cls,
272  result_code, data,
273  data_size);
274  GNUNET_free(op);
275  return GNUNET_YES;
276 }
277 
278 
298 int
300  uint64_t op_id,
301  int64_t result_code,
302  const void *data,
303  uint16_t data_size,
304  void **ctx)
305 {
307  "%p Received result for operation #%" PRIu64 ": %" PRId64 " (size: %u)\n",
308  h, op_id, result_code, data_size);
309  return op_result(h, op_id, result_code, data, data_size, ctx, GNUNET_NO);
310 }
311 
312 
324 int
326  uint64_t op_id)
327 {
329  "%p Cancelling operation #%" PRIu64 "\n",
330  h, op_id);
331  return op_result(h, op_id, 0, NULL, 0, NULL, GNUNET_YES);
332 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct OperationListItem * prev
Definition: op.c:36
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:196
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:243
struct OperationListItem * op_tail
Last operation in the linked list.
Definition: op.c:74
#define GNUNET_NO
Definition: gnunet_common.h:78
#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:325
void * ctx
User context.
Definition: op.c:57
static struct OperationListItem * op_find(struct GNUNET_OP_Handle *h, uint64_t op_id)
Find operation by ID.
Definition: op.c:129
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:112
void * cls
Closure for result_cb.
Definition: op.c:52
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:299
uint64_t last_op_id
Last operation ID used.
Definition: op.c:79
uint64_t op_id
Operation ID.
Definition: op.c:42
struct GNUNET_OP_Handle * GNUNET_OP_create()
Create new operations handle.
Definition: op.c:87
#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:97
Operations handle.
Definition: op.c:65
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:159
#define GNUNET_YES
Definition: gnunet_common.h:77
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:47
static size_t data_size
Number of bytes in data.
struct OperationListItem * op_head
First operation in the linked list.
Definition: op.c:69
struct OperationListItem * next
Definition: op.c:37
#define GNUNET_free(ptr)
Wrapper around free.