GNUnet  0.10.x
Data Structures | Macros | Functions
op.c File Reference

Asynchronous operations; register callbacks for operations and call them when a response arrives. More...

#include <inttypes.h>
#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for op.c:

Go to the source code of this file.

Data Structures

struct  OperationListItem
 
struct  GNUNET_OP_Handle
 Operations handle. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from(kind, "util-op", __VA_ARGS__)
 

Functions

struct GNUNET_OP_HandleGNUNET_OP_create ()
 Create new operations handle. More...
 
void GNUNET_OP_destroy (struct GNUNET_OP_Handle *h)
 Destroy operations handle. More...
 
uint64_t GNUNET_OP_get_next_id (struct GNUNET_OP_Handle *h)
 Get a unique operation ID to distinguish between asynchronous requests. More...
 
static struct OperationListItemop_find (struct GNUNET_OP_Handle *h, uint64_t op_id)
 Find operation by ID. More...
 
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. More...
 
uint64_t GNUNET_OP_add (struct GNUNET_OP_Handle *h, GNUNET_ResultCallback result_cb, void *cls, void *ctx)
 Add a new operation. More...
 
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). More...
 
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. More...
 
int GNUNET_OP_remove (struct GNUNET_OP_Handle *h, uint64_t op_id)
 Remove / cancel an operation. More...
 

Detailed Description

Asynchronous operations; register callbacks for operations and call them when a response arrives.

Author
Gabor X Toth

Definition in file op.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from(kind, "util-op", __VA_ARGS__)

Definition at line 33 of file op.c.

Referenced by GNUNET_OP_add(), GNUNET_OP_remove(), GNUNET_OP_result(), and op_result().

Function Documentation

◆ GNUNET_OP_create()

struct GNUNET_OP_Handle* GNUNET_OP_create ( )

Create new operations handle.

Definition at line 87 of file op.c.

References GNUNET_new.

88 {
89  return GNUNET_new(struct GNUNET_OP_Handle);
90 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Operations handle.
Definition: op.c:65

◆ GNUNET_OP_destroy()

void GNUNET_OP_destroy ( struct GNUNET_OP_Handle h)

Destroy operations handle.

Definition at line 97 of file op.c.

References GNUNET_free.

98 {
99  GNUNET_free(h);
100 }
#define GNUNET_free(ptr)
Wrapper around free.

◆ GNUNET_OP_get_next_id()

uint64_t GNUNET_OP_get_next_id ( struct GNUNET_OP_Handle h)

Get a unique operation ID to distinguish between asynchronous requests.

Parameters
hOperations handle.
Returns
Operation ID to use.

Definition at line 112 of file op.c.

References GNUNET_OP_Handle::last_op_id.

Referenced by GNUNET_OP_add().

113 {
114  return ++h->last_op_id;
115 }
uint64_t last_op_id
Last operation ID used.
Definition: op.c:79
Here is the caller graph for this function:

◆ op_find()

static struct OperationListItem* op_find ( struct GNUNET_OP_Handle h,
uint64_t  op_id 
)
static

Find operation by ID.

Parameters
hOperations handle.
op_idOperation ID to look up.
Returns
Operation, or NULL if not found.

Definition at line 129 of file op.c.

References OperationListItem::next, op, GNUNET_OP_Handle::op_head, and OperationListItem::op_id.

Referenced by GNUNET_OP_get(), and op_result().

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 }
uint64_t op_id
Operation ID.
Definition: op.c:42
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:139
struct OperationListItem * op_head
First operation in the linked list.
Definition: op.c:69
struct OperationListItem * next
Definition: op.c:37
Here is the caller graph for this function:

◆ GNUNET_OP_get()

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.

Parameters
hOperations handle.
op_idOperation ID to look up.
[out]result_cbIf an operation was found, its result callback is returned here.
[out]clsIf an operation was found, its closure is returned here.
[out]ctxIf an operation was found, its user context is returned here.
Returns
GNUNET_YES if an operation was found, GNUNET_NO if not found.

Definition at line 159 of file op.c.

References OperationListItem::cls, OperationListItem::ctx, GNUNET_NO, GNUNET_YES, op, op_find(), and OperationListItem::result_cb.

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 }
#define GNUNET_NO
Definition: gnunet_common.h:78
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
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
void * cls
Closure for result_cb.
Definition: op.c:52
uint64_t op_id
Operation ID.
Definition: op.c:42
#define GNUNET_YES
Definition: gnunet_common.h:77
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:139
GNUNET_ResultCallback result_cb
Continuation to invoke with the result of an operation.
Definition: op.c:47
Here is the call graph for this function:

◆ GNUNET_OP_add()

uint64_t GNUNET_OP_add ( struct GNUNET_OP_Handle h,
GNUNET_ResultCallback  result_cb,
void *  cls,
void *  ctx 
)

Add a new operation.

Parameters
hOperations handle.
result_cbFunction to call with the result of the operation.
clsClosure for result_cb.
ctxUser context.
Returns
ID of the new operation.

Definition at line 196 of file op.c.

References OperationListItem::cls, OperationListItem::ctx, GNUNET_CONTAINER_DLL_insert_tail, GNUNET_ERROR_TYPE_DEBUG, GNUNET_new, GNUNET_OP_get_next_id(), LOG, op, GNUNET_OP_Handle::op_head, OperationListItem::op_id, GNUNET_OP_Handle::op_tail, and OperationListItem::result_cb.

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 }
struct OperationListItem * op_tail
Last operation in the linked list.
Definition: op.c:74
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define LOG(kind,...)
Definition: op.c:33
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
void * ctx
User context.
Definition: op.c:57
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
uint64_t op_id
Operation ID.
Definition: op.c:42
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:139
GNUNET_ResultCallback result_cb
Continuation to invoke with the result of an operation.
Definition: op.c:47
struct OperationListItem * op_head
First operation in the linked list.
Definition: op.c:69
Here is the call graph for this function:

◆ op_result()

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 
)
static

Remove an operation, and call its result callback (unless it was cancelled).

Parameters
hOperations handle.
op_idOperation ID.
result_codeResult of the operation.
dataData result of the operation.
data_sizeSize of data.
[out]ctxUser context.
cancelIs the operation cancelled? GNUNET_NO Not cancelled, result callback is called. GNUNET_YES Cancelled, result callback is not called.
Returns
GNUNET_YES if the operation was found and removed, GNUNET_NO if the operation was not found.

Definition at line 243 of file op.c.

References OperationListItem::cls, OperationListItem::ctx, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_NO, GNUNET_YES, LOG, op, op_find(), GNUNET_OP_Handle::op_head, GNUNET_OP_Handle::op_tail, and OperationListItem::result_cb.

Referenced by cadet_disconnect_adapter(), check_statistics_collect_completed(), dht_disconnect_adapter(), fs_disconnect_adapter(), GNUNET_OP_remove(), GNUNET_OP_result(), rps_disconnect_adapter(), and statistics_da().

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 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct OperationListItem * op_tail
Last operation in the linked list.
Definition: op.c:74
#define GNUNET_NO
Definition: gnunet_common.h:78
#define LOG(kind,...)
Definition: op.c:33
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
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
void * cls
Closure for result_cb.
Definition: op.c:52
uint64_t op_id
Operation ID.
Definition: op.c:42
#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
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OP_result()

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.

Parameters
hOperations handle.
op_idOperation ID.
result_codeResult of the operation.
dataData result of the operation.
data_sizeSize of data.
[out]ctxUser context.
Returns
GNUNET_YES if the operation was found and removed, GNUNET_NO if the operation was not found.

Definition at line 299 of file op.c.

References GNUNET_ERROR_TYPE_DEBUG, GNUNET_NO, LOG, and op_result().

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 }
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
#define GNUNET_NO
Definition: gnunet_common.h:78
#define LOG(kind,...)
Definition: op.c:33
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
uint64_t op_id
Operation ID.
Definition: op.c:42
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
Here is the call graph for this function:

◆ GNUNET_OP_remove()

int GNUNET_OP_remove ( struct GNUNET_OP_Handle h,
uint64_t  op_id 
)

Remove / cancel an operation.

Parameters
hOperations handle.
op_idOperation ID.
Returns
GNUNET_YES if the operation was found and removed, GNUNET_NO if the operation was not found.

Definition at line 325 of file op.c.

References GNUNET_ERROR_TYPE_DEBUG, GNUNET_YES, LOG, and op_result().

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 }
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
#define LOG(kind,...)
Definition: op.c:33
uint64_t op_id
Operation ID.
Definition: op.c:42
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the call graph for this function: