From 69a1faaa3fdbb8d87cf5f80f6ff21fca27ed3487 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sat, 29 Feb 2020 02:32:03 +0100 Subject: [PATCH] chore: cleanup jsonrpc library --- CMakeLists.txt | 2 +- cmake/jsonrpc.cmake | 13 +- cmake/unit_tests.cmake | 2 +- include/jsonrpc.h | 14 ++ include/jsonrpc/api.h | 8 + include/jsonrpc/method_invoke_fn.h | 26 ++++ include/jsonrpc/proxy.h | 41 ++--- include/jsonrpc/proxy_finished_fn.h | 20 +++ include/jsonrpc/request.h | 17 ++- include/jsonrpc/response.h | 29 +--- include/jsonrpc/send_fn.h | 1 + include/jsonrpc/server.h | 23 ++- include/jsonrpc/status.h | 1 + lib/jsonrpc/api.c | 144 ++++++++++++++++++ lib/jsonrpc/{ => impl}/error.c | 8 +- lib/jsonrpc/{ => impl}/error.h | 10 +- lib/jsonrpc/{ => impl}/method.c | 6 +- .../jsonrpc => lib/jsonrpc/impl}/method.h | 20 +-- lib/jsonrpc/{ => impl}/proxy.c | 72 +++++---- lib/jsonrpc/impl/proxy.h | 84 ++++++++++ lib/jsonrpc/{ => impl}/request.c | 34 +++-- lib/jsonrpc/impl/request.h | 53 +++++++ lib/jsonrpc/{ => impl}/response.c | 17 ++- lib/jsonrpc/impl/response.h | 40 +++++ lib/jsonrpc/impl/server.c | 132 ++++++++++++++++ lib/jsonrpc/impl/server.h | 41 +++++ lib/jsonrpc/impl/unused_param.h | 10 ++ lib/jsonrpc/server.c | 95 ------------ lib/webfuse/adapter/impl/operations.c | 2 +- lib/webfuse/adapter/impl/server_protocol.c | 15 +- lib/webfuse/adapter/impl/server_protocol.h | 2 +- lib/webfuse/adapter/impl/session.c | 11 +- lib/webfuse/adapter/impl/session.h | 2 +- test/jsonrpc/test_proxy.cc | 81 +++++----- test/jsonrpc/test_request.cc | 5 +- test/jsonrpc/test_response.cc | 14 +- .../test_response_parser.cc | 14 +- test/jsonrpc/test_server.cc | 28 ++-- 38 files changed, 792 insertions(+), 345 deletions(-) create mode 100644 include/jsonrpc.h create mode 100644 include/jsonrpc/api.h create mode 100644 include/jsonrpc/method_invoke_fn.h create mode 100644 include/jsonrpc/proxy_finished_fn.h create mode 100644 lib/jsonrpc/api.c rename lib/jsonrpc/{ => impl}/error.c (75%) rename lib/jsonrpc/{ => impl}/error.h (65%) rename lib/jsonrpc/{ => impl}/method.c (80%) rename {include/jsonrpc => lib/jsonrpc/impl}/method.h (50%) rename lib/jsonrpc/{ => impl}/proxy.c (71%) create mode 100644 lib/jsonrpc/impl/proxy.h rename lib/jsonrpc/{ => impl}/request.c (70%) create mode 100644 lib/jsonrpc/impl/request.h rename lib/jsonrpc/{ => impl}/response.c (76%) create mode 100644 lib/jsonrpc/impl/response.h create mode 100644 lib/jsonrpc/impl/server.c create mode 100644 lib/jsonrpc/impl/server.h create mode 100644 lib/jsonrpc/impl/unused_param.h delete mode 100644 lib/jsonrpc/server.c rename test/{webfuse/tests/adapter => jsonrpc}/test_response_parser.cc (81%) diff --git a/CMakeLists.txt b/CMakeLists.txt index cbdf5f2..6e87f52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ set(CMAKE_C_STANDARD 99) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(C_WARNINGS -Wall -Wextra) +set(C_WARNINGS -Wall -Wextra -Werror) set(CMAKE_POSITION_INDEPENDENT_CODE ON) include_directories( diff --git a/cmake/jsonrpc.cmake b/cmake/jsonrpc.cmake index 40be948..fb417bb 100644 --- a/cmake/jsonrpc.cmake +++ b/cmake/jsonrpc.cmake @@ -1,12 +1,13 @@ # jsonrpc add_library(jsonrpc STATIC - lib/jsonrpc/proxy.c - lib/jsonrpc/server.c - lib/jsonrpc/method.c - lib/jsonrpc/request.c - lib/jsonrpc/response.c - lib/jsonrpc/error.c + lib/jsonrpc/api.c + lib/jsonrpc/impl/proxy.c + lib/jsonrpc/impl/server.c + lib/jsonrpc/impl/method.c + lib/jsonrpc/impl/request.c + lib/jsonrpc/impl/response.c + lib/jsonrpc/impl/error.c ) target_include_directories(jsonrpc PUBLIC lib) diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index 43c6a4f..37ef85f 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -14,6 +14,7 @@ add_executable(alltests test/jsonrpc/test_response.cc test/jsonrpc/test_server.cc test/jsonrpc/test_proxy.cc + test/jsonrpc/test_response_parser.cc test/webfuse/utils/tempdir.cc test/webfuse/utils/file_utils.cc test/webfuse/utils/msleep.cc @@ -32,7 +33,6 @@ add_executable(alltests test/webfuse/tests/core/test_status.cc test/webfuse/tests/core/test_message.cc test/webfuse/tests/core/test_message_queue.cc - test/webfuse/tests/adapter/test_response_parser.cc test/webfuse/tests/adapter/test_server.cc test/webfuse/tests/adapter/test_server_config.cc test/webfuse/tests/adapter/test_timepoint.cc diff --git a/include/jsonrpc.h b/include/jsonrpc.h new file mode 100644 index 0000000..b0e2999 --- /dev/null +++ b/include/jsonrpc.h @@ -0,0 +1,14 @@ +#ifndef JSONRPC_H +#define JSONRPC_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif diff --git a/include/jsonrpc/api.h b/include/jsonrpc/api.h new file mode 100644 index 0000000..41b8563 --- /dev/null +++ b/include/jsonrpc/api.h @@ -0,0 +1,8 @@ +#ifndef JSONRPC_API_H +#define JSONRPC_API_H + +#ifndef JSONRPC_API +#define JSONRPC_API +#endif + +#endif diff --git a/include/jsonrpc/method_invoke_fn.h b/include/jsonrpc/method_invoke_fn.h new file mode 100644 index 0000000..96fecea --- /dev/null +++ b/include/jsonrpc/method_invoke_fn.h @@ -0,0 +1,26 @@ +#ifndef JSONRPC_METHOD_INVOKE_FN_H +#define JSONRPC_METHOD_INVOKE_FN_H + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct jsonrpc_request; + +typedef void jsonrpc_method_invoke_fn( + struct jsonrpc_request * request, + char const * method_name, + json_t * params, + void * user_data); + + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/include/jsonrpc/proxy.h b/include/jsonrpc/proxy.h index eb44991..d433253 100644 --- a/include/jsonrpc/proxy.h +++ b/include/jsonrpc/proxy.h @@ -12,7 +12,10 @@ using std::size_t; #endif #include -#include "jsonrpc/send_fn.h" +#include +#include +#include + #include "webfuse/adapter/impl/time/timeout_manager.h" #include "webfuse/adapter/impl/time/timer.h" @@ -20,40 +23,20 @@ using std::size_t; extern "C" { #endif -typedef void jsonrpc_proxy_finished_fn( - void * user_data, - json_t const * result, - json_t const * error); +struct jsonrpc_proxy; - -struct jsonrpc_request -{ - bool is_pending; - jsonrpc_proxy_finished_fn * finished; - void * user_data; - int id; - struct wf_impl_timer timer; -}; - -struct jsonrpc_proxy -{ - struct jsonrpc_request request; - int timeout; - jsonrpc_send_fn * send; - void * user_data; -}; - -extern void jsonrpc_proxy_init( - struct jsonrpc_proxy * proxy, +extern JSONRPC_API struct jsonrpc_proxy * +jsonrpc_proxy_create( struct wf_impl_timeout_manager * manager, int timeout, jsonrpc_send_fn * send, void * user_data); -extern void jsonrpc_proxy_cleanup( +extern JSONRPC_API void jsonrpc_proxy_dispose( struct jsonrpc_proxy * proxy); -extern void jsonrpc_proxy_invoke( + +extern JSONRPC_API void jsonrpc_proxy_invoke( struct jsonrpc_proxy * proxy, jsonrpc_proxy_finished_fn * finished, void * user_data, @@ -62,14 +45,14 @@ extern void jsonrpc_proxy_invoke( ... ); -extern void jsonrpc_proxy_notify( +extern JSONRPC_API void jsonrpc_proxy_notify( struct jsonrpc_proxy * proxy, char const * method_name, char const * param_info, ... ); -extern void jsonrpc_proxy_onresult( +extern JSONRPC_API void jsonrpc_proxy_onresult( struct jsonrpc_proxy * proxy, json_t * message); diff --git a/include/jsonrpc/proxy_finished_fn.h b/include/jsonrpc/proxy_finished_fn.h new file mode 100644 index 0000000..4c96b2b --- /dev/null +++ b/include/jsonrpc/proxy_finished_fn.h @@ -0,0 +1,20 @@ +#ifndef JSONRPC_PROXY_FINISHED_FN_H +#define JSONRPC_PROXY_FINISHED_FN_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef void jsonrpc_proxy_finished_fn( + void * user_data, + json_t const * result, + json_t const * error); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/jsonrpc/request.h b/include/jsonrpc/request.h index 496a4e0..8c5cbae 100644 --- a/include/jsonrpc/request.h +++ b/include/jsonrpc/request.h @@ -12,7 +12,7 @@ using std::size_t; #endif #include -#include "webfuse/core/status.h" +#include #include "jsonrpc/send_fn.h" #ifdef __cplusplus @@ -22,27 +22,28 @@ extern "C" struct jsonrpc_request; -extern bool jsonrpc_is_request( +extern JSONRPC_API bool jsonrpc_is_request( json_t * message); -extern struct jsonrpc_request * jsonrpc_request_create( +extern JSONRPC_API struct jsonrpc_request * jsonrpc_request_create( int id, jsonrpc_send_fn * send, void * user_data); -extern void jsonrpc_request_dispose( +extern JSONRPC_API void jsonrpc_request_dispose( struct jsonrpc_request * request); -extern void * jsonrpc_request_get_userdata( +extern JSONRPC_API void * jsonrpc_request_get_userdata( struct jsonrpc_request * request); -extern void jsonrpc_respond( +extern JSONRPC_API void jsonrpc_respond( struct jsonrpc_request * request, json_t * result); -extern void jsonrpc_respond_error( +extern JSONRPC_API void jsonrpc_respond_error( struct jsonrpc_request * request, - wf_status status); + int code, + char const * message); #ifdef __cplusplus diff --git a/include/jsonrpc/response.h b/include/jsonrpc/response.h index fbdaa7e..64414ec 100644 --- a/include/jsonrpc/response.h +++ b/include/jsonrpc/response.h @@ -1,40 +1,23 @@ -#ifndef WF_JSONRPC_RESPONSE_H -#define WF_JSONRPC_RESPONSE_H +#ifndef JSONRPC_RESPONSE_H +#define JSONRPC_RESPONSE_H #ifndef __cplusplus #include -#include -#else -#include -using std::size_t; #endif #include +#include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -struct jsonrpc_response -{ - json_t * result; - json_t * error; - int id; -}; - -extern bool jsonrpc_is_response( +extern JSONRPC_API bool jsonrpc_is_response( json_t * message); -extern void jsonrpc_response_init( - struct jsonrpc_response * response, - json_t * message); - -extern void jsonrpc_response_cleanup( - struct jsonrpc_response * response); - #ifdef __cplusplus } #endif #endif - diff --git a/include/jsonrpc/send_fn.h b/include/jsonrpc/send_fn.h index b25bdbb..fe866af 100644 --- a/include/jsonrpc/send_fn.h +++ b/include/jsonrpc/send_fn.h @@ -6,6 +6,7 @@ #endif #include +#include #ifdef __cplusplus extern "C" diff --git a/include/jsonrpc/server.h b/include/jsonrpc/server.h index 778a7e2..7c215ec 100644 --- a/include/jsonrpc/server.h +++ b/include/jsonrpc/server.h @@ -9,32 +9,31 @@ #endif #include -#include "jsonrpc/send_fn.h" -#include "jsonrpc/method.h" +#include +#include +#include #ifdef __cplusplus extern "C" { #endif -struct jsonrpc_server -{ - struct jsonrpc_method * methods; -}; +struct jsonrpc_server; -extern void jsonrpc_server_init( +extern JSONRPC_API struct jsonrpc_server * +jsonrpc_server_create(void); + +extern JSONRPC_API void +jsonrpc_server_dispose( struct jsonrpc_server * server); -extern void jsonrpc_server_cleanup( - struct jsonrpc_server * server); - -extern void jsonrpc_server_add( +extern JSONRPC_API void jsonrpc_server_add( struct jsonrpc_server * server, char const * method_name, jsonrpc_method_invoke_fn * invoke, void * user_data); -extern void jsonrpc_server_process( +extern JSONRPC_API void jsonrpc_server_process( struct jsonrpc_server * server, json_t * request, jsonrpc_send_fn * send, diff --git a/include/jsonrpc/status.h b/include/jsonrpc/status.h index b7368b3..905a161 100644 --- a/include/jsonrpc/status.h +++ b/include/jsonrpc/status.h @@ -3,6 +3,7 @@ #define JSONRPC_GOOD 0 #define JSONRPC_BAD -1 +#define JSONRPC_BAD_NOTIMPLEMENTED -2 #define JSONRPC_BAD_TIMEOUT -3 #define JSONRPC_BAD_BUSY -4 #define JSONRPC_BAD_FORMAT -5 diff --git a/lib/jsonrpc/api.c b/lib/jsonrpc/api.c new file mode 100644 index 0000000..a6d7d1c --- /dev/null +++ b/lib/jsonrpc/api.c @@ -0,0 +1,144 @@ +#include "jsonrpc.h" + +#include "jsonrpc/impl/proxy.h" +#include "jsonrpc/impl/request.h" +#include "jsonrpc/impl/response.h" +#include "jsonrpc/impl/server.h" + +// proxy + +struct jsonrpc_proxy * +jsonrpc_proxy_create( + struct wf_impl_timeout_manager * manager, + int timeout, + jsonrpc_send_fn * send, + void * user_data) +{ + return jsonrpc_impl_proxy_create(manager, timeout, send, user_data); +} + +void jsonrpc_proxy_dispose( + struct jsonrpc_proxy * proxy) +{ + jsonrpc_impl_proxy_dispose(proxy); +} + +void jsonrpc_proxy_invoke( + struct jsonrpc_proxy * proxy, + jsonrpc_proxy_finished_fn * finished, + void * user_data, + char const * method_name, + char const * param_info, + ... +) +{ + va_list args; + va_start(args, param_info); + jsonrpc_impl_proxy_invoke(proxy, finished, user_data, method_name, param_info, args); + va_end(args); +} + +void jsonrpc_proxy_notify( + struct jsonrpc_proxy * proxy, + char const * method_name, + char const * param_info, + ... +) +{ + va_list args; + va_start(args, param_info); + jsonrpc_impl_proxy_notify(proxy, method_name, param_info, args); + va_end(args); +} + +void jsonrpc_proxy_onresult( + struct jsonrpc_proxy * proxy, + json_t * message) +{ + jsonrpc_impl_proxy_onresult(proxy, message); +} + + +// request + +bool jsonrpc_is_request( + json_t * message) +{ + return jsonrpc_impl_is_request(message); +} + +struct jsonrpc_request * jsonrpc_request_create( + int id, + jsonrpc_send_fn * send, + void * user_data) +{ + return jsonrpc_impl_request_create(id, send, user_data); +} + +void jsonrpc_request_dispose( + struct jsonrpc_request * request) +{ + jsonrpc_impl_request_dispose(request); +} + +void * jsonrpc_request_get_userdata( + struct jsonrpc_request * request) +{ + return jsonrpc_impl_request_get_userdata(request); +} + +void jsonrpc_respond( + struct jsonrpc_request * request, + json_t * result) +{ + jsonrpc_impl_respond(request, result); +} + +void jsonrpc_respond_error( + struct jsonrpc_request * request, + int code, + char const * message) +{ + jsonrpc_impl_respond_error(request, code, message); +} + +// response + +bool jsonrpc_is_response( + json_t * message) +{ + return jsonrpc_impl_is_response(message); +} + +// server + +struct jsonrpc_server * +jsonrpc_server_create(void) +{ + return jsonrpc_impl_server_create(); +} + +void +jsonrpc_server_dispose( + struct jsonrpc_server * server) +{ + jsonrpc_impl_server_dispose(server); +} + +void jsonrpc_server_add( + struct jsonrpc_server * server, + char const * method_name, + jsonrpc_method_invoke_fn * invoke, + void * user_data) +{ + jsonrpc_impl_server_add(server, method_name, invoke, user_data); +} + +void jsonrpc_server_process( + struct jsonrpc_server * server, + json_t * request, + jsonrpc_send_fn * send, + void * user_data) +{ + jsonrpc_impl_server_process(server, request, send, user_data); +} diff --git a/lib/jsonrpc/error.c b/lib/jsonrpc/impl/error.c similarity index 75% rename from lib/jsonrpc/error.c rename to lib/jsonrpc/impl/error.c index 7613766..716f6de 100644 --- a/lib/jsonrpc/error.c +++ b/lib/jsonrpc/impl/error.c @@ -1,7 +1,7 @@ -#include "jsonrpc/error.h" +#include "jsonrpc/impl/error.h" json_t * -jsonrpc_error( +jsonrpc_impl_error( int code, char const * message) { @@ -13,13 +13,13 @@ jsonrpc_error( } void -jsonrpc_propate_error( +jsonrpc_impl_propate_error( jsonrpc_proxy_finished_fn * finised, void * user_data, int code, char const * message) { - json_t * error = jsonrpc_error(code, message); + json_t * error = jsonrpc_impl_error(code, message); finised(user_data, NULL, error); json_decref(error); diff --git a/lib/jsonrpc/error.h b/lib/jsonrpc/impl/error.h similarity index 65% rename from lib/jsonrpc/error.h rename to lib/jsonrpc/impl/error.h index 38bbcd2..6485f05 100644 --- a/lib/jsonrpc/error.h +++ b/lib/jsonrpc/impl/error.h @@ -1,8 +1,8 @@ -#ifndef JSONRPC_ERROR_H -#define JSONRPC_ERROR_H +#ifndef JSONRPC_IMPL_ERROR_H +#define JSONRPC_IMPL_ERROR_H #include -#include "jsonrpc/proxy.h" +#include "jsonrpc/proxy_finished_fn.h" #ifdef __cplusplus extern "C" @@ -10,12 +10,12 @@ extern "C" #endif extern json_t * -jsonrpc_error( +jsonrpc_impl_error( int code, char const * message); extern void -jsonrpc_propate_error( +jsonrpc_impl_propate_error( jsonrpc_proxy_finished_fn * finised, void * user_data, int code, diff --git a/lib/jsonrpc/method.c b/lib/jsonrpc/impl/method.c similarity index 80% rename from lib/jsonrpc/method.c rename to lib/jsonrpc/impl/method.c index 31414d4..073e8b3 100644 --- a/lib/jsonrpc/method.c +++ b/lib/jsonrpc/impl/method.c @@ -1,8 +1,8 @@ -#include "jsonrpc/method.h" +#include "jsonrpc/impl/method.h" #include #include -struct jsonrpc_method * jsonrpc_method_create( +struct jsonrpc_method * jsonrpc_impl_method_create( char const * method_name, jsonrpc_method_invoke_fn * invoke, void * user_data) @@ -19,7 +19,7 @@ struct jsonrpc_method * jsonrpc_method_create( return method; } -void jsonrpc_method_dispose( +void jsonrpc_impl_method_dispose( struct jsonrpc_method * method) { free(method->name); diff --git a/include/jsonrpc/method.h b/lib/jsonrpc/impl/method.h similarity index 50% rename from include/jsonrpc/method.h rename to lib/jsonrpc/impl/method.h index 8e9cb3a..595d35d 100644 --- a/include/jsonrpc/method.h +++ b/lib/jsonrpc/impl/method.h @@ -1,21 +1,13 @@ -#ifndef JSONRPC_METHOD_H -#define JSONRPC_METHOD_H +#ifndef JSONRPC_IMPL_METHOD_H +#define JSONRPC_IMPL_METHOD_H -#include +#include "jsonrpc/method_invoke_fn.h" #ifdef __cplusplus extern "C" { #endif -struct jsonrpc_request; - -typedef void jsonrpc_method_invoke_fn( - struct jsonrpc_request * request, - char const * method_name, - json_t * params, - void * user_data); - struct jsonrpc_method { struct jsonrpc_method * next; @@ -24,12 +16,14 @@ struct jsonrpc_method void * user_data; }; -extern struct jsonrpc_method * jsonrpc_method_create( +extern struct jsonrpc_method * +jsonrpc_impl_method_create( char const * method_name, jsonrpc_method_invoke_fn * invoke, void * user_data); -extern void jsonrpc_method_dispose( +extern void +jsonrpc_impl_method_dispose( struct jsonrpc_method * method); #ifdef __cplusplus diff --git a/lib/jsonrpc/proxy.c b/lib/jsonrpc/impl/proxy.c similarity index 71% rename from lib/jsonrpc/proxy.c rename to lib/jsonrpc/impl/proxy.c index b12571e..446cd91 100644 --- a/lib/jsonrpc/proxy.c +++ b/lib/jsonrpc/impl/proxy.c @@ -1,11 +1,35 @@ -#include "jsonrpc/proxy.h" -#include "jsonrpc/response.h" -#include "jsonrpc/error.h" +#include "jsonrpc/impl/proxy.h" +#include "jsonrpc/impl/response.h" +#include "jsonrpc/impl/error.h" #include "jsonrpc/status.h" +#include #include -static void jsonrpc_proxy_timeout( +struct jsonrpc_proxy * +jsonrpc_impl_proxy_create( + struct wf_impl_timeout_manager * manager, + int timeout, + jsonrpc_send_fn * send, + void * user_data) +{ + struct jsonrpc_proxy * proxy = malloc(sizeof(struct jsonrpc_proxy)); + if (NULL != proxy) + { + jsonrpc_impl_proxy_init(proxy, manager, timeout, send, user_data); + } + + return proxy; +} + +void jsonrpc_impl_proxy_dispose( + struct jsonrpc_proxy * proxy) +{ + jsonrpc_impl_proxy_cleanup(proxy); + free(proxy); +} + +static void jsonrpc_impl_proxy_timeout( struct wf_impl_timer * timer) { struct jsonrpc_proxy * proxy = timer->user_data; @@ -21,11 +45,11 @@ static void jsonrpc_proxy_timeout( proxy->request.finished = NULL; wf_impl_timer_cancel(&proxy->request.timer); - jsonrpc_propate_error(finished, user_data, JSONRPC_BAD_TIMEOUT, "Timeout"); + jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_TIMEOUT, "Timeout"); } } -static json_t * jsonrpc_request_create( +static json_t * jsonrpc_impl_request_create( char const * method, int id, char const * param_info, @@ -69,7 +93,7 @@ static json_t * jsonrpc_request_create( return request; } -void jsonrpc_proxy_init( +void jsonrpc_impl_proxy_init( struct jsonrpc_proxy * proxy, struct wf_impl_timeout_manager * timeout_manager, int timeout, @@ -84,7 +108,7 @@ void jsonrpc_proxy_init( wf_impl_timer_init(&proxy->request.timer, timeout_manager); } -void jsonrpc_proxy_cleanup( +void jsonrpc_impl_proxy_cleanup( struct jsonrpc_proxy * proxy) { if (proxy->request.is_pending) @@ -98,19 +122,19 @@ void jsonrpc_proxy_cleanup( proxy->request.id = 0; wf_impl_timer_cancel(&proxy->request.timer); - jsonrpc_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); + jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); } wf_impl_timer_cleanup(&proxy->request.timer); } -void jsonrpc_proxy_invoke( +void jsonrpc_impl_proxy_invoke( struct jsonrpc_proxy * proxy, jsonrpc_proxy_finished_fn * finished, void * user_data, char const * method_name, char const * param_info, - ... + va_list args ) { if (!proxy->request.is_pending) @@ -120,12 +144,9 @@ void jsonrpc_proxy_invoke( proxy->request.user_data = user_data; proxy->request.id = 42; wf_impl_timer_start(&proxy->request.timer, wf_impl_timepoint_in_msec(proxy->timeout), - &jsonrpc_proxy_timeout, proxy); + &jsonrpc_impl_proxy_timeout, proxy); - va_list args; - va_start(args, param_info); - json_t * request = jsonrpc_request_create(method_name, proxy->request.id, param_info, args); - va_end(args); + json_t * request = jsonrpc_impl_request_create(method_name, proxy->request.id, param_info, args); bool const is_send = ((NULL != request) && (proxy->send(request, proxy->user_data))); if (!is_send) @@ -136,7 +157,7 @@ void jsonrpc_proxy_invoke( proxy->request.id = 0; wf_impl_timer_cancel(&proxy->request.timer); - jsonrpc_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); + jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); } if (NULL != request) @@ -146,21 +167,18 @@ void jsonrpc_proxy_invoke( } else { - jsonrpc_propate_error(finished, user_data, JSONRPC_BAD_BUSY, "Busy"); + jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_BUSY, "Busy"); } } -extern void jsonrpc_proxy_notify( +extern void jsonrpc_impl_proxy_notify( struct jsonrpc_proxy * proxy, char const * method_name, char const * param_info, - ... + va_list args ) { - va_list args; - va_start(args, param_info); - json_t * request = jsonrpc_request_create(method_name, 0, param_info, args); - va_end(args); + json_t * request = jsonrpc_impl_request_create(method_name, 0, param_info, args); if (NULL != request) { proxy->send(request, proxy->user_data); @@ -169,12 +187,12 @@ extern void jsonrpc_proxy_notify( } -void jsonrpc_proxy_onresult( +void jsonrpc_impl_proxy_onresult( struct jsonrpc_proxy * proxy, json_t * message) { struct jsonrpc_response response; - jsonrpc_response_init(&response, message); + jsonrpc_impl_response_init(&response, message); if ((proxy->request.is_pending) && (response.id == proxy->request.id)) { @@ -190,6 +208,6 @@ void jsonrpc_proxy_onresult( finished(user_data, response.result, response.error); } - jsonrpc_response_cleanup(&response); + jsonrpc_impl_response_cleanup(&response); } diff --git a/lib/jsonrpc/impl/proxy.h b/lib/jsonrpc/impl/proxy.h new file mode 100644 index 0000000..0556e0d --- /dev/null +++ b/lib/jsonrpc/impl/proxy.h @@ -0,0 +1,84 @@ +#ifndef JSONRPC_IMPL_PROXY_H +#define JSONRPC_IMPL_PROXY_H + +#include "jsonrpc/proxy_finished_fn.h" +#include "jsonrpc/send_fn.h" + +#include "webfuse/adapter/impl/time/timeout_manager.h" +#include "webfuse/adapter/impl/time/timer.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct jsonrpc_request +{ + bool is_pending; + jsonrpc_proxy_finished_fn * finished; + void * user_data; + int id; + struct wf_impl_timer timer; +}; + +struct jsonrpc_proxy +{ + struct jsonrpc_request request; + int timeout; + jsonrpc_send_fn * send; + void * user_data; +}; + +extern void +jsonrpc_impl_proxy_init( + struct jsonrpc_proxy * proxy, + struct wf_impl_timeout_manager * manager, + int timeout, + jsonrpc_send_fn * send, + void * user_data); + +extern void +jsonrpc_impl_proxy_cleanup( + struct jsonrpc_proxy * proxy); + +extern struct jsonrpc_proxy * +jsonrpc_impl_proxy_create( + struct wf_impl_timeout_manager * manager, + int timeout, + jsonrpc_send_fn * send, + void * user_data); + +extern void +jsonrpc_impl_proxy_dispose( + struct jsonrpc_proxy * proxy); + + +extern void +jsonrpc_impl_proxy_invoke( + struct jsonrpc_proxy * proxy, + jsonrpc_proxy_finished_fn * finished, + void * user_data, + char const * method_name, + char const * param_info, + va_list args +); + +extern void +jsonrpc_impl_proxy_notify( + struct jsonrpc_proxy * proxy, + char const * method_name, + char const * param_info, + va_list args +); + +extern void +jsonrpc_impl_proxy_onresult( + struct jsonrpc_proxy * proxy, + json_t * message); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/jsonrpc/request.c b/lib/jsonrpc/impl/request.c similarity index 70% rename from lib/jsonrpc/request.c rename to lib/jsonrpc/impl/request.c index 8790ce9..91ffd96 100644 --- a/lib/jsonrpc/request.c +++ b/lib/jsonrpc/impl/request.c @@ -1,5 +1,5 @@ -#include "jsonrpc/request.h" -#include "webfuse/core/status_intern.h" +#include "jsonrpc/impl/request.h" +#include "jsonrpc/impl/error.h" #include struct jsonrpc_request @@ -9,7 +9,8 @@ struct jsonrpc_request void * user_data; }; -bool jsonrpc_is_request( +bool +jsonrpc_impl_is_request( json_t * message) { json_t * id = json_object_get(message, "id"); @@ -21,7 +22,8 @@ bool jsonrpc_is_request( } -struct jsonrpc_request * jsonrpc_request_create( +struct jsonrpc_request * +jsonrpc_impl_request_create( int id, jsonrpc_send_fn * send, void * user_data) @@ -37,20 +39,23 @@ struct jsonrpc_request * jsonrpc_request_create( return request; } -void jsonrpc_request_dispose( +void +jsonrpc_impl_request_dispose( struct jsonrpc_request * request) { free(request); } -void * jsonrpc_request_get_userdata( +void * +jsonrpc_impl_request_get_userdata( struct jsonrpc_request * request) { return request->user_data; } -void jsonrpc_respond( +void +jsonrpc_impl_respond( struct jsonrpc_request * request, json_t * result) { @@ -60,23 +65,20 @@ void jsonrpc_respond( request->send(response, request->user_data); json_decref(response); - jsonrpc_request_dispose(request); + jsonrpc_impl_request_dispose(request); } -void jsonrpc_respond_error( +void jsonrpc_impl_respond_error( struct jsonrpc_request * request, - wf_status status) + int code, + char const * message) { - json_t * err = json_object(); - json_object_set_new(err, "code", json_integer(status)); - json_object_set_new(err, "message", json_string(wf_status_tostring(status))); - json_t * response = json_object(); - json_object_set_new(response, "error", err); + json_object_set_new(response, "error", jsonrpc_impl_error(code, message)); json_object_set_new(response, "id", json_integer(request->id)); request->send(response, request->user_data); json_decref(response); - jsonrpc_request_dispose(request); + jsonrpc_impl_request_dispose(request); } diff --git a/lib/jsonrpc/impl/request.h b/lib/jsonrpc/impl/request.h new file mode 100644 index 0000000..234a508 --- /dev/null +++ b/lib/jsonrpc/impl/request.h @@ -0,0 +1,53 @@ +#ifndef JSONRPC_IMPL_REQUEST_H +#define JSONRPC_IMPL_REQUEST_H + +#ifndef __cplusplus +#include +#include +#include +#else +#include +#include +using std::size_t; +#endif + +#include +#include +#include "jsonrpc/send_fn.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct jsonrpc_request; + +extern bool jsonrpc_impl_is_request( + json_t * message); + +extern struct jsonrpc_request * jsonrpc_impl_request_create( + int id, + jsonrpc_send_fn * send, + void * user_data); + +extern void jsonrpc_impl_request_dispose( + struct jsonrpc_request * request); + +extern void * jsonrpc_impl_request_get_userdata( + struct jsonrpc_request * request); + +extern void jsonrpc_impl_respond( + struct jsonrpc_request * request, + json_t * result); + +extern void jsonrpc_impl_respond_error( + struct jsonrpc_request * request, + int code, + char const * message); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/jsonrpc/response.c b/lib/jsonrpc/impl/response.c similarity index 76% rename from lib/jsonrpc/response.c rename to lib/jsonrpc/impl/response.c index c11bbc7..9f3d04e 100644 --- a/lib/jsonrpc/response.c +++ b/lib/jsonrpc/impl/response.c @@ -1,8 +1,9 @@ -#include "jsonrpc/response.h" -#include "jsonrpc/error.h" +#include "jsonrpc/impl/response.h" +#include "jsonrpc/impl/error.h" #include "jsonrpc/status.h" -extern bool jsonrpc_is_response( +bool +jsonrpc_impl_is_response( json_t * message) { json_t * id = json_object_get(message, "id"); @@ -14,7 +15,8 @@ extern bool jsonrpc_is_response( } -void jsonrpc_response_init( +void +jsonrpc_impl_response_init( struct jsonrpc_response * result, json_t * response) { @@ -25,7 +27,7 @@ void jsonrpc_response_init( json_t * id_holder = json_object_get(response, "id"); if ((NULL == id_holder) || (!json_is_integer(id_holder))) { - result->error = jsonrpc_error(JSONRPC_BAD_FORMAT, "invalid format: missing id"); + result->error = jsonrpc_impl_error(JSONRPC_BAD_FORMAT, "invalid format: missing id"); return; } @@ -45,12 +47,13 @@ void jsonrpc_response_init( } else { - result->error = jsonrpc_error(JSONRPC_BAD_FORMAT, "invalid format: invalid error object"); + result->error = jsonrpc_impl_error(JSONRPC_BAD_FORMAT, "invalid format: invalid error object"); } } } -void jsonrpc_response_cleanup( +void +jsonrpc_impl_response_cleanup( struct jsonrpc_response * response) { if (NULL != response->result) diff --git a/lib/jsonrpc/impl/response.h b/lib/jsonrpc/impl/response.h new file mode 100644 index 0000000..b39e30e --- /dev/null +++ b/lib/jsonrpc/impl/response.h @@ -0,0 +1,40 @@ +#ifndef JSONRPC_IMPL_RESPONSE_H +#define JSONRPC_IMPL_RESPONSE_H + +#ifndef __cplusplus +#include +#include +#else +#include +using std::size_t; +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct jsonrpc_response +{ + json_t * result; + json_t * error; + int id; +}; + +extern bool jsonrpc_impl_is_response( + json_t * message); + +extern void jsonrpc_impl_response_init( + struct jsonrpc_response * response, + json_t * message); + +extern void jsonrpc_impl_response_cleanup( + struct jsonrpc_response * response); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/lib/jsonrpc/impl/server.c b/lib/jsonrpc/impl/server.c new file mode 100644 index 0000000..7da42a3 --- /dev/null +++ b/lib/jsonrpc/impl/server.c @@ -0,0 +1,132 @@ +#include "jsonrpc/impl/server.h" +#include "jsonrpc/impl/method.h" +#include "jsonrpc/impl/request.h" +#include "jsonrpc/impl/unused_param.h" +#include "jsonrpc/status.h" + +#include +#include + +struct jsonrpc_server +{ + struct jsonrpc_method * methods; +}; + +static void +jsonrpc_impl_server_init( + struct jsonrpc_server * server); + +static void +jsonrpc_impl_server_cleanup( + struct jsonrpc_server * server); + +struct jsonrpc_server * +jsonrpc_impl_server_create(void) +{ + struct jsonrpc_server * server = malloc(sizeof(struct jsonrpc_server)); + if (NULL != server) + { + jsonrpc_impl_server_init(server); + } + + return server; +} + +void +jsonrpc_impl_server_dispose( + struct jsonrpc_server * server) +{ + jsonrpc_impl_server_cleanup(server); + free(server); +} + + +static void jsonrpc_impl_server_init( + struct jsonrpc_server * server) +{ + server->methods = NULL; +} + +static void jsonrpc_impl_server_cleanup( + struct jsonrpc_server * server) +{ + struct jsonrpc_method * current = server->methods; + while (NULL != current) + { + struct jsonrpc_method * next = current->next; + jsonrpc_impl_method_dispose(current); + current = next; + } + server->methods = NULL; +} + +void jsonrpc_impl_server_add( + struct jsonrpc_server * server, + char const * method_name, + jsonrpc_method_invoke_fn * invoke, + void * user_data) +{ + struct jsonrpc_method * method = jsonrpc_impl_method_create(method_name, invoke, user_data); + method->next = server->methods; + server->methods = method; +} + +static void jsonrpc_impl_server_invalid_method_invoke( + struct jsonrpc_request * request, + char const * JSONRPC_UNUSED_PARAM(method_name), + json_t * JSONRPC_UNUSED_PARAM(params), + void * JSONRPC_UNUSED_PARAM(user_data)) +{ + jsonrpc_impl_respond_error(request, JSONRPC_BAD_NOTIMPLEMENTED, "not implemented"); +} + +static struct jsonrpc_method const jsonrpc_impl_server_invalid_method = +{ + .next = NULL, + .name = "", + .invoke = &jsonrpc_impl_server_invalid_method_invoke, + .user_data = NULL +}; + +static struct jsonrpc_method const * +jsonrpc_impl_server_get_method( + struct jsonrpc_server * server, + char const * method_name) +{ + struct jsonrpc_method const * current = server->methods; + while (NULL != current) + { + if (0 == strcmp(method_name, current->name)) + { + return current; + } + + current = current->next; + } + + return &jsonrpc_impl_server_invalid_method; +} + +void jsonrpc_impl_server_process( + struct jsonrpc_server * server, + json_t * request_data, + jsonrpc_send_fn * send, + void * user_data) +{ + json_t * method_holder = json_object_get(request_data, "method"); + json_t * params = json_object_get(request_data, "params"); + json_t * id_holder = json_object_get(request_data, "id"); + + if (json_is_string(method_holder) && + (json_is_array(params) || (json_is_object(params))) && + json_is_integer(id_holder)) + { + char const * method_name = json_string_value(method_holder); + int id = json_integer_value(id_holder); + struct jsonrpc_request * request = jsonrpc_impl_request_create(id, send, user_data); + struct jsonrpc_method const * method = jsonrpc_impl_server_get_method(server, method_name); + + method->invoke(request, method_name, params, method->user_data); + } +} + diff --git a/lib/jsonrpc/impl/server.h b/lib/jsonrpc/impl/server.h new file mode 100644 index 0000000..6263c66 --- /dev/null +++ b/lib/jsonrpc/impl/server.h @@ -0,0 +1,41 @@ +#ifndef JSONRPC_IMPL_SERVER_H +#define JSONRPC_IMPL_SERVER_H + +#include +#include "jsonrpc/method_invoke_fn.h" +#include "jsonrpc/send_fn.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct jsonrpc_server; + +extern struct jsonrpc_server * +jsonrpc_impl_server_create(void); + +extern void +jsonrpc_impl_server_dispose( + struct jsonrpc_server * server); + +extern void +jsonrpc_impl_server_add( + struct jsonrpc_server * server, + char const * method_name, + jsonrpc_method_invoke_fn * invoke, + void * user_data); + +extern void +jsonrpc_impl_server_process( + struct jsonrpc_server * server, + json_t * request, + jsonrpc_send_fn * send, + void * user_data); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/lib/jsonrpc/impl/unused_param.h b/lib/jsonrpc/impl/unused_param.h new file mode 100644 index 0000000..cde12d0 --- /dev/null +++ b/lib/jsonrpc/impl/unused_param.h @@ -0,0 +1,10 @@ +#ifndef JSONRPC_UTIL_H +#define JSONRPC_UTIL_H + +#ifdef __GNUC__ +#define JSONRPC_UNUSED_PARAM(param) param __attribute__((unused)) +#else +#define JSONRPC_UNUSED_PARAM(param) +#endif + +#endif diff --git a/lib/jsonrpc/server.c b/lib/jsonrpc/server.c deleted file mode 100644 index 6e62c3b..0000000 --- a/lib/jsonrpc/server.c +++ /dev/null @@ -1,95 +0,0 @@ -#include "jsonrpc/server.h" -#include "jsonrpc/method.h" -#include "jsonrpc/request.h" -#include "webfuse/core/util.h" - -#include - -void jsonrpc_server_init( - struct jsonrpc_server * server) -{ - server->methods = NULL; -} - -void jsonrpc_server_cleanup( - struct jsonrpc_server * server) -{ - struct jsonrpc_method * current = server->methods; - while (NULL != current) - { - struct jsonrpc_method * next = current->next; - jsonrpc_method_dispose(current); - current = next; - } - server->methods = NULL; -} - -void jsonrpc_server_add( - struct jsonrpc_server * server, - char const * method_name, - jsonrpc_method_invoke_fn * invoke, - void * user_data) -{ - struct jsonrpc_method * method = jsonrpc_method_create(method_name, invoke, user_data); - method->next = server->methods; - server->methods = method; -} - -static void jsonrpc_server_invalid_method_invoke( - struct jsonrpc_request * request, - char const * WF_UNUSED_PARAM(method_name), - json_t * WF_UNUSED_PARAM(params), - void * WF_UNUSED_PARAM(user_data)) -{ - jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED); -} - -static struct jsonrpc_method const jsonrpc_server_invalid_method = -{ - .next = NULL, - .name = "", - .invoke = &jsonrpc_server_invalid_method_invoke, - .user_data = NULL -}; - -static struct jsonrpc_method const * jsonrpc_server_get_method( - struct jsonrpc_server * server, - char const * method_name) -{ - struct jsonrpc_method const * current = server->methods; - while (NULL != current) - { - if (0 == strcmp(method_name, current->name)) - { - return current; - } - - current = current->next; - } - - return &jsonrpc_server_invalid_method; -} - -void jsonrpc_server_process( - struct jsonrpc_server * server, - json_t * request_data, - jsonrpc_send_fn * send, - void * user_data) -{ - json_t * method_holder = json_object_get(request_data, "method"); - json_t * params = json_object_get(request_data, "params"); - json_t * id_holder = json_object_get(request_data, "id"); - - if (json_is_string(method_holder) && - (json_is_array(params) || (json_is_object(params))) && - json_is_integer(id_holder)) - { - char const * method_name = json_string_value(method_holder); - int id = json_integer_value(id_holder); - struct jsonrpc_request * request = jsonrpc_request_create(id, send, user_data); - struct jsonrpc_method const * method = jsonrpc_server_get_method(server, method_name); - - method->invoke(request, method_name, params, method->user_data); - } -} - diff --git a/lib/webfuse/adapter/impl/operations.c b/lib/webfuse/adapter/impl/operations.c index ac9fb96..d3f9e96 100644 --- a/lib/webfuse/adapter/impl/operations.c +++ b/lib/webfuse/adapter/impl/operations.c @@ -11,7 +11,7 @@ struct jsonrpc_proxy * wf_impl_operations_context_get_proxy( struct wf_impl_session * session = context->session; if (NULL != session) { - proxy = &session->rpc; + proxy = session->rpc; } return proxy; diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index 746df5c..061fa5c 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -11,6 +11,7 @@ #include "webfuse/adapter/impl/credentials.h" #include "jsonrpc/request.h" #include "webfuse/adapter/impl/uuid_mountpoint_factory.h" +#include "webfuse/core/status_intern.h" static int wf_impl_server_protocol_callback( struct lws * wsi, @@ -42,7 +43,7 @@ static int wf_impl_server_protocol_callback( &protocol->authenticators, &protocol->mountpoint_factory, &protocol->timeout_manager, - &protocol->server); + protocol->server); if (NULL != session) { @@ -159,7 +160,7 @@ static void wf_impl_server_protocol_authenticate( } else { - jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED); + jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED, wf_status_tostring(WF_BAD_ACCESS_DENIED)); } } @@ -222,7 +223,7 @@ static void wf_impl_server_protocol_add_filesystem( } else { - jsonrpc_respond_error(request, status); + jsonrpc_respond_error(request, status, wf_status_tostring(status)); } @@ -240,9 +241,9 @@ void wf_impl_server_protocol_init( wf_impl_session_manager_init(&protocol->session_manager); wf_impl_authenticators_init(&protocol->authenticators); - jsonrpc_server_init(&protocol->server); - jsonrpc_server_add(&protocol->server, "authenticate", &wf_impl_server_protocol_authenticate, protocol); - jsonrpc_server_add(&protocol->server, "add_filesystem", &wf_impl_server_protocol_add_filesystem, protocol); + protocol->server = jsonrpc_server_create(); + jsonrpc_server_add(protocol->server, "authenticate", &wf_impl_server_protocol_authenticate, protocol); + jsonrpc_server_add(protocol->server, "add_filesystem", &wf_impl_server_protocol_add_filesystem, protocol); } void wf_impl_server_protocol_cleanup( @@ -250,7 +251,7 @@ void wf_impl_server_protocol_cleanup( { protocol->is_operational = false; - jsonrpc_server_cleanup(&protocol->server); + jsonrpc_server_dispose(protocol->server); wf_impl_timeout_manager_cleanup(&protocol->timeout_manager); wf_impl_authenticators_cleanup(&protocol->authenticators); wf_impl_session_manager_cleanup(&protocol->session_manager); diff --git a/lib/webfuse/adapter/impl/server_protocol.h b/lib/webfuse/adapter/impl/server_protocol.h index 85c94c1..0677c84 100644 --- a/lib/webfuse/adapter/impl/server_protocol.h +++ b/lib/webfuse/adapter/impl/server_protocol.h @@ -25,7 +25,7 @@ struct wf_server_protocol struct wf_impl_authenticators authenticators; struct wf_impl_mountpoint_factory mountpoint_factory; struct wf_impl_session_manager session_manager; - struct jsonrpc_server server; + struct jsonrpc_server * server; bool is_operational; }; diff --git a/lib/webfuse/adapter/impl/session.c b/lib/webfuse/adapter/impl/session.c index 5690c4f..deafb92 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -59,7 +59,7 @@ struct wf_impl_session * wf_impl_session_create( session->authenticators = authenticators; session->server = server; session->mountpoint_factory = mountpoint_factory; - jsonrpc_proxy_init(&session->rpc, timeout_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); + session->rpc = jsonrpc_proxy_create(timeout_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); wf_slist_init(&session->messages); } @@ -83,15 +83,10 @@ static void wf_impl_session_dispose_filesystems( void wf_impl_session_dispose( struct wf_impl_session * session) { - jsonrpc_proxy_cleanup(&session->rpc); + jsonrpc_proxy_dispose(session->rpc); wf_message_queue_cleanup(&session->messages); wf_impl_session_dispose_filesystems(&session->filesystems); - session->is_authenticated = false; - session->wsi = NULL; - session->authenticators = NULL; - session->mountpoint_factory = NULL; - session->server = NULL; free(session); } @@ -161,7 +156,7 @@ void wf_impl_session_receive( { if (jsonrpc_is_response(message)) { - jsonrpc_proxy_onresult(&session->rpc, message); + jsonrpc_proxy_onresult(session->rpc, message); } else if (jsonrpc_is_request(message)) { diff --git a/lib/webfuse/adapter/impl/session.h b/lib/webfuse/adapter/impl/session.h index 90f8f4e..cc95911 100644 --- a/lib/webfuse/adapter/impl/session.h +++ b/lib/webfuse/adapter/impl/session.h @@ -36,7 +36,7 @@ struct wf_impl_session struct wf_impl_authenticators * authenticators; struct wf_impl_mountpoint_factory * mountpoint_factory; struct jsonrpc_server * server; - struct jsonrpc_proxy rpc; + struct jsonrpc_proxy * rpc; struct wf_slist filesystems; }; diff --git a/test/jsonrpc/test_proxy.cc b/test/jsonrpc/test_proxy.cc index 79fb090..834a7ef 100644 --- a/test/jsonrpc/test_proxy.cc +++ b/test/jsonrpc/test_proxy.cc @@ -92,10 +92,9 @@ TEST(jsonrpc_proxy, init) SendContext context; void * user_data = reinterpret_cast(&context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); ASSERT_FALSE(context.is_called); @@ -108,12 +107,11 @@ TEST(jsonrpc_proxy, invoke) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -135,7 +133,7 @@ TEST(jsonrpc_proxy, invoke) ASSERT_FALSE(finished_context.is_called); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); ASSERT_TRUE(finished_context.is_called); @@ -149,12 +147,11 @@ TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails) SendContext send_context(false); void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -162,7 +159,7 @@ TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails) ASSERT_TRUE(finished_context.is_called); ASSERT_FALSE(nullptr == finished_context.error); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -173,16 +170,15 @@ TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); FinishedContext finished_context2; void * finished_data2 = reinterpret_cast(&finished_context2); - jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data2, "foo", ""); + jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", ""); ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(json_is_object(send_context.response)); @@ -192,7 +188,7 @@ TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending) ASSERT_TRUE(finished_context2.is_called); ASSERT_EQ(WF_BAD_BUSY, wf_impl_jsonrpc_get_status(finished_context2.error)); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -203,19 +199,18 @@ TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "?", "error"); + 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_BAD, wf_impl_jsonrpc_get_status(finished_context.error)); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -226,12 +221,11 @@ TEST(jsonrpc_proxy, on_result) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -243,7 +237,7 @@ TEST(jsonrpc_proxy, on_result) json_object_set_new(response, "result", json_string("okay")); json_object_set(response, "id", id); - jsonrpc_proxy_onresult(&proxy, response); + jsonrpc_proxy_onresult(proxy, response); json_decref(response); ASSERT_TRUE(finished_context.is_called); @@ -251,7 +245,7 @@ TEST(jsonrpc_proxy, on_result) ASSERT_TRUE(json_is_string(finished_context.result)); ASSERT_STREQ("okay", json_string_value(finished_context.result)); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -262,12 +256,11 @@ TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -279,12 +272,12 @@ TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id) json_object_set_new(response, "result", json_string("okay")); json_object_set_new(response, "id", json_integer(1 + json_integer_value(id))); - jsonrpc_proxy_onresult(&proxy, response); + jsonrpc_proxy_onresult(proxy, response); json_decref(response); ASSERT_FALSE(finished_context.is_called); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -295,12 +288,11 @@ TEST(jsonrpc_proxy, timeout) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, 0, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 0, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -311,7 +303,7 @@ TEST(jsonrpc_proxy, timeout) ASSERT_TRUE(finished_context.is_called); ASSERT_EQ(WF_BAD_TIMEOUT, wf_impl_jsonrpc_get_status(finished_context.error)); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -322,12 +314,11 @@ TEST(jsonrpc_proxy, cleanup_pending_request) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, 10, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 10, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -335,7 +326,7 @@ TEST(jsonrpc_proxy, cleanup_pending_request) ASSERT_FALSE(finished_context.is_called); ASSERT_NE(nullptr, timeout_manager.timers); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); ASSERT_TRUE(finished_context.is_called); ASSERT_EQ(nullptr, timeout_manager.timers); @@ -352,10 +343,9 @@ TEST(jsonrpc_proxy, notify) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); - jsonrpc_proxy_notify(&proxy, "foo", "si", "bar", 42); + jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42); ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(json_is_object(send_context.response)); @@ -376,7 +366,7 @@ TEST(jsonrpc_proxy, notify) ASSERT_EQ(nullptr, id); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -387,13 +377,12 @@ TEST(jsonrpc_proxy, notify_dont_send_invalid_request) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy proxy; - jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); - jsonrpc_proxy_notify(&proxy, "foo", "?"); + jsonrpc_proxy_notify(proxy, "foo", "?"); ASSERT_FALSE(send_context.is_called); - jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_dispose(proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } diff --git a/test/jsonrpc/test_request.cc b/test/jsonrpc/test_request.cc index 94f87ee..a5f956e 100644 --- a/test/jsonrpc/test_request.cc +++ b/test/jsonrpc/test_request.cc @@ -1,5 +1,6 @@ #include #include "jsonrpc/request.h" +#include "jsonrpc/status.h" namespace { @@ -73,7 +74,7 @@ TEST(jsonrpc_request, respond_error) struct jsonrpc_request * request = jsonrpc_request_create(42, &jsonrpc_send, user_data); - jsonrpc_respond_error(request, WF_BAD); + jsonrpc_respond_error(request, JSONRPC_BAD, "Bad"); ASSERT_NE(nullptr, context.response); @@ -92,7 +93,7 @@ TEST(jsonrpc_request, respond_error) json_t * err_code = json_object_get(err, "code"); ASSERT_TRUE(json_is_integer(err_code)); - ASSERT_EQ(WF_BAD, json_integer_value(err_code)); + ASSERT_EQ(JSONRPC_BAD, json_integer_value(err_code)); json_t * err_message = json_object_get(err, "message"); ASSERT_TRUE(json_is_string(err_message)); diff --git a/test/jsonrpc/test_response.cc b/test/jsonrpc/test_response.cc index 1745bdb..bdf6e61 100644 --- a/test/jsonrpc/test_response.cc +++ b/test/jsonrpc/test_response.cc @@ -1,5 +1,5 @@ #include -#include "jsonrpc/response.h" +#include "jsonrpc/impl/response.h" #include "webfuse/core/status.h" #include "webfuse/core/json_util.h" @@ -10,14 +10,14 @@ TEST(json_response, init_result) json_object_set_new(message, "id", json_integer(11)); struct jsonrpc_response response; - jsonrpc_response_init(&response, message); + 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); - jsonrpc_response_cleanup(&response); + jsonrpc_impl_response_cleanup(&response); json_decref(message); } @@ -31,13 +31,13 @@ TEST(json_response, init_error) json_object_set_new(message, "id", json_integer(23)); struct jsonrpc_response response; - jsonrpc_response_init(&response, message); + jsonrpc_impl_response_init(&response, message); ASSERT_EQ(WF_BAD_ACCESS_DENIED, wf_impl_jsonrpc_get_status(response.error)) << json_string_value(json_object_get(response.error, "message")); ASSERT_EQ(nullptr, response.result); ASSERT_EQ(23, response.id); - jsonrpc_response_cleanup(&response); + jsonrpc_impl_response_cleanup(&response); json_decref(message); } @@ -47,12 +47,12 @@ TEST(json_response, init_format_error) json_object_set_new(message, "id", json_integer(12)); struct jsonrpc_response response; - jsonrpc_response_init(&response, message); + jsonrpc_impl_response_init(&response, message); ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_get_status(response.error)); ASSERT_EQ(nullptr, response.result); ASSERT_EQ(12, response.id); - jsonrpc_response_cleanup(&response); + jsonrpc_impl_response_cleanup(&response); json_decref(message); } diff --git a/test/webfuse/tests/adapter/test_response_parser.cc b/test/jsonrpc/test_response_parser.cc similarity index 81% rename from test/webfuse/tests/adapter/test_response_parser.cc rename to test/jsonrpc/test_response_parser.cc index 0973e9c..f65fa6f 100644 --- a/test/webfuse/tests/adapter/test_response_parser.cc +++ b/test/jsonrpc/test_response_parser.cc @@ -1,7 +1,7 @@ #include #include -#include "jsonrpc/response.h" +#include "jsonrpc/impl/response.h" #include "webfuse/core/json_util.h" @@ -12,7 +12,7 @@ static void response_parse_str( json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr); if (nullptr != message) { - jsonrpc_response_init(response, message); + jsonrpc_impl_response_init(response, message); json_decref(message); } } @@ -26,21 +26,21 @@ TEST(response_parser, test) ASSERT_NE(nullptr, response.error); ASSERT_EQ(-1, response.id); ASSERT_EQ(nullptr, response.result); - jsonrpc_response_cleanup(&response); + 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); - jsonrpc_response_cleanup(&response); + 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); - jsonrpc_response_cleanup(&response); + jsonrpc_impl_response_cleanup(&response); // custom error code response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response); @@ -48,12 +48,12 @@ TEST(response_parser, test) ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code"))); ASSERT_EQ(42, response.id); ASSERT_EQ(nullptr, response.result); - jsonrpc_response_cleanup(&response); + jsonrpc_impl_response_cleanup(&response); // valid response response_parse_str("{\"result\": true, \"id\": 42}", &response); ASSERT_EQ(WF_GOOD, response.error); ASSERT_EQ(42, response.id); ASSERT_NE(nullptr, response.result); - jsonrpc_response_cleanup(&response); + jsonrpc_impl_response_cleanup(&response); } diff --git a/test/jsonrpc/test_server.cc b/test/jsonrpc/test_server.cc index 4e0b2bc..e8e879a 100644 --- a/test/jsonrpc/test_server.cc +++ b/test/jsonrpc/test_server.cc @@ -1,6 +1,7 @@ #include #include "jsonrpc/server.h" #include "jsonrpc/request.h" +#include "jsonrpc/status.h" namespace { @@ -40,9 +41,8 @@ namespace TEST(jsonrpc_server, process_request) { - struct jsonrpc_server server; - jsonrpc_server_init(&server); - jsonrpc_server_add(&server, "sayHello", &sayHello, nullptr); + struct jsonrpc_server * server = jsonrpc_server_create(); + jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); Context context{nullptr, false}; void * user_data = reinterpret_cast(&context); @@ -50,7 +50,7 @@ TEST(jsonrpc_server, process_request) 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)); - jsonrpc_server_process(&server, request, &jsonrpc_send, user_data); + jsonrpc_server_process(server, request, &jsonrpc_send, user_data); ASSERT_TRUE(context.is_called); ASSERT_NE(nullptr, context.response); @@ -66,14 +66,13 @@ TEST(jsonrpc_server, process_request) json_decref(context.response); json_decref(request); - jsonrpc_server_cleanup(&server); + jsonrpc_server_dispose(server); } TEST(jsonrpc_server, invoke_unknown_method) { - struct jsonrpc_server server; - jsonrpc_server_init(&server); - jsonrpc_server_add(&server, "sayHello", &sayHello, nullptr); + struct jsonrpc_server * server = jsonrpc_server_create(); + jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); Context context{nullptr, false}; void * user_data = reinterpret_cast(&context); @@ -81,7 +80,7 @@ TEST(jsonrpc_server, invoke_unknown_method) 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)); - jsonrpc_server_process(&server, request, &jsonrpc_send, user_data); + jsonrpc_server_process(server, request, &jsonrpc_send, user_data); ASSERT_TRUE(context.is_called); ASSERT_NE(nullptr, context.response); @@ -96,30 +95,29 @@ TEST(jsonrpc_server, invoke_unknown_method) json_t * err_code = json_object_get(err, "code"); ASSERT_TRUE(json_is_integer(err_code)); - ASSERT_EQ(WF_BAD_NOTIMPLEMENTED, json_integer_value(err_code)); + ASSERT_EQ(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); - jsonrpc_server_cleanup(&server); + jsonrpc_server_dispose(server); } TEST(jsonrpc_server, skip_invalid_request) { - struct jsonrpc_server server; - jsonrpc_server_init(&server); + struct jsonrpc_server * server = jsonrpc_server_create(); Context context{nullptr, false}; void * user_data = reinterpret_cast(&context); json_t * request = json_object(); json_object_set_new(request, "method", json_string("sayHello")); json_object_set_new(request, "params", json_array()); - jsonrpc_server_process(&server, request, &jsonrpc_send, user_data); + jsonrpc_server_process(server, request, &jsonrpc_send, user_data); ASSERT_FALSE(context.is_called); json_decref(request); - jsonrpc_server_cleanup(&server); + jsonrpc_server_dispose(server); }