Statistics
| Branch: | Revision:

grapes / src / Utils / request_handler.h @ 42cbd06c

History | View | Annotate | Download (1.99 KB)

1 42cbd06c Andrea Zito
/*
2
 *  Copyright (c) 2011 Andrea Zito
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 *
6
 *  This module provide an easy way to perform request and receive response in an
7
 *  asynchronous way using blocking functions. Request and response are kept in a
8
 *  dedicated FIFO queue.
9
 */
10
11
#ifndef CLOUD_HELPER_DELEGATE_UTILS_H
12
#define CLOUD_HELPER_DELEGATE_UTILS_H
13
14
15
16
struct req_handler_ctx;
17
18
/* Callback called to process the req specified by req_data. Should return 0
19
   on success, 1 to requeue the request (as last element), -1 to abort the
20
   request. On success the request handler should allocate and populate the
21
   rsp_data pointer with the context of the response. If no response is
22
   needed, rsp_data should be assigned NULL. */
23
typedef int (*process_request_callback_p)(void *req_data, void **rsp_data);
24
25
typedef void (*free_request_callback_p)(void *req_data);
26
27
/* Initialize the data structures and threads and return a pointer to the
28
   cloud_req_handler context */
29
struct req_handler_ctx* req_handler_init();
30
31
/* Release the resource acquired  by init */
32
void req_handler_destroy(struct req_handler_ctx *ctx);
33
34
/* Add a request to the queue. Such a request will be handled by the function
35
   pointed by req_callback and freed by the function pointed by free_req_data.
36
   Return 0 on success, 1 on failure */
37
int req_handler_add_request(struct req_handler_ctx *ctx,
38
                            process_request_callback_p req_callback,
39
                            void *req_data,
40
                            free_request_callback_p free_req_data);
41
42
/* Wait for a response for at most tout */
43
void* req_handler_wait4response(struct req_handler_ctx *ctx,
44
                                struct timeval *tout);
45
46
/* Return the first response in the queue or NULL if none is ready */
47
void* req_handler_get_response(struct req_handler_ctx *ctx);
48
49
/* Return and remove the first response in the queue or NULL if none is ready*/
50
void* req_handler_remove_response(struct req_handler_ctx *ctx);
51
52
#endif /* CLOUD_HELPER_DELEGATE_UTILS_H */