Merge pull request #51 from falk-werner/provider_authentication
Feature: enable authentication for provider/clientpull/52/head
commit
d040440e07
@ -0,0 +1,24 @@
|
||||
# wf_jsonrpc
|
||||
|
||||
add_library(wf_jsonrpc STATIC
|
||||
lib/wf/jsonrpc/src/wf/jsonrpc/api.c
|
||||
lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c
|
||||
lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c
|
||||
lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c
|
||||
lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c
|
||||
lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.c
|
||||
lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.c
|
||||
)
|
||||
|
||||
target_link_libraries(wf_jsonrpc PUBLIC wf_timer)
|
||||
|
||||
target_include_directories(wf_jsonrpc PRIVATE
|
||||
lib/wf/timer/include
|
||||
lib/wf/jsonrpc/src
|
||||
)
|
||||
|
||||
target_include_directories(wf_jsonrpc PUBLIC
|
||||
lib/wf/jsonrpc/include
|
||||
)
|
||||
|
||||
set_target_properties(wf_jsonrpc PROPERTIES C_VISIBILITY_PRESET hidden)
|
@ -0,0 +1,18 @@
|
||||
# timer
|
||||
|
||||
add_library(wf_timer STATIC
|
||||
lib/wf/timer/src/wf/timer/api.c
|
||||
lib/wf/timer/src/wf/timer/impl/manager.c
|
||||
lib/wf/timer/src/wf/timer/impl/timepoint.c
|
||||
lib/wf/timer/src/wf/timer/impl/timer.c
|
||||
)
|
||||
|
||||
target_include_directories(wf_timer PRIVATE
|
||||
lib/wf/timer/src
|
||||
)
|
||||
|
||||
target_include_directories(wf_timer PUBLIC
|
||||
lib/wf/timer/include
|
||||
)
|
||||
|
||||
set_target_properties(wf_timer PROPERTIES C_VISIBILITY_PRESET hidden)
|
@ -0,0 +1,30 @@
|
||||
#ifndef WF_PROVIDER_CREDENTIALS_H
|
||||
#define WF_PROVIDER_CREDENTIALS_H
|
||||
|
||||
#include <webfuse/provider/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_credentials;
|
||||
|
||||
typedef void wfp_get_credentials_fn(
|
||||
struct wfp_credentials * credentials,
|
||||
void * user_data);
|
||||
|
||||
extern WFP_API void wfp_credentials_set_type(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * type);
|
||||
|
||||
extern WFP_API void wfp_credentials_add(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * key,
|
||||
char const * value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,40 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_JSONRPC_METHOD_H
|
||||
#define WF_ADAPTER_IMPL_JSONRPC_METHOD_H
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_impl_jsonrpc_request;
|
||||
|
||||
typedef void wf_impl_jsonrpc_method_invoke_fn(
|
||||
struct wf_impl_jsonrpc_request * request,
|
||||
char const * method_name,
|
||||
json_t * params,
|
||||
void * user_data);
|
||||
|
||||
struct wf_impl_jsonrpc_method
|
||||
{
|
||||
struct wf_impl_jsonrpc_method * next;
|
||||
char * name;
|
||||
wf_impl_jsonrpc_method_invoke_fn * invoke;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
extern struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create(
|
||||
char const * method_name,
|
||||
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_impl_jsonrpc_method_dispose(
|
||||
struct wf_impl_jsonrpc_method * method);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -1,194 +0,0 @@
|
||||
#include "webfuse/adapter/impl/jsonrpc/proxy.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "webfuse/adapter/impl/jsonrpc/response.h"
|
||||
|
||||
static void wf_impl_jsonrpc_proxy_timeout(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
struct wf_impl_jsonrpc_proxy * proxy = timer->user_data;
|
||||
|
||||
if (proxy->request.is_pending)
|
||||
{
|
||||
wf_impl_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
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;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
finished(user_data, WF_BAD_TIMEOUT, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static json_t * wf_impl_jsonrpc_request_create(
|
||||
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;
|
||||
default:
|
||||
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
|
||||
json_decref(params);
|
||||
json_decref(request);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
json_object_set_new(request, "params", params);
|
||||
if (0 != id)
|
||||
{
|
||||
json_object_set_new(request, "id", json_integer(id));
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_proxy_init(
|
||||
struct wf_impl_jsonrpc_proxy * proxy,
|
||||
struct wf_impl_timeout_manager * timeout_manager,
|
||||
int timeout,
|
||||
wf_impl_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
proxy->send = send;
|
||||
proxy->timeout = timeout;
|
||||
proxy->user_data = user_data;
|
||||
proxy->request.is_pending = false;
|
||||
|
||||
wf_impl_timer_init(&proxy->request.timer, timeout_manager);
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_proxy_cleanup(
|
||||
struct wf_impl_jsonrpc_proxy * proxy)
|
||||
{
|
||||
if (proxy->request.is_pending)
|
||||
{
|
||||
void * user_data = proxy->request.user_data;
|
||||
wf_impl_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.finished = NULL;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.id = 0;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
finished(user_data, WF_BAD, NULL);
|
||||
}
|
||||
|
||||
wf_impl_timer_cleanup(&proxy->request.timer);
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_proxy_invoke(
|
||||
struct wf_impl_jsonrpc_proxy * proxy,
|
||||
wf_impl_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
)
|
||||
{
|
||||
if (!proxy->request.is_pending)
|
||||
{
|
||||
proxy->request.is_pending = true;
|
||||
proxy->request.finished = finished;
|
||||
proxy->request.user_data = user_data;
|
||||
proxy->request.id = 42;
|
||||
wf_impl_timer_start(&proxy->request.timer, wf_impl_timepoint_in_msec(proxy->timeout),
|
||||
&wf_impl_jsonrpc_proxy_timeout, proxy);
|
||||
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
json_t * request = wf_impl_jsonrpc_request_create(method_name, proxy->request.id, param_info, args);
|
||||
va_end(args);
|
||||
|
||||
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;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
finished(user_data, WF_BAD, NULL);
|
||||
|
||||
}
|
||||
|
||||
if (NULL != request)
|
||||
{
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
finished(user_data, WF_BAD_BUSY, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
extern void wf_impl_jsonrpc_proxy_notify(
|
||||
struct wf_impl_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
json_t * request = wf_impl_jsonrpc_request_create(method_name, 0, param_info, args);
|
||||
va_end(args);
|
||||
if (NULL != request)
|
||||
{
|
||||
proxy->send(request, proxy->user_data);
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void wf_impl_jsonrpc_proxy_onresult(
|
||||
struct wf_impl_jsonrpc_proxy * proxy,
|
||||
json_t * message)
|
||||
{
|
||||
struct wf_impl_jsonrpc_response response;
|
||||
wf_impl_jsonrpc_response_init(&response, message);
|
||||
|
||||
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
|
||||
{
|
||||
wf_impl_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
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;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
finished(user_data, response.status, response.result);
|
||||
}
|
||||
|
||||
wf_impl_jsonrpc_response_cleanup(&response);
|
||||
}
|
||||
|
@ -1,82 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_JSONRPC_PROXY_H
|
||||
#define WF_ADAPTER_IMPL_JSONRPC_PROXY_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse/adapter/impl/jsonrpc/send_fn.h"
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
#include "webfuse/adapter/impl/time/timer.h"
|
||||
#include "webfuse/core/status.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void wf_impl_jsonrpc_proxy_finished_fn(
|
||||
void * user_data,
|
||||
wf_status status,
|
||||
struct json_t const * result);
|
||||
|
||||
|
||||
struct wf_impl_jsonrpc_request
|
||||
{
|
||||
bool is_pending;
|
||||
wf_impl_jsonrpc_proxy_finished_fn * finished;
|
||||
void * user_data;
|
||||
int id;
|
||||
struct wf_impl_timer timer;
|
||||
};
|
||||
|
||||
struct wf_impl_jsonrpc_proxy
|
||||
{
|
||||
struct wf_impl_jsonrpc_request request;
|
||||
int timeout;
|
||||
wf_impl_jsonrpc_send_fn * send;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
extern void wf_impl_jsonrpc_proxy_init(
|
||||
struct wf_impl_jsonrpc_proxy * proxy,
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
int timeout,
|
||||
wf_impl_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_impl_jsonrpc_proxy_cleanup(
|
||||
struct wf_impl_jsonrpc_proxy * proxy);
|
||||
|
||||
extern void wf_impl_jsonrpc_proxy_invoke(
|
||||
struct wf_impl_jsonrpc_proxy * proxy,
|
||||
wf_impl_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
);
|
||||
|
||||
extern void wf_impl_jsonrpc_proxy_notify(
|
||||
struct wf_impl_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
);
|
||||
|
||||
extern void wf_impl_jsonrpc_proxy_onresult(
|
||||
struct wf_impl_jsonrpc_proxy * proxy,
|
||||
json_t * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -1,52 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_JSONRPC_REQUEST_H
|
||||
#define WF_ADAPTER_IMPL_JSONRPC_REQUEST_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse/core/status.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_impl_jsonrpc_request;
|
||||
|
||||
extern bool wf_impl_jsonrpc_is_request(
|
||||
json_t * message);
|
||||
|
||||
extern struct wf_impl_jsonrpc_request * wf_impl_jsonrpc_request_create(
|
||||
int id,
|
||||
wf_impl_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_impl_jsonrpc_request_dispose(
|
||||
struct wf_impl_jsonrpc_request * request);
|
||||
|
||||
extern void * wf_impl_jsonrpc_request_get_userdata(
|
||||
struct wf_impl_jsonrpc_request * request);
|
||||
|
||||
extern void wf_impl_jsonrpc_respond(
|
||||
struct wf_impl_jsonrpc_request * request,
|
||||
json_t * result);
|
||||
|
||||
extern void wf_impl_jsonrpc_respond_error(
|
||||
struct wf_impl_jsonrpc_request * request,
|
||||
wf_status status);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,41 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_JSONRPC_RESPONSE_H
|
||||
#define WF_ADAPTER_IMPL_JSONRPC_RESPONSE_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse/core/status.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct wf_impl_jsonrpc_response
|
||||
{
|
||||
wf_status status;
|
||||
int id;
|
||||
json_t * result;
|
||||
};
|
||||
|
||||
extern bool wf_impl_jsonrpc_is_response(
|
||||
json_t * message);
|
||||
|
||||
extern void wf_impl_jsonrpc_response_init(
|
||||
struct wf_impl_jsonrpc_response * response,
|
||||
json_t * message);
|
||||
|
||||
extern void wf_impl_jsonrpc_response_cleanup(
|
||||
struct wf_impl_jsonrpc_response * response);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -1,95 +0,0 @@
|
||||
#include "webfuse/adapter/impl/jsonrpc/server.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/method.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/request.h"
|
||||
#include "webfuse/core/util.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void wf_impl_jsonrpc_server_init(
|
||||
struct wf_impl_jsonrpc_server * server)
|
||||
{
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_server_cleanup(
|
||||
struct wf_impl_jsonrpc_server * server)
|
||||
{
|
||||
struct wf_impl_jsonrpc_method * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
struct wf_impl_jsonrpc_method * next = current->next;
|
||||
wf_impl_jsonrpc_method_dispose(current);
|
||||
current = next;
|
||||
}
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_server_add(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_impl_jsonrpc_method * method = wf_impl_jsonrpc_method_create(method_name, invoke, user_data);
|
||||
method->next = server->methods;
|
||||
server->methods = method;
|
||||
}
|
||||
|
||||
static void wf_impl_jsonrpc_server_invalid_method_invoke(
|
||||
struct wf_impl_jsonrpc_request * request,
|
||||
char const * WF_UNUSED_PARAM(method_name),
|
||||
json_t * WF_UNUSED_PARAM(params),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
wf_impl_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED);
|
||||
}
|
||||
|
||||
static struct wf_impl_jsonrpc_method const wf_impl_jsonrpc_server_invalid_method =
|
||||
{
|
||||
.next = NULL,
|
||||
.name = "<invalid>",
|
||||
.invoke = &wf_impl_jsonrpc_server_invalid_method_invoke,
|
||||
.user_data = NULL
|
||||
};
|
||||
|
||||
static struct wf_impl_jsonrpc_method const * wf_impl_jsonrpc_server_get_method(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
char const * method_name)
|
||||
{
|
||||
struct wf_impl_jsonrpc_method const * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
if (0 == strcmp(method_name, current->name))
|
||||
{
|
||||
return current;
|
||||
}
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
return &wf_impl_jsonrpc_server_invalid_method;
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_server_process(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
json_t * request_data,
|
||||
wf_impl_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
json_t * method_holder = json_object_get(request_data, "method");
|
||||
json_t * params = json_object_get(request_data, "params");
|
||||
json_t * id_holder = json_object_get(request_data, "id");
|
||||
|
||||
if (json_is_string(method_holder) &&
|
||||
(json_is_array(params) || (json_is_object(params))) &&
|
||||
json_is_integer(id_holder))
|
||||
{
|
||||
char const * method_name = json_string_value(method_holder);
|
||||
int id = json_integer_value(id_holder);
|
||||
struct wf_impl_jsonrpc_request * request = wf_impl_jsonrpc_request_create(id, send, user_data);
|
||||
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_get_method(server, method_name);
|
||||
|
||||
method->invoke(request, method_name, params, method->user_data);
|
||||
}
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_JSONRPC_SERVER_H
|
||||
#define WF_ADAPTER_IMPL_JSONRPC_SERVER_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse/core/status.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/send_fn.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/method.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_impl_jsonrpc_server
|
||||
{
|
||||
struct wf_impl_jsonrpc_method * methods;
|
||||
};
|
||||
|
||||
extern void wf_impl_jsonrpc_server_init(
|
||||
struct wf_impl_jsonrpc_server * server);
|
||||
|
||||
extern void wf_impl_jsonrpc_server_cleanup(
|
||||
struct wf_impl_jsonrpc_server * server);
|
||||
|
||||
extern void wf_impl_jsonrpc_server_add(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_impl_jsonrpc_server_process(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
json_t * request,
|
||||
wf_impl_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,14 +0,0 @@
|
||||
#include "webfuse/adapter/impl/jsonrpc/util.h"
|
||||
|
||||
int wf_impl_json_get_int(json_t const * object, char const * key, int default_value)
|
||||
{
|
||||
int result = default_value;
|
||||
|
||||
json_t * holder = json_object_get(object, key);
|
||||
if ((NULL != holder) && (json_is_integer(holder)))
|
||||
{
|
||||
result = json_integer_value(holder);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_JSON_UTIL_H
|
||||
#define WF_ADAPTER_IMPL_JSON_UTIL_H
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern int wf_impl_json_get_int(json_t const * object, char const * key, int default_value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,84 +0,0 @@
|
||||
#include "webfuse/adapter/impl/time/timeout_manager_intern.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include "webfuse/adapter/impl/time/timer_intern.h"
|
||||
#include "webfuse/adapter/impl/time/timepoint.h"
|
||||
|
||||
void wf_impl_timeout_manager_init(
|
||||
struct wf_impl_timeout_manager * manager)
|
||||
{
|
||||
manager->timers = NULL;
|
||||
}
|
||||
|
||||
void wf_impl_timeout_manager_cleanup(
|
||||
struct wf_impl_timeout_manager * manager)
|
||||
{
|
||||
struct wf_impl_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_impl_timer * next = timer->next;
|
||||
|
||||
wf_impl_timer_trigger(timer);
|
||||
|
||||
timer = next;
|
||||
}
|
||||
|
||||
manager->timers = NULL;
|
||||
|
||||
}
|
||||
|
||||
void wf_impl_timeout_manager_check(
|
||||
struct wf_impl_timeout_manager * manager)
|
||||
{
|
||||
struct wf_impl_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_impl_timer * next = timer->next;
|
||||
|
||||
if (wf_impl_timer_is_timeout(timer))
|
||||
{
|
||||
wf_impl_timeout_manager_removetimer(manager, timer);
|
||||
wf_impl_timer_trigger(timer);
|
||||
}
|
||||
|
||||
timer = next;
|
||||
}
|
||||
}
|
||||
|
||||
void wf_impl_timeout_manager_addtimer(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
if (NULL != manager->timers)
|
||||
{
|
||||
manager->timers->prev = timer;
|
||||
}
|
||||
|
||||
timer->next = manager->timers;
|
||||
timer->prev = NULL;
|
||||
manager->timers = timer;
|
||||
}
|
||||
|
||||
void wf_impl_timeout_manager_removetimer(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
struct wf_impl_timer * prev = timer->prev;
|
||||
struct wf_impl_timer * next = timer->next;
|
||||
|
||||
if (NULL != prev)
|
||||
{
|
||||
prev->next = next;
|
||||
}
|
||||
|
||||
if (NULL != next)
|
||||
{
|
||||
next->prev = prev;
|
||||
}
|
||||
|
||||
if (manager->timers == timer)
|
||||
{
|
||||
manager->timers = next;
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_impl_timer;
|
||||
struct wf_impl_timeout_manager
|
||||
{
|
||||
struct wf_impl_timer * timers;
|
||||
};
|
||||
|
||||
extern void wf_impl_timeout_manager_init(
|
||||
struct wf_impl_timeout_manager * manager);
|
||||
|
||||
extern void wf_impl_timeout_manager_cleanup(
|
||||
struct wf_impl_timeout_manager * manager);
|
||||
|
||||
extern void wf_impl_timeout_manager_check(
|
||||
struct wf_impl_timeout_manager * manager);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,24 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_INTERN_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_INTERN_H
|
||||
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wf_impl_timeout_manager_addtimer(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
extern void wf_impl_timeout_manager_removetimer(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -1,31 +0,0 @@
|
||||
#include "webfuse/adapter/impl/time/timepoint.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define WF_MSEC_PER_SEC ((wf_impl_timepoint) 1000)
|
||||
#define WF_NSEC_PER_MSEC ((wf_impl_timepoint) 1000 * 1000)
|
||||
|
||||
wf_impl_timepoint wf_impl_timepoint_now(void)
|
||||
{
|
||||
struct timespec tp;
|
||||
clock_gettime(CLOCK_MONOTONIC, &tp);
|
||||
|
||||
wf_impl_timepoint const now = (tp.tv_sec * WF_MSEC_PER_SEC) + (tp.tv_nsec / WF_NSEC_PER_MSEC);
|
||||
return now;
|
||||
}
|
||||
|
||||
wf_impl_timepoint wf_impl_timepoint_in_msec(wf_impl_timediff value)
|
||||
{
|
||||
wf_impl_timepoint const now = wf_impl_timepoint_now();
|
||||
wf_impl_timepoint result = now + ((wf_impl_timepoint) value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wf_impl_timepoint_is_elapsed(wf_impl_timepoint tp)
|
||||
{
|
||||
wf_impl_timepoint const now = wf_impl_timepoint_now();
|
||||
wf_impl_timediff const diff = (wf_impl_timediff) (tp - now);
|
||||
|
||||
return (0 > diff);
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMEPOINT_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMEPOINT_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
#else
|
||||
#include <cinttypes>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef uint64_t wf_impl_timepoint;
|
||||
typedef int64_t wf_impl_timediff;
|
||||
|
||||
extern wf_impl_timepoint wf_impl_timepoint_now(void);
|
||||
|
||||
extern wf_impl_timepoint wf_impl_timepoint_in_msec(
|
||||
wf_impl_timediff value);
|
||||
|
||||
extern bool wf_impl_timepoint_is_elapsed(
|
||||
wf_impl_timepoint timepoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,65 +0,0 @@
|
||||
#include "webfuse/adapter/impl/time/timer_intern.h"
|
||||
#include "webfuse/adapter/impl/time/timeout_manager_intern.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
void wf_impl_timer_init(
|
||||
struct wf_impl_timer * timer,
|
||||
struct wf_impl_timeout_manager * manager)
|
||||
{
|
||||
timer->manager = manager;
|
||||
timer->timeout = 0;
|
||||
timer->timeout_handler = NULL;
|
||||
timer->user_data = NULL;
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
}
|
||||
|
||||
void wf_impl_timer_cleanup(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
memset(timer, 0, sizeof(struct wf_impl_timer));
|
||||
}
|
||||
|
||||
void wf_impl_timer_start(
|
||||
struct wf_impl_timer * timer,
|
||||
wf_impl_timepoint absolute_timeout,
|
||||
wf_impl_timer_timeout_fn * handler,
|
||||
void * user_data)
|
||||
{
|
||||
timer->timeout = absolute_timeout;
|
||||
timer->timeout_handler = handler;
|
||||
timer->user_data = user_data;
|
||||
|
||||
wf_impl_timeout_manager_addtimer(timer->manager, timer);
|
||||
}
|
||||
|
||||
void wf_impl_timer_cancel(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
wf_impl_timeout_manager_removetimer(timer->manager, timer);
|
||||
|
||||
timer->timeout = 0;
|
||||
timer->timeout_handler = NULL;
|
||||
timer->user_data = NULL;
|
||||
}
|
||||
|
||||
bool wf_impl_timer_is_timeout(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
return wf_impl_timepoint_is_elapsed(timer->timeout);
|
||||
}
|
||||
|
||||
|
||||
void wf_impl_timer_trigger(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
if (NULL != timer->timeout_handler)
|
||||
{
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
timer->timeout_handler(timer);
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
|
||||
#include "webfuse/adapter/impl/time/timepoint.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_impl_timer;
|
||||
struct wf_impl_timeout_manager;
|
||||
|
||||
typedef void wf_impl_timer_timeout_fn(struct wf_impl_timer * timer);
|
||||
|
||||
struct wf_impl_timer
|
||||
{
|
||||
struct wf_impl_timeout_manager * manager;
|
||||
wf_impl_timepoint timeout;
|
||||
wf_impl_timer_timeout_fn * timeout_handler;
|
||||
void * user_data;
|
||||
struct wf_impl_timer * next;
|
||||
struct wf_impl_timer * prev;
|
||||
};
|
||||
|
||||
extern void wf_impl_timer_init(
|
||||
struct wf_impl_timer * timer,
|
||||
struct wf_impl_timeout_manager * manager);
|
||||
|
||||
extern void wf_impl_timer_cleanup(
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
extern void wf_impl_timer_start(
|
||||
struct wf_impl_timer * timer,
|
||||
wf_impl_timepoint absolute_timeout,
|
||||
wf_impl_timer_timeout_fn * handler,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_impl_timer_cancel(
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -1,25 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMER_INTERN_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMER_INTERN_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "webfuse/adapter/impl/time/timer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern bool wf_impl_timer_is_timeout(
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
extern void wf_impl_timer_trigger(
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,48 @@
|
||||
#include "webfuse/core/json_util.h"
|
||||
#include "wf/jsonrpc/status.h"
|
||||
|
||||
int wf_impl_json_get_int(json_t const * object, char const * key, int default_value)
|
||||
{
|
||||
int result = default_value;
|
||||
|
||||
json_t * holder = json_object_get(object, key);
|
||||
if ((NULL != holder) && (json_is_integer(holder)))
|
||||
{
|
||||
result = json_integer_value(holder);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static wf_status wf_impl_jsonrc_code_to_status(int code)
|
||||
{
|
||||
switch (code)
|
||||
{
|
||||
case WF_JSONRPC_GOOD:
|
||||
return WF_GOOD;
|
||||
case WF_JSONRPC_BAD:
|
||||
return WF_BAD;
|
||||
case WF_JSONRPC_BAD_BUSY:
|
||||
return WF_BAD_BUSY;
|
||||
case WF_JSONRPC_BAD_TIMEOUT:
|
||||
return WF_BAD_TIMEOUT;
|
||||
case WF_JSONRPC_BAD_FORMAT:
|
||||
return WF_BAD_FORMAT;
|
||||
default:
|
||||
return (wf_status) code;
|
||||
}
|
||||
}
|
||||
|
||||
wf_status
|
||||
wf_impl_jsonrpc_get_status(
|
||||
json_t const * error)
|
||||
{
|
||||
wf_status status = WF_GOOD;
|
||||
if (NULL != error)
|
||||
{
|
||||
int code = wf_impl_json_get_int(error, "code", WF_BAD_FORMAT);
|
||||
status = wf_impl_jsonrc_code_to_status(code);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
#ifndef WF_JSON_UTIL_H
|
||||
#define WF_JSON_UTIL_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse/core/status.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern int
|
||||
wf_impl_json_get_int(
|
||||
json_t const * object,
|
||||
char const * key,
|
||||
int default_value);
|
||||
|
||||
extern wf_status
|
||||
wf_impl_jsonrpc_get_status(
|
||||
json_t const * error);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,46 @@
|
||||
#include "webfuse/provider/impl/credentials.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void wfp_impl_credentials_init(
|
||||
struct wfp_credentials * credentials)
|
||||
{
|
||||
credentials->type = NULL;
|
||||
credentials->contents = json_object();
|
||||
}
|
||||
|
||||
void wfp_impl_credentials_cleanup(
|
||||
struct wfp_credentials * credentials)
|
||||
{
|
||||
free(credentials->type);
|
||||
json_decref(credentials->contents);
|
||||
}
|
||||
|
||||
void wfp_impl_credentials_set_type(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * type)
|
||||
{
|
||||
free(credentials->type);
|
||||
credentials->type = strdup(type);
|
||||
}
|
||||
|
||||
void wfp_impl_credentials_add(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * key,
|
||||
char const * value)
|
||||
{
|
||||
json_object_set_new(credentials->contents, key, json_string(value));
|
||||
}
|
||||
|
||||
char const * wfp_impl_credentials_get_type(
|
||||
struct wfp_credentials * credentials)
|
||||
{
|
||||
return credentials->type;
|
||||
}
|
||||
|
||||
json_t * wfp_impl_credentials_get(
|
||||
struct wfp_credentials * credentials)
|
||||
{
|
||||
return credentials->contents;
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
#ifndef WF_PROVIDER_IMPL_CREDENTIALS_H
|
||||
#define WF_PROVIDER_IMPL_CREDENTIALS_H
|
||||
|
||||
#include "webfuse/provider/credentials.h"
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_credentials
|
||||
{
|
||||
char * type;
|
||||
json_t * contents;
|
||||
};
|
||||
|
||||
extern void wfp_impl_credentials_init(
|
||||
struct wfp_credentials * credentials);
|
||||
|
||||
extern void wfp_impl_credentials_cleanup(
|
||||
struct wfp_credentials * credentials);
|
||||
|
||||
extern void wfp_impl_credentials_set_type(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * type);
|
||||
|
||||
extern void wfp_impl_credentials_add(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * key,
|
||||
char const * value);
|
||||
|
||||
extern char const * wfp_impl_credentials_get_type(
|
||||
struct wfp_credentials * credentials);
|
||||
|
||||
extern json_t * wfp_impl_credentials_get(
|
||||
struct wfp_credentials * credentials);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,14 @@
|
||||
#ifndef WF_JSONRPC_H
|
||||
#define WF_JSONRPC_H
|
||||
|
||||
#include <wf/jsonrpc/api.h>
|
||||
#include <wf/jsonrpc/method_invoke_fn.h>
|
||||
#include <wf/jsonrpc/proxy.h>
|
||||
#include <wf/jsonrpc/proxy_finished_fn.h>
|
||||
#include <wf/jsonrpc/request.h>
|
||||
#include <wf/jsonrpc/response.h>
|
||||
#include <wf/jsonrpc/send_fn.h>
|
||||
#include <wf/jsonrpc/server.h>
|
||||
#include <wf/jsonrpc/status.h>
|
||||
|
||||
#endif
|
@ -0,0 +1,8 @@
|
||||
#ifndef WF_JSONRPC_API_H
|
||||
#define WF_JSONRPC_API_H
|
||||
|
||||
#ifndef WF_JSONRPC_API
|
||||
#define WF_JSONRPC_API
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,26 @@
|
||||
#ifndef WF_JSONRPC_METHOD_INVOKE_FN_H
|
||||
#define WF_JSONRPC_METHOD_INVOKE_FN_H
|
||||
|
||||
#include <wf/jsonrpc/api.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_request;
|
||||
|
||||
typedef void wf_jsonrpc_method_invoke_fn(
|
||||
struct wf_jsonrpc_request * request,
|
||||
char const * method_name,
|
||||
json_t * params,
|
||||
void * user_data);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,75 @@
|
||||
#ifndef WF_JSONRPC_PROXY_H
|
||||
#define WF_JSONRPC_PROXY_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
#include <wf/jsonrpc/send_fn.h>
|
||||
#include <wf/jsonrpc/proxy_finished_fn.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_proxy;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_JSONRPC_API struct wf_jsonrpc_proxy *
|
||||
wf_jsonrpc_proxy_create(
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_proxy_dispose(
|
||||
struct wf_jsonrpc_proxy * proxy);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// \brief Invokes a method.
|
||||
///
|
||||
/// Creates a method an sends it using the send function.
|
||||
/// Proxy keeps track of method invokation. If no response is returned within
|
||||
/// timeout, an error is propagated.
|
||||
///
|
||||
/// \param proxy pointer to proxy instance
|
||||
/// \param finished function which is called exactly once, either on success or
|
||||
/// on failure.
|
||||
/// \param method_name name of the method to invoke
|
||||
/// \param param_info types of the param (s = string, i = integer, j = json)
|
||||
/// \param ... params
|
||||
//------------------------------------------------------------------------------
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_proxy_invoke(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_proxy_notify(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_proxy_onresult(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
json_t * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,20 @@
|
||||
#ifndef WF_JSONRPC_PROXY_FINISHED_FN_H
|
||||
#define WF_JSONRPC_PROXY_FINISHED_FN_H
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef void wf_jsonrpc_proxy_finished_fn(
|
||||
void * user_data,
|
||||
json_t const * result,
|
||||
json_t const * error);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,54 @@
|
||||
#ifndef WF_JSONRPC_REQUEST_H
|
||||
#define WF_JSONRPC_REQUEST_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
#include "wf/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_request;
|
||||
|
||||
extern WF_JSONRPC_API bool wf_jsonrpc_is_request(
|
||||
json_t * message);
|
||||
|
||||
extern WF_JSONRPC_API struct wf_jsonrpc_request *
|
||||
wf_jsonrpc_request_create(
|
||||
int id,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_request_dispose(
|
||||
struct wf_jsonrpc_request * request);
|
||||
|
||||
extern WF_JSONRPC_API void * wf_jsonrpc_request_get_userdata(
|
||||
struct wf_jsonrpc_request * request);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_respond(
|
||||
struct wf_jsonrpc_request * request,
|
||||
json_t * result);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_respond_error(
|
||||
struct wf_jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,23 @@
|
||||
#ifndef WF_JSONRPC_RESPONSE_H
|
||||
#define WF_JSONRPC_RESPONSE_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern WF_JSONRPC_API bool wf_jsonrpc_is_response(
|
||||
json_t * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,18 +1,19 @@
|
||||
#ifndef WF_ADAPTER_IMPL_JSONRPC_SEND_FN_H
|
||||
#define WF_ADAPTER_IMPL_JSONRPC_SEND_FN_H
|
||||
#ifndef WF_JSONRPC_SEND_FN_H
|
||||
#define WF_JSONRPC_SEND_FN_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef bool wf_impl_jsonrpc_send_fn(
|
||||
typedef bool wf_jsonrpc_send_fn(
|
||||
json_t * request,
|
||||
void * user_data);
|
||||
|
@ -0,0 +1,46 @@
|
||||
#ifndef WF_JSONRPC_SERVER_H
|
||||
#define WF_JSONRPC_SERVER_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
#include <wf/jsonrpc/send_fn.h>
|
||||
#include <wf/jsonrpc/method_invoke_fn.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_server;
|
||||
|
||||
extern WF_JSONRPC_API struct wf_jsonrpc_server *
|
||||
wf_jsonrpc_server_create(void);
|
||||
|
||||
extern WF_JSONRPC_API void
|
||||
wf_jsonrpc_server_dispose(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_server_add(
|
||||
struct wf_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_server_process(
|
||||
struct wf_jsonrpc_server * server,
|
||||
json_t * request,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,11 @@
|
||||
#ifndef WF_JSONRPC_STATUS_H
|
||||
#define WF_JSONRPC_STATUS_H
|
||||
|
||||
#define WF_JSONRPC_GOOD 0
|
||||
#define WF_JSONRPC_BAD -1
|
||||
#define WF_JSONRPC_BAD_NOTIMPLEMENTED -2
|
||||
#define WF_JSONRPC_BAD_TIMEOUT -3
|
||||
#define WF_JSONRPC_BAD_BUSY -4
|
||||
#define WF_JSONRPC_BAD_FORMAT -5
|
||||
|
||||
#endif
|
@ -0,0 +1,144 @@
|
||||
#include "wf/jsonrpc.h"
|
||||
|
||||
#include "wf/jsonrpc/impl/proxy.h"
|
||||
#include "wf/jsonrpc/impl/request.h"
|
||||
#include "wf/jsonrpc/impl/response.h"
|
||||
#include "wf/jsonrpc/impl/server.h"
|
||||
|
||||
// proxy
|
||||
|
||||
struct wf_jsonrpc_proxy *
|
||||
wf_jsonrpc_proxy_create(
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
return wf_jsonrpc_impl_proxy_create(manager, timeout, send, user_data);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_proxy_dispose(
|
||||
struct wf_jsonrpc_proxy * proxy)
|
||||
{
|
||||
wf_jsonrpc_impl_proxy_dispose(proxy);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_proxy_invoke(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
wf_jsonrpc_impl_proxy_invoke(proxy, finished, user_data, method_name, param_info, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_proxy_notify(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
wf_jsonrpc_impl_proxy_notify(proxy, method_name, param_info, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_proxy_onresult(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
json_t * message)
|
||||
{
|
||||
wf_jsonrpc_impl_proxy_onresult(proxy, message);
|
||||
}
|
||||
|
||||
|
||||
// request
|
||||
|
||||
bool wf_jsonrpc_is_request(
|
||||
json_t * message)
|
||||
{
|
||||
return wf_jsonrpc_impl_is_request(message);
|
||||
}
|
||||
|
||||
struct wf_jsonrpc_request * wf_jsonrpc_request_create(
|
||||
int id,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
return wf_jsonrpc_impl_request_create(id, send, user_data);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_request_dispose(
|
||||
struct wf_jsonrpc_request * request)
|
||||
{
|
||||
wf_jsonrpc_impl_request_dispose(request);
|
||||
}
|
||||
|
||||
void * wf_jsonrpc_request_get_userdata(
|
||||
struct wf_jsonrpc_request * request)
|
||||
{
|
||||
return wf_jsonrpc_impl_request_get_userdata(request);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_respond(
|
||||
struct wf_jsonrpc_request * request,
|
||||
json_t * result)
|
||||
{
|
||||
wf_jsonrpc_impl_respond(request, result);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_respond_error(
|
||||
struct wf_jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
wf_jsonrpc_impl_respond_error(request, code, message);
|
||||
}
|
||||
|
||||
// response
|
||||
|
||||
bool wf_jsonrpc_is_response(
|
||||
json_t * message)
|
||||
{
|
||||
return wf_jsonrpc_impl_is_response(message);
|
||||
}
|
||||
|
||||
// server
|
||||
|
||||
struct wf_jsonrpc_server *
|
||||
wf_jsonrpc_server_create(void)
|
||||
{
|
||||
return wf_jsonrpc_impl_server_create();
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_server_dispose(
|
||||
struct wf_jsonrpc_server * server)
|
||||
{
|
||||
wf_jsonrpc_impl_server_dispose(server);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_server_add(
|
||||
struct wf_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
wf_jsonrpc_impl_server_add(server, method_name, invoke, user_data);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_server_process(
|
||||
struct wf_jsonrpc_server * server,
|
||||
json_t * request,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
wf_jsonrpc_impl_server_process(server, request, send, user_data);
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
#include "wf/jsonrpc/impl/error.h"
|
||||
|
||||
json_t *
|
||||
wf_jsonrpc_impl_error(
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * error = json_object();
|
||||
json_object_set_new(error, "code", json_integer(code));
|
||||
json_object_set_new(error, "message", json_string(message));
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_impl_propate_error(
|
||||
wf_jsonrpc_proxy_finished_fn * finised,
|
||||
void * user_data,
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * error = wf_jsonrpc_impl_error(code, message);
|
||||
finised(user_data, NULL, error);
|
||||
|
||||
json_decref(error);
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
#ifndef WF_JSONRPC_IMPL_ERROR_H
|
||||
#define WF_JSONRPC_IMPL_ERROR_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "wf/jsonrpc/proxy_finished_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern json_t *
|
||||
wf_jsonrpc_impl_error(
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_propate_error(
|
||||
wf_jsonrpc_proxy_finished_fn * finised,
|
||||
void * user_data,
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,34 @@
|
||||
#ifndef WF_JSONRPC_IMPL_METHOD_H
|
||||
#define WF_JSONRPC_IMPL_METHOD_H
|
||||
|
||||
#include "wf/jsonrpc/method_invoke_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_method
|
||||
{
|
||||
struct wf_jsonrpc_method * next;
|
||||
char * name;
|
||||
wf_jsonrpc_method_invoke_fn * invoke;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
extern struct wf_jsonrpc_method *
|
||||
wf_jsonrpc_impl_method_create(
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_method_dispose(
|
||||
struct wf_jsonrpc_method * method);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,220 @@
|
||||
#include "wf/jsonrpc/impl/proxy.h"
|
||||
#include "wf/jsonrpc/impl/response.h"
|
||||
#include "wf/jsonrpc/impl/error.h"
|
||||
#include "wf/jsonrpc/status.h"
|
||||
|
||||
#include <wf/timer/timer.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_jsonrpc_proxy *
|
||||
wf_jsonrpc_impl_proxy_create(
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_jsonrpc_proxy * proxy = malloc(sizeof(struct wf_jsonrpc_proxy));
|
||||
if (NULL != proxy)
|
||||
{
|
||||
wf_jsonrpc_impl_proxy_init(proxy, manager, timeout, send, user_data);
|
||||
}
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_proxy_dispose(
|
||||
struct wf_jsonrpc_proxy * proxy)
|
||||
{
|
||||
wf_jsonrpc_impl_proxy_cleanup(proxy);
|
||||
free(proxy);
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_impl_proxy_on_timeout(
|
||||
struct wf_timer * timer, void * proxy_ptr)
|
||||
{
|
||||
struct wf_jsonrpc_proxy * proxy = proxy_ptr;
|
||||
|
||||
if (proxy->request.is_pending)
|
||||
{
|
||||
wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
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;
|
||||
wf_timer_cancel(timer);
|
||||
|
||||
wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD_TIMEOUT, "Timeout");
|
||||
}
|
||||
}
|
||||
|
||||
static json_t * wf_jsonrpc_impl_request_create(
|
||||
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;
|
||||
case 'j':
|
||||
{
|
||||
json_t * const value = va_arg(args, json_t *);
|
||||
json_array_append_new(params, value);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
|
||||
json_decref(params);
|
||||
json_decref(request);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
json_object_set_new(request, "params", params);
|
||||
if (0 != id)
|
||||
{
|
||||
json_object_set_new(request, "id", json_integer(id));
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_proxy_init(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
struct wf_timer_manager * timeout_manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
proxy->send = send;
|
||||
proxy->timeout = timeout;
|
||||
proxy->user_data = user_data;
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.timer = wf_timer_create(timeout_manager,
|
||||
&wf_jsonrpc_impl_proxy_on_timeout, proxy);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_proxy_cleanup(
|
||||
struct wf_jsonrpc_proxy * proxy)
|
||||
{
|
||||
if (proxy->request.is_pending)
|
||||
{
|
||||
void * user_data = proxy->request.user_data;
|
||||
wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.finished = NULL;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.id = 0;
|
||||
wf_timer_cancel(proxy->request.timer);
|
||||
|
||||
wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD, "Bad: cancelled pending request during shutdown");
|
||||
}
|
||||
|
||||
wf_timer_dispose(proxy->request.timer);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_proxy_invoke(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args
|
||||
)
|
||||
{
|
||||
if (!proxy->request.is_pending)
|
||||
{
|
||||
proxy->request.is_pending = true;
|
||||
proxy->request.finished = finished;
|
||||
proxy->request.user_data = user_data;
|
||||
proxy->request.id = 42;
|
||||
wf_timer_start(proxy->request.timer, proxy->timeout);
|
||||
|
||||
json_t * request = wf_jsonrpc_impl_request_create(method_name, proxy->request.id, param_info, args);
|
||||
|
||||
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;
|
||||
wf_timer_cancel(proxy->request.timer);
|
||||
|
||||
wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD, "Bad: requenst is not sent");
|
||||
}
|
||||
|
||||
if (NULL != request)
|
||||
{
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD_BUSY, "Busy");
|
||||
}
|
||||
}
|
||||
|
||||
extern void wf_jsonrpc_impl_proxy_notify(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args
|
||||
)
|
||||
{
|
||||
json_t * request = wf_jsonrpc_impl_request_create(method_name, 0, param_info, args);
|
||||
if (NULL != request)
|
||||
{
|
||||
proxy->send(request, proxy->user_data);
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void wf_jsonrpc_impl_proxy_onresult(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
json_t * message)
|
||||
{
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_impl_response_init(&response, message);
|
||||
|
||||
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
|
||||
{
|
||||
wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
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;
|
||||
wf_timer_cancel(proxy->request.timer);
|
||||
|
||||
finished(user_data, response.result, response.error);
|
||||
}
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
}
|
||||
|
@ -0,0 +1,84 @@
|
||||
#ifndef WF_JSONRPC_IMPL_PROXY_H
|
||||
#define WF_JSONRPC_IMPL_PROXY_H
|
||||
|
||||
#include "wf/jsonrpc/proxy_finished_fn.h"
|
||||
#include "wf/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
struct wf_timer;
|
||||
|
||||
struct wf_jsonrpc_request
|
||||
{
|
||||
bool is_pending;
|
||||
wf_jsonrpc_proxy_finished_fn * finished;
|
||||
void * user_data;
|
||||
int id;
|
||||
struct wf_timer * timer;
|
||||
};
|
||||
|
||||
struct wf_jsonrpc_proxy
|
||||
{
|
||||
struct wf_jsonrpc_request request;
|
||||
int timeout;
|
||||
wf_jsonrpc_send_fn * send;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_proxy_init(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_proxy_cleanup(
|
||||
struct wf_jsonrpc_proxy * proxy);
|
||||
|
||||
extern struct wf_jsonrpc_proxy *
|
||||
wf_jsonrpc_impl_proxy_create(
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_proxy_dispose(
|
||||
struct wf_jsonrpc_proxy * proxy);
|
||||
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_proxy_invoke(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args
|
||||
);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_proxy_notify(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args
|
||||
);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_proxy_onresult(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
json_t * message);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,53 @@
|
||||
#ifndef WF_JSONRPC_IMPL_REQUEST_H
|
||||
#define WF_JSONRPC_IMPL_REQUEST_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
#include "wf/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_request;
|
||||
|
||||
extern bool wf_jsonrpc_impl_is_request(
|
||||
json_t * message);
|
||||
|
||||
extern struct wf_jsonrpc_request * wf_jsonrpc_impl_request_create(
|
||||
int id,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_jsonrpc_impl_request_dispose(
|
||||
struct wf_jsonrpc_request * request);
|
||||
|
||||
extern void * wf_jsonrpc_impl_request_get_userdata(
|
||||
struct wf_jsonrpc_request * request);
|
||||
|
||||
extern void wf_jsonrpc_impl_respond(
|
||||
struct wf_jsonrpc_request * request,
|
||||
json_t * result);
|
||||
|
||||
extern void wf_jsonrpc_impl_respond_error(
|
||||
struct wf_jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,40 @@
|
||||
#ifndef WF_JSONRPC_IMPL_RESPONSE_H
|
||||
#define WF_JSONRPC_IMPL_RESPONSE_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_response
|
||||
{
|
||||
json_t * result;
|
||||
json_t * error;
|
||||
int id;
|
||||
};
|
||||
|
||||
extern bool wf_jsonrpc_impl_is_response(
|
||||
json_t * message);
|
||||
|
||||
extern void wf_jsonrpc_impl_response_init(
|
||||
struct wf_jsonrpc_response * response,
|
||||
json_t * message);
|
||||
|
||||
extern void wf_jsonrpc_impl_response_cleanup(
|
||||
struct wf_jsonrpc_response * response);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,132 @@
|
||||
#include "wf/jsonrpc/impl/server.h"
|
||||
#include "wf/jsonrpc/impl/method.h"
|
||||
#include "wf/jsonrpc/impl/request.h"
|
||||
#include "wf/jsonrpc/impl/unused_param.h"
|
||||
#include "wf/jsonrpc/status.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_jsonrpc_server
|
||||
{
|
||||
struct wf_jsonrpc_method * methods;
|
||||
};
|
||||
|
||||
static void
|
||||
wf_jsonrpc_impl_server_init(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
static void
|
||||
wf_jsonrpc_impl_server_cleanup(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
struct wf_jsonrpc_server *
|
||||
wf_jsonrpc_impl_server_create(void)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = malloc(sizeof(struct wf_jsonrpc_server));
|
||||
if (NULL != server)
|
||||
{
|
||||
wf_jsonrpc_impl_server_init(server);
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_impl_server_dispose(
|
||||
struct wf_jsonrpc_server * server)
|
||||
{
|
||||
wf_jsonrpc_impl_server_cleanup(server);
|
||||
free(server);
|
||||
}
|
||||
|
||||
|
||||
static void wf_jsonrpc_impl_server_init(
|
||||
struct wf_jsonrpc_server * server)
|
||||
{
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_impl_server_cleanup(
|
||||
struct wf_jsonrpc_server * server)
|
||||
{
|
||||
struct wf_jsonrpc_method * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
struct wf_jsonrpc_method * next = current->next;
|
||||
wf_jsonrpc_impl_method_dispose(current);
|
||||
current = next;
|
||||
}
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_server_add(
|
||||
struct wf_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_jsonrpc_method * method = wf_jsonrpc_impl_method_create(method_name, invoke, user_data);
|
||||
method->next = server->methods;
|
||||
server->methods = method;
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_impl_server_invalid_method_invoke(
|
||||
struct wf_jsonrpc_request * request,
|
||||
char const * WF_JSONRPC_UNUSED_PARAM(method_name),
|
||||
json_t * WF_JSONRPC_UNUSED_PARAM(params),
|
||||
void * WF_JSONRPC_UNUSED_PARAM(user_data))
|
||||
{
|
||||
wf_jsonrpc_impl_respond_error(request, WF_JSONRPC_BAD_NOTIMPLEMENTED, "not implemented");
|
||||
}
|
||||
|
||||
static struct wf_jsonrpc_method const wf_jsonrpc_impl_server_invalid_method =
|
||||
{
|
||||
.next = NULL,
|
||||
.name = "<invalid>",
|
||||
.invoke = &wf_jsonrpc_impl_server_invalid_method_invoke,
|
||||
.user_data = NULL
|
||||
};
|
||||
|
||||
static struct wf_jsonrpc_method const *
|
||||
wf_jsonrpc_impl_server_get_method(
|
||||
struct wf_jsonrpc_server * server,
|
||||
char const * method_name)
|
||||
{
|
||||
struct wf_jsonrpc_method const * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
if (0 == strcmp(method_name, current->name))
|
||||
{
|
||||
return current;
|
||||
}
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
return &wf_jsonrpc_impl_server_invalid_method;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_server_process(
|
||||
struct wf_jsonrpc_server * server,
|
||||
json_t * request_data,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
json_t * method_holder = json_object_get(request_data, "method");
|
||||
json_t * params = json_object_get(request_data, "params");
|
||||
json_t * id_holder = json_object_get(request_data, "id");
|
||||
|
||||
if (json_is_string(method_holder) &&
|
||||
(json_is_array(params) || (json_is_object(params))) &&
|
||||
json_is_integer(id_holder))
|
||||
{
|
||||
char const * method_name = json_string_value(method_holder);
|
||||
int id = json_integer_value(id_holder);
|
||||
struct wf_jsonrpc_request * request = wf_jsonrpc_impl_request_create(id, send, user_data);
|
||||
struct wf_jsonrpc_method const * method = wf_jsonrpc_impl_server_get_method(server, method_name);
|
||||
|
||||
method->invoke(request, method_name, params, method->user_data);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
#ifndef WF_JSONRPC_IMPL_SERVER_H
|
||||
#define WF_JSONRPC_IMPL_SERVER_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "wf/jsonrpc/method_invoke_fn.h"
|
||||
#include "wf/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_server;
|
||||
|
||||
extern struct wf_jsonrpc_server *
|
||||
wf_jsonrpc_impl_server_create(void);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_server_dispose(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_server_add(
|
||||
struct wf_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_server_process(
|
||||
struct wf_jsonrpc_server * server,
|
||||
json_t * request,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,10 @@
|
||||
#ifndef WF_JSONRPC_UTIL_H
|
||||
#define WF_JSONRPC_UTIL_H
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define WF_JSONRPC_UNUSED_PARAM(param) param __attribute__((unused))
|
||||
#else
|
||||
#define WF_JSONRPC_UNUSED_PARAM(param)
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,112 +1,112 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse/adapter/impl/jsonrpc/request.h"
|
||||
#include "wf/jsonrpc/request.h"
|
||||
|
||||
TEST(jsonrpc_is_request, request_with_object_params)
|
||||
TEST(wf_jsonrpc_is_request, request_with_object_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_impl_jsonrpc_is_request(request));
|
||||
ASSERT_TRUE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, request_with_array_params)
|
||||
TEST(wf_jsonrpc_is_request, request_with_array_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_impl_jsonrpc_is_request(request));
|
||||
ASSERT_TRUE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, null_request)
|
||||
TEST(wf_jsonrpc_is_request, null_request)
|
||||
{
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_request(nullptr));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(nullptr));
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, invalid_request)
|
||||
TEST(wf_jsonrpc_is_request, invalid_request)
|
||||
{
|
||||
json_t * request = json_array();
|
||||
json_array_append_new(request, json_string("method"));
|
||||
json_array_append_new(request, json_object());
|
||||
json_array_append_new(request, json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, invalid_request_without_id)
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_without_id)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, invalid_request_due_to_invalid_id)
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_id)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_string("42"));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, invalid_request_without_method)
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_without_method)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, invalid_request_due_to_invalid_method)
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_method)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_integer(42));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, invalid_request_without_params)
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_without_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, invalid_request_due_to_invalid_params)
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "methdo", json_string("method"));
|
||||
json_object_set_new(request, "params", json_string("params"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
@ -1,94 +1,94 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse/adapter/impl/jsonrpc/response.h"
|
||||
#include "wf/jsonrpc/response.h"
|
||||
|
||||
TEST(jsonrpc_is_response, valid_result)
|
||||
TEST(wf_jsonrpc_is_response, valid_result)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_impl_jsonrpc_is_response(message));
|
||||
ASSERT_TRUE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_response, valid_result_string)
|
||||
TEST(wf_jsonrpc_is_response, valid_result_string)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_string("also valid"));
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_impl_jsonrpc_is_response(message));
|
||||
ASSERT_TRUE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_response, valid_error)
|
||||
TEST(wf_jsonrpc_is_response, valid_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "error", json_object());
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_impl_jsonrpc_is_response(message));
|
||||
ASSERT_TRUE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_response, invalid_null)
|
||||
TEST(wf_jsonrpc_is_response, invalid_null)
|
||||
{
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_response(nullptr));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(nullptr));
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_response, invalid_message)
|
||||
TEST(wf_jsonrpc_is_response, invalid_message)
|
||||
{
|
||||
json_t * message = json_array();
|
||||
json_array_append_new(message, json_object());
|
||||
json_array_append_new(message, json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_response, invalid_missing_id)
|
||||
TEST(wf_jsonrpc_is_response, invalid_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_response, invalid_id_wrong_type)
|
||||
TEST(wf_jsonrpc_is_response, invalid_id_wrong_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
json_object_set_new(message, "id", json_string("42"));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
|
||||
TEST(jsonrpc_is_response, invalid_missing_result_and_error)
|
||||
TEST(wf_jsonrpc_is_response, invalid_missing_result_and_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_response, invalid_error_wrong_type)
|
||||
TEST(wf_jsonrpc_is_response, invalid_error_wrong_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "error", json_array());
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "wf/jsonrpc/impl/response.h"
|
||||
#include "wf/jsonrpc/status.h"
|
||||
|
||||
TEST(wf_json_response, init_result)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
json_object_set_new(message, "id", json_integer(11));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_impl_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(nullptr, response.error);
|
||||
ASSERT_TRUE(json_is_integer(response.result));
|
||||
ASSERT_EQ(47, json_integer_value(response.result));
|
||||
ASSERT_EQ(11, response.id);
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "code", json_integer(42));
|
||||
json_object_set_new(err, "message", json_string("Don't Panic!"));
|
||||
json_object_set_new(message, "error", err);
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_impl_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_STREQ("Don't Panic!", json_string_value(json_object_get(response.error, "message")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_format_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(12));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_impl_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_JSONRPC_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(12, response.id);
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
@ -1,54 +1,58 @@
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse/adapter/impl/jsonrpc/response.h"
|
||||
#include "wf/jsonrpc/impl/response.h"
|
||||
|
||||
|
||||
static void response_parse_str(
|
||||
std::string const & buffer,
|
||||
struct wf_impl_jsonrpc_response * response)
|
||||
struct wf_jsonrpc_response * response)
|
||||
{
|
||||
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
|
||||
if (nullptr != message)
|
||||
{
|
||||
wf_impl_jsonrpc_response_init(response, message);
|
||||
wf_jsonrpc_impl_response_init(response, message);
|
||||
json_decref(message);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(response_parser, test)
|
||||
{
|
||||
struct wf_impl_jsonrpc_response response;
|
||||
struct wf_jsonrpc_response response;
|
||||
|
||||
// no object
|
||||
response_parse_str("[]", &response);
|
||||
ASSERT_NE(WF_GOOD, response.status);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
|
||||
// empty
|
||||
response_parse_str("{}", &response);
|
||||
ASSERT_NE(WF_GOOD, response.status);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
|
||||
// no data
|
||||
response_parse_str("{\"id\":42}", &response);
|
||||
ASSERT_NE(WF_GOOD, response.status);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
|
||||
// custom error code
|
||||
response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response);
|
||||
ASSERT_NE(WF_GOOD, response.status);
|
||||
ASSERT_EQ(42, response.status);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
|
||||
// valid response
|
||||
response_parse_str("{\"result\": true, \"id\": 42}", &response);
|
||||
ASSERT_EQ(WF_GOOD, response.status);
|
||||
ASSERT_EQ(nullptr, response.error);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_NE(nullptr, response.result);
|
||||
json_decref(response.result);
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#ifndef WF_TIMER_H
|
||||
#define WF_TIMER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
#include <wf/timer/on_timer_fn.h>
|
||||
#include <wf/timer/timer.h>
|
||||
#include <wf/timer/manager.h>
|
||||
|
||||
#endif
|
@ -0,0 +1,8 @@
|
||||
#ifndef WF_TIMER_API_H
|
||||
#define WF_TIMER_API_H
|
||||
|
||||
#ifndef WF_TIMER_API
|
||||
#define WF_TIMER_API
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,29 @@
|
||||
#ifndef WF_TIMER_MANAGER_H
|
||||
#define WF_TIMER_MANAGER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_TIMER_API struct wf_timer_manager *
|
||||
wf_timer_manager_create(void);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,19 @@
|
||||
#ifndef WF_TIMER_ON_TIMER_FN_H
|
||||
#define WF_TIMER_ON_TIMER_FN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
|
||||
typedef void wf_timer_on_timer_fn(
|
||||
struct wf_timer * timer,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,38 @@
|
||||
#ifndef WF_TIMER_TIMER_H
|
||||
#define WF_TIMER_TIMER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
#include <wf/timer/on_timer_fn.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_TIMER_API struct wf_timer *
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_cancel(
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,59 @@
|
||||
#include "wf/timer.h"
|
||||
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timer.h"
|
||||
|
||||
// manager
|
||||
|
||||
struct wf_timer_manager *
|
||||
wf_timer_manager_create(void)
|
||||
{
|
||||
return wf_timer_impl_manager_create();
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
wf_timer_impl_manager_dispose(manager);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
wf_timer_impl_manager_check(manager);
|
||||
}
|
||||
|
||||
// timer
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data)
|
||||
{
|
||||
return wf_timer_impl_create(manager, on_timer, user_data);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_dispose(timer);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms)
|
||||
{
|
||||
wf_timer_impl_start(timer, timeout_ms);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_cancel(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_cancel(timer);
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timer.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct wf_timer_manager
|
||||
{
|
||||
struct wf_timer * timers;
|
||||
};
|
||||
|
||||
struct wf_timer_manager *
|
||||
wf_timer_impl_manager_create(void)
|
||||
{
|
||||
struct wf_timer_manager * manager = malloc(sizeof(struct wf_timer_manager));
|
||||
if (NULL != manager)
|
||||
{
|
||||
manager->timers = NULL;
|
||||
}
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_impl_manager_dispose(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
wf_timer_impl_trigger(timer);
|
||||
timer = next;
|
||||
}
|
||||
|
||||
free(manager);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_impl_manager_check(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
if (wf_timer_impl_is_timeout(timer))
|
||||
{
|
||||
wf_timer_impl_manager_removetimer(manager, timer);
|
||||
wf_timer_impl_trigger(timer);
|
||||
}
|
||||
|
||||
timer = next;
|
||||
}
|
||||
}
|
||||
|
||||
void wf_timer_impl_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (NULL != manager->timers)
|
||||
{
|
||||
manager->timers->prev = timer;
|
||||
}
|
||||
|
||||
timer->next = manager->timers;
|
||||
timer->prev = NULL;
|
||||
manager->timers = timer;
|
||||
}
|
||||
|
||||
void wf_timer_impl_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
struct wf_timer * prev = timer->prev;
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
if (NULL != prev)
|
||||
{
|
||||
prev->next = next;
|
||||
}
|
||||
|
||||
if (NULL != next)
|
||||
{
|
||||
next->prev = prev;
|
||||
}
|
||||
|
||||
if (manager->timers == timer)
|
||||
{
|
||||
manager->timers = next;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
#ifndef WF_TIMER_IMPL_MANAGER_H
|
||||
#define WF_TIMER_IMPL_MANAGER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern struct wf_timer_manager *
|
||||
wf_timer_impl_manager_create(void);
|
||||
|
||||
extern void
|
||||
wf_timer_impl_manager_dispose(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern void
|
||||
wf_timer_impl_manager_check(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern void wf_timer_impl_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_impl_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,31 @@
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define WF_TIMER_MSEC_PER_SEC ((wf_timer_timepoint) 1000)
|
||||
#define WF_TIMER_NSEC_PER_MSEC ((wf_timer_timepoint) 1000 * 1000)
|
||||
|
||||
wf_timer_timepoint wf_timer_impl_timepoint_now(void)
|
||||
{
|
||||
struct timespec tp;
|
||||
clock_gettime(CLOCK_MONOTONIC, &tp);
|
||||
|
||||
wf_timer_timepoint const now = (tp.tv_sec * WF_TIMER_MSEC_PER_SEC) + (tp.tv_nsec / WF_TIMER_NSEC_PER_MSEC);
|
||||
return now;
|
||||
}
|
||||
|
||||
wf_timer_timepoint wf_timer_impl_timepoint_in_msec(wf_timer_timediff value)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timepoint result = now + ((wf_timer_timepoint) value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wf_timer_impl_timepoint_is_elapsed(wf_timer_timepoint tp)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timediff const diff = (wf_timer_timediff) (tp - now);
|
||||
|
||||
return (0 > diff);
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#ifndef WF_TIMER_IMPL_TIMEPOINT_H
|
||||
#define WF_TIMER_IMPL_TIMEPOINT_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
#else
|
||||
#include <cinttypes>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef uint64_t wf_timer_timepoint;
|
||||
typedef int64_t wf_timer_timediff;
|
||||
|
||||
extern wf_timer_timepoint wf_timer_impl_timepoint_now(void);
|
||||
|
||||
extern wf_timer_timepoint wf_timer_impl_timepoint_in_msec(
|
||||
wf_timer_timediff value);
|
||||
|
||||
extern bool wf_timer_impl_timepoint_is_elapsed(
|
||||
wf_timer_timepoint timepoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,67 @@
|
||||
#include "wf/timer/impl/timer.h"
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_impl_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_timer * timer = malloc(sizeof(struct wf_timer));
|
||||
timer->manager = manager;
|
||||
timer->timeout = 0;
|
||||
timer->on_timer = on_timer;
|
||||
timer->user_data = user_data;
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
return timer;
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_impl_dispose(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
free(timer);
|
||||
}
|
||||
|
||||
void wf_timer_impl_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms)
|
||||
{
|
||||
timer->timeout = wf_timer_impl_timepoint_in_msec(timeout_ms);
|
||||
|
||||
wf_timer_impl_manager_addtimer(timer->manager, timer);
|
||||
}
|
||||
|
||||
void wf_timer_impl_cancel(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_manager_removetimer(timer->manager, timer);
|
||||
|
||||
timer->timeout = 0;
|
||||
}
|
||||
|
||||
bool wf_timer_impl_is_timeout(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
return wf_timer_impl_timepoint_is_elapsed(timer->timeout);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_impl_trigger(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (0 != timer->on_timer)
|
||||
{
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
timer->on_timer(timer, timer->user_data);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
|
||||
#include "wf/timer/on_timer_fn.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
|
||||
struct wf_timer
|
||||
{
|
||||
struct wf_timer_manager * manager;
|
||||
wf_timer_timepoint timeout;
|
||||
wf_timer_on_timer_fn * on_timer;
|
||||
void * user_data;
|
||||
struct wf_timer * next;
|
||||
struct wf_timer * prev;
|
||||
};
|
||||
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_impl_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data);
|
||||
|
||||
void
|
||||
wf_timer_impl_dispose(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_impl_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms);
|
||||
|
||||
extern void wf_timer_impl_cancel(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern bool wf_timer_impl_is_timeout(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_impl_trigger(
|
||||
struct wf_timer * timer);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,38 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
TEST(wf_timer_timepoint, now)
|
||||
{
|
||||
wf_timer_timepoint start = wf_timer_impl_timepoint_now();
|
||||
std::this_thread::sleep_for(42ms);
|
||||
wf_timer_timepoint end = wf_timer_impl_timepoint_now();
|
||||
|
||||
ASSERT_LT(start, end);
|
||||
ASSERT_LT(end, start + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, in_msec)
|
||||
{
|
||||
wf_timer_timepoint now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timepoint later = wf_timer_impl_timepoint_in_msec(42);
|
||||
|
||||
ASSERT_LT(now, later);
|
||||
ASSERT_LT(later, now + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, elapsed)
|
||||
{
|
||||
wf_timer_timepoint now;
|
||||
|
||||
now = wf_timer_impl_timepoint_now();
|
||||
ASSERT_TRUE(wf_timer_impl_timepoint_is_elapsed(now - 1));
|
||||
|
||||
now = wf_timer_impl_timepoint_now();
|
||||
ASSERT_FALSE(wf_timer_impl_timepoint_is_elapsed(now + 500));
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "wf/timer/timer.h"
|
||||
#include "wf/timer/manager.h"
|
||||
|
||||
using std::size_t;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void on_timeout(struct wf_timer * timer, void * user_data)
|
||||
{
|
||||
(void) timer;
|
||||
|
||||
bool * triggered = reinterpret_cast<bool*>(user_data);
|
||||
*triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wf_timer, init)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, 250);
|
||||
std::this_thread::sleep_for(500ms);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger_on_dispose)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, (5 * 60 * 1000));
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, 250);
|
||||
std::this_thread::sleep_for(500ms);
|
||||
wf_timer_cancel(timer);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel_multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
|
||||
bool triggered = false;
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
wf_timer_start(timer[i], 0);
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_cancel(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_check(manager);
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
bool triggered[count];
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered[i]));
|
||||
triggered[i] = false;
|
||||
wf_timer_start(timer[i], (300 - (50 * i)));
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
wf_timer_manager_check(manager);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
ASSERT_TRUE(triggered[i]);
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue