mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
integrated libwf_timer and libwf_jsonrpc into libwebfuse-core
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#include <errno.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/util.h"
|
||||
|
||||
void wf_impl_operation_close(
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/json_util.h"
|
||||
#include "webfuse/core/util.h"
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/json_util.h"
|
||||
#include "webfuse/core/util.h"
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <errno.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/util.h"
|
||||
#include "webfuse/core/status.h"
|
||||
#include "webfuse/core/json_util.h"
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <limits.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/base64.h"
|
||||
#include "webfuse/core/json_util.h"
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/util.h"
|
||||
#include "webfuse/core/json_util.h"
|
||||
|
||||
|
||||
@@ -11,9 +11,9 @@
|
||||
#include "webfuse/adapter/impl/credentials.h"
|
||||
#include "webfuse/core/status_intern.h"
|
||||
|
||||
#include "wf/jsonrpc/request.h"
|
||||
#include "wf/timer/manager.h"
|
||||
#include "wf/timer/timer.h"
|
||||
#include "webfuse/core/jsonrpc/request.h"
|
||||
#include "webfuse/core/timer/manager.h"
|
||||
#include "webfuse/core/timer/timer.h"
|
||||
|
||||
static int wf_impl_server_protocol_callback(
|
||||
struct lws * wsi,
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#include "webfuse/adapter/impl/authenticators.h"
|
||||
#include "webfuse/adapter/impl/mountpoint_factory.h"
|
||||
#include "webfuse/adapter/impl/session_manager.h"
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "wf/jsonrpc/server.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/server.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
#include "webfuse/core/container_of.h"
|
||||
#include "webfuse/core/util.h"
|
||||
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "wf/jsonrpc/request.h"
|
||||
#include "wf/jsonrpc/response.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/request.h"
|
||||
#include "webfuse/core/jsonrpc/response.h"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <stddef.h>
|
||||
|
||||
@@ -13,8 +13,8 @@ using std::size_t;
|
||||
#include "webfuse/adapter/impl/filesystem.h"
|
||||
#include "webfuse/core/slist.h"
|
||||
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "wf/jsonrpc/server.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/server.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#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)
|
||||
{
|
||||
@@ -14,25 +13,6 @@ int wf_impl_json_get_int(json_t const * object, char const * key, int default_va
|
||||
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)
|
||||
@@ -40,8 +20,7 @@ wf_impl_jsonrpc_get_status(
|
||||
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);
|
||||
status = wf_impl_json_get_int(error, "code", WF_BAD_FORMAT);
|
||||
}
|
||||
|
||||
return status;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "wf/jsonrpc/impl/error.h"
|
||||
#include "webfuse/core/jsonrpc/error.h"
|
||||
|
||||
json_t *
|
||||
wf_jsonrpc_impl_error(
|
||||
wf_jsonrpc_error(
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
@@ -13,13 +13,13 @@ wf_jsonrpc_impl_error(
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_impl_propate_error(
|
||||
wf_jsonrpc_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);
|
||||
json_t * error = wf_jsonrpc_error(code, message);
|
||||
finised(user_data, NULL, error);
|
||||
|
||||
json_decref(error);
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef WF_JSONRPC_IMPL_ERROR_H
|
||||
#define WF_JSONRPC_IMPL_ERROR_H
|
||||
#ifndef WF_JSONRPC_ERROR_H
|
||||
#define WF_JSONRPC_ERROR_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "wf/jsonrpc/proxy_finished_fn.h"
|
||||
#include "webfuse/core/jsonrpc/proxy_finished_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -10,12 +10,12 @@ extern "C"
|
||||
#endif
|
||||
|
||||
extern json_t *
|
||||
wf_jsonrpc_impl_error(
|
||||
wf_jsonrpc_error(
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_propate_error(
|
||||
wf_jsonrpc_propate_error(
|
||||
wf_jsonrpc_proxy_finished_fn * finised,
|
||||
void * user_data,
|
||||
int code,
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "wf/jsonrpc/impl/method.h"
|
||||
#include "webfuse/core/jsonrpc/method.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_jsonrpc_method * wf_jsonrpc_impl_method_create(
|
||||
struct wf_jsonrpc_method * wf_jsonrpc_method_create(
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
@@ -16,7 +16,7 @@ struct wf_jsonrpc_method * wf_jsonrpc_impl_method_create(
|
||||
return method;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_method_dispose(
|
||||
void wf_jsonrpc_method_dispose(
|
||||
struct wf_jsonrpc_method * method)
|
||||
{
|
||||
free(method->name);
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef WF_JSONRPC_IMPL_METHOD_H
|
||||
#define WF_JSONRPC_IMPL_METHOD_H
|
||||
#ifndef WF_JSONRPC_METHOD_H
|
||||
#define WF_JSONRPC_METHOD_H
|
||||
|
||||
#include "wf/jsonrpc/method_invoke_fn.h"
|
||||
#include "webfuse/core/jsonrpc/method_invoke_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -17,13 +17,13 @@ struct wf_jsonrpc_method
|
||||
};
|
||||
|
||||
extern struct wf_jsonrpc_method *
|
||||
wf_jsonrpc_impl_method_create(
|
||||
wf_jsonrpc_method_create(
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_impl_method_dispose(
|
||||
wf_jsonrpc_method_dispose(
|
||||
struct wf_jsonrpc_method * method);
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef WF_JSONRPC_METHOD_INVOKE_FN_H
|
||||
#define WF_JSONRPC_METHOD_INVOKE_FN_H
|
||||
|
||||
#include <wf/jsonrpc/api.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -17,7 +16,6 @@ typedef void wf_jsonrpc_method_invoke_fn(
|
||||
json_t * params,
|
||||
void * user_data);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,34 +1,34 @@
|
||||
#include "wf/jsonrpc/impl/proxy.h"
|
||||
#include "wf/jsonrpc/impl/response.h"
|
||||
#include "wf/jsonrpc/impl/error.h"
|
||||
#include "wf/jsonrpc/status.h"
|
||||
#include "webfuse/core/jsonrpc/proxy_intern.h"
|
||||
#include "webfuse/core/jsonrpc/response_intern.h"
|
||||
#include "webfuse/core/jsonrpc/error.h"
|
||||
#include "webfuse/core/status.h"
|
||||
|
||||
#include <wf/timer/timer.h>
|
||||
#include "webfuse/core/timer/timer.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_jsonrpc_proxy *
|
||||
wf_jsonrpc_impl_proxy_create(
|
||||
wf_jsonrpc_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));
|
||||
wf_jsonrpc_impl_proxy_init(proxy, manager, timeout, send, user_data);
|
||||
wf_jsonrpc_proxy_init(proxy, manager, timeout, send, user_data);
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_proxy_dispose(
|
||||
void wf_jsonrpc_proxy_dispose(
|
||||
struct wf_jsonrpc_proxy * proxy)
|
||||
{
|
||||
wf_jsonrpc_impl_proxy_cleanup(proxy);
|
||||
wf_jsonrpc_proxy_cleanup(proxy);
|
||||
free(proxy);
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_impl_proxy_on_timeout(
|
||||
static void wf_jsonrpc_proxy_on_timeout(
|
||||
struct wf_timer * timer, void * proxy_ptr)
|
||||
{
|
||||
struct wf_jsonrpc_proxy * proxy = proxy_ptr;
|
||||
@@ -44,11 +44,11 @@ static void wf_jsonrpc_impl_proxy_on_timeout(
|
||||
proxy->request.finished = NULL;
|
||||
wf_timer_cancel(timer);
|
||||
|
||||
wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD_TIMEOUT, "Timeout");
|
||||
wf_jsonrpc_propate_error(finished, user_data, WF_BAD_TIMEOUT, "Timeout");
|
||||
}
|
||||
}
|
||||
|
||||
static json_t * wf_jsonrpc_impl_request_create(
|
||||
static json_t * wf_jsonrpc_request_create(
|
||||
char const * method,
|
||||
int id,
|
||||
char const * param_info,
|
||||
@@ -98,7 +98,7 @@ static json_t * wf_jsonrpc_impl_request_create(
|
||||
return request;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_proxy_init(
|
||||
void wf_jsonrpc_proxy_init(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
struct wf_timer_manager * timeout_manager,
|
||||
int timeout,
|
||||
@@ -110,10 +110,10 @@ void wf_jsonrpc_impl_proxy_init(
|
||||
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);
|
||||
&wf_jsonrpc_proxy_on_timeout, proxy);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_proxy_cleanup(
|
||||
void wf_jsonrpc_proxy_cleanup(
|
||||
struct wf_jsonrpc_proxy * proxy)
|
||||
{
|
||||
if (proxy->request.is_pending)
|
||||
@@ -127,19 +127,19 @@ void wf_jsonrpc_impl_proxy_cleanup(
|
||||
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_jsonrpc_propate_error(finished, user_data, WF_BAD, "Bad: cancelled pending request during shutdown");
|
||||
}
|
||||
|
||||
wf_timer_dispose(proxy->request.timer);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_proxy_invoke(
|
||||
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
|
||||
...
|
||||
)
|
||||
{
|
||||
if (!proxy->request.is_pending)
|
||||
@@ -150,7 +150,10 @@ void wf_jsonrpc_impl_proxy_invoke(
|
||||
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);
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
json_t * request = wf_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)
|
||||
@@ -161,7 +164,7 @@ void wf_jsonrpc_impl_proxy_invoke(
|
||||
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");
|
||||
wf_jsonrpc_propate_error(finished, user_data, WF_BAD, "Bad: requenst is not sent");
|
||||
}
|
||||
|
||||
if (NULL != request)
|
||||
@@ -171,18 +174,22 @@ void wf_jsonrpc_impl_proxy_invoke(
|
||||
}
|
||||
else
|
||||
{
|
||||
wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD_BUSY, "Busy");
|
||||
wf_jsonrpc_propate_error(finished, user_data, WF_BAD_BUSY, "Busy");
|
||||
}
|
||||
}
|
||||
|
||||
extern void wf_jsonrpc_impl_proxy_notify(
|
||||
extern void wf_jsonrpc_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);
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
json_t * request = wf_jsonrpc_request_create(method_name, 0, param_info, args);
|
||||
va_end(args);
|
||||
|
||||
if (NULL != request)
|
||||
{
|
||||
proxy->send(request, proxy->user_data);
|
||||
@@ -191,12 +198,12 @@ extern void wf_jsonrpc_impl_proxy_notify(
|
||||
}
|
||||
|
||||
|
||||
void wf_jsonrpc_impl_proxy_onresult(
|
||||
void wf_jsonrpc_proxy_onresult(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
json_t * message)
|
||||
{
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_impl_response_init(&response, message);
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
|
||||
{
|
||||
@@ -212,6 +219,6 @@ void wf_jsonrpc_impl_proxy_onresult(
|
||||
finished(user_data, response.result, response.error);
|
||||
}
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
}
|
||||
|
||||
@@ -12,9 +12,8 @@ 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>
|
||||
#include "webfuse/core/jsonrpc/send_fn.h"
|
||||
#include "webfuse/core/jsonrpc/proxy_finished_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -23,14 +22,14 @@ extern "C" {
|
||||
struct wf_jsonrpc_proxy;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_JSONRPC_API struct wf_jsonrpc_proxy *
|
||||
extern 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(
|
||||
extern void wf_jsonrpc_proxy_dispose(
|
||||
struct wf_jsonrpc_proxy * proxy);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -47,7 +46,7 @@ extern WF_JSONRPC_API void wf_jsonrpc_proxy_dispose(
|
||||
/// \param param_info types of the param (s = string, i = integer, j = json)
|
||||
/// \param ... params
|
||||
//------------------------------------------------------------------------------
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_proxy_invoke(
|
||||
extern void wf_jsonrpc_proxy_invoke(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
@@ -56,14 +55,14 @@ extern WF_JSONRPC_API void wf_jsonrpc_proxy_invoke(
|
||||
...
|
||||
);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_proxy_notify(
|
||||
extern 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(
|
||||
extern void wf_jsonrpc_proxy_onresult(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
json_t * message);
|
||||
|
||||
48
lib/webfuse/core/jsonrpc/proxy_intern.h
Normal file
48
lib/webfuse/core/jsonrpc/proxy_intern.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef WF_JSONRPC_PROXY_INTERN_H
|
||||
#define WF_JSONRPC_PROXY_INTERN_H
|
||||
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/proxy_finished_fn.h"
|
||||
#include "webfuse/core/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
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_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_proxy_cleanup(
|
||||
struct wf_jsonrpc_proxy * proxy);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "wf/jsonrpc/impl/request.h"
|
||||
#include "wf/jsonrpc/impl/error.h"
|
||||
#include "webfuse/core/jsonrpc/request.h"
|
||||
#include "webfuse/core/jsonrpc/error.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct wf_jsonrpc_request
|
||||
@@ -10,7 +10,7 @@ struct wf_jsonrpc_request
|
||||
};
|
||||
|
||||
bool
|
||||
wf_jsonrpc_impl_is_request(
|
||||
wf_jsonrpc_is_request(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
@@ -23,7 +23,7 @@ wf_jsonrpc_impl_is_request(
|
||||
|
||||
|
||||
struct wf_jsonrpc_request *
|
||||
wf_jsonrpc_impl_request_create(
|
||||
wf_jsonrpc_request_create(
|
||||
int id,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
@@ -37,14 +37,14 @@ wf_jsonrpc_impl_request_create(
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_impl_request_dispose(
|
||||
wf_jsonrpc_request_dispose(
|
||||
struct wf_jsonrpc_request * request)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
void *
|
||||
wf_jsonrpc_impl_request_get_userdata(
|
||||
wf_jsonrpc_request_get_userdata(
|
||||
struct wf_jsonrpc_request * request)
|
||||
{
|
||||
return request->user_data;
|
||||
@@ -52,7 +52,7 @@ wf_jsonrpc_impl_request_get_userdata(
|
||||
|
||||
|
||||
void
|
||||
wf_jsonrpc_impl_respond(
|
||||
wf_jsonrpc_respond(
|
||||
struct wf_jsonrpc_request * request,
|
||||
json_t * result)
|
||||
{
|
||||
@@ -62,20 +62,20 @@ wf_jsonrpc_impl_respond(
|
||||
|
||||
request->send(response, request->user_data);
|
||||
json_decref(response);
|
||||
wf_jsonrpc_impl_request_dispose(request);
|
||||
wf_jsonrpc_request_dispose(request);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_respond_error(
|
||||
void wf_jsonrpc_respond_error(
|
||||
struct wf_jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "error", wf_jsonrpc_impl_error(code, message));
|
||||
json_object_set_new(response, "error", wf_jsonrpc_error(code, message));
|
||||
json_object_set_new(response, "id", json_integer(request->id));
|
||||
|
||||
request->send(response, request->user_data);
|
||||
json_decref(response);
|
||||
wf_jsonrpc_impl_request_dispose(request);
|
||||
wf_jsonrpc_request_dispose(request);
|
||||
}
|
||||
|
||||
@@ -12,8 +12,7 @@ using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
#include "wf/jsonrpc/send_fn.h"
|
||||
#include "webfuse/core/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -22,26 +21,26 @@ extern "C"
|
||||
|
||||
struct wf_jsonrpc_request;
|
||||
|
||||
extern WF_JSONRPC_API bool wf_jsonrpc_is_request(
|
||||
extern bool wf_jsonrpc_is_request(
|
||||
json_t * message);
|
||||
|
||||
extern WF_JSONRPC_API struct wf_jsonrpc_request *
|
||||
extern 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(
|
||||
extern void wf_jsonrpc_request_dispose(
|
||||
struct wf_jsonrpc_request * request);
|
||||
|
||||
extern WF_JSONRPC_API void * wf_jsonrpc_request_get_userdata(
|
||||
extern void * wf_jsonrpc_request_get_userdata(
|
||||
struct wf_jsonrpc_request * request);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_respond(
|
||||
extern void wf_jsonrpc_respond(
|
||||
struct wf_jsonrpc_request * request,
|
||||
json_t * result);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_respond_error(
|
||||
extern void wf_jsonrpc_respond_error(
|
||||
struct wf_jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message);
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "wf/jsonrpc/impl/response.h"
|
||||
#include "wf/jsonrpc/impl/error.h"
|
||||
#include "wf/jsonrpc/status.h"
|
||||
#include "webfuse/core/jsonrpc/response_intern.h"
|
||||
#include "webfuse/core/jsonrpc/error.h"
|
||||
#include "webfuse/core/status.h"
|
||||
|
||||
bool
|
||||
wf_jsonrpc_impl_is_response(
|
||||
wf_jsonrpc_is_response(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
@@ -16,7 +16,7 @@ wf_jsonrpc_impl_is_response(
|
||||
|
||||
|
||||
void
|
||||
wf_jsonrpc_impl_response_init(
|
||||
wf_jsonrpc_response_init(
|
||||
struct wf_jsonrpc_response * result,
|
||||
json_t * response)
|
||||
{
|
||||
@@ -27,7 +27,7 @@ wf_jsonrpc_impl_response_init(
|
||||
json_t * id_holder = json_object_get(response, "id");
|
||||
if (!json_is_integer(id_holder))
|
||||
{
|
||||
result->error = wf_jsonrpc_impl_error(WF_JSONRPC_BAD_FORMAT, "invalid format: missing id");
|
||||
result->error = wf_jsonrpc_error(WF_BAD_FORMAT, "invalid format: missing id");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -47,13 +47,13 @@ wf_jsonrpc_impl_response_init(
|
||||
}
|
||||
else
|
||||
{
|
||||
result->error = wf_jsonrpc_impl_error(WF_JSONRPC_BAD_FORMAT, "invalid format: invalid error object");
|
||||
result->error = wf_jsonrpc_error(WF_BAD_FORMAT, "invalid format: invalid error object");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_impl_response_cleanup(
|
||||
wf_jsonrpc_response_cleanup(
|
||||
struct wf_jsonrpc_response * response)
|
||||
{
|
||||
if (NULL != response->result)
|
||||
@@ -6,14 +6,13 @@
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern WF_JSONRPC_API bool wf_jsonrpc_is_response(
|
||||
extern bool wf_jsonrpc_is_response(
|
||||
json_t * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -1,16 +1,15 @@
|
||||
#ifndef WF_JSONRPC_IMPL_RESPONSE_H
|
||||
#define WF_JSONRPC_IMPL_RESPONSE_H
|
||||
#ifndef WF_JSONRPC_RESPONSE_INTERN_H
|
||||
#define WF_JSONRPC_RESPONSE_INTERN_H
|
||||
|
||||
#include "webfuse/core/jsonrpc/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
|
||||
@@ -22,14 +21,11 @@ struct wf_jsonrpc_response
|
||||
int id;
|
||||
};
|
||||
|
||||
extern bool wf_jsonrpc_impl_is_response(
|
||||
json_t * message);
|
||||
|
||||
extern void wf_jsonrpc_impl_response_init(
|
||||
extern void wf_jsonrpc_response_init(
|
||||
struct wf_jsonrpc_response * response,
|
||||
json_t * message);
|
||||
|
||||
extern void wf_jsonrpc_impl_response_cleanup(
|
||||
extern void wf_jsonrpc_response_cleanup(
|
||||
struct wf_jsonrpc_response * response);
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -6,7 +6,6 @@
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -1,8 +1,8 @@
|
||||
#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 "webfuse/core/jsonrpc/server.h"
|
||||
#include "webfuse/core/jsonrpc/method.h"
|
||||
#include "webfuse/core/jsonrpc/request.h"
|
||||
#include "webfuse/core/status.h"
|
||||
#include "webfuse/core/util.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -13,80 +13,79 @@ struct wf_jsonrpc_server
|
||||
};
|
||||
|
||||
static void
|
||||
wf_jsonrpc_impl_server_init(
|
||||
wf_jsonrpc_server_init(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
static void
|
||||
wf_jsonrpc_impl_server_cleanup(
|
||||
wf_jsonrpc_server_cleanup(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
struct wf_jsonrpc_server *
|
||||
wf_jsonrpc_impl_server_create(void)
|
||||
wf_jsonrpc_server_create(void)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = malloc(sizeof(struct wf_jsonrpc_server));
|
||||
wf_jsonrpc_impl_server_init(server);
|
||||
wf_jsonrpc_server_init(server);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_impl_server_dispose(
|
||||
wf_jsonrpc_server_dispose(
|
||||
struct wf_jsonrpc_server * server)
|
||||
{
|
||||
wf_jsonrpc_impl_server_cleanup(server);
|
||||
wf_jsonrpc_server_cleanup(server);
|
||||
free(server);
|
||||
}
|
||||
|
||||
|
||||
static void wf_jsonrpc_impl_server_init(
|
||||
static void wf_jsonrpc_server_init(
|
||||
struct wf_jsonrpc_server * server)
|
||||
{
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_impl_server_cleanup(
|
||||
static void wf_jsonrpc_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);
|
||||
wf_jsonrpc_method_dispose(current);
|
||||
current = next;
|
||||
}
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_server_add(
|
||||
void wf_jsonrpc_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);
|
||||
struct wf_jsonrpc_method * method = wf_jsonrpc_method_create(method_name, invoke, user_data);
|
||||
method->next = server->methods;
|
||||
server->methods = method;
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_impl_server_invalid_method_invoke(
|
||||
static void wf_jsonrpc_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))
|
||||
char const * WF_UNUSED_PARAM(method_name),
|
||||
json_t * WF_UNUSED_PARAM(params),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
wf_jsonrpc_impl_respond_error(request, WF_JSONRPC_BAD_NOTIMPLEMENTED, "not implemented");
|
||||
wf_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED, "not implemented");
|
||||
}
|
||||
|
||||
static struct wf_jsonrpc_method const wf_jsonrpc_impl_server_invalid_method =
|
||||
static struct wf_jsonrpc_method const wf_jsonrpc_server_invalid_method =
|
||||
{
|
||||
.next = NULL,
|
||||
.name = "<invalid>",
|
||||
.invoke = &wf_jsonrpc_impl_server_invalid_method_invoke,
|
||||
.invoke = &wf_jsonrpc_server_invalid_method_invoke,
|
||||
.user_data = NULL
|
||||
};
|
||||
|
||||
static struct wf_jsonrpc_method const *
|
||||
wf_jsonrpc_impl_server_get_method(
|
||||
wf_jsonrpc_server_get_method(
|
||||
struct wf_jsonrpc_server * server,
|
||||
char const * method_name)
|
||||
{
|
||||
@@ -101,10 +100,10 @@ wf_jsonrpc_impl_server_get_method(
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
return &wf_jsonrpc_impl_server_invalid_method;
|
||||
return &wf_jsonrpc_server_invalid_method;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_impl_server_process(
|
||||
void wf_jsonrpc_server_process(
|
||||
struct wf_jsonrpc_server * server,
|
||||
json_t * request_data,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
@@ -120,8 +119,8 @@ void wf_jsonrpc_impl_server_process(
|
||||
{
|
||||
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);
|
||||
struct wf_jsonrpc_request * request = wf_jsonrpc_request_create(id, send, user_data);
|
||||
struct wf_jsonrpc_method const * method = wf_jsonrpc_server_get_method(server, method_name);
|
||||
|
||||
method->invoke(request, method_name, params, method->user_data);
|
||||
}
|
||||
@@ -9,9 +9,8 @@
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <wf/jsonrpc/api.h>
|
||||
#include <wf/jsonrpc/send_fn.h>
|
||||
#include <wf/jsonrpc/method_invoke_fn.h>
|
||||
#include "webfuse/core/jsonrpc/method_invoke_fn.h"
|
||||
#include "webfuse/core/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -20,20 +19,20 @@ extern "C"
|
||||
|
||||
struct wf_jsonrpc_server;
|
||||
|
||||
extern WF_JSONRPC_API struct wf_jsonrpc_server *
|
||||
extern struct wf_jsonrpc_server *
|
||||
wf_jsonrpc_server_create(void);
|
||||
|
||||
extern WF_JSONRPC_API void
|
||||
extern void
|
||||
wf_jsonrpc_server_dispose(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
extern WF_JSONRPC_API void wf_jsonrpc_server_add(
|
||||
extern 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(
|
||||
extern void wf_jsonrpc_server_process(
|
||||
struct wf_jsonrpc_server * server,
|
||||
json_t * request,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timer.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
#include "webfuse/core/timer/manager_intern.h"
|
||||
#include "webfuse/core/timer/timer_intern.h"
|
||||
#include "webfuse/core/timer/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
@@ -11,7 +11,7 @@ struct wf_timer_manager
|
||||
};
|
||||
|
||||
struct wf_timer_manager *
|
||||
wf_timer_impl_manager_create(void)
|
||||
wf_timer_manager_create(void)
|
||||
{
|
||||
struct wf_timer_manager * manager = malloc(sizeof(struct wf_timer_manager));
|
||||
manager->timers = NULL;
|
||||
@@ -20,7 +20,7 @@ wf_timer_impl_manager_create(void)
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_impl_manager_dispose(
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
@@ -28,7 +28,7 @@ wf_timer_impl_manager_dispose(
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
wf_timer_impl_trigger(timer);
|
||||
wf_timer_trigger(timer);
|
||||
timer = next;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ wf_timer_impl_manager_dispose(
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_impl_manager_check(
|
||||
void wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
@@ -44,17 +44,17 @@ void wf_timer_impl_manager_check(
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
if (wf_timer_impl_is_timeout(timer))
|
||||
if (wf_timer_is_timeout(timer))
|
||||
{
|
||||
wf_timer_impl_manager_removetimer(manager, timer);
|
||||
wf_timer_impl_trigger(timer);
|
||||
wf_timer_manager_removetimer(manager, timer);
|
||||
wf_timer_trigger(timer);
|
||||
}
|
||||
|
||||
timer = next;
|
||||
}
|
||||
}
|
||||
|
||||
void wf_timer_impl_manager_addtimer(
|
||||
void wf_timer_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
@@ -68,7 +68,7 @@ void wf_timer_impl_manager_addtimer(
|
||||
manager->timers = timer;
|
||||
}
|
||||
|
||||
void wf_timer_impl_manager_removetimer(
|
||||
void wf_timer_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
@@ -1,8 +1,6 @@
|
||||
#ifndef WF_TIMER_MANAGER_H
|
||||
#define WF_TIMER_MANAGER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
@@ -10,14 +8,14 @@ extern "C"
|
||||
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_TIMER_API struct wf_timer_manager *
|
||||
extern struct wf_timer_manager *
|
||||
wf_timer_manager_create(void);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
extern void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
extern void
|
||||
wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
26
lib/webfuse/core/timer/manager_intern.h
Normal file
26
lib/webfuse/core/timer/manager_intern.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WF_TIMER_MANAGER_INTERN_H
|
||||
#define WF_TIMER_MANAGER_INTERN_H
|
||||
|
||||
#include "webfuse/core/timer/manager.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
|
||||
extern void wf_timer_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
#include "webfuse/core/timer/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)
|
||||
wf_timer_timepoint wf_timer_timepoint_now(void)
|
||||
{
|
||||
struct timespec tp;
|
||||
clock_gettime(CLOCK_MONOTONIC, &tp);
|
||||
@@ -14,17 +14,17 @@ wf_timer_timepoint wf_timer_impl_timepoint_now(void)
|
||||
return now;
|
||||
}
|
||||
|
||||
wf_timer_timepoint wf_timer_impl_timepoint_in_msec(wf_timer_timediff value)
|
||||
wf_timer_timepoint wf_timer_timepoint_in_msec(wf_timer_timediff value)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timepoint const now = wf_timer_timepoint_now();
|
||||
wf_timer_timepoint result = now + ((wf_timer_timepoint) value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wf_timer_impl_timepoint_is_elapsed(wf_timer_timepoint tp)
|
||||
bool wf_timer_timepoint_is_elapsed(wf_timer_timepoint tp)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timepoint const now = wf_timer_timepoint_now();
|
||||
wf_timer_timediff const diff = (wf_timer_timediff) (tp - now);
|
||||
|
||||
return (0 > diff);
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef WF_TIMER_IMPL_TIMEPOINT_H
|
||||
#define WF_TIMER_IMPL_TIMEPOINT_H
|
||||
#ifndef WF_TIMER_TIMEPOINT_H
|
||||
#define WF_TIMER_TIMEPOINT_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
@@ -16,12 +16,12 @@ extern "C"
|
||||
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_timepoint_now(void);
|
||||
|
||||
extern wf_timer_timepoint wf_timer_impl_timepoint_in_msec(
|
||||
extern wf_timer_timepoint wf_timer_timepoint_in_msec(
|
||||
wf_timer_timediff value);
|
||||
|
||||
extern bool wf_timer_impl_timepoint_is_elapsed(
|
||||
extern bool wf_timer_timepoint_is_elapsed(
|
||||
wf_timer_timepoint timepoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -1,13 +1,13 @@
|
||||
#include "wf/timer/impl/timer.h"
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
#include "webfuse/core/timer/timer_intern.h"
|
||||
#include "webfuse/core/timer/manager_intern.h"
|
||||
#include "webfuse/core/timer/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_impl_create(
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data)
|
||||
@@ -24,37 +24,37 @@ wf_timer_impl_create(
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_impl_dispose(
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
free(timer);
|
||||
}
|
||||
|
||||
void wf_timer_impl_start(
|
||||
void wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms)
|
||||
{
|
||||
timer->timeout = wf_timer_impl_timepoint_in_msec(timeout_ms);
|
||||
timer->timeout = wf_timer_timepoint_in_msec(timeout_ms);
|
||||
|
||||
wf_timer_impl_manager_addtimer(timer->manager, timer);
|
||||
wf_timer_manager_addtimer(timer->manager, timer);
|
||||
}
|
||||
|
||||
void wf_timer_impl_cancel(
|
||||
void wf_timer_cancel(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_manager_removetimer(timer->manager, timer);
|
||||
wf_timer_manager_removetimer(timer->manager, timer);
|
||||
|
||||
timer->timeout = 0;
|
||||
}
|
||||
|
||||
bool wf_timer_impl_is_timeout(
|
||||
bool wf_timer_is_timeout(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
return wf_timer_impl_timepoint_is_elapsed(timer->timeout);
|
||||
return wf_timer_timepoint_is_elapsed(timer->timeout);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_impl_trigger(
|
||||
void wf_timer_trigger(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (0 != timer->on_timer)
|
||||
@@ -1,8 +1,7 @@
|
||||
#ifndef WF_TIMER_TIMER_H
|
||||
#define WF_TIMER_TIMER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
#include <wf/timer/on_timer_fn.h>
|
||||
#include "webfuse/core/timer/on_timer_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -12,22 +11,22 @@ extern "C"
|
||||
struct wf_timer;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_TIMER_API struct wf_timer *
|
||||
extern 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
|
||||
extern void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
extern void
|
||||
wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
extern void
|
||||
wf_timer_cancel(
|
||||
struct wf_timer * timer);
|
||||
|
||||
40
lib/webfuse/core/timer/timer_intern.h
Normal file
40
lib/webfuse/core/timer/timer_intern.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef WF_TIMER_TIMER_H
|
||||
#define WF_TIMER_TIMER_H
|
||||
|
||||
#include "webfuse/core/timer/timer.h"
|
||||
#include "webfuse/core/timer/on_timer_fn.h"
|
||||
#include "webfuse/core/timer/timepoint.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
extern bool wf_timer_is_timeout(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_trigger(
|
||||
struct wf_timer * timer);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -16,11 +16,11 @@
|
||||
#include "webfuse/provider/impl/url.h"
|
||||
#include "webfuse/core/protocol_names.h"
|
||||
|
||||
#include "wf/timer/manager.h"
|
||||
#include "webfuse/core/timer/manager.h"
|
||||
|
||||
#include "wf/jsonrpc/response.h"
|
||||
#include "wf/jsonrpc/request.h"
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "webfuse/core/jsonrpc/response.h"
|
||||
#include "webfuse/core/jsonrpc/request.h"
|
||||
#include "webfuse/core/jsonrpc/proxy.h"
|
||||
|
||||
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
||||
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
#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
|
||||
@@ -1,8 +0,0 @@
|
||||
#ifndef WF_JSONRPC_API_H
|
||||
#define WF_JSONRPC_API_H
|
||||
|
||||
#ifndef WF_JSONRPC_API
|
||||
#define WF_JSONRPC_API
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
#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
|
||||
@@ -1,144 +0,0 @@
|
||||
#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);
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
#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
|
||||
@@ -1,53 +0,0 @@
|
||||
#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
|
||||
@@ -1,41 +0,0 @@
|
||||
#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
|
||||
@@ -1,10 +0,0 @@
|
||||
#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,31 +0,0 @@
|
||||
#include "wf/jsonrpc/mock_timer.hpp"
|
||||
#include "wf/jsonrpc/wrap.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
static wf_jsonrpc_test::ITimer * wf_jsonrpc_MockTimer = nullptr;
|
||||
|
||||
WF_WRAP_FUNC0(wf_jsonrpc_MockTimer, wf_timer_manager *, wf_timer_manager_create);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_manager_dispose, wf_timer_manager *);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_manager_check, wf_timer_manager *);
|
||||
|
||||
WF_WRAP_FUNC3(wf_jsonrpc_MockTimer, wf_timer *, wf_timer_create, wf_timer_manager *, wf_timer_on_timer_fn *, void *);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_dispose, wf_timer *);
|
||||
WF_WRAP_FUNC2(wf_jsonrpc_MockTimer, void, wf_timer_start, wf_timer *, int);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_cancel, wf_timer *);
|
||||
|
||||
}
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
MockTimer::MockTimer()
|
||||
{
|
||||
wf_jsonrpc_MockTimer = this;
|
||||
}
|
||||
|
||||
MockTimer::~MockTimer()
|
||||
{
|
||||
wf_jsonrpc_MockTimer = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#ifndef WF_JSONRPC_MOCK_TIMERMANAGER_HPP
|
||||
#define WF_JSONRPC_MOCK_TIMERMANAGER_HPP
|
||||
|
||||
#include <wf/timer.h>
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
|
||||
class ITimer
|
||||
{
|
||||
public:
|
||||
virtual ~ITimer() = default;
|
||||
virtual wf_timer_manager * wf_timer_manager_create() = 0;
|
||||
virtual void wf_timer_manager_dispose(wf_timer_manager * manager) = 0;
|
||||
virtual void wf_timer_manager_check(wf_timer_manager * manager) = 0;
|
||||
virtual wf_timer * wf_timer_create(
|
||||
wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data) = 0;
|
||||
virtual void wf_timer_dispose(wf_timer * timer) = 0;
|
||||
virtual void wf_timer_start(wf_timer * timer, int timeout_ms) = 0;
|
||||
virtual void wf_timer_cancel(wf_timer * timer) = 0;
|
||||
};
|
||||
|
||||
class MockTimer: public ITimer
|
||||
{
|
||||
public:
|
||||
MockTimer();
|
||||
~MockTimer() override;
|
||||
MOCK_METHOD0(wf_timer_manager_create, wf_timer_manager * ());
|
||||
MOCK_METHOD1(wf_timer_manager_dispose, void(wf_timer_manager * manager));
|
||||
MOCK_METHOD1(wf_timer_manager_check, void (wf_timer_manager * manager));
|
||||
MOCK_METHOD3(wf_timer_create, wf_timer *(
|
||||
wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data));
|
||||
MOCK_METHOD1(wf_timer_dispose, void (wf_timer * timer));
|
||||
MOCK_METHOD2(wf_timer_start, void (wf_timer * timer, int timeout_ms));
|
||||
MOCK_METHOD1(wf_timer_cancel, void (wf_timer * timer));
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,41 +0,0 @@
|
||||
#include "wf/jsonrpc/mock_timer_callback.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using wf_jsonrpc_test::MockTimerCallback;
|
||||
|
||||
static void wf_jsonrpc_test_MockTimerCallback_on_timer(
|
||||
wf_timer * timer,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<MockTimerCallback*>(user_data);
|
||||
self->on_timer(timer, user_data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
|
||||
MockTimerCallback::MockTimerCallback()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MockTimerCallback::~MockTimerCallback()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
wf_timer_on_timer_fn * MockTimerCallback::on_timer_fn()
|
||||
{
|
||||
return &wf_jsonrpc_test_MockTimerCallback_on_timer;
|
||||
}
|
||||
|
||||
void * MockTimerCallback::user_data()
|
||||
{
|
||||
return reinterpret_cast<void*>(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef WF_JSONRPC_MOCK_TIMERCALLBACK_HPP
|
||||
#define WF_JSONRPC_MOCK_TIMERCALLBACK_HPP
|
||||
|
||||
#include "wf/timer.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
class MockTimerCallback
|
||||
{
|
||||
public:
|
||||
MockTimerCallback();
|
||||
virtual ~MockTimerCallback();
|
||||
wf_timer_on_timer_fn * on_timer_fn();
|
||||
void * user_data();
|
||||
|
||||
MOCK_METHOD2(on_timer, void (wf_timer * timer, void * user_data));
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,112 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "wf/jsonrpc/request.h"
|
||||
|
||||
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_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, null_request)
|
||||
{
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(nullptr));
|
||||
}
|
||||
|
||||
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_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "wf/jsonrpc/response.h"
|
||||
|
||||
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_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_null)
|
||||
{
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(nullptr));
|
||||
}
|
||||
|
||||
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_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
|
||||
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_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
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_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
@@ -1,429 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "wf/jsonrpc/proxy.h"
|
||||
#include "wf/jsonrpc/status.h"
|
||||
#include "wf/timer/manager.h"
|
||||
|
||||
#include "wf/jsonrpc/mock_timer.hpp"
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using wf_jsonrpc_test::MockTimer;
|
||||
using testing::Return;
|
||||
using testing::_;
|
||||
using testing::DoAll;
|
||||
using testing::SaveArg;
|
||||
|
||||
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
||||
|
||||
namespace
|
||||
{
|
||||
int jsonrpc_get_status(json_t * error)
|
||||
{
|
||||
json_t * code = json_object_get(error, "code");
|
||||
return (json_is_integer(code)) ? json_integer_value(code) : WF_JSONRPC_BAD_FORMAT;
|
||||
}
|
||||
|
||||
struct SendContext
|
||||
{
|
||||
json_t * response;
|
||||
bool result;
|
||||
bool is_called;
|
||||
|
||||
explicit SendContext(bool result_ = true)
|
||||
: response(nullptr)
|
||||
, result(result_)
|
||||
, is_called(false)
|
||||
{
|
||||
}
|
||||
|
||||
~SendContext()
|
||||
{
|
||||
if (nullptr != response)
|
||||
{
|
||||
json_decref(response);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool jsonrpc_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
SendContext * context = reinterpret_cast<SendContext*>(user_data);
|
||||
context->is_called = true;
|
||||
context->response = request;
|
||||
json_incref(request);
|
||||
|
||||
return context->result;
|
||||
}
|
||||
|
||||
struct FinishedContext
|
||||
{
|
||||
bool is_called;
|
||||
json_t * result;
|
||||
json_t * error;
|
||||
|
||||
FinishedContext()
|
||||
: is_called(false)
|
||||
, result(nullptr)
|
||||
, error(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~FinishedContext()
|
||||
{
|
||||
if (nullptr != result)
|
||||
{
|
||||
json_decref(result);
|
||||
}
|
||||
|
||||
if (nullptr != error)
|
||||
{
|
||||
json_decref(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void jsonrpc_finished(
|
||||
void * user_data,
|
||||
json_t const * result,
|
||||
json_t const * error)
|
||||
{
|
||||
FinishedContext * context = reinterpret_cast<FinishedContext*>(user_data);
|
||||
context->is_called = true;
|
||||
context->result = json_deep_copy(result);
|
||||
context->error = json_deep_copy(error);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, init)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext context;
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * method = json_object_get(send_context.response, "method");
|
||||
ASSERT_TRUE(json_is_string(method));
|
||||
ASSERT_STREQ("foo", json_string_value(method));
|
||||
|
||||
json_t * params = json_object_get(send_context.response, "params");
|
||||
ASSERT_TRUE(json_is_array(params));
|
||||
ASSERT_EQ(2, json_array_size(params));
|
||||
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
|
||||
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
|
||||
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
|
||||
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke_calls_finish_if_send_fails)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context(false);
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
FinishedContext finished_context2;
|
||||
void * finished_data2 = reinterpret_cast<void*>(&finished_context2);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", "");
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
ASSERT_TRUE(finished_context2.is_called);
|
||||
ASSERT_EQ(WF_JSONRPC_BAD_BUSY, jsonrpc_get_status(finished_context2.error));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke_fails_if_request_is_invalid)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "?", "error");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WF_JSONRPC_BAD, jsonrpc_get_status(finished_context.error));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, on_result)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_TRUE(json_is_number(id));
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_string("okay"));
|
||||
json_object_set(response, "id", id);
|
||||
|
||||
wf_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(nullptr, finished_context.error);
|
||||
ASSERT_TRUE(json_is_string(finished_context.result));
|
||||
ASSERT_STREQ("okay", json_string_value(finished_context.result));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, on_result_reject_response_with_unknown_id)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_TRUE(json_is_number(id));
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_string("okay"));
|
||||
json_object_set_new(response, "id", json_integer(1 + json_integer_value(id)));
|
||||
|
||||
wf_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, timeout)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, 0, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
wf_timer_manager_check(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WF_JSONRPC_BAD_TIMEOUT, jsonrpc_get_status(finished_context.error));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, cleanup_pending_request)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, 10, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(wf_jsonrpc_proxy, notify)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
wf_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * method = json_object_get(send_context.response, "method");
|
||||
ASSERT_TRUE(json_is_string(method));
|
||||
ASSERT_STREQ("foo", json_string_value(method));
|
||||
|
||||
json_t * params = json_object_get(send_context.response, "params");
|
||||
ASSERT_TRUE(json_is_array(params));
|
||||
ASSERT_EQ(2, json_array_size(params));
|
||||
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
|
||||
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
|
||||
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
|
||||
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_EQ(nullptr, id);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, notify_dont_send_invalid_request)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
wf_jsonrpc_proxy_notify(proxy, "foo", "?");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, swallow_timeout_if_no_request_pending)
|
||||
{
|
||||
MockTimer timer_api;
|
||||
|
||||
wf_timer_on_timer_fn * on_timer = nullptr;
|
||||
void * timer_context = nullptr;
|
||||
EXPECT_CALL(timer_api, wf_timer_create(_, _, _))
|
||||
.Times(1)
|
||||
.WillOnce(DoAll(SaveArg<1>(&on_timer), SaveArg<2>(&timer_context), Return(nullptr)));
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(nullptr, 1, &jsonrpc_send, send_data);
|
||||
|
||||
on_timer(nullptr, timer_context);
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, on_result_swallow_if_no_request_pending)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_string("okay"));
|
||||
json_object_set_new(response, "id", json_integer(42));
|
||||
|
||||
wf_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
@@ -1,138 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "wf/jsonrpc/request.h"
|
||||
#include "wf/jsonrpc/status.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct Context
|
||||
{
|
||||
json_t * response;
|
||||
};
|
||||
|
||||
bool jsonrpc_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
Context * context = reinterpret_cast<Context*>(user_data);
|
||||
context->response = request;
|
||||
json_incref(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, create_dispose)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wf_jsonrpc_request * request =
|
||||
wf_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_NE(nullptr, request);
|
||||
ASSERT_EQ(user_data, wf_jsonrpc_request_get_userdata(request));
|
||||
|
||||
wf_jsonrpc_request_dispose(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, respond)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wf_jsonrpc_request * request =
|
||||
wf_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
wf_jsonrpc_respond(request, json_string("okay"));
|
||||
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
|
||||
|
||||
json_t * response = reinterpret_cast<json_t*>(context.response);
|
||||
ASSERT_TRUE(json_is_object(response));
|
||||
|
||||
json_t * id = json_object_get(response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(42, json_integer_value(id));
|
||||
|
||||
json_t * result = json_object_get(response, "result");
|
||||
ASSERT_TRUE(json_is_string(result));
|
||||
ASSERT_STREQ("okay", json_string_value(result));
|
||||
|
||||
ASSERT_EQ(nullptr, json_object_get(response, "error"));
|
||||
|
||||
json_decref(response);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, respond_error)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wf_jsonrpc_request * request =
|
||||
wf_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
wf_jsonrpc_respond_error(request, WF_JSONRPC_BAD, "Bad");
|
||||
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
|
||||
|
||||
json_t * response = reinterpret_cast<json_t*>(context.response);
|
||||
ASSERT_TRUE(json_is_object(response));
|
||||
|
||||
json_t * id = json_object_get(response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(42, json_integer_value(id));
|
||||
|
||||
ASSERT_EQ(nullptr, json_object_get(response, "result"));
|
||||
|
||||
json_t * err = json_object_get(response, "error");
|
||||
ASSERT_TRUE(json_is_object(err));
|
||||
|
||||
json_t * err_code = json_object_get(err, "code");
|
||||
ASSERT_TRUE(json_is_integer(err_code));
|
||||
ASSERT_EQ(WF_JSONRPC_BAD, json_integer_value(err_code));
|
||||
|
||||
json_t * err_message = json_object_get(err, "message");
|
||||
ASSERT_TRUE(json_is_string(err_message));
|
||||
ASSERT_STREQ("Bad", json_string_value(err_message));
|
||||
|
||||
json_decref(response);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, is_request_object_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("some_method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, is_request_fail_missing_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("some_method"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, is_request_fail_params_wrong_type)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("some_method"));
|
||||
json_object_set_new(request, "params", json_string("invalid_params"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
#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_fail_missing_result_and_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);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
|
||||
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(-1, response.id);
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_wrong_id_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
json_object_set_new(message, "id", json_string("42"));
|
||||
|
||||
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(-1, response.id);
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_error_missing_code)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
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(WF_JSONRPC_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_error_wrong_code_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "code", json_string("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(WF_JSONRPC_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_error_wrong_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "error", json_string("invalid error type"));
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
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(23, response.id);
|
||||
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "wf/jsonrpc/impl/response.h"
|
||||
|
||||
|
||||
static void response_parse_str(
|
||||
std::string const & buffer,
|
||||
struct wf_jsonrpc_response * response)
|
||||
{
|
||||
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
|
||||
if (nullptr != message)
|
||||
{
|
||||
wf_jsonrpc_impl_response_init(response, message);
|
||||
json_decref(message);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(response_parser, test)
|
||||
{
|
||||
struct wf_jsonrpc_response response;
|
||||
|
||||
// no object
|
||||
response_parse_str("[]", &response);
|
||||
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(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(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(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(nullptr, response.error);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_NE(nullptr, response.result);
|
||||
wf_jsonrpc_impl_response_cleanup(&response);
|
||||
}
|
||||
@@ -1,241 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "wf/jsonrpc/server.h"
|
||||
#include "wf/jsonrpc/request.h"
|
||||
#include "wf/jsonrpc/status.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Context
|
||||
{
|
||||
json_t * response;
|
||||
bool is_called;
|
||||
};
|
||||
|
||||
bool jsonrpc_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
Context * context = reinterpret_cast<Context*>(user_data);
|
||||
context->is_called = true;
|
||||
context->response = request;
|
||||
json_incref(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void sayHello(
|
||||
struct wf_jsonrpc_request * request,
|
||||
char const * method_name,
|
||||
json_t * params,
|
||||
void * user_data)
|
||||
{
|
||||
(void) method_name;
|
||||
(void) params;
|
||||
(void) user_data;
|
||||
|
||||
json_t * result = json_string("Hello");
|
||||
wf_jsonrpc_respond(request, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, process_request)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(23));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_TRUE(context.is_called);
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
ASSERT_TRUE(json_is_object(context.response));
|
||||
|
||||
json_t * id = json_object_get(context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(23, json_integer_value(id));
|
||||
|
||||
json_t * result = json_object_get(context.response, "result");
|
||||
ASSERT_TRUE(json_is_string(result));
|
||||
ASSERT_STREQ("Hello", json_string_value(result));
|
||||
|
||||
json_decref(context.response);
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, process_request_with_oject_params)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(23));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_TRUE(context.is_called);
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
ASSERT_TRUE(json_is_object(context.response));
|
||||
|
||||
json_t * id = json_object_get(context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(23, json_integer_value(id));
|
||||
|
||||
json_t * result = json_object_get(context.response, "result");
|
||||
ASSERT_TRUE(json_is_string(result));
|
||||
ASSERT_STREQ("Hello", json_string_value(result));
|
||||
|
||||
json_decref(context.response);
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, invoke_unknown_method)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("greet"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_TRUE(context.is_called);
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
ASSERT_TRUE(json_is_object(context.response));
|
||||
|
||||
json_t * id = json_object_get(context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(42, json_integer_value(id));
|
||||
|
||||
json_t * err = json_object_get(context.response, "error");
|
||||
ASSERT_TRUE(json_is_object(err));
|
||||
|
||||
json_t * err_code = json_object_get(err, "code");
|
||||
ASSERT_TRUE(json_is_integer(err_code));
|
||||
ASSERT_EQ(WF_JSONRPC_BAD_NOTIMPLEMENTED, json_integer_value(err_code));
|
||||
|
||||
json_t * err_message = json_object_get(err, "message");
|
||||
ASSERT_TRUE(json_is_string(err_message));
|
||||
|
||||
json_decref(context.response);
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_missing_id)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_wrong_id_type)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_string("42"));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_missing_params)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_wrong_params_type)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_string("invalid"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_missing_method)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_wront_method_type)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_integer(42));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
#ifndef WF_WRAP_HPP
|
||||
#define WF_WRAP_HPP
|
||||
|
||||
#define WF_WRAP_FUNC0( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME () \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC1( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC2( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC3( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC4( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4); \
|
||||
} \
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,9 +0,0 @@
|
||||
#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
|
||||
@@ -1,8 +0,0 @@
|
||||
#ifndef WF_TIMER_API_H
|
||||
#define WF_TIMER_API_H
|
||||
|
||||
#ifndef WF_TIMER_API
|
||||
#define WF_TIMER_API
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,59 +0,0 @@
|
||||
#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);
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
#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
|
||||
@@ -1,59 +0,0 @@
|
||||
#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
|
||||
@@ -1,38 +0,0 @@
|
||||
#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));
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
#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, -1);
|
||||
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);
|
||||
}
|
||||
|
||||
TEST(wf_timer, dont_trigger_null_callback)
|
||||
{
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, nullptr, nullptr);
|
||||
|
||||
wf_timer_start(timer, -1);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
Reference in New Issue
Block a user