2020-03-29 15:40:33 +00:00
|
|
|
#include "webfuse/core/jsonrpc/proxy_intern.h"
|
|
|
|
#include "webfuse/core/jsonrpc/response_intern.h"
|
|
|
|
#include "webfuse/core/jsonrpc/error.h"
|
|
|
|
#include "webfuse/core/status.h"
|
2019-04-01 20:15:12 +00:00
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
#include "webfuse/core/timer/timer.h"
|
2020-02-29 20:06:40 +00:00
|
|
|
|
2020-02-29 01:32:03 +00:00
|
|
|
#include <stdlib.h>
|
2020-02-28 22:17:41 +00:00
|
|
|
#include <string.h>
|
2019-04-01 20:15:12 +00:00
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_proxy *
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_proxy_create(
|
2020-02-29 20:06:40 +00:00
|
|
|
struct wf_timer_manager * manager,
|
2020-02-29 01:32:03 +00:00
|
|
|
int timeout,
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_send_fn * send,
|
2020-02-29 01:32:03 +00:00
|
|
|
void * user_data)
|
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = malloc(sizeof(struct wf_jsonrpc_proxy));
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_proxy_init(proxy, manager, timeout, send, user_data);
|
2020-02-29 01:32:03 +00:00
|
|
|
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
void wf_jsonrpc_proxy_dispose(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy)
|
2020-02-29 01:32:03 +00:00
|
|
|
{
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_proxy_cleanup(proxy);
|
2020-02-29 01:32:03 +00:00
|
|
|
free(proxy);
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
static void wf_jsonrpc_proxy_on_timeout(
|
2020-02-29 20:06:40 +00:00
|
|
|
struct wf_timer * timer, void * proxy_ptr)
|
2019-04-01 20:15:12 +00:00
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = proxy_ptr;
|
2019-04-01 20:15:12 +00:00
|
|
|
|
|
|
|
if (proxy->request.is_pending)
|
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
2019-04-01 20:15:12 +00:00
|
|
|
void * user_data = proxy->request.user_data;
|
|
|
|
|
|
|
|
proxy->request.is_pending = false;
|
|
|
|
proxy->request.id = 0;
|
|
|
|
proxy->request.user_data = NULL;
|
|
|
|
proxy->request.finished = NULL;
|
2020-02-29 20:06:40 +00:00
|
|
|
wf_timer_cancel(timer);
|
2019-04-01 20:15:12 +00:00
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_propate_error(finished, user_data, WF_BAD_TIMEOUT, "Timeout");
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
static json_t * wf_jsonrpc_request_create(
|
2019-04-01 20:15:12 +00:00
|
|
|
char const * method,
|
|
|
|
int id,
|
|
|
|
char const * param_info,
|
|
|
|
va_list args)
|
|
|
|
{
|
|
|
|
json_t * request = json_object();
|
|
|
|
json_object_set_new(request, "method", json_string(method));
|
|
|
|
json_t * params = json_array();
|
|
|
|
|
|
|
|
for (char const * param_type = param_info; '\0' != *param_type; param_type++)
|
|
|
|
{
|
|
|
|
switch(*param_type)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
char const * const value = va_arg(args, char const *);
|
|
|
|
json_array_append_new(params, json_string(value));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
int const value = va_arg(args, int);
|
|
|
|
json_array_append_new(params, json_integer(value));
|
|
|
|
}
|
|
|
|
break;
|
2020-03-01 12:42:46 +00:00
|
|
|
case 'j':
|
|
|
|
{
|
|
|
|
json_t * const value = va_arg(args, json_t *);
|
|
|
|
json_array_append_new(params, value);
|
|
|
|
}
|
|
|
|
break;
|
2019-04-01 20:15:12 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
|
2019-05-19 12:33:42 +00:00
|
|
|
json_decref(params);
|
|
|
|
json_decref(request);
|
|
|
|
return NULL;
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
json_object_set_new(request, "params", params);
|
|
|
|
if (0 != id)
|
|
|
|
{
|
|
|
|
json_object_set_new(request, "id", json_integer(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
void wf_jsonrpc_proxy_init(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy,
|
2020-02-29 20:06:40 +00:00
|
|
|
struct wf_timer_manager * timeout_manager,
|
2019-05-19 12:33:42 +00:00
|
|
|
int timeout,
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_send_fn * send,
|
2019-04-01 20:15:12 +00:00
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
proxy->send = send;
|
2019-05-19 12:33:42 +00:00
|
|
|
proxy->timeout = timeout;
|
2019-04-01 20:15:12 +00:00
|
|
|
proxy->user_data = user_data;
|
|
|
|
proxy->request.is_pending = false;
|
2020-02-29 20:06:40 +00:00
|
|
|
proxy->request.timer = wf_timer_create(timeout_manager,
|
2020-03-29 15:40:33 +00:00
|
|
|
&wf_jsonrpc_proxy_on_timeout, proxy);
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
void wf_jsonrpc_proxy_cleanup(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy)
|
2019-04-01 20:15:12 +00:00
|
|
|
{
|
|
|
|
if (proxy->request.is_pending)
|
|
|
|
{
|
2019-05-19 12:33:42 +00:00
|
|
|
void * user_data = proxy->request.user_data;
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
proxy->request.is_pending = false;
|
2019-05-19 12:33:42 +00:00
|
|
|
proxy->request.finished = NULL;
|
|
|
|
proxy->request.user_data = NULL;
|
|
|
|
proxy->request.id = 0;
|
2020-02-29 20:06:40 +00:00
|
|
|
wf_timer_cancel(proxy->request.timer);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_propate_error(finished, user_data, WF_BAD, "Bad: cancelled pending request during shutdown");
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-02-29 20:06:40 +00:00
|
|
|
wf_timer_dispose(proxy->request.timer);
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 06:32:26 +00:00
|
|
|
void wf_jsonrpc_proxy_vinvoke(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy,
|
|
|
|
wf_jsonrpc_proxy_finished_fn * finished,
|
2019-04-01 20:15:12 +00:00
|
|
|
void * user_data,
|
|
|
|
char const * method_name,
|
|
|
|
char const * param_info,
|
2020-04-04 06:32:26 +00:00
|
|
|
va_list args)
|
2019-04-01 20:15:12 +00:00
|
|
|
{
|
|
|
|
if (!proxy->request.is_pending)
|
|
|
|
{
|
|
|
|
proxy->request.is_pending = true;
|
|
|
|
proxy->request.finished = finished;
|
|
|
|
proxy->request.user_data = user_data;
|
|
|
|
proxy->request.id = 42;
|
2020-02-29 20:06:40 +00:00
|
|
|
wf_timer_start(proxy->request.timer, proxy->timeout);
|
2019-04-01 20:15:12 +00:00
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
json_t * request = wf_jsonrpc_request_create(method_name, proxy->request.id, param_info, args);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
bool const is_send = ((NULL != request) && (proxy->send(request, proxy->user_data)));
|
|
|
|
if (!is_send)
|
|
|
|
{
|
|
|
|
proxy->request.is_pending = false;
|
|
|
|
proxy->request.finished = NULL;
|
|
|
|
proxy->request.user_data = NULL;
|
|
|
|
proxy->request.id = 0;
|
2020-02-29 20:06:40 +00:00
|
|
|
wf_timer_cancel(proxy->request.timer);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_propate_error(finished, user_data, WF_BAD, "Bad: requenst is not sent");
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
if (NULL != request)
|
|
|
|
{
|
|
|
|
json_decref(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_propate_error(finished, user_data, WF_BAD_BUSY, "Busy");
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:55:59 +00:00
|
|
|
extern void wf_jsonrpc_proxy_vnotify(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy,
|
2019-04-01 20:15:12 +00:00
|
|
|
char const * method_name,
|
|
|
|
char const * param_info,
|
2020-04-04 06:55:59 +00:00
|
|
|
va_list args)
|
2020-03-29 15:40:33 +00:00
|
|
|
{
|
|
|
|
json_t * request = wf_jsonrpc_request_create(method_name, 0, param_info, args);
|
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
if (NULL != request)
|
|
|
|
{
|
|
|
|
proxy->send(request, proxy->user_data);
|
|
|
|
json_decref(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
void wf_jsonrpc_proxy_onresult(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy,
|
2019-04-01 20:15:12 +00:00
|
|
|
json_t * message)
|
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_response response;
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_response_init(&response, message);
|
2019-04-01 20:15:12 +00:00
|
|
|
|
|
|
|
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
|
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
2019-04-01 20:15:12 +00:00
|
|
|
void * user_data = proxy->request.user_data;
|
|
|
|
|
|
|
|
proxy->request.is_pending = false;
|
|
|
|
proxy->request.id = 0;
|
|
|
|
proxy->request.user_data = NULL;
|
|
|
|
proxy->request.finished = NULL;
|
2020-02-29 20:06:40 +00:00
|
|
|
wf_timer_cancel(proxy->request.timer);
|
2019-04-01 20:15:12 +00:00
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
finished(user_data, response.result, response.error);
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_response_cleanup(&response);
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
|