From 767bafcd01696e5b3ded34a3d0cd5db99836a375 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Tue, 25 Feb 2020 15:36:28 +0100 Subject: [PATCH 01/15] added first idea of API --- include/webfuse/provider/client_config.h | 21 ++++++ lib/webfuse/provider/api.c | 17 +++++ lib/webfuse/provider/impl/client_config.c | 26 +++++++ lib/webfuse/provider/impl/client_config.h | 10 +++ .../tests/provider/test_client_protocol.cc | 72 ++++++++++++++++++- 5 files changed, 143 insertions(+), 3 deletions(-) diff --git a/include/webfuse/provider/client_config.h b/include/webfuse/provider/client_config.h index 73775f1..ec4c85b 100644 --- a/include/webfuse/provider/client_config.h +++ b/include/webfuse/provider/client_config.h @@ -227,6 +227,27 @@ extern WFP_API void wfp_client_config_set_onread( struct wfp_client_config * config, wfp_read_fn * handler); +//------------------------------------------------------------------------------ +/// \brief Enabled authentication with username and password. +/// +/// Sets username and password for built-in username authentication. +/// +/// \param config pointer to client configuration +/// \param username pointer to username +/// \param password pointer to password +//------------------------------------------------------------------------------ +extern WFP_API void wfp_client_config_set_username_credentials( + struct wfp_client_config * config, + char const * username, + char const * password); + +//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ +extern WFP_API void wfp_client_config_set_generic_credentials( + struct wfp_client_config * config, + char const * credentials_type, + char const * contents[]); + #ifdef __cplusplus } #endif diff --git a/lib/webfuse/provider/api.c b/lib/webfuse/provider/api.c index 0c0e88a..6e507e8 100644 --- a/lib/webfuse/provider/api.c +++ b/lib/webfuse/provider/api.c @@ -155,6 +155,23 @@ void wfp_client_config_set_onread( wfp_impl_client_config_set_onread(config, handler); } +void wfp_client_config_set_username_credentials( + struct wfp_client_config * config, + char const * username, + char const * password) +{ + wfp_impl_client_config_set_username_credentials(config, username, password); +} + +void wfp_client_config_set_generic_credentials( + struct wfp_client_config * config, + char const * credentials_type, + char const * contents[]) +{ + wfp_impl_client_config_set_generic_credentials(config, credentials_type, contents); +} + + // protocol diff --git a/lib/webfuse/provider/impl/client_config.c b/lib/webfuse/provider/impl/client_config.c index 7d121da..7163830 100644 --- a/lib/webfuse/provider/impl/client_config.c +++ b/lib/webfuse/provider/impl/client_config.c @@ -110,3 +110,29 @@ void wfp_impl_client_config_set_onread( { config->provider.read = handler; } + + +void wfp_impl_client_config_set_username_credentials( + struct wfp_client_config * config, + char const * username, + char const * password) +{ + (void) config; + (void) username; + (void) password; + + // ToDo: implement me +} + +void wfp_impl_client_config_set_generic_credentials( + struct wfp_client_config * config, + char const * credentials_type, + char const * contents[]) +{ + (void) config; + (void) credentials_type; + (void) contents; + + // ToDo: implement me + +} diff --git a/lib/webfuse/provider/impl/client_config.h b/lib/webfuse/provider/impl/client_config.h index 190a3cb..4b94f49 100644 --- a/lib/webfuse/provider/impl/client_config.h +++ b/lib/webfuse/provider/impl/client_config.h @@ -70,6 +70,16 @@ extern void wfp_impl_client_config_set_onread( struct wfp_client_config * config, wfp_read_fn * handler); +extern void wfp_impl_client_config_set_username_credentials( + struct wfp_client_config * config, + char const * username, + char const * password); + +extern void wfp_impl_client_config_set_generic_credentials( + struct wfp_client_config * config, + char const * credentials_type, + char const * contents[]); + #ifdef __cplusplus } #endif diff --git a/test/webfuse/tests/provider/test_client_protocol.cc b/test/webfuse/tests/provider/test_client_protocol.cc index ff7be74..c5e5008 100644 --- a/test/webfuse/tests/provider/test_client_protocol.cc +++ b/test/webfuse/tests/provider/test_client_protocol.cc @@ -45,6 +45,13 @@ public: wfp_client_config_dispose(config); } + void SetUsernameCredentials( + std::string const & username, + std::string const & password) + { + wfp_client_config_set_username_credentials(config, username.c_str(), password.c_str()); + } + void Connect() { wfp_client_protocol_connect(protocol, server->getContext(), "ws://localhost:54321/"); @@ -61,6 +68,47 @@ public: return server->receiveMessage(); } + void AwaitAuthentication( + std::string const & expected_username, + std::string const & expected_password) + { + json_t * request = server->receiveMessage(); + ASSERT_TRUE(json_is_object(request)); + + json_t * method = json_object_get(request, "method"); + ASSERT_TRUE(json_is_string(method)); + ASSERT_STREQ("authenticate", json_string_value(method)); + + json_t * id = json_object_get(request, "id"); + ASSERT_TRUE(json_is_integer(id)); + + json_t * params = json_object_get(request, "params"); + ASSERT_TRUE(json_is_array(params)); + ASSERT_EQ(2, json_array_size(params)); + + json_t * type = json_array_get(params, 0); + ASSERT_TRUE(json_is_string(type)); + ASSERT_STREQ("username", json_string_value(type)); + + json_t * credentials = json_array_get(params, 1); + ASSERT_TRUE(json_is_object(credentials)); + + json_t * username = json_object_get(credentials, "username"); + ASSERT_TRUE(json_is_string(username)); + ASSERT_STREQ(expected_username.c_str(), json_string_value(username)); + + json_t * password = json_object_get(credentials, "password"); + ASSERT_TRUE(json_is_string(password)); + ASSERT_STREQ(expected_password.c_str(), json_string_value(password)); + + json_t * response = json_object(); + json_object_set_new(response, "result", json_object()); + json_object_set(response, "id", id); + server->sendMessage(response); + + json_decref(request); + } + void AwaitAddFilesystem(std::string& filesystemName) { json_t * addFilesystemRequest = server->receiveMessage(); @@ -91,7 +139,6 @@ public: server->sendMessage(response); json_decref(addFilesystemRequest); - } private: @@ -100,10 +147,8 @@ private: wfp_client_protocol * protocol; }; - } - TEST(client_protocol, connect) { MockProviderClient provider; @@ -120,6 +165,27 @@ TEST(client_protocol, connect) if (HasFatalFailure()) { return; } } +TEST(client_protocol, connect_with_username_authentication) +{ + MockProviderClient provider; + ClientProtocolFixture fixture(provider); + fixture.SetUsernameCredentials("bob", "secret"); + + EXPECT_CALL(provider, OnConnected()).Times(AtMost(1)); + EXPECT_CALL(provider, OnDisconnected()).Times(1); + + fixture.Connect(); + if (HasFatalFailure()) { return; } + + fixture.AwaitAuthentication("bob", "secret"); + if (HasFatalFailure()) { return; } + + std::string filesystem; + fixture.AwaitAddFilesystem(filesystem); + if (HasFatalFailure()) { return; } + +} + TEST(client_protocol, getattr) { MockProviderClient provider; From c6ca2e14bdb330ba41e42a8ec76f8d7f5c83627c Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Tue, 25 Feb 2020 22:05:48 +0100 Subject: [PATCH 02/15] changed credentials API --- cmake/webfuse_provider.cmake | 1 + include/webfuse/provider/client_config.h | 21 +++------- include/webfuse/provider/credentials.h | 30 +++++++++++++++ include/webfuse_provider.h | 1 + lib/webfuse/provider/api.c | 34 ++++++++++------- lib/webfuse/provider/impl/client_config.c | 25 ++---------- lib/webfuse/provider/impl/client_config.h | 10 +---- lib/webfuse/provider/impl/credentials.c | 34 +++++++++++++++++ lib/webfuse/provider/impl/credentials.h | 38 +++++++++++++++++++ lib/webfuse/provider/impl/provider.c | 2 + lib/webfuse/provider/impl/provider.h | 1 + .../tests/provider/test_client_protocol.cc | 9 +---- 12 files changed, 140 insertions(+), 66 deletions(-) create mode 100644 include/webfuse/provider/credentials.h create mode 100644 lib/webfuse/provider/impl/credentials.c create mode 100644 lib/webfuse/provider/impl/credentials.h diff --git a/cmake/webfuse_provider.cmake b/cmake/webfuse_provider.cmake index 5079cb1..0d80d2d 100644 --- a/cmake/webfuse_provider.cmake +++ b/cmake/webfuse_provider.cmake @@ -9,6 +9,7 @@ add_library(webfuse-provider-static STATIC lib/webfuse/provider/impl/provider.c lib/webfuse/provider/impl/request.c lib/webfuse/provider/impl/dirbuffer.c + lib/webfuse/provider/impl/credentials.c lib/webfuse/provider/impl/operation/lookup.c lib/webfuse/provider/impl/operation/getattr.c lib/webfuse/provider/impl/operation/readdir.c diff --git a/include/webfuse/provider/client_config.h b/include/webfuse/provider/client_config.h index ec4c85b..0c79dd7 100644 --- a/include/webfuse/provider/client_config.h +++ b/include/webfuse/provider/client_config.h @@ -14,6 +14,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" @@ -228,25 +229,15 @@ extern WFP_API void wfp_client_config_set_onread( wfp_read_fn * handler); //------------------------------------------------------------------------------ -/// \brief Enabled authentication with username and password. -/// -/// Sets username and password for built-in username authentication. +/// \brief Enabled authentication. /// /// \param config pointer to client configuration -/// \param username pointer to username -/// \param password pointer to password +/// \param get_credentials pointer to function providing credentials when +// needed. //------------------------------------------------------------------------------ -extern WFP_API void wfp_client_config_set_username_credentials( +extern WFP_API void wfp_client_config_enable_authentication( struct wfp_client_config * config, - char const * username, - char const * password); - -//------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ -extern WFP_API void wfp_client_config_set_generic_credentials( - struct wfp_client_config * config, - char const * credentials_type, - char const * contents[]); + wfp_get_credentials_fn * get_credentials); #ifdef __cplusplus } diff --git a/include/webfuse/provider/credentials.h b/include/webfuse/provider/credentials.h new file mode 100644 index 0000000..a6b0806 --- /dev/null +++ b/include/webfuse/provider/credentials.h @@ -0,0 +1,30 @@ +#ifndef WF_PROVIDER_CREDENTIALS_H +#define WF_PROVIDER_CREDENTIALS_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wfp_credentials; + +typedef void wfp_get_credentials_fn( + struct wfp_credentials * credentials, + void * user_data); + +extern WFP_API void wfp_credentials_set_type( + struct wfp_credentials * credentials, + char const * type); + +extern WFP_API void wfp_credentials_add( + struct wfp_credentials * credentials, + char const * key, + char const * value); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/webfuse_provider.h b/include/webfuse_provider.h index 7af3934..19afad2 100644 --- a/include/webfuse_provider.h +++ b/include/webfuse_provider.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include diff --git a/lib/webfuse/provider/api.c b/lib/webfuse/provider/api.c index 6e507e8..7c3220b 100644 --- a/lib/webfuse/provider/api.c +++ b/lib/webfuse/provider/api.c @@ -11,6 +11,7 @@ #include "webfuse/provider/impl/client_config.h" #include "webfuse/provider/impl/client.h" #include "webfuse/provider/impl/dirbuffer.h" +#include "webfuse/provider/impl/credentials.h" // respond @@ -155,23 +156,13 @@ void wfp_client_config_set_onread( wfp_impl_client_config_set_onread(config, handler); } -void wfp_client_config_set_username_credentials( +void wfp_client_config_enable_authentication( struct wfp_client_config * config, - char const * username, - char const * password) + wfp_get_credentials_fn * get_credentials) { - wfp_impl_client_config_set_username_credentials(config, username, password); + wfp_impl_client_config_enable_authentication(config, get_credentials); } -void wfp_client_config_set_generic_credentials( - struct wfp_client_config * config, - char const * credentials_type, - char const * contents[]) -{ - wfp_impl_client_config_set_generic_credentials(config, credentials_type, contents); -} - - // protocol @@ -257,3 +248,20 @@ void wfp_dirbuffer_add( { wfp_impl_dirbuffer_add(buffer, name, inode); } + +// credentials + +void wfp_credentials_set_type( + struct wfp_credentials * credentials, + char const * type) +{ + wfp_impl_credentials_set_type(credentials, type); +} + +void wfp_credentials_add( + struct wfp_credentials * credentials, + char const * key, + char const * value) +{ + wfp_impl_credentials_add(credentials, key, value); +} diff --git a/lib/webfuse/provider/impl/client_config.c b/lib/webfuse/provider/impl/client_config.c index 7163830..0e8afc6 100644 --- a/lib/webfuse/provider/impl/client_config.c +++ b/lib/webfuse/provider/impl/client_config.c @@ -111,28 +111,9 @@ void wfp_impl_client_config_set_onread( config->provider.read = handler; } - -void wfp_impl_client_config_set_username_credentials( +void wfp_impl_client_config_enable_authentication( struct wfp_client_config * config, - char const * username, - char const * password) + wfp_get_credentials_fn * get_credentials) { - (void) config; - (void) username; - (void) password; - - // ToDo: implement me -} - -void wfp_impl_client_config_set_generic_credentials( - struct wfp_client_config * config, - char const * credentials_type, - char const * contents[]) -{ - (void) config; - (void) credentials_type; - (void) contents; - - // ToDo: implement me - + config->provider.get_credentials = get_credentials; } diff --git a/lib/webfuse/provider/impl/client_config.h b/lib/webfuse/provider/impl/client_config.h index 4b94f49..f8d5b9b 100644 --- a/lib/webfuse/provider/impl/client_config.h +++ b/lib/webfuse/provider/impl/client_config.h @@ -70,15 +70,9 @@ extern void wfp_impl_client_config_set_onread( struct wfp_client_config * config, wfp_read_fn * handler); -extern void wfp_impl_client_config_set_username_credentials( +extern void wfp_impl_client_config_enable_authentication( struct wfp_client_config * config, - char const * username, - char const * password); - -extern void wfp_impl_client_config_set_generic_credentials( - struct wfp_client_config * config, - char const * credentials_type, - char const * contents[]); + wfp_get_credentials_fn * get_credentials); #ifdef __cplusplus } diff --git a/lib/webfuse/provider/impl/credentials.c b/lib/webfuse/provider/impl/credentials.c new file mode 100644 index 0000000..8e788ca --- /dev/null +++ b/lib/webfuse/provider/impl/credentials.c @@ -0,0 +1,34 @@ +#include "webfuse/provider/impl/credentials.h" + +#include +#include + +void wfp_impl_credentials_init( + struct wfp_credentials * credentials) +{ + credentials->type = NULL; + credentials->contents = json_object(); +} + +void wfp_impl_credentials_cleanup( + struct wfp_credentials * credentials) +{ + free(credentials->type); + json_decref(credentials->contents); +} + +void wfp_impl_credentials_set_type( + struct wfp_credentials * credentials, + char const * type) +{ + free(credentials->type); + credentials->type = strdup(type); +} + +void wfp_impl_credentials_add( + struct wfp_credentials * credentials, + char const * key, + char const * value) +{ + json_object_set_new(credentials->contents, key, json_string(value)); +} diff --git a/lib/webfuse/provider/impl/credentials.h b/lib/webfuse/provider/impl/credentials.h new file mode 100644 index 0000000..6da40b9 --- /dev/null +++ b/lib/webfuse/provider/impl/credentials.h @@ -0,0 +1,38 @@ +#ifndef WF_PROVIDER_IMPL_CREDENTIALS_H +#define WF_PROVIDER_IMPL_CREDENTIALS_H + +#include "webfuse/provider/credentials.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wfp_credentials +{ + char * type; + json_t * contents; +}; + +extern void wfp_impl_credentials_init( + struct wfp_credentials * credentials); + +extern void wfp_impl_credentials_cleanup( + struct wfp_credentials * credentials); + +extern void wfp_impl_credentials_set_type( + struct wfp_credentials * credentials, + char const * type); + +extern void wfp_impl_credentials_add( + struct wfp_credentials * credentials, + char const * key, + char const * value); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/webfuse/provider/impl/provider.c b/lib/webfuse/provider/impl/provider.c index def9e4a..d3ba96c 100644 --- a/lib/webfuse/provider/impl/provider.c +++ b/lib/webfuse/provider/impl/provider.c @@ -67,6 +67,7 @@ void wfp_impl_provider_init( provider->connected = &wfp_impl_connected_default; provider->disconnected = &wfp_impl_disconnected_default; provider->ontimer = &wfp_impl_ontimer_default; + provider->get_credentials = NULL; } void wfp_impl_provider_init_from_prototype( @@ -82,6 +83,7 @@ void wfp_impl_provider_init_from_prototype( provider->connected = prototype->connected; provider->disconnected = prototype->disconnected; provider->ontimer = prototype->ontimer; + provider->get_credentials = prototype->get_credentials; } void wfp_impl_provider_invoke( diff --git a/lib/webfuse/provider/impl/provider.h b/lib/webfuse/provider/impl/provider.h index ddee453..8f6d115 100644 --- a/lib/webfuse/provider/impl/provider.h +++ b/lib/webfuse/provider/impl/provider.h @@ -20,6 +20,7 @@ struct wfp_provider wfp_open_fn * open; wfp_close_fn * close; wfp_read_fn * read; + wfp_get_credentials_fn * get_credentials; }; struct wfp_impl_invokation_context diff --git a/test/webfuse/tests/provider/test_client_protocol.cc b/test/webfuse/tests/provider/test_client_protocol.cc index c5e5008..b9d6252 100644 --- a/test/webfuse/tests/provider/test_client_protocol.cc +++ b/test/webfuse/tests/provider/test_client_protocol.cc @@ -45,13 +45,6 @@ public: wfp_client_config_dispose(config); } - void SetUsernameCredentials( - std::string const & username, - std::string const & password) - { - wfp_client_config_set_username_credentials(config, username.c_str(), password.c_str()); - } - void Connect() { wfp_client_protocol_connect(protocol, server->getContext(), "ws://localhost:54321/"); @@ -169,7 +162,7 @@ TEST(client_protocol, connect_with_username_authentication) { MockProviderClient provider; ClientProtocolFixture fixture(provider); - fixture.SetUsernameCredentials("bob", "secret"); + // ToDo: enable authentication EXPECT_CALL(provider, OnConnected()).Times(AtMost(1)); EXPECT_CALL(provider, OnDisconnected()).Times(1); From e3a3427ca89c617d7a38cb634a8e374170250a21 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 28 Feb 2020 23:17:41 +0100 Subject: [PATCH 03/15] refactor: made jsonrpc an independent library --- CMakeLists.txt | 1 + cmake/jsonrpc.cmake | 13 ++ cmake/unit_tests.cmake | 15 ++- cmake/webfuse_adapter.cmake | 8 +- cmake/webfuse_core.cmake | 1 + include/jsonrpc/method.h | 40 ++++++ include/jsonrpc/proxy.h | 81 ++++++++++++ include/jsonrpc/request.h | 52 ++++++++ include/jsonrpc/response.h | 40 ++++++ .../impl => include}/jsonrpc/send_fn.h | 6 +- include/jsonrpc/server.h | 47 +++++++ include/jsonrpc/status.h | 10 ++ lib/jsonrpc/error.c | 27 ++++ lib/jsonrpc/error.h | 29 +++++ .../adapter/impl => }/jsonrpc/method.c | 12 +- .../adapter/impl => }/jsonrpc/proxy.c | 67 +++++----- .../adapter/impl => }/jsonrpc/request.c | 34 ++--- .../adapter/impl => }/jsonrpc/response.c | 39 +++--- lib/jsonrpc/server.c | 95 ++++++++++++++ lib/webfuse/adapter/impl/jsonrpc/method.h | 40 ------ lib/webfuse/adapter/impl/jsonrpc/proxy.h | 82 ------------ lib/webfuse/adapter/impl/jsonrpc/request.h | 52 -------- lib/webfuse/adapter/impl/jsonrpc/response.h | 41 ------ lib/webfuse/adapter/impl/jsonrpc/server.c | 95 -------------- lib/webfuse/adapter/impl/jsonrpc/server.h | 48 ------- lib/webfuse/adapter/impl/jsonrpc/util.c | 14 -- lib/webfuse/adapter/impl/jsonrpc/util.h | 17 --- lib/webfuse/adapter/impl/operation/close.c | 6 +- lib/webfuse/adapter/impl/operation/getattr.c | 27 ++-- lib/webfuse/adapter/impl/operation/lookup.c | 29 +++-- lib/webfuse/adapter/impl/operation/open.c | 12 +- lib/webfuse/adapter/impl/operation/read.c | 21 +-- lib/webfuse/adapter/impl/operation/readdir.c | 12 +- lib/webfuse/adapter/impl/operations.c | 4 +- lib/webfuse/adapter/impl/operations.h | 4 +- lib/webfuse/adapter/impl/server_protocol.c | 26 ++-- lib/webfuse/adapter/impl/server_protocol.h | 6 +- lib/webfuse/adapter/impl/session.c | 22 ++-- lib/webfuse/adapter/impl/session.h | 10 +- lib/webfuse/adapter/impl/session_manager.c | 2 +- lib/webfuse/adapter/impl/session_manager.h | 4 +- lib/webfuse/core/json_util.c | 48 +++++++ lib/webfuse/core/json_util.h | 26 ++++ .../adapter => }/jsonrpc/test_is_request.cc | 22 ++-- .../adapter => }/jsonrpc/test_is_response.cc | 20 +-- .../tests/adapter => }/jsonrpc/test_proxy.cc | 122 +++++++++--------- .../adapter => }/jsonrpc/test_request.cc | 22 ++-- .../adapter => }/jsonrpc/test_response.cc | 28 ++-- .../tests/adapter => }/jsonrpc/test_server.cc | 36 +++--- .../tests/adapter => }/jsonrpc/test_util.cc | 2 +- test/webfuse/mocks/mock_provider_client.cc | 23 +++- test/webfuse/mocks/mock_provider_client.hpp | 6 +- .../tests/adapter/test_response_parser.cc | 27 ++-- .../tests/provider/test_client_protocol.cc | 16 ++- 54 files changed, 882 insertions(+), 707 deletions(-) create mode 100644 cmake/jsonrpc.cmake create mode 100644 include/jsonrpc/method.h create mode 100644 include/jsonrpc/proxy.h create mode 100644 include/jsonrpc/request.h create mode 100644 include/jsonrpc/response.h rename {lib/webfuse/adapter/impl => include}/jsonrpc/send_fn.h (61%) create mode 100644 include/jsonrpc/server.h create mode 100644 include/jsonrpc/status.h create mode 100644 lib/jsonrpc/error.c create mode 100644 lib/jsonrpc/error.h rename lib/{webfuse/adapter/impl => }/jsonrpc/method.c (50%) rename lib/{webfuse/adapter/impl => }/jsonrpc/proxy.c (68%) rename lib/{webfuse/adapter/impl => }/jsonrpc/request.c (64%) rename lib/{webfuse/adapter/impl => }/jsonrpc/response.c (53%) create mode 100644 lib/jsonrpc/server.c delete mode 100644 lib/webfuse/adapter/impl/jsonrpc/method.h delete mode 100644 lib/webfuse/adapter/impl/jsonrpc/proxy.h delete mode 100644 lib/webfuse/adapter/impl/jsonrpc/request.h delete mode 100644 lib/webfuse/adapter/impl/jsonrpc/response.h delete mode 100644 lib/webfuse/adapter/impl/jsonrpc/server.c delete mode 100644 lib/webfuse/adapter/impl/jsonrpc/server.h delete mode 100644 lib/webfuse/adapter/impl/jsonrpc/util.c delete mode 100644 lib/webfuse/adapter/impl/jsonrpc/util.h create mode 100644 lib/webfuse/core/json_util.c create mode 100644 lib/webfuse/core/json_util.h rename test/{webfuse/tests/adapter => }/jsonrpc/test_is_request.cc (81%) rename test/{webfuse/tests/adapter => }/jsonrpc/test_is_response.cc (76%) rename test/{webfuse/tests/adapter => }/jsonrpc/test_proxy.cc (72%) rename test/{webfuse/tests/adapter => }/jsonrpc/test_request.cc (75%) rename test/{webfuse/tests/adapter => }/jsonrpc/test_response.cc (59%) rename test/{webfuse/tests/adapter => }/jsonrpc/test_server.cc (75%) rename test/{webfuse/tests/adapter => }/jsonrpc/test_util.cc (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b77e46..cbdf5f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ add_compile_options( "-pthread" ) +include(jsonrpc) include(webfuse_core) include(webfuse_adapter) include(webfuse_provider) diff --git a/cmake/jsonrpc.cmake b/cmake/jsonrpc.cmake new file mode 100644 index 0000000..40be948 --- /dev/null +++ b/cmake/jsonrpc.cmake @@ -0,0 +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 +) + +target_include_directories(jsonrpc PUBLIC lib) +set_target_properties(jsonrpc PROPERTIES C_VISIBILITY_PRESET hidden) diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index 1ff1be5..43c6a4f 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -7,6 +7,13 @@ include(GoogleTest) pkg_check_modules(GMOCK gmock) add_executable(alltests + test/jsonrpc/test_util.cc + test/jsonrpc/test_is_request.cc + test/jsonrpc/test_request.cc + test/jsonrpc/test_is_response.cc + test/jsonrpc/test_response.cc + test/jsonrpc/test_server.cc + test/jsonrpc/test_proxy.cc test/webfuse/utils/tempdir.cc test/webfuse/utils/file_utils.cc test/webfuse/utils/msleep.cc @@ -37,13 +44,6 @@ add_executable(alltests test/webfuse/tests/adapter/test_uuid_mountpoint.cc test/webfuse/tests/adapter/test_uuid_mountpoint_factory.cc test/webfuse/tests/adapter/test_fuse_req.cc - test/webfuse/tests/adapter/jsonrpc/test_util.cc - test/webfuse/tests/adapter/jsonrpc/test_is_request.cc - test/webfuse/tests/adapter/jsonrpc/test_request.cc - test/webfuse/tests/adapter/jsonrpc/test_is_response.cc - test/webfuse/tests/adapter/jsonrpc/test_response.cc - test/webfuse/tests/adapter/jsonrpc/test_server.cc - test/webfuse/tests/adapter/jsonrpc/test_proxy.cc test/webfuse/tests/provider/test_url.cc test/webfuse/tests/provider/test_client_protocol.cc test/webfuse/tests/integration/test_integration.cc @@ -65,6 +65,7 @@ target_link_libraries(alltests PUBLIC webfuse-adapter-static webfuse-provider-static webfuse-core + jsonrpc ${FUSE3_LIBRARIES} ${LWS_LIBRARIES} ${JANSSON_LIBRARIES} diff --git a/cmake/webfuse_adapter.cmake b/cmake/webfuse_adapter.cmake index 269204d..719f699 100644 --- a/cmake/webfuse_adapter.cmake +++ b/cmake/webfuse_adapter.cmake @@ -28,12 +28,6 @@ add_library(webfuse-adapter-static STATIC lib/webfuse/adapter/impl/operation/open.c lib/webfuse/adapter/impl/operation/close.c lib/webfuse/adapter/impl/operation/read.c - lib/webfuse/adapter/impl/jsonrpc/proxy.c - lib/webfuse/adapter/impl/jsonrpc/server.c - lib/webfuse/adapter/impl/jsonrpc/method.c - lib/webfuse/adapter/impl/jsonrpc/request.c - lib/webfuse/adapter/impl/jsonrpc/response.c - lib/webfuse/adapter/impl/jsonrpc/util.c ) target_include_directories(webfuse-adapter-static PRIVATE @@ -70,7 +64,7 @@ set_target_properties(webfuse-adapter PROPERTIES SOVERSION 0) set_target_properties(webfuse-adapter PROPERTIES C_VISIBILITY_PRESET hidden) set_target_properties(webfuse-adapter PROPERTIES COMPILE_DEFINITIONS "WF_API=WF_EXPORT") -target_link_libraries(webfuse-adapter PRIVATE webfuse-adapter-static webfuse-core) +target_link_libraries(webfuse-adapter PRIVATE webfuse-adapter-static webfuse-core jsonrpc) file(WRITE "${PROJECT_BINARY_DIR}/libwebfuse-adapter.pc" "prefix=\"${CMAKE_INSTALL_PREFIX}\" diff --git a/cmake/webfuse_core.cmake b/cmake/webfuse_core.cmake index 52c7681..2926384 100644 --- a/cmake/webfuse_core.cmake +++ b/cmake/webfuse_core.cmake @@ -8,6 +8,7 @@ add_library(webfuse-core STATIC lib/webfuse/core/string.c lib/webfuse/core/base64.c lib/webfuse/core/lws_log.c + lib/webfuse/core/json_util.c ) set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core) diff --git a/include/jsonrpc/method.h b/include/jsonrpc/method.h new file mode 100644 index 0000000..8e9cb3a --- /dev/null +++ b/include/jsonrpc/method.h @@ -0,0 +1,40 @@ +#ifndef JSONRPC_METHOD_H +#define JSONRPC_METHOD_H + +#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); + +struct jsonrpc_method +{ + struct jsonrpc_method * next; + char * name; + jsonrpc_method_invoke_fn * invoke; + void * user_data; +}; + +extern struct jsonrpc_method * jsonrpc_method_create( + char const * method_name, + jsonrpc_method_invoke_fn * invoke, + void * user_data); + +extern void jsonrpc_method_dispose( + struct jsonrpc_method * method); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/include/jsonrpc/proxy.h b/include/jsonrpc/proxy.h new file mode 100644 index 0000000..eb44991 --- /dev/null +++ b/include/jsonrpc/proxy.h @@ -0,0 +1,81 @@ +#ifndef JSONRPC_PROXY_H +#define JSONRPC_PROXY_H + +#ifndef __cplusplus +#include +#include +#include +#else +#include +#include +using std::size_t; +#endif + +#include +#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 + +typedef void jsonrpc_proxy_finished_fn( + void * user_data, + json_t const * result, + json_t const * error); + + +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, + struct wf_impl_timeout_manager * manager, + int timeout, + jsonrpc_send_fn * send, + void * user_data); + +extern void jsonrpc_proxy_cleanup( + struct jsonrpc_proxy * proxy); + +extern void jsonrpc_proxy_invoke( + struct jsonrpc_proxy * proxy, + jsonrpc_proxy_finished_fn * finished, + void * user_data, + char const * method_name, + char const * param_info, + ... +); + +extern void jsonrpc_proxy_notify( + struct jsonrpc_proxy * proxy, + char const * method_name, + char const * param_info, + ... +); + +extern void jsonrpc_proxy_onresult( + struct jsonrpc_proxy * proxy, + json_t * message); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/include/jsonrpc/request.h b/include/jsonrpc/request.h new file mode 100644 index 0000000..496a4e0 --- /dev/null +++ b/include/jsonrpc/request.h @@ -0,0 +1,52 @@ +#ifndef JSONRPC_REQUEST_H +#define JSONRPC_REQUEST_H + +#ifndef __cplusplus +#include +#include +#include +#else +#include +#include +using std::size_t; +#endif + +#include +#include "webfuse/core/status.h" +#include "jsonrpc/send_fn.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct jsonrpc_request; + +extern bool jsonrpc_is_request( + json_t * message); + +extern struct jsonrpc_request * jsonrpc_request_create( + int id, + jsonrpc_send_fn * send, + void * user_data); + +extern void jsonrpc_request_dispose( + struct jsonrpc_request * request); + +extern void * jsonrpc_request_get_userdata( + struct jsonrpc_request * request); + +extern void jsonrpc_respond( + struct jsonrpc_request * request, + json_t * result); + +extern void jsonrpc_respond_error( + struct jsonrpc_request * request, + wf_status status); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/jsonrpc/response.h b/include/jsonrpc/response.h new file mode 100644 index 0000000..fbdaa7e --- /dev/null +++ b/include/jsonrpc/response.h @@ -0,0 +1,40 @@ +#ifndef WF_JSONRPC_RESPONSE_H +#define WF_JSONRPC_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_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/lib/webfuse/adapter/impl/jsonrpc/send_fn.h b/include/jsonrpc/send_fn.h similarity index 61% rename from lib/webfuse/adapter/impl/jsonrpc/send_fn.h rename to include/jsonrpc/send_fn.h index baeedf7..b25bdbb 100644 --- a/lib/webfuse/adapter/impl/jsonrpc/send_fn.h +++ b/include/jsonrpc/send_fn.h @@ -1,5 +1,5 @@ -#ifndef WF_ADAPTER_IMPL_JSONRPC_SEND_FN_H -#define WF_ADAPTER_IMPL_JSONRPC_SEND_FN_H +#ifndef JSONRPC_SEND_FN_H +#define JSONRPC_SEND_FN_H #ifndef __cplusplus #include @@ -12,7 +12,7 @@ extern "C" { #endif -typedef bool wf_impl_jsonrpc_send_fn( +typedef bool jsonrpc_send_fn( json_t * request, void * user_data); diff --git a/include/jsonrpc/server.h b/include/jsonrpc/server.h new file mode 100644 index 0000000..778a7e2 --- /dev/null +++ b/include/jsonrpc/server.h @@ -0,0 +1,47 @@ +#ifndef JSONRPC_SERVER_H +#define JSONRPC_SERVER_H + +#ifndef __cplusplus +#include +#include +#else +#include +#endif + +#include +#include "jsonrpc/send_fn.h" +#include "jsonrpc/method.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct jsonrpc_server +{ + struct jsonrpc_method * methods; +}; + +extern void jsonrpc_server_init( + struct jsonrpc_server * server); + +extern void jsonrpc_server_cleanup( + struct jsonrpc_server * server); + +extern 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( + struct jsonrpc_server * server, + json_t * request, + jsonrpc_send_fn * send, + void * user_data); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/jsonrpc/status.h b/include/jsonrpc/status.h new file mode 100644 index 0000000..b7368b3 --- /dev/null +++ b/include/jsonrpc/status.h @@ -0,0 +1,10 @@ +#ifndef JSONRPC_STATUS_H +#define JSONRPC_STATUS_H + +#define JSONRPC_GOOD 0 +#define JSONRPC_BAD -1 +#define JSONRPC_BAD_TIMEOUT -3 +#define JSONRPC_BAD_BUSY -4 +#define JSONRPC_BAD_FORMAT -5 + +#endif diff --git a/lib/jsonrpc/error.c b/lib/jsonrpc/error.c new file mode 100644 index 0000000..7613766 --- /dev/null +++ b/lib/jsonrpc/error.c @@ -0,0 +1,27 @@ +#include "jsonrpc/error.h" + +json_t * +jsonrpc_error( + int code, + char const * message) +{ + json_t * error = json_object(); + json_object_set_new(error, "code", json_integer(code)); + json_object_set_new(error, "message", json_string(message)); + + return error; +} + +void +jsonrpc_propate_error( + jsonrpc_proxy_finished_fn * finised, + void * user_data, + int code, + char const * message) +{ + json_t * error = jsonrpc_error(code, message); + finised(user_data, NULL, error); + + json_decref(error); +} + diff --git a/lib/jsonrpc/error.h b/lib/jsonrpc/error.h new file mode 100644 index 0000000..38bbcd2 --- /dev/null +++ b/lib/jsonrpc/error.h @@ -0,0 +1,29 @@ +#ifndef JSONRPC_ERROR_H +#define JSONRPC_ERROR_H + +#include +#include "jsonrpc/proxy.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern json_t * +jsonrpc_error( + int code, + char const * message); + +extern void +jsonrpc_propate_error( + jsonrpc_proxy_finished_fn * finised, + void * user_data, + int code, + char const * message); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/lib/webfuse/adapter/impl/jsonrpc/method.c b/lib/jsonrpc/method.c similarity index 50% rename from lib/webfuse/adapter/impl/jsonrpc/method.c rename to lib/jsonrpc/method.c index 8485471..31414d4 100644 --- a/lib/webfuse/adapter/impl/jsonrpc/method.c +++ b/lib/jsonrpc/method.c @@ -1,13 +1,13 @@ -#include "webfuse/adapter/impl/jsonrpc/method.h" +#include "jsonrpc/method.h" #include #include -struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create( +struct jsonrpc_method * jsonrpc_method_create( char const * method_name, - wf_impl_jsonrpc_method_invoke_fn * invoke, + jsonrpc_method_invoke_fn * invoke, void * user_data) { - struct wf_impl_jsonrpc_method * method = malloc(sizeof(struct wf_impl_jsonrpc_method)); + struct jsonrpc_method * method = malloc(sizeof(struct jsonrpc_method)); if (NULL != method) { method->next = NULL; @@ -19,8 +19,8 @@ struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create( return method; } -void wf_impl_jsonrpc_method_dispose( - struct wf_impl_jsonrpc_method * method) +void jsonrpc_method_dispose( + struct jsonrpc_method * method) { free(method->name); free(method); diff --git a/lib/webfuse/adapter/impl/jsonrpc/proxy.c b/lib/jsonrpc/proxy.c similarity index 68% rename from lib/webfuse/adapter/impl/jsonrpc/proxy.c rename to lib/jsonrpc/proxy.c index ec0da2e..b12571e 100644 --- a/lib/webfuse/adapter/impl/jsonrpc/proxy.c +++ b/lib/jsonrpc/proxy.c @@ -1,16 +1,18 @@ -#include "webfuse/adapter/impl/jsonrpc/proxy.h" +#include "jsonrpc/proxy.h" +#include "jsonrpc/response.h" +#include "jsonrpc/error.h" +#include "jsonrpc/status.h" + #include -#include "webfuse/adapter/impl/jsonrpc/response.h" - -static void wf_impl_jsonrpc_proxy_timeout( +static void jsonrpc_proxy_timeout( struct wf_impl_timer * timer) { - struct wf_impl_jsonrpc_proxy * proxy = timer->user_data; + struct jsonrpc_proxy * proxy = timer->user_data; if (proxy->request.is_pending) { - wf_impl_jsonrpc_proxy_finished_fn * finished = proxy->request.finished; + jsonrpc_proxy_finished_fn * finished = proxy->request.finished; void * user_data = proxy->request.user_data; proxy->request.is_pending = false; @@ -19,11 +21,11 @@ static void wf_impl_jsonrpc_proxy_timeout( proxy->request.finished = NULL; wf_impl_timer_cancel(&proxy->request.timer); - finished(user_data, WF_BAD_TIMEOUT, NULL); + jsonrpc_propate_error(finished, user_data, JSONRPC_BAD_TIMEOUT, "Timeout"); } } -static json_t * wf_impl_jsonrpc_request_create( +static json_t * jsonrpc_request_create( char const * method, int id, char const * param_info, @@ -67,11 +69,11 @@ static json_t * wf_impl_jsonrpc_request_create( return request; } -void wf_impl_jsonrpc_proxy_init( - struct wf_impl_jsonrpc_proxy * proxy, +void jsonrpc_proxy_init( + struct jsonrpc_proxy * proxy, struct wf_impl_timeout_manager * timeout_manager, int timeout, - wf_impl_jsonrpc_send_fn * send, + jsonrpc_send_fn * send, void * user_data) { proxy->send = send; @@ -82,13 +84,13 @@ void wf_impl_jsonrpc_proxy_init( wf_impl_timer_init(&proxy->request.timer, timeout_manager); } -void wf_impl_jsonrpc_proxy_cleanup( - struct wf_impl_jsonrpc_proxy * proxy) +void jsonrpc_proxy_cleanup( + struct jsonrpc_proxy * proxy) { if (proxy->request.is_pending) { void * user_data = proxy->request.user_data; - wf_impl_jsonrpc_proxy_finished_fn * finished = proxy->request.finished; + jsonrpc_proxy_finished_fn * finished = proxy->request.finished; proxy->request.is_pending = false; proxy->request.finished = NULL; @@ -96,15 +98,15 @@ void wf_impl_jsonrpc_proxy_cleanup( proxy->request.id = 0; wf_impl_timer_cancel(&proxy->request.timer); - finished(user_data, WF_BAD, NULL); + jsonrpc_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); } wf_impl_timer_cleanup(&proxy->request.timer); } -void wf_impl_jsonrpc_proxy_invoke( - struct wf_impl_jsonrpc_proxy * proxy, - wf_impl_jsonrpc_proxy_finished_fn * finished, +void jsonrpc_proxy_invoke( + struct jsonrpc_proxy * proxy, + jsonrpc_proxy_finished_fn * finished, void * user_data, char const * method_name, char const * param_info, @@ -118,11 +120,11 @@ void wf_impl_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), - &wf_impl_jsonrpc_proxy_timeout, proxy); + &jsonrpc_proxy_timeout, proxy); va_list args; va_start(args, param_info); - json_t * request = wf_impl_jsonrpc_request_create(method_name, proxy->request.id, param_info, args); + json_t * request = 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))); @@ -134,8 +136,7 @@ void wf_impl_jsonrpc_proxy_invoke( proxy->request.id = 0; wf_impl_timer_cancel(&proxy->request.timer); - finished(user_data, WF_BAD, NULL); - + jsonrpc_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); } if (NULL != request) @@ -145,12 +146,12 @@ void wf_impl_jsonrpc_proxy_invoke( } else { - finished(user_data, WF_BAD_BUSY, NULL); + jsonrpc_propate_error(finished, user_data, JSONRPC_BAD_BUSY, "Busy"); } } -extern void wf_impl_jsonrpc_proxy_notify( - struct wf_impl_jsonrpc_proxy * proxy, +extern void jsonrpc_proxy_notify( + struct jsonrpc_proxy * proxy, char const * method_name, char const * param_info, ... @@ -158,7 +159,7 @@ extern void wf_impl_jsonrpc_proxy_notify( { va_list args; va_start(args, param_info); - json_t * request = wf_impl_jsonrpc_request_create(method_name, 0, param_info, args); + json_t * request = jsonrpc_request_create(method_name, 0, param_info, args); va_end(args); if (NULL != request) { @@ -168,16 +169,16 @@ extern void wf_impl_jsonrpc_proxy_notify( } -void wf_impl_jsonrpc_proxy_onresult( - struct wf_impl_jsonrpc_proxy * proxy, +void jsonrpc_proxy_onresult( + struct jsonrpc_proxy * proxy, json_t * message) { - struct wf_impl_jsonrpc_response response; - wf_impl_jsonrpc_response_init(&response, message); + struct jsonrpc_response response; + jsonrpc_response_init(&response, message); if ((proxy->request.is_pending) && (response.id == proxy->request.id)) { - wf_impl_jsonrpc_proxy_finished_fn * finished = proxy->request.finished; + jsonrpc_proxy_finished_fn * finished = proxy->request.finished; void * user_data = proxy->request.user_data; proxy->request.is_pending = false; @@ -186,9 +187,9 @@ void wf_impl_jsonrpc_proxy_onresult( proxy->request.finished = NULL; wf_impl_timer_cancel(&proxy->request.timer); - finished(user_data, response.status, response.result); + finished(user_data, response.result, response.error); } - wf_impl_jsonrpc_response_cleanup(&response); + jsonrpc_response_cleanup(&response); } diff --git a/lib/webfuse/adapter/impl/jsonrpc/request.c b/lib/jsonrpc/request.c similarity index 64% rename from lib/webfuse/adapter/impl/jsonrpc/request.c rename to lib/jsonrpc/request.c index e016e1c..8790ce9 100644 --- a/lib/webfuse/adapter/impl/jsonrpc/request.c +++ b/lib/jsonrpc/request.c @@ -1,15 +1,15 @@ -#include "webfuse/adapter/impl/jsonrpc/request.h" +#include "jsonrpc/request.h" #include "webfuse/core/status_intern.h" #include -struct wf_impl_jsonrpc_request +struct jsonrpc_request { int id; - wf_impl_jsonrpc_send_fn * send; + jsonrpc_send_fn * send; void * user_data; }; -bool wf_impl_jsonrpc_is_request( +bool jsonrpc_is_request( json_t * message) { json_t * id = json_object_get(message, "id"); @@ -21,12 +21,12 @@ bool wf_impl_jsonrpc_is_request( } -struct wf_impl_jsonrpc_request * wf_impl_jsonrpc_request_create( +struct jsonrpc_request * jsonrpc_request_create( int id, - wf_impl_jsonrpc_send_fn * send, + jsonrpc_send_fn * send, void * user_data) { - struct wf_impl_jsonrpc_request * request = malloc(sizeof(struct wf_impl_jsonrpc_request)); + struct jsonrpc_request * request = malloc(sizeof(struct jsonrpc_request)); if (NULL != request) { request->id = id; @@ -37,21 +37,21 @@ struct wf_impl_jsonrpc_request * wf_impl_jsonrpc_request_create( return request; } -void wf_impl_jsonrpc_request_dispose( - struct wf_impl_jsonrpc_request * request) +void jsonrpc_request_dispose( + struct jsonrpc_request * request) { free(request); } -void * wf_impl_jsonrpc_request_get_userdata( - struct wf_impl_jsonrpc_request * request) +void * jsonrpc_request_get_userdata( + struct jsonrpc_request * request) { return request->user_data; } -void wf_impl_jsonrpc_respond( - struct wf_impl_jsonrpc_request * request, +void jsonrpc_respond( + struct jsonrpc_request * request, json_t * result) { json_t * response = json_object(); @@ -60,11 +60,11 @@ void wf_impl_jsonrpc_respond( request->send(response, request->user_data); json_decref(response); - wf_impl_jsonrpc_request_dispose(request); + jsonrpc_request_dispose(request); } -void wf_impl_jsonrpc_respond_error( - struct wf_impl_jsonrpc_request * request, +void jsonrpc_respond_error( + struct jsonrpc_request * request, wf_status status) { json_t * err = json_object(); @@ -77,6 +77,6 @@ void wf_impl_jsonrpc_respond_error( request->send(response, request->user_data); json_decref(response); - wf_impl_jsonrpc_request_dispose(request); + jsonrpc_request_dispose(request); } diff --git a/lib/webfuse/adapter/impl/jsonrpc/response.c b/lib/jsonrpc/response.c similarity index 53% rename from lib/webfuse/adapter/impl/jsonrpc/response.c rename to lib/jsonrpc/response.c index 00eb396..c11bbc7 100644 --- a/lib/webfuse/adapter/impl/jsonrpc/response.c +++ b/lib/jsonrpc/response.c @@ -1,6 +1,8 @@ -#include "webfuse/adapter/impl/jsonrpc/response.h" +#include "jsonrpc/response.h" +#include "jsonrpc/error.h" +#include "jsonrpc/status.h" -extern bool wf_impl_jsonrpc_is_response( +extern bool jsonrpc_is_response( json_t * message) { json_t * id = json_object_get(message, "id"); @@ -12,22 +14,21 @@ extern bool wf_impl_jsonrpc_is_response( } -void wf_impl_jsonrpc_response_init( - struct wf_impl_jsonrpc_response * result, +void jsonrpc_response_init( + struct jsonrpc_response * result, json_t * response) { - result->status = WF_BAD; result->id = -1; result->result = NULL; + result->error = NULL; json_t * id_holder = json_object_get(response, "id"); if ((NULL == id_holder) || (!json_is_integer(id_holder))) { - result->status = WF_BAD_FORMAT; + result->error = jsonrpc_error(JSONRPC_BAD_FORMAT, "invalid format: missing id"); return; } - result->status = WF_GOOD; result->id = json_integer_value(id_holder); result->result = json_object_get(response, "result"); if (NULL != result->result) @@ -36,25 +37,29 @@ void wf_impl_jsonrpc_response_init( } else { - result->status = WF_BAD_FORMAT; - json_t * error = json_object_get(response, "error"); - if (NULL != error) + if ((json_is_object(error)) && (json_is_integer(json_object_get(error, "code")))) { - json_t * error_code = json_object_get(error, "code"); - if ((NULL != error_code) && (json_is_integer(error_code))) - { - result->status = json_integer_value(error_code); - } + result->error = error; + json_incref(result->error); + } + else + { + result->error = jsonrpc_error(JSONRPC_BAD_FORMAT, "invalid format: invalid error object"); } } } -void wf_impl_jsonrpc_response_cleanup( - struct wf_impl_jsonrpc_response * response) +void jsonrpc_response_cleanup( + struct jsonrpc_response * response) { if (NULL != response->result) { json_decref(response->result); } + + if (NULL != response->error) + { + json_decref(response->error); + } } diff --git a/lib/jsonrpc/server.c b/lib/jsonrpc/server.c new file mode 100644 index 0000000..6e62c3b --- /dev/null +++ b/lib/jsonrpc/server.c @@ -0,0 +1,95 @@ +#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/jsonrpc/method.h b/lib/webfuse/adapter/impl/jsonrpc/method.h deleted file mode 100644 index 739f692..0000000 --- a/lib/webfuse/adapter/impl/jsonrpc/method.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_JSONRPC_METHOD_H -#define WF_ADAPTER_IMPL_JSONRPC_METHOD_H - -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_impl_jsonrpc_request; - -typedef void wf_impl_jsonrpc_method_invoke_fn( - struct wf_impl_jsonrpc_request * request, - char const * method_name, - json_t * params, - void * user_data); - -struct wf_impl_jsonrpc_method -{ - struct wf_impl_jsonrpc_method * next; - char * name; - wf_impl_jsonrpc_method_invoke_fn * invoke; - void * user_data; -}; - -extern struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create( - char const * method_name, - wf_impl_jsonrpc_method_invoke_fn * invoke, - void * user_data); - -extern void wf_impl_jsonrpc_method_dispose( - struct wf_impl_jsonrpc_method * method); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/lib/webfuse/adapter/impl/jsonrpc/proxy.h b/lib/webfuse/adapter/impl/jsonrpc/proxy.h deleted file mode 100644 index 0433747..0000000 --- a/lib/webfuse/adapter/impl/jsonrpc/proxy.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_JSONRPC_PROXY_H -#define WF_ADAPTER_IMPL_JSONRPC_PROXY_H - -#ifndef __cplusplus -#include -#include -#include -#else -#include -#include -using std::size_t; -#endif - -#include -#include "webfuse/adapter/impl/jsonrpc/send_fn.h" -#include "webfuse/adapter/impl/time/timeout_manager.h" -#include "webfuse/adapter/impl/time/timer.h" -#include "webfuse/core/status.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef void wf_impl_jsonrpc_proxy_finished_fn( - void * user_data, - wf_status status, - struct json_t const * result); - - -struct wf_impl_jsonrpc_request -{ - bool is_pending; - wf_impl_jsonrpc_proxy_finished_fn * finished; - void * user_data; - int id; - struct wf_impl_timer timer; -}; - -struct wf_impl_jsonrpc_proxy -{ - struct wf_impl_jsonrpc_request request; - int timeout; - wf_impl_jsonrpc_send_fn * send; - void * user_data; -}; - -extern void wf_impl_jsonrpc_proxy_init( - struct wf_impl_jsonrpc_proxy * proxy, - struct wf_impl_timeout_manager * manager, - int timeout, - wf_impl_jsonrpc_send_fn * send, - void * user_data); - -extern void wf_impl_jsonrpc_proxy_cleanup( - struct wf_impl_jsonrpc_proxy * proxy); - -extern void wf_impl_jsonrpc_proxy_invoke( - struct wf_impl_jsonrpc_proxy * proxy, - wf_impl_jsonrpc_proxy_finished_fn * finished, - void * user_data, - char const * method_name, - char const * param_info, - ... -); - -extern void wf_impl_jsonrpc_proxy_notify( - struct wf_impl_jsonrpc_proxy * proxy, - char const * method_name, - char const * param_info, - ... -); - -extern void wf_impl_jsonrpc_proxy_onresult( - struct wf_impl_jsonrpc_proxy * proxy, - json_t * message); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/lib/webfuse/adapter/impl/jsonrpc/request.h b/lib/webfuse/adapter/impl/jsonrpc/request.h deleted file mode 100644 index 7d26228..0000000 --- a/lib/webfuse/adapter/impl/jsonrpc/request.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_JSONRPC_REQUEST_H -#define WF_ADAPTER_IMPL_JSONRPC_REQUEST_H - -#ifndef __cplusplus -#include -#include -#include -#else -#include -#include -using std::size_t; -#endif - -#include -#include "webfuse/core/status.h" -#include "webfuse/adapter/impl/jsonrpc/send_fn.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_impl_jsonrpc_request; - -extern bool wf_impl_jsonrpc_is_request( - json_t * message); - -extern struct wf_impl_jsonrpc_request * wf_impl_jsonrpc_request_create( - int id, - wf_impl_jsonrpc_send_fn * send, - void * user_data); - -extern void wf_impl_jsonrpc_request_dispose( - struct wf_impl_jsonrpc_request * request); - -extern void * wf_impl_jsonrpc_request_get_userdata( - struct wf_impl_jsonrpc_request * request); - -extern void wf_impl_jsonrpc_respond( - struct wf_impl_jsonrpc_request * request, - json_t * result); - -extern void wf_impl_jsonrpc_respond_error( - struct wf_impl_jsonrpc_request * request, - wf_status status); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/lib/webfuse/adapter/impl/jsonrpc/response.h b/lib/webfuse/adapter/impl/jsonrpc/response.h deleted file mode 100644 index d53f667..0000000 --- a/lib/webfuse/adapter/impl/jsonrpc/response.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_JSONRPC_RESPONSE_H -#define WF_ADAPTER_IMPL_JSONRPC_RESPONSE_H - -#ifndef __cplusplus -#include -#include -#else -#include -using std::size_t; -#endif - -#include -#include "webfuse/core/status.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct wf_impl_jsonrpc_response -{ - wf_status status; - int id; - json_t * result; -}; - -extern bool wf_impl_jsonrpc_is_response( - json_t * message); - -extern void wf_impl_jsonrpc_response_init( - struct wf_impl_jsonrpc_response * response, - json_t * message); - -extern void wf_impl_jsonrpc_response_cleanup( - struct wf_impl_jsonrpc_response * response); - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/lib/webfuse/adapter/impl/jsonrpc/server.c b/lib/webfuse/adapter/impl/jsonrpc/server.c deleted file mode 100644 index 717d71b..0000000 --- a/lib/webfuse/adapter/impl/jsonrpc/server.c +++ /dev/null @@ -1,95 +0,0 @@ -#include "webfuse/adapter/impl/jsonrpc/server.h" -#include "webfuse/adapter/impl/jsonrpc/method.h" -#include "webfuse/adapter/impl/jsonrpc/request.h" -#include "webfuse/core/util.h" - -#include - -void wf_impl_jsonrpc_server_init( - struct wf_impl_jsonrpc_server * server) -{ - server->methods = NULL; -} - -void wf_impl_jsonrpc_server_cleanup( - struct wf_impl_jsonrpc_server * server) -{ - struct wf_impl_jsonrpc_method * current = server->methods; - while (NULL != current) - { - struct wf_impl_jsonrpc_method * next = current->next; - wf_impl_jsonrpc_method_dispose(current); - current = next; - } - server->methods = NULL; -} - -void wf_impl_jsonrpc_server_add( - struct wf_impl_jsonrpc_server * server, - char const * method_name, - wf_impl_jsonrpc_method_invoke_fn * invoke, - void * user_data) -{ - struct wf_impl_jsonrpc_method * method = wf_impl_jsonrpc_method_create(method_name, invoke, user_data); - method->next = server->methods; - server->methods = method; -} - -static void wf_impl_jsonrpc_server_invalid_method_invoke( - struct wf_impl_jsonrpc_request * request, - char const * WF_UNUSED_PARAM(method_name), - json_t * WF_UNUSED_PARAM(params), - void * WF_UNUSED_PARAM(user_data)) -{ - wf_impl_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED); -} - -static struct wf_impl_jsonrpc_method const wf_impl_jsonrpc_server_invalid_method = -{ - .next = NULL, - .name = "", - .invoke = &wf_impl_jsonrpc_server_invalid_method_invoke, - .user_data = NULL -}; - -static struct wf_impl_jsonrpc_method const * wf_impl_jsonrpc_server_get_method( - struct wf_impl_jsonrpc_server * server, - char const * method_name) -{ - struct wf_impl_jsonrpc_method const * current = server->methods; - while (NULL != current) - { - if (0 == strcmp(method_name, current->name)) - { - return current; - } - - current = current->next; - } - - return &wf_impl_jsonrpc_server_invalid_method; -} - -void wf_impl_jsonrpc_server_process( - struct wf_impl_jsonrpc_server * server, - json_t * request_data, - wf_impl_jsonrpc_send_fn * send, - void * user_data) -{ - json_t * method_holder = json_object_get(request_data, "method"); - json_t * params = json_object_get(request_data, "params"); - json_t * id_holder = json_object_get(request_data, "id"); - - if (json_is_string(method_holder) && - (json_is_array(params) || (json_is_object(params))) && - json_is_integer(id_holder)) - { - char const * method_name = json_string_value(method_holder); - int id = json_integer_value(id_holder); - struct wf_impl_jsonrpc_request * request = wf_impl_jsonrpc_request_create(id, send, user_data); - struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_get_method(server, method_name); - - method->invoke(request, method_name, params, method->user_data); - } -} - diff --git a/lib/webfuse/adapter/impl/jsonrpc/server.h b/lib/webfuse/adapter/impl/jsonrpc/server.h deleted file mode 100644 index 51c827b..0000000 --- a/lib/webfuse/adapter/impl/jsonrpc/server.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_JSONRPC_SERVER_H -#define WF_ADAPTER_IMPL_JSONRPC_SERVER_H - -#ifndef __cplusplus -#include -#include -#else -#include -#endif - -#include -#include "webfuse/core/status.h" -#include "webfuse/adapter/impl/jsonrpc/send_fn.h" -#include "webfuse/adapter/impl/jsonrpc/method.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_impl_jsonrpc_server -{ - struct wf_impl_jsonrpc_method * methods; -}; - -extern void wf_impl_jsonrpc_server_init( - struct wf_impl_jsonrpc_server * server); - -extern void wf_impl_jsonrpc_server_cleanup( - struct wf_impl_jsonrpc_server * server); - -extern void wf_impl_jsonrpc_server_add( - struct wf_impl_jsonrpc_server * server, - char const * method_name, - wf_impl_jsonrpc_method_invoke_fn * invoke, - void * user_data); - -extern void wf_impl_jsonrpc_server_process( - struct wf_impl_jsonrpc_server * server, - json_t * request, - wf_impl_jsonrpc_send_fn * send, - void * user_data); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/lib/webfuse/adapter/impl/jsonrpc/util.c b/lib/webfuse/adapter/impl/jsonrpc/util.c deleted file mode 100644 index 439eddb..0000000 --- a/lib/webfuse/adapter/impl/jsonrpc/util.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "webfuse/adapter/impl/jsonrpc/util.h" - -int wf_impl_json_get_int(json_t const * object, char const * key, int default_value) -{ - int result = default_value; - - json_t * holder = json_object_get(object, key); - if ((NULL != holder) && (json_is_integer(holder))) - { - result = json_integer_value(holder); - } - - return result; -} \ No newline at end of file diff --git a/lib/webfuse/adapter/impl/jsonrpc/util.h b/lib/webfuse/adapter/impl/jsonrpc/util.h deleted file mode 100644 index 0d5c6ab..0000000 --- a/lib/webfuse/adapter/impl/jsonrpc/util.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_JSON_UTIL_H -#define WF_ADAPTER_IMPL_JSON_UTIL_H - -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -extern int wf_impl_json_get_int(json_t const * object, char const * key, int default_value); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/lib/webfuse/adapter/impl/operation/close.c b/lib/webfuse/adapter/impl/operation/close.c index ca63a8e..fc1b1a0 100644 --- a/lib/webfuse/adapter/impl/operation/close.c +++ b/lib/webfuse/adapter/impl/operation/close.c @@ -4,7 +4,7 @@ #include #include -#include "webfuse/adapter/impl/jsonrpc/proxy.h" +#include "jsonrpc/proxy.h" #include "webfuse/core/util.h" void wf_impl_operation_close( @@ -13,12 +13,12 @@ void wf_impl_operation_close( struct fuse_file_info * file_info) { struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_impl_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { int handle = (int) (file_info->fh & INT_MAX); - wf_impl_jsonrpc_proxy_notify(rpc, "close", "siii", user_data->name, inode, handle, file_info->flags); + jsonrpc_proxy_notify(rpc, "close", "siii", user_data->name, inode, handle, file_info->flags); } fuse_reply_err(request, 0); diff --git a/lib/webfuse/adapter/impl/operation/getattr.c b/lib/webfuse/adapter/impl/operation/getattr.c index cef8eac..f7be79d 100644 --- a/lib/webfuse/adapter/impl/operation/getattr.c +++ b/lib/webfuse/adapter/impl/operation/getattr.c @@ -7,8 +7,8 @@ #include #include -#include "webfuse/adapter/impl/jsonrpc/proxy.h" -#include "webfuse/adapter/impl/jsonrpc/util.h" +#include "jsonrpc/proxy.h" +#include "webfuse/core/json_util.h" #include "webfuse/core/util.h" struct wf_impl_operation_getattr_context @@ -22,16 +22,17 @@ struct wf_impl_operation_getattr_context static void wf_impl_operation_getattr_finished( void * user_data, - wf_status status, - json_t const * data) + json_t const * result, + json_t const * error) { + wf_status status = wf_impl_jsonrpc_get_status(error); struct wf_impl_operation_getattr_context * context = user_data; struct stat buffer; - if (NULL != data) + if (NULL != result) { - json_t * mode_holder = json_object_get(data, "mode"); - json_t * type_holder = json_object_get(data, "type"); + json_t * mode_holder = json_object_get(result, "mode"); + json_t * type_holder = json_object_get(result, "type"); if ((NULL != mode_holder) && (json_is_integer(mode_holder)) && (NULL != type_holder) && (json_is_string(type_holder))) { @@ -52,10 +53,10 @@ static void wf_impl_operation_getattr_finished( buffer.st_uid = context->uid; buffer.st_gid = context->gid; buffer.st_nlink = 1; - buffer.st_size = wf_impl_json_get_int(data, "size", 0); - buffer.st_atime = wf_impl_json_get_int(data, "atime", 0); - buffer.st_mtime = wf_impl_json_get_int(data, "mtime", 0); - buffer.st_ctime = wf_impl_json_get_int(data, "ctime", 0); + buffer.st_size = wf_impl_json_get_int(result, "size", 0); + buffer.st_atime = wf_impl_json_get_int(result, "atime", 0); + buffer.st_mtime = wf_impl_json_get_int(result, "mtime", 0); + buffer.st_ctime = wf_impl_json_get_int(result, "ctime", 0); } else { @@ -82,7 +83,7 @@ void wf_impl_operation_getattr ( { struct fuse_ctx const * context = fuse_req_ctx(request); struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_impl_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { @@ -93,7 +94,7 @@ void wf_impl_operation_getattr ( getattr_context->gid = context->gid; getattr_context->timeout = user_data->timeout; - wf_impl_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_getattr_finished, getattr_context, "getattr", "si", user_data->name, inode); + jsonrpc_proxy_invoke(rpc, &wf_impl_operation_getattr_finished, getattr_context, "getattr", "si", user_data->name, inode); } else { diff --git a/lib/webfuse/adapter/impl/operation/lookup.c b/lib/webfuse/adapter/impl/operation/lookup.c index 3f0cd3b..eaacf8d 100644 --- a/lib/webfuse/adapter/impl/operation/lookup.c +++ b/lib/webfuse/adapter/impl/operation/lookup.c @@ -10,8 +10,8 @@ #include -#include "webfuse/adapter/impl/jsonrpc/proxy.h" -#include "webfuse/adapter/impl/jsonrpc/util.h" +#include "jsonrpc/proxy.h" +#include "webfuse/core/json_util.h" #include "webfuse/core/util.h" struct wf_impl_operation_lookup_context @@ -24,18 +24,19 @@ struct wf_impl_operation_lookup_context static void wf_impl_operation_lookup_finished( void * user_data, - wf_status status, - json_t const * data + json_t const * result, + json_t const * error ) { + wf_status status = wf_impl_jsonrpc_get_status(error); struct wf_impl_operation_lookup_context * context = user_data; struct fuse_entry_param buffer; - if (NULL != data) + if (NULL != result) { - json_t * inode_holder = json_object_get(data, "inode"); - json_t * mode_holder = json_object_get(data, "mode"); - json_t * type_holder = json_object_get(data, "type"); + json_t * inode_holder = json_object_get(result, "inode"); + json_t * mode_holder = json_object_get(result, "mode"); + json_t * type_holder = json_object_get(result, "type"); if ((NULL != inode_holder) && (json_is_integer(inode_holder)) && (NULL != mode_holder) && (json_is_integer(mode_holder)) && (NULL != type_holder) && (json_is_string(type_holder))) @@ -61,10 +62,10 @@ static void wf_impl_operation_lookup_finished( buffer.attr.st_uid = context->uid; buffer.attr.st_gid = context->gid; buffer.attr.st_nlink = 1; - buffer.attr.st_size = wf_impl_json_get_int(data, "size", 0); - buffer.attr.st_atime = wf_impl_json_get_int(data, "atime", 0); - buffer.attr.st_mtime = wf_impl_json_get_int(data, "mtime", 0); - buffer.attr.st_ctime = wf_impl_json_get_int(data, "ctime", 0); + buffer.attr.st_size = wf_impl_json_get_int(result, "size", 0); + buffer.attr.st_atime = wf_impl_json_get_int(result, "atime", 0); + buffer.attr.st_mtime = wf_impl_json_get_int(result, "mtime", 0); + buffer.attr.st_ctime = wf_impl_json_get_int(result, "ctime", 0); } else { @@ -91,7 +92,7 @@ void wf_impl_operation_lookup ( { struct fuse_ctx const * context = fuse_req_ctx(request); struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_impl_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { @@ -101,7 +102,7 @@ void wf_impl_operation_lookup ( lookup_context->gid = context->gid; lookup_context->timeout = user_data->timeout; - wf_impl_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_lookup_finished, lookup_context, "lookup", "sis", user_data->name, (int) (parent & INT_MAX), name); + jsonrpc_proxy_invoke(rpc, &wf_impl_operation_lookup_finished, lookup_context, "lookup", "sis", user_data->name, (int) (parent & INT_MAX), name); } else { diff --git a/lib/webfuse/adapter/impl/operation/open.c b/lib/webfuse/adapter/impl/operation/open.c index e82b865..05fcec3 100644 --- a/lib/webfuse/adapter/impl/operation/open.c +++ b/lib/webfuse/adapter/impl/operation/open.c @@ -4,15 +4,17 @@ #include #include -#include "webfuse/adapter/impl/jsonrpc/proxy.h" +#include "jsonrpc/proxy.h" #include "webfuse/core/util.h" #include "webfuse/core/status.h" +#include "webfuse/core/json_util.h" static void wf_impl_operation_open_finished( void * user_data, - wf_status status, - json_t const * result) + json_t const * result, + json_t const * error) { + wf_status status = wf_impl_jsonrpc_get_status(error); fuse_req_t request = user_data; struct fuse_file_info file_info; memset(&file_info, 0, sizeof(struct fuse_file_info)); @@ -47,11 +49,11 @@ void wf_impl_operation_open( struct fuse_file_info * file_info) { struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_impl_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { - wf_impl_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_open_finished, request, "open", "sii", user_data->name, inode, file_info->flags); + jsonrpc_proxy_invoke(rpc, &wf_impl_operation_open_finished, request, "open", "sii", user_data->name, inode, file_info->flags); } else { diff --git a/lib/webfuse/adapter/impl/operation/read.c b/lib/webfuse/adapter/impl/operation/read.c index ec434c5..b9cfa1a 100644 --- a/lib/webfuse/adapter/impl/operation/read.c +++ b/lib/webfuse/adapter/impl/operation/read.c @@ -5,8 +5,9 @@ #include #include -#include "webfuse/adapter/impl/jsonrpc/proxy.h" +#include "jsonrpc/proxy.h" #include "webfuse/core/base64.h" +#include "webfuse/core/json_util.h" #define WF_MAX_READ_LENGTH 4096 @@ -38,17 +39,21 @@ static char * wf_impl_fill_buffer( return buffer; } -static void wf_impl_operation_read_finished(void * user_data, wf_status status, json_t const * data) +static void wf_impl_operation_read_finished( + void * user_data, + json_t const * result, + json_t const * error) { + wf_status status = wf_impl_jsonrpc_get_status(error); fuse_req_t request = user_data; char * buffer = NULL; size_t length = 0; - if (NULL != data) + if (NULL != result) { - json_t * data_holder = json_object_get(data, "data"); - json_t * format_holder = json_object_get(data, "format"); - json_t * count_holder = json_object_get(data, "count"); + json_t * data_holder = json_object_get(result, "data"); + json_t * format_holder = json_object_get(result, "format"); + json_t * count_holder = json_object_get(result, "count"); if (json_is_string(data_holder) && json_is_string(format_holder) && @@ -87,13 +92,13 @@ void wf_impl_operation_read( struct fuse_file_info * file_info) { struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_impl_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { int const length = (size <= WF_MAX_READ_LENGTH) ? (int) size : WF_MAX_READ_LENGTH; int handle = (file_info->fh & INT_MAX); - wf_impl_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_read_finished, request, "read", "siiii", user_data->name, (int) inode, handle, (int) offset, length); + jsonrpc_proxy_invoke(rpc, &wf_impl_operation_read_finished, request, "read", "siiii", user_data->name, (int) inode, handle, (int) offset, length); } else { diff --git a/lib/webfuse/adapter/impl/operation/readdir.c b/lib/webfuse/adapter/impl/operation/readdir.c index 85cfd5b..76414ea 100644 --- a/lib/webfuse/adapter/impl/operation/readdir.c +++ b/lib/webfuse/adapter/impl/operation/readdir.c @@ -8,8 +8,9 @@ #include #include -#include "webfuse/adapter/impl/jsonrpc/proxy.h" +#include "jsonrpc/proxy.h" #include "webfuse/core/util.h" +#include "webfuse/core/json_util.h" #define WF_DIRBUFFER_INITIAL_SIZE 1024 @@ -73,9 +74,10 @@ static size_t wf_impl_min(size_t a, size_t b) static void wf_impl_operation_readdir_finished( void * user_data, - wf_status status, - json_t const * result) + json_t const * result, + json_t const * error) { + wf_status status = wf_impl_jsonrpc_get_status(error); struct wf_impl_operation_readdir_context * context = user_data; struct wf_impl_dirbuffer buffer; @@ -137,7 +139,7 @@ void wf_impl_operation_readdir ( struct fuse_file_info * WF_UNUSED_PARAM(file_info)) { struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_impl_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { @@ -146,7 +148,7 @@ void wf_impl_operation_readdir ( readdir_context->size = size; readdir_context->offset = offset; - wf_impl_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_readdir_finished, readdir_context, "readdir", "si", user_data->name, inode); + jsonrpc_proxy_invoke(rpc, &wf_impl_operation_readdir_finished, readdir_context, "readdir", "si", user_data->name, inode); } else { diff --git a/lib/webfuse/adapter/impl/operations.c b/lib/webfuse/adapter/impl/operations.c index 393ab99..ac9fb96 100644 --- a/lib/webfuse/adapter/impl/operations.c +++ b/lib/webfuse/adapter/impl/operations.c @@ -3,10 +3,10 @@ #include "webfuse/adapter/impl/session.h" #include -struct wf_impl_jsonrpc_proxy * wf_impl_operations_context_get_proxy( +struct jsonrpc_proxy * wf_impl_operations_context_get_proxy( struct wf_impl_operations_context * context) { - struct wf_impl_jsonrpc_proxy * proxy = NULL; + struct jsonrpc_proxy * proxy = NULL; struct wf_impl_session * session = context->session; if (NULL != session) diff --git a/lib/webfuse/adapter/impl/operations.h b/lib/webfuse/adapter/impl/operations.h index debe4fa..50bcef6 100644 --- a/lib/webfuse/adapter/impl/operations.h +++ b/lib/webfuse/adapter/impl/operations.h @@ -8,7 +8,7 @@ extern "C" { #endif struct wf_impl_session; -struct wf_impl_jsonrpc_proxy; +struct jsonrpc_proxy; struct wf_impl_operations_context { @@ -49,7 +49,7 @@ extern void wf_impl_operation_read( fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi); -extern struct wf_impl_jsonrpc_proxy * wf_impl_operations_context_get_proxy( +extern struct jsonrpc_proxy * wf_impl_operations_context_get_proxy( struct wf_impl_operations_context * context); #ifdef __cplusplus diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index 74d4631..746df5c 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -9,7 +9,7 @@ #include "webfuse/core/protocol_names.h" #include "webfuse/adapter/impl/credentials.h" -#include "webfuse/adapter/impl/jsonrpc/request.h" +#include "jsonrpc/request.h" #include "webfuse/adapter/impl/uuid_mountpoint_factory.h" static int wf_impl_server_protocol_callback( @@ -129,7 +129,7 @@ void wf_impl_server_protocol_init_lws( } static void wf_impl_server_protocol_authenticate( - struct wf_impl_jsonrpc_request * request, + struct jsonrpc_request * request, char const * WF_UNUSED_PARAM(method_name), json_t * params, void * WF_UNUSED_PARAM(user_data)) @@ -145,7 +145,7 @@ static void wf_impl_server_protocol_authenticate( struct wf_credentials creds; wf_impl_credentials_init(&creds, type, creds_holder); - struct wf_impl_session * session = wf_impl_jsonrpc_request_get_userdata(request); + struct wf_impl_session * session = jsonrpc_request_get_userdata(request); result = wf_impl_session_authenticate(session, &creds); wf_impl_credentials_cleanup(&creds); @@ -155,11 +155,11 @@ static void wf_impl_server_protocol_authenticate( if (result) { json_t * result = json_object(); - wf_impl_jsonrpc_respond(request, result); + jsonrpc_respond(request, result); } else { - wf_impl_jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED); + jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED); } } @@ -179,12 +179,12 @@ static bool wf_impl_server_protocol_check_name(char const * value) } static void wf_impl_server_protocol_add_filesystem( - struct wf_impl_jsonrpc_request * request, + struct jsonrpc_request * request, char const * WF_UNUSED_PARAM(method_name), json_t * params, void * WF_UNUSED_PARAM(user_data)) { - struct wf_impl_session * session = wf_impl_jsonrpc_request_get_userdata(request); + struct wf_impl_session * session = jsonrpc_request_get_userdata(request); wf_status status = (session->is_authenticated) ? WF_GOOD : WF_BAD_ACCESS_DENIED; char const * name = NULL; @@ -218,11 +218,11 @@ static void wf_impl_server_protocol_add_filesystem( { json_t * result = json_object(); json_object_set_new(result, "id", json_string(name)); - wf_impl_jsonrpc_respond(request, result); + jsonrpc_respond(request, result); } else { - wf_impl_jsonrpc_respond_error(request, status); + jsonrpc_respond_error(request, status); } @@ -240,9 +240,9 @@ void wf_impl_server_protocol_init( wf_impl_session_manager_init(&protocol->session_manager); wf_impl_authenticators_init(&protocol->authenticators); - wf_impl_jsonrpc_server_init(&protocol->server); - wf_impl_jsonrpc_server_add(&protocol->server, "authenticate", &wf_impl_server_protocol_authenticate, protocol); - wf_impl_jsonrpc_server_add(&protocol->server, "add_filesystem", &wf_impl_server_protocol_add_filesystem, protocol); + 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); } void wf_impl_server_protocol_cleanup( @@ -250,7 +250,7 @@ void wf_impl_server_protocol_cleanup( { protocol->is_operational = false; - wf_impl_jsonrpc_server_cleanup(&protocol->server); + jsonrpc_server_cleanup(&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 16da34d..85c94c1 100644 --- a/lib/webfuse/adapter/impl/server_protocol.h +++ b/lib/webfuse/adapter/impl/server_protocol.h @@ -1,12 +1,12 @@ #ifndef WF_ADAPTER_IMPL_SERVER_PROTOCOL_H #define WF_ADAPTER_IMPL_SERVER_PROTOCOL_H -#include "webfuse/adapter/impl/jsonrpc/proxy.h" +#include "jsonrpc/proxy.h" #include "webfuse/adapter/impl/time/timeout_manager.h" #include "webfuse/adapter/impl/authenticators.h" #include "webfuse/adapter/impl/mountpoint_factory.h" #include "webfuse/adapter/impl/session_manager.h" -#include "webfuse/adapter/impl/jsonrpc/server.h" +#include "jsonrpc/server.h" #ifndef __cplusplus #include @@ -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 wf_impl_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 fb2885a..5690c4f 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -2,9 +2,9 @@ #include "webfuse/adapter/impl/authenticators.h" #include "webfuse/core/message_queue.h" #include "webfuse/core/message.h" -#include "webfuse/adapter/impl/jsonrpc/proxy.h" -#include "webfuse/adapter/impl/jsonrpc/request.h" -#include "webfuse/adapter/impl/jsonrpc/response.h" +#include "jsonrpc/proxy.h" +#include "jsonrpc/request.h" +#include "jsonrpc/response.h" #include "webfuse/adapter/impl/mountpoint_factory.h" #include "webfuse/adapter/impl/mountpoint.h" @@ -24,7 +24,7 @@ static bool wf_impl_session_send( struct wf_impl_session * session = user_data; struct wf_message * message = wf_message_create(request); - bool result = (session->is_authenticated || wf_impl_jsonrpc_is_response(request)) && (NULL != session->wsi); + bool result = (session->is_authenticated || jsonrpc_is_response(request)) && (NULL != session->wsi); if (result) { @@ -45,7 +45,7 @@ struct wf_impl_session * wf_impl_session_create( struct lws * wsi, struct wf_impl_authenticators * authenticators, struct wf_impl_timeout_manager * timeout_manager, - struct wf_impl_jsonrpc_server * server, + struct jsonrpc_server * server, struct wf_impl_mountpoint_factory * mountpoint_factory) { @@ -59,7 +59,7 @@ struct wf_impl_session * wf_impl_session_create( session->authenticators = authenticators; session->server = server; session->mountpoint_factory = mountpoint_factory; - wf_impl_jsonrpc_proxy_init(&session->rpc, timeout_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); + jsonrpc_proxy_init(&session->rpc, timeout_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); wf_slist_init(&session->messages); } @@ -83,7 +83,7 @@ static void wf_impl_session_dispose_filesystems( void wf_impl_session_dispose( struct wf_impl_session * session) { - wf_impl_jsonrpc_proxy_cleanup(&session->rpc); + jsonrpc_proxy_cleanup(&session->rpc); wf_message_queue_cleanup(&session->messages); wf_impl_session_dispose_filesystems(&session->filesystems); @@ -159,13 +159,13 @@ void wf_impl_session_receive( json_t * message = json_loadb(data, length, 0, NULL); if (NULL != message) { - if (wf_impl_jsonrpc_is_response(message)) + if (jsonrpc_is_response(message)) { - wf_impl_jsonrpc_proxy_onresult(&session->rpc, message); + jsonrpc_proxy_onresult(&session->rpc, message); } - else if (wf_impl_jsonrpc_is_request(message)) + else if (jsonrpc_is_request(message)) { - wf_impl_jsonrpc_server_process(session->server, message, &wf_impl_session_send, session); + jsonrpc_server_process(session->server, message, &wf_impl_session_send, session); } json_decref(message); diff --git a/lib/webfuse/adapter/impl/session.h b/lib/webfuse/adapter/impl/session.h index 9f9b10a..90f8f4e 100644 --- a/lib/webfuse/adapter/impl/session.h +++ b/lib/webfuse/adapter/impl/session.h @@ -10,8 +10,8 @@ using std::size_t; #endif #include "webfuse/core/message_queue.h" -#include "webfuse/adapter/impl/jsonrpc/proxy.h" -#include "webfuse/adapter/impl/jsonrpc/server.h" +#include "jsonrpc/proxy.h" +#include "jsonrpc/server.h" #include "webfuse/adapter/impl/filesystem.h" #include "webfuse/core/slist.h" @@ -35,8 +35,8 @@ struct wf_impl_session struct wf_slist messages; struct wf_impl_authenticators * authenticators; struct wf_impl_mountpoint_factory * mountpoint_factory; - struct wf_impl_jsonrpc_server * server; - struct wf_impl_jsonrpc_proxy rpc; + struct jsonrpc_server * server; + struct jsonrpc_proxy rpc; struct wf_slist filesystems; }; @@ -44,7 +44,7 @@ extern struct wf_impl_session * wf_impl_session_create( struct lws * wsi, struct wf_impl_authenticators * authenticators, struct wf_impl_timeout_manager * timeout_manager, - struct wf_impl_jsonrpc_server * server, + struct jsonrpc_server * server, struct wf_impl_mountpoint_factory * mountpoint_factory); extern void wf_impl_session_dispose( diff --git a/lib/webfuse/adapter/impl/session_manager.c b/lib/webfuse/adapter/impl/session_manager.c index 0574c98..e7e109d 100644 --- a/lib/webfuse/adapter/impl/session_manager.c +++ b/lib/webfuse/adapter/impl/session_manager.c @@ -29,7 +29,7 @@ struct wf_impl_session * wf_impl_session_manager_add( struct wf_impl_authenticators * authenticators, struct wf_impl_mountpoint_factory * mountpoint_factory, struct wf_impl_timeout_manager * timeout_manager, - struct wf_impl_jsonrpc_server * server) + struct jsonrpc_server * server) { struct wf_impl_session * session = wf_impl_session_create( wsi, authenticators, timeout_manager, server, mountpoint_factory); diff --git a/lib/webfuse/adapter/impl/session_manager.h b/lib/webfuse/adapter/impl/session_manager.h index 25b4367..51c8910 100644 --- a/lib/webfuse/adapter/impl/session_manager.h +++ b/lib/webfuse/adapter/impl/session_manager.h @@ -16,7 +16,7 @@ extern "C" struct lws; struct wf_impl_timeout_manager; -struct wf_impl_jsonrpc_server; +struct jsonrpc_server; struct wf_impl_session_manager { @@ -35,7 +35,7 @@ extern struct wf_impl_session * wf_impl_session_manager_add( struct wf_impl_authenticators * authenticators, struct wf_impl_mountpoint_factory * mountpoint_factory, struct wf_impl_timeout_manager * timeout_manager, - struct wf_impl_jsonrpc_server * server); + struct jsonrpc_server * server); extern struct wf_impl_session * wf_impl_session_manager_get( struct wf_impl_session_manager * manager, diff --git a/lib/webfuse/core/json_util.c b/lib/webfuse/core/json_util.c new file mode 100644 index 0000000..dfe95cd --- /dev/null +++ b/lib/webfuse/core/json_util.c @@ -0,0 +1,48 @@ +#include "webfuse/core/json_util.h" +#include "jsonrpc/status.h" + +int wf_impl_json_get_int(json_t const * object, char const * key, int default_value) +{ + int result = default_value; + + json_t * holder = json_object_get(object, key); + if ((NULL != holder) && (json_is_integer(holder))) + { + result = json_integer_value(holder); + } + + return result; +} + +static wf_status wf_impl_jsonrc_code_to_status(int code) +{ + switch (code) + { + case JSONRPC_GOOD: + return WF_GOOD; + case JSONRPC_BAD: + return WF_BAD; + case JSONRPC_BAD_BUSY: + return WF_BAD_BUSY; + case JSONRPC_BAD_TIMEOUT: + return WF_BAD_TIMEOUT; + case JSONRPC_BAD_FORMAT: + return WF_BAD_FORMAT; + default: + return (wf_status) code; + } +} + +wf_status +wf_impl_jsonrpc_get_status( + json_t const * error) +{ + wf_status status = WF_GOOD; + if (NULL != error) + { + int code = wf_impl_json_get_int(error, "code", WF_BAD_FORMAT); + status = wf_impl_jsonrc_code_to_status(code); + } + + return status; +} diff --git a/lib/webfuse/core/json_util.h b/lib/webfuse/core/json_util.h new file mode 100644 index 0000000..db21ed1 --- /dev/null +++ b/lib/webfuse/core/json_util.h @@ -0,0 +1,26 @@ +#ifndef WF_JSON_UTIL_H +#define WF_JSON_UTIL_H + +#include +#include "webfuse/core/status.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern int +wf_impl_json_get_int( + json_t const * object, + char const * key, + int default_value); + +extern wf_status +wf_impl_jsonrpc_get_status( + json_t const * error); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/test/webfuse/tests/adapter/jsonrpc/test_is_request.cc b/test/jsonrpc/test_is_request.cc similarity index 81% rename from test/webfuse/tests/adapter/jsonrpc/test_is_request.cc rename to test/jsonrpc/test_is_request.cc index 5fef81e..03d95f7 100644 --- a/test/webfuse/tests/adapter/jsonrpc/test_is_request.cc +++ b/test/jsonrpc/test_is_request.cc @@ -1,5 +1,5 @@ #include -#include "webfuse/adapter/impl/jsonrpc/request.h" +#include "jsonrpc/request.h" TEST(jsonrpc_is_request, request_with_object_params) { @@ -8,7 +8,7 @@ TEST(jsonrpc_is_request, request_with_object_params) json_object_set_new(request, "params", json_object()); json_object_set_new(request, "id", json_integer(42)); - ASSERT_TRUE(wf_impl_jsonrpc_is_request(request)); + ASSERT_TRUE(jsonrpc_is_request(request)); json_decref(request); } @@ -20,14 +20,14 @@ TEST(jsonrpc_is_request, request_with_array_params) json_object_set_new(request, "params", json_array()); json_object_set_new(request, "id", json_integer(42)); - ASSERT_TRUE(wf_impl_jsonrpc_is_request(request)); + ASSERT_TRUE(jsonrpc_is_request(request)); json_decref(request); } TEST(jsonrpc_is_request, null_request) { - ASSERT_FALSE(wf_impl_jsonrpc_is_request(nullptr)); + ASSERT_FALSE(jsonrpc_is_request(nullptr)); } TEST(jsonrpc_is_request, invalid_request) @@ -37,7 +37,7 @@ TEST(jsonrpc_is_request, invalid_request) json_array_append_new(request, json_object()); json_array_append_new(request, json_integer(42)); - ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); + ASSERT_FALSE(jsonrpc_is_request(request)); json_decref(request); } @@ -48,7 +48,7 @@ TEST(jsonrpc_is_request, invalid_request_without_id) json_object_set_new(request, "method", json_string("method")); json_object_set_new(request, "params", json_object()); - ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); + ASSERT_FALSE(jsonrpc_is_request(request)); json_decref(request); } @@ -60,7 +60,7 @@ TEST(jsonrpc_is_request, invalid_request_due_to_invalid_id) json_object_set_new(request, "params", json_object()); json_object_set_new(request, "id", json_string("42")); - ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); + ASSERT_FALSE(jsonrpc_is_request(request)); json_decref(request); } @@ -71,7 +71,7 @@ TEST(jsonrpc_is_request, invalid_request_without_method) json_object_set_new(request, "params", json_object()); json_object_set_new(request, "id", json_integer(42)); - ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); + ASSERT_FALSE(jsonrpc_is_request(request)); json_decref(request); } @@ -83,7 +83,7 @@ TEST(jsonrpc_is_request, invalid_request_due_to_invalid_method) json_object_set_new(request, "params", json_object()); json_object_set_new(request, "id", json_integer(42)); - ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); + ASSERT_FALSE(jsonrpc_is_request(request)); json_decref(request); } @@ -94,7 +94,7 @@ TEST(jsonrpc_is_request, invalid_request_without_params) json_object_set_new(request, "method", json_string("method")); json_object_set_new(request, "id", json_integer(42)); - ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); + ASSERT_FALSE(jsonrpc_is_request(request)); json_decref(request); } @@ -106,7 +106,7 @@ TEST(jsonrpc_is_request, invalid_request_due_to_invalid_params) json_object_set_new(request, "params", json_string("params")); json_object_set_new(request, "id", json_integer(42)); - ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); + ASSERT_FALSE(jsonrpc_is_request(request)); json_decref(request); } diff --git a/test/webfuse/tests/adapter/jsonrpc/test_is_response.cc b/test/jsonrpc/test_is_response.cc similarity index 76% rename from test/webfuse/tests/adapter/jsonrpc/test_is_response.cc rename to test/jsonrpc/test_is_response.cc index 6182cd0..b32a284 100644 --- a/test/webfuse/tests/adapter/jsonrpc/test_is_response.cc +++ b/test/jsonrpc/test_is_response.cc @@ -1,5 +1,5 @@ #include -#include "webfuse/adapter/impl/jsonrpc/response.h" +#include "jsonrpc/response.h" TEST(jsonrpc_is_response, valid_result) { @@ -7,7 +7,7 @@ TEST(jsonrpc_is_response, valid_result) json_object_set_new(message, "result", json_object()); json_object_set_new(message, "id", json_integer(42)); - ASSERT_TRUE(wf_impl_jsonrpc_is_response(message)); + ASSERT_TRUE(jsonrpc_is_response(message)); json_decref(message); } @@ -18,7 +18,7 @@ TEST(jsonrpc_is_response, valid_result_string) json_object_set_new(message, "result", json_string("also valid")); json_object_set_new(message, "id", json_integer(42)); - ASSERT_TRUE(wf_impl_jsonrpc_is_response(message)); + ASSERT_TRUE(jsonrpc_is_response(message)); json_decref(message); } @@ -29,14 +29,14 @@ TEST(jsonrpc_is_response, valid_error) json_object_set_new(message, "error", json_object()); json_object_set_new(message, "id", json_integer(42)); - ASSERT_TRUE(wf_impl_jsonrpc_is_response(message)); + ASSERT_TRUE(jsonrpc_is_response(message)); json_decref(message); } TEST(jsonrpc_is_response, invalid_null) { - ASSERT_FALSE(wf_impl_jsonrpc_is_response(nullptr)); + ASSERT_FALSE(jsonrpc_is_response(nullptr)); } TEST(jsonrpc_is_response, invalid_message) @@ -45,7 +45,7 @@ TEST(jsonrpc_is_response, invalid_message) json_array_append_new(message, json_object()); json_array_append_new(message, json_integer(42)); - ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); + ASSERT_FALSE(jsonrpc_is_response(message)); json_decref(message); } @@ -55,7 +55,7 @@ TEST(jsonrpc_is_response, invalid_missing_id) json_t * message = json_object(); json_object_set_new(message, "result", json_object()); - ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); + ASSERT_FALSE(jsonrpc_is_response(message)); json_decref(message); } @@ -66,7 +66,7 @@ TEST(jsonrpc_is_response, invalid_id_wrong_type) json_object_set_new(message, "result", json_object()); json_object_set_new(message, "id", json_string("42")); - ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); + ASSERT_FALSE(jsonrpc_is_response(message)); json_decref(message); } @@ -77,7 +77,7 @@ TEST(jsonrpc_is_response, invalid_missing_result_and_error) json_t * message = json_object(); json_object_set_new(message, "id", json_integer(42)); - ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); + ASSERT_FALSE(jsonrpc_is_response(message)); json_decref(message); } @@ -88,7 +88,7 @@ TEST(jsonrpc_is_response, invalid_error_wrong_type) json_object_set_new(message, "error", json_array()); json_object_set_new(message, "id", json_integer(42)); - ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); + ASSERT_FALSE(jsonrpc_is_response(message)); json_decref(message); } diff --git a/test/webfuse/tests/adapter/jsonrpc/test_proxy.cc b/test/jsonrpc/test_proxy.cc similarity index 72% rename from test/webfuse/tests/adapter/jsonrpc/test_proxy.cc rename to test/jsonrpc/test_proxy.cc index 8f99312..79fb090 100644 --- a/test/webfuse/tests/adapter/jsonrpc/test_proxy.cc +++ b/test/jsonrpc/test_proxy.cc @@ -1,7 +1,8 @@ #include -#include "webfuse/adapter/impl/jsonrpc/proxy.h" +#include "jsonrpc/proxy.h" #include "webfuse/adapter/impl/time/timeout_manager.h" #include "webfuse/utils/msleep.hpp" +#include "webfuse/core/json_util.h" using webfuse_test::msleep; @@ -47,13 +48,13 @@ namespace struct FinishedContext { bool is_called; - wf_status status; json_t * result; + json_t * error; FinishedContext() : is_called(false) - , status(WF_BAD) , result(nullptr) + , error(nullptr) { } @@ -64,18 +65,23 @@ namespace { json_decref(result); } + + if (nullptr != error) + { + json_decref(error); + } } }; void jsonrpc_finished( void * user_data, - wf_status status, - struct json_t const * result) + json_t const * result, + json_t const * error) { FinishedContext * context = reinterpret_cast(user_data); context->is_called = true; - context->status = status; context->result = json_deep_copy(result); + context->error = json_deep_copy(error); } } @@ -86,10 +92,10 @@ TEST(jsonrpc_proxy, init) SendContext context; void * user_data = reinterpret_cast(&context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); ASSERT_FALSE(context.is_called); @@ -102,12 +108,12 @@ TEST(jsonrpc_proxy, invoke) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - wf_impl_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)); @@ -129,11 +135,11 @@ TEST(jsonrpc_proxy, invoke) ASSERT_FALSE(finished_context.is_called); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); ASSERT_TRUE(finished_context.is_called); - ASSERT_FALSE(WF_GOOD == finished_context.status); + ASSERT_FALSE(nullptr == finished_context.error); } TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails) @@ -143,20 +149,20 @@ TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails) SendContext send_context(false); void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - wf_impl_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)); ASSERT_TRUE(finished_context.is_called); - ASSERT_FALSE(WF_GOOD == finished_context.status); + ASSERT_FALSE(nullptr == finished_context.error); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -167,16 +173,16 @@ TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - wf_impl_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); - wf_impl_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)); @@ -184,9 +190,9 @@ TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending) ASSERT_FALSE(finished_context.is_called); ASSERT_TRUE(finished_context2.is_called); - ASSERT_EQ(WF_BAD_BUSY, finished_context2.status); + ASSERT_EQ(WF_BAD_BUSY, wf_impl_jsonrpc_get_status(finished_context2.error)); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -197,19 +203,19 @@ TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - wf_impl_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, finished_context.status); + ASSERT_EQ(WF_BAD, wf_impl_jsonrpc_get_status(finished_context.error)); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -220,12 +226,12 @@ TEST(jsonrpc_proxy, on_result) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - wf_impl_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)); @@ -237,15 +243,15 @@ TEST(jsonrpc_proxy, on_result) json_object_set_new(response, "result", json_string("okay")); json_object_set(response, "id", id); - wf_impl_jsonrpc_proxy_onresult(&proxy, response); + jsonrpc_proxy_onresult(&proxy, response); json_decref(response); ASSERT_TRUE(finished_context.is_called); - ASSERT_EQ(WF_GOOD, finished_context.status); + ASSERT_EQ(nullptr, finished_context.error); ASSERT_TRUE(json_is_string(finished_context.result)); ASSERT_STREQ("okay", json_string_value(finished_context.result)); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -256,12 +262,12 @@ TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - wf_impl_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)); @@ -273,12 +279,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))); - wf_impl_jsonrpc_proxy_onresult(&proxy, response); + jsonrpc_proxy_onresult(&proxy, response); json_decref(response); ASSERT_FALSE(finished_context.is_called); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -289,12 +295,12 @@ TEST(jsonrpc_proxy, timeout) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, 0, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, 0, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - wf_impl_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)); @@ -303,9 +309,9 @@ TEST(jsonrpc_proxy, timeout) wf_impl_timeout_manager_check(&timeout_manager); ASSERT_TRUE(finished_context.is_called); - ASSERT_EQ(WF_BAD_TIMEOUT, finished_context.status); + ASSERT_EQ(WF_BAD_TIMEOUT, wf_impl_jsonrpc_get_status(finished_context.error)); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -316,12 +322,12 @@ TEST(jsonrpc_proxy, cleanup_pending_request) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, 10, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, 10, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); - wf_impl_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)); @@ -329,7 +335,7 @@ TEST(jsonrpc_proxy, cleanup_pending_request) ASSERT_FALSE(finished_context.is_called); ASSERT_NE(nullptr, timeout_manager.timers); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); ASSERT_TRUE(finished_context.is_called); ASSERT_EQ(nullptr, timeout_manager.timers); @@ -346,10 +352,10 @@ TEST(jsonrpc_proxy, notify) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); - wf_impl_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)); @@ -370,7 +376,7 @@ TEST(jsonrpc_proxy, notify) ASSERT_EQ(nullptr, id); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } @@ -381,13 +387,13 @@ TEST(jsonrpc_proxy, notify_dont_send_invalid_request) SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct wf_impl_jsonrpc_proxy proxy; - wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy proxy; + jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); - wf_impl_jsonrpc_proxy_notify(&proxy, "foo", "?"); + jsonrpc_proxy_notify(&proxy, "foo", "?"); ASSERT_FALSE(send_context.is_called); - wf_impl_jsonrpc_proxy_cleanup(&proxy); + jsonrpc_proxy_cleanup(&proxy); wf_impl_timeout_manager_cleanup(&timeout_manager); } diff --git a/test/webfuse/tests/adapter/jsonrpc/test_request.cc b/test/jsonrpc/test_request.cc similarity index 75% rename from test/webfuse/tests/adapter/jsonrpc/test_request.cc rename to test/jsonrpc/test_request.cc index 07c3da8..94f87ee 100644 --- a/test/webfuse/tests/adapter/jsonrpc/test_request.cc +++ b/test/jsonrpc/test_request.cc @@ -1,5 +1,5 @@ #include -#include "webfuse/adapter/impl/jsonrpc/request.h" +#include "jsonrpc/request.h" namespace { @@ -27,13 +27,13 @@ TEST(jsonrpc_request, create_dispose) Context context{nullptr}; void * user_data = reinterpret_cast(&context); - struct wf_impl_jsonrpc_request * request = - wf_impl_jsonrpc_request_create(42, &jsonrpc_send, user_data); + struct jsonrpc_request * request = + jsonrpc_request_create(42, &jsonrpc_send, user_data); ASSERT_NE(nullptr, request); - ASSERT_EQ(user_data, wf_impl_jsonrpc_request_get_userdata(request)); + ASSERT_EQ(user_data, jsonrpc_request_get_userdata(request)); - wf_impl_jsonrpc_request_dispose(request); + jsonrpc_request_dispose(request); } TEST(jsonrpc_request, respond) @@ -41,10 +41,10 @@ TEST(jsonrpc_request, respond) Context context{nullptr}; void * user_data = reinterpret_cast(&context); - struct wf_impl_jsonrpc_request * request = - wf_impl_jsonrpc_request_create(42, &jsonrpc_send, user_data); + struct jsonrpc_request * request = + jsonrpc_request_create(42, &jsonrpc_send, user_data); - wf_impl_jsonrpc_respond(request, json_string("okay")); + jsonrpc_respond(request, json_string("okay")); ASSERT_NE(nullptr, context.response); @@ -70,10 +70,10 @@ TEST(jsonrpc_request, respond_error) Context context{nullptr}; void * user_data = reinterpret_cast(&context); - struct wf_impl_jsonrpc_request * request = - wf_impl_jsonrpc_request_create(42, &jsonrpc_send, user_data); + struct jsonrpc_request * request = + jsonrpc_request_create(42, &jsonrpc_send, user_data); - wf_impl_jsonrpc_respond_error(request, WF_BAD); + jsonrpc_respond_error(request, WF_BAD); ASSERT_NE(nullptr, context.response); diff --git a/test/webfuse/tests/adapter/jsonrpc/test_response.cc b/test/jsonrpc/test_response.cc similarity index 59% rename from test/webfuse/tests/adapter/jsonrpc/test_response.cc rename to test/jsonrpc/test_response.cc index 3a63d0f..1745bdb 100644 --- a/test/webfuse/tests/adapter/jsonrpc/test_response.cc +++ b/test/jsonrpc/test_response.cc @@ -1,5 +1,7 @@ #include -#include "webfuse/adapter/impl/jsonrpc/response.h" +#include "jsonrpc/response.h" +#include "webfuse/core/status.h" +#include "webfuse/core/json_util.h" TEST(json_response, init_result) { @@ -7,15 +9,15 @@ TEST(json_response, init_result) json_object_set_new(message, "result", json_integer(47)); json_object_set_new(message, "id", json_integer(11)); - struct wf_impl_jsonrpc_response response; - wf_impl_jsonrpc_response_init(&response, message); + struct jsonrpc_response response; + jsonrpc_response_init(&response, message); - ASSERT_EQ(WF_GOOD, response.status); + 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_impl_jsonrpc_response_cleanup(&response); + jsonrpc_response_cleanup(&response); json_decref(message); } @@ -28,14 +30,14 @@ TEST(json_response, init_error) json_object_set_new(message, "error", err); json_object_set_new(message, "id", json_integer(23)); - struct wf_impl_jsonrpc_response response; - wf_impl_jsonrpc_response_init(&response, message); + struct jsonrpc_response response; + jsonrpc_response_init(&response, message); - ASSERT_EQ(WF_BAD_ACCESS_DENIED, response.status); + 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); - wf_impl_jsonrpc_response_cleanup(&response); + jsonrpc_response_cleanup(&response); json_decref(message); } @@ -44,13 +46,13 @@ TEST(json_response, init_format_error) json_t * message = json_object(); json_object_set_new(message, "id", json_integer(12)); - struct wf_impl_jsonrpc_response response; - wf_impl_jsonrpc_response_init(&response, message); + struct jsonrpc_response response; + jsonrpc_response_init(&response, message); - ASSERT_EQ(WF_BAD_FORMAT, response.status); + ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_get_status(response.error)); ASSERT_EQ(nullptr, response.result); ASSERT_EQ(12, response.id); - wf_impl_jsonrpc_response_cleanup(&response); + jsonrpc_response_cleanup(&response); json_decref(message); } diff --git a/test/webfuse/tests/adapter/jsonrpc/test_server.cc b/test/jsonrpc/test_server.cc similarity index 75% rename from test/webfuse/tests/adapter/jsonrpc/test_server.cc rename to test/jsonrpc/test_server.cc index 20d5d0f..4e0b2bc 100644 --- a/test/webfuse/tests/adapter/jsonrpc/test_server.cc +++ b/test/jsonrpc/test_server.cc @@ -1,6 +1,6 @@ #include -#include "webfuse/adapter/impl/jsonrpc/server.h" -#include "webfuse/adapter/impl/jsonrpc/request.h" +#include "jsonrpc/server.h" +#include "jsonrpc/request.h" namespace { @@ -23,7 +23,7 @@ namespace } void sayHello( - struct wf_impl_jsonrpc_request * request, + struct jsonrpc_request * request, char const * method_name, json_t * params, void * user_data) @@ -33,16 +33,16 @@ namespace (void) user_data; json_t * result = json_string("Hello"); - wf_impl_jsonrpc_respond(request, result); + jsonrpc_respond(request, result); } } TEST(jsonrpc_server, process_request) { - struct wf_impl_jsonrpc_server server; - wf_impl_jsonrpc_server_init(&server); - wf_impl_jsonrpc_server_add(&server, "sayHello", &sayHello, nullptr); + struct jsonrpc_server server; + jsonrpc_server_init(&server); + 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)); - wf_impl_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,14 @@ TEST(jsonrpc_server, process_request) json_decref(context.response); json_decref(request); - wf_impl_jsonrpc_server_cleanup(&server); + jsonrpc_server_cleanup(&server); } TEST(jsonrpc_server, invoke_unknown_method) { - struct wf_impl_jsonrpc_server server; - wf_impl_jsonrpc_server_init(&server); - wf_impl_jsonrpc_server_add(&server, "sayHello", &sayHello, nullptr); + struct jsonrpc_server server; + jsonrpc_server_init(&server); + jsonrpc_server_add(&server, "sayHello", &sayHello, nullptr); Context context{nullptr, false}; void * user_data = reinterpret_cast(&context); @@ -81,7 +81,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)); - wf_impl_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); @@ -103,23 +103,23 @@ TEST(jsonrpc_server, invoke_unknown_method) json_decref(context.response); json_decref(request); - wf_impl_jsonrpc_server_cleanup(&server); + jsonrpc_server_cleanup(&server); } TEST(jsonrpc_server, skip_invalid_request) { - struct wf_impl_jsonrpc_server server; - wf_impl_jsonrpc_server_init(&server); + struct jsonrpc_server server; + jsonrpc_server_init(&server); 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()); - wf_impl_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); - wf_impl_jsonrpc_server_cleanup(&server); + jsonrpc_server_cleanup(&server); } diff --git a/test/webfuse/tests/adapter/jsonrpc/test_util.cc b/test/jsonrpc/test_util.cc similarity index 95% rename from test/webfuse/tests/adapter/jsonrpc/test_util.cc rename to test/jsonrpc/test_util.cc index 562b7c4..3e4b75d 100644 --- a/test/webfuse/tests/adapter/jsonrpc/test_util.cc +++ b/test/jsonrpc/test_util.cc @@ -1,5 +1,5 @@ #include -#include "webfuse/adapter/impl/jsonrpc/util.h" +#include "webfuse/core/json_util.h" TEST(jsonrpc_util, get_int) { diff --git a/test/webfuse/mocks/mock_provider_client.cc b/test/webfuse/mocks/mock_provider_client.cc index a624ba3..ff73bd9 100644 --- a/test/webfuse/mocks/mock_provider_client.cc +++ b/test/webfuse/mocks/mock_provider_client.cc @@ -167,6 +167,22 @@ static void webfuse_test_iproviderclient_onread( delete[] data; } +static void webfuse_test_iproviderclient_get_credentials( + wfp_credentials * credentials, + void * user_data) +{ + auto * self = reinterpret_cast(user_data); + + try + { + self->GetCredentials(credentials); + } + catch (...) + { + // swallow + } +} + } namespace webfuse_test @@ -185,7 +201,7 @@ wf_status ProviderClientException::GetErrorCode() } -void IProviderClient::AttachTo(wfp_client_config * config) +void IProviderClient::AttachTo(wfp_client_config * config, bool enableAuthentication) { void * self = reinterpret_cast(this); wfp_client_config_set_userdata(config, self); @@ -199,6 +215,11 @@ void IProviderClient::AttachTo(wfp_client_config * config) wfp_client_config_set_onopen(config, &webfuse_test_iproviderclient_onopen); wfp_client_config_set_onclose(config, &webfuse_test_iproviderclient_onclose); wfp_client_config_set_onread(config, &webfuse_test_iproviderclient_onread); + + if (enableAuthentication) + { + wfp_client_config_enable_authentication(config, &webfuse_test_iproviderclient_get_credentials); + } } } \ No newline at end of file diff --git a/test/webfuse/mocks/mock_provider_client.hpp b/test/webfuse/mocks/mock_provider_client.hpp index 45fdf91..57db042 100644 --- a/test/webfuse/mocks/mock_provider_client.hpp +++ b/test/webfuse/mocks/mock_provider_client.hpp @@ -16,8 +16,6 @@ namespace webfuse_test private: wf_status error_code_; }; - - class IProviderClient { @@ -32,8 +30,9 @@ namespace webfuse_test virtual void Open(ino_t inode, int flags, uint32_t * handle) = 0; virtual void Close(ino_t inode, uint32_t handle, int flags) = 0; virtual void Read(ino_t inode, uint32_t handle, size_t offset, size_t length, char * buffer, size_t * bytes_read) = 0; + virtual void GetCredentials(wfp_credentials * credentials) = 0; - void AttachTo(wfp_client_config * config); + void AttachTo(wfp_client_config * config, bool enableAuthentication = false); }; class MockProviderClient: public IProviderClient @@ -49,6 +48,7 @@ namespace webfuse_test MOCK_METHOD3( Open, void(ino_t inode, int flags, uint32_t * handle)); MOCK_METHOD3( Close, void(ino_t inode, uint32_t handle, int flags)); MOCK_METHOD6( Read, void(ino_t inode, uint32_t handle, size_t offset, size_t length, char * buffer, size_t * bytes_read)); + MOCK_METHOD1( GetCredentials, void (wfp_credentials * credentials)); }; } diff --git a/test/webfuse/tests/adapter/test_response_parser.cc b/test/webfuse/tests/adapter/test_response_parser.cc index db7fe2f..0973e9c 100644 --- a/test/webfuse/tests/adapter/test_response_parser.cc +++ b/test/webfuse/tests/adapter/test_response_parser.cc @@ -1,54 +1,59 @@ #include #include -#include "webfuse/adapter/impl/jsonrpc/response.h" +#include "jsonrpc/response.h" +#include "webfuse/core/json_util.h" static void response_parse_str( std::string const & buffer, - struct wf_impl_jsonrpc_response * response) + struct jsonrpc_response * response) { json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr); if (nullptr != message) { - wf_impl_jsonrpc_response_init(response, message); + jsonrpc_response_init(response, message); json_decref(message); } } TEST(response_parser, test) { - struct wf_impl_jsonrpc_response response; + struct jsonrpc_response response; // no object response_parse_str("[]", &response); - ASSERT_NE(WF_GOOD, response.status); + ASSERT_NE(nullptr, response.error); ASSERT_EQ(-1, response.id); ASSERT_EQ(nullptr, response.result); + jsonrpc_response_cleanup(&response); // empty response_parse_str("{}", &response); - ASSERT_NE(WF_GOOD, response.status); + ASSERT_NE(nullptr, response.error); ASSERT_EQ(-1, response.id); ASSERT_EQ(nullptr, response.result); + jsonrpc_response_cleanup(&response); // no data response_parse_str("{\"id\":42}", &response); - ASSERT_NE(WF_GOOD, response.status); + ASSERT_NE(nullptr, response.error); ASSERT_EQ(42, response.id); ASSERT_EQ(nullptr, response.result); + jsonrpc_response_cleanup(&response); // custom error code response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response); - ASSERT_NE(WF_GOOD, response.status); - ASSERT_EQ(42, response.status); + ASSERT_NE(nullptr, response.error); + ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code"))); ASSERT_EQ(42, response.id); ASSERT_EQ(nullptr, response.result); + jsonrpc_response_cleanup(&response); // valid response response_parse_str("{\"result\": true, \"id\": 42}", &response); - ASSERT_EQ(WF_GOOD, response.status); + ASSERT_EQ(WF_GOOD, response.error); ASSERT_EQ(42, response.id); ASSERT_NE(nullptr, response.result); - json_decref(response.result); + jsonrpc_response_cleanup(&response); } diff --git a/test/webfuse/tests/provider/test_client_protocol.cc b/test/webfuse/tests/provider/test_client_protocol.cc index b9d6252..53529c7 100644 --- a/test/webfuse/tests/provider/test_client_protocol.cc +++ b/test/webfuse/tests/provider/test_client_protocol.cc @@ -15,6 +15,7 @@ using webfuse_test::MockProviderClient; using webfuse_test::IProviderClient; using testing::_; using testing::AtMost; +using testing::Invoke; namespace { @@ -24,10 +25,10 @@ class ClientProtocolFixture ClientProtocolFixture(ClientProtocolFixture const &) = delete; ClientProtocolFixture& operator=(ClientProtocolFixture const &) = delete; public: - explicit ClientProtocolFixture(IProviderClient& client) + explicit ClientProtocolFixture(IProviderClient& client, bool enableAuthentication = false) { config = wfp_client_config_create(); - client.AttachTo(config); + client.AttachTo(config, enableAuthentication); protocol = wfp_client_protocol_create(config); @@ -140,6 +141,13 @@ private: wfp_client_protocol * protocol; }; +void GetCredentials(wfp_credentials * credentials) +{ + wfp_credentials_set_type(credentials, "username"); + wfp_credentials_add(credentials, "username", "bob"); + wfp_credentials_add(credentials, "password", "secret"); +} + } TEST(client_protocol, connect) @@ -161,11 +169,11 @@ TEST(client_protocol, connect) TEST(client_protocol, connect_with_username_authentication) { MockProviderClient provider; - ClientProtocolFixture fixture(provider); - // ToDo: enable authentication + ClientProtocolFixture fixture(provider, true); EXPECT_CALL(provider, OnConnected()).Times(AtMost(1)); EXPECT_CALL(provider, OnDisconnected()).Times(1); + EXPECT_CALL(provider, GetCredentials(_)).WillOnce(Invoke(GetCredentials)).Times(1); fixture.Connect(); if (HasFatalFailure()) { return; } From 69a1faaa3fdbb8d87cf5f80f6ff21fca27ed3487 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sat, 29 Feb 2020 02:32:03 +0100 Subject: [PATCH 04/15] 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); } From b2d6ed87543bac7a21c57c0e06d134d6777906c2 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sat, 29 Feb 2020 02:44:26 +0100 Subject: [PATCH 05/15] reorganized library code --- cmake/jsonrpc.cmake | 21 ++++++++++++------- cmake/unit_tests.cmake | 17 ++++++++------- cmake/webfuse_adapter.cmake | 1 + cmake/webfuse_core.cmake | 2 +- {include => lib/jsonrpc/include}/jsonrpc.h | 0 .../jsonrpc/include}/jsonrpc/api.h | 0 .../include}/jsonrpc/method_invoke_fn.h | 0 .../jsonrpc/include}/jsonrpc/proxy.h | 0 .../include}/jsonrpc/proxy_finished_fn.h | 0 .../jsonrpc/include}/jsonrpc/request.h | 0 .../jsonrpc/include}/jsonrpc/response.h | 0 .../jsonrpc/include}/jsonrpc/send_fn.h | 0 .../jsonrpc/include}/jsonrpc/server.h | 0 .../jsonrpc/include}/jsonrpc/status.h | 0 lib/jsonrpc/{ => src/jsonrpc}/api.c | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/error.c | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/error.h | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/method.c | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/method.h | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/proxy.c | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/proxy.h | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/request.c | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/request.h | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/response.c | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/response.h | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/server.c | 0 lib/jsonrpc/{ => src/jsonrpc}/impl/server.h | 0 .../{ => src/jsonrpc}/impl/unused_param.h | 0 .../jsonrpc/test}/jsonrpc/test_is_request.cc | 0 .../jsonrpc/test}/jsonrpc/test_is_response.cc | 0 .../jsonrpc/test}/jsonrpc/test_proxy.cc | 0 .../jsonrpc/test}/jsonrpc/test_request.cc | 0 .../jsonrpc/test}/jsonrpc/test_response.cc | 0 .../test}/jsonrpc/test_response_parser.cc | 0 .../jsonrpc/test}/jsonrpc/test_server.cc | 0 .../jsonrpc/test}/jsonrpc/test_util.cc | 0 36 files changed, 24 insertions(+), 17 deletions(-) rename {include => lib/jsonrpc/include}/jsonrpc.h (100%) rename {include => lib/jsonrpc/include}/jsonrpc/api.h (100%) rename {include => lib/jsonrpc/include}/jsonrpc/method_invoke_fn.h (100%) rename {include => lib/jsonrpc/include}/jsonrpc/proxy.h (100%) rename {include => lib/jsonrpc/include}/jsonrpc/proxy_finished_fn.h (100%) rename {include => lib/jsonrpc/include}/jsonrpc/request.h (100%) rename {include => lib/jsonrpc/include}/jsonrpc/response.h (100%) rename {include => lib/jsonrpc/include}/jsonrpc/send_fn.h (100%) rename {include => lib/jsonrpc/include}/jsonrpc/server.h (100%) rename {include => lib/jsonrpc/include}/jsonrpc/status.h (100%) rename lib/jsonrpc/{ => src/jsonrpc}/api.c (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/error.c (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/error.h (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/method.c (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/method.h (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/proxy.c (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/proxy.h (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/request.c (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/request.h (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/response.c (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/response.h (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/server.c (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/server.h (100%) rename lib/jsonrpc/{ => src/jsonrpc}/impl/unused_param.h (100%) rename {test => lib/jsonrpc/test}/jsonrpc/test_is_request.cc (100%) rename {test => lib/jsonrpc/test}/jsonrpc/test_is_response.cc (100%) rename {test => lib/jsonrpc/test}/jsonrpc/test_proxy.cc (100%) rename {test => lib/jsonrpc/test}/jsonrpc/test_request.cc (100%) rename {test => lib/jsonrpc/test}/jsonrpc/test_response.cc (100%) rename {test => lib/jsonrpc/test}/jsonrpc/test_response_parser.cc (100%) rename {test => lib/jsonrpc/test}/jsonrpc/test_server.cc (100%) rename {test => lib/jsonrpc/test}/jsonrpc/test_util.cc (100%) diff --git a/cmake/jsonrpc.cmake b/cmake/jsonrpc.cmake index fb417bb..76653bb 100644 --- a/cmake/jsonrpc.cmake +++ b/cmake/jsonrpc.cmake @@ -1,14 +1,19 @@ # jsonrpc add_library(jsonrpc STATIC - 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 + lib/jsonrpc/src/jsonrpc/api.c + lib/jsonrpc/src/jsonrpc/impl/proxy.c + lib/jsonrpc/src/jsonrpc/impl/server.c + lib/jsonrpc/src/jsonrpc/impl/method.c + lib/jsonrpc/src/jsonrpc/impl/request.c + lib/jsonrpc/src/jsonrpc/impl/response.c + lib/jsonrpc/src/jsonrpc/impl/error.c +) + +target_include_directories(jsonrpc PUBLIC + lib/jsonrpc/include + lib/jsonrpc/src + lib ) -target_include_directories(jsonrpc PUBLIC lib) set_target_properties(jsonrpc PROPERTIES C_VISIBILITY_PRESET hidden) diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index 37ef85f..a6fbc8e 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -7,14 +7,14 @@ include(GoogleTest) pkg_check_modules(GMOCK gmock) add_executable(alltests - test/jsonrpc/test_util.cc - test/jsonrpc/test_is_request.cc - test/jsonrpc/test_request.cc - test/jsonrpc/test_is_response.cc - test/jsonrpc/test_response.cc - test/jsonrpc/test_server.cc - test/jsonrpc/test_proxy.cc - test/jsonrpc/test_response_parser.cc + lib/jsonrpc/test/jsonrpc/test_util.cc + lib/jsonrpc/test/jsonrpc/test_is_request.cc + lib/jsonrpc/test/jsonrpc/test_request.cc + lib/jsonrpc/test/jsonrpc/test_is_response.cc + lib/jsonrpc/test/jsonrpc/test_response.cc + lib/jsonrpc/test/jsonrpc/test_server.cc + lib/jsonrpc/test/jsonrpc/test_proxy.cc + lib/jsonrpc/test/jsonrpc/test_response_parser.cc test/webfuse/utils/tempdir.cc test/webfuse/utils/file_utils.cc test/webfuse/utils/msleep.cc @@ -52,6 +52,7 @@ add_executable(alltests ) target_include_directories(alltests PRIVATE + lib/jsonrpc/include ${FUSE3_INCLUDE_DIRS} ${UUID_INCLUDE_DIRS} ) diff --git a/cmake/webfuse_adapter.cmake b/cmake/webfuse_adapter.cmake index 719f699..7362802 100644 --- a/cmake/webfuse_adapter.cmake +++ b/cmake/webfuse_adapter.cmake @@ -32,6 +32,7 @@ add_library(webfuse-adapter-static STATIC target_include_directories(webfuse-adapter-static PRIVATE lib + lib/jsonrpc/include ${FUSE3_INCLUDE_DIRS} ${UUID_INCLUDE_DIRS} ) diff --git a/cmake/webfuse_core.cmake b/cmake/webfuse_core.cmake index 2926384..8db1dad 100644 --- a/cmake/webfuse_core.cmake +++ b/cmake/webfuse_core.cmake @@ -12,7 +12,7 @@ add_library(webfuse-core STATIC ) set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core) -target_include_directories(webfuse-core PUBLIC lib) +target_include_directories(webfuse-core PUBLIC lib lib/jsonrpc/include) set_target_properties(webfuse-core PROPERTIES C_VISIBILITY_PRESET hidden) install(DIRECTORY include/webfuse/core DESTINATION include/webfuse COMPONENT headers) diff --git a/include/jsonrpc.h b/lib/jsonrpc/include/jsonrpc.h similarity index 100% rename from include/jsonrpc.h rename to lib/jsonrpc/include/jsonrpc.h diff --git a/include/jsonrpc/api.h b/lib/jsonrpc/include/jsonrpc/api.h similarity index 100% rename from include/jsonrpc/api.h rename to lib/jsonrpc/include/jsonrpc/api.h diff --git a/include/jsonrpc/method_invoke_fn.h b/lib/jsonrpc/include/jsonrpc/method_invoke_fn.h similarity index 100% rename from include/jsonrpc/method_invoke_fn.h rename to lib/jsonrpc/include/jsonrpc/method_invoke_fn.h diff --git a/include/jsonrpc/proxy.h b/lib/jsonrpc/include/jsonrpc/proxy.h similarity index 100% rename from include/jsonrpc/proxy.h rename to lib/jsonrpc/include/jsonrpc/proxy.h diff --git a/include/jsonrpc/proxy_finished_fn.h b/lib/jsonrpc/include/jsonrpc/proxy_finished_fn.h similarity index 100% rename from include/jsonrpc/proxy_finished_fn.h rename to lib/jsonrpc/include/jsonrpc/proxy_finished_fn.h diff --git a/include/jsonrpc/request.h b/lib/jsonrpc/include/jsonrpc/request.h similarity index 100% rename from include/jsonrpc/request.h rename to lib/jsonrpc/include/jsonrpc/request.h diff --git a/include/jsonrpc/response.h b/lib/jsonrpc/include/jsonrpc/response.h similarity index 100% rename from include/jsonrpc/response.h rename to lib/jsonrpc/include/jsonrpc/response.h diff --git a/include/jsonrpc/send_fn.h b/lib/jsonrpc/include/jsonrpc/send_fn.h similarity index 100% rename from include/jsonrpc/send_fn.h rename to lib/jsonrpc/include/jsonrpc/send_fn.h diff --git a/include/jsonrpc/server.h b/lib/jsonrpc/include/jsonrpc/server.h similarity index 100% rename from include/jsonrpc/server.h rename to lib/jsonrpc/include/jsonrpc/server.h diff --git a/include/jsonrpc/status.h b/lib/jsonrpc/include/jsonrpc/status.h similarity index 100% rename from include/jsonrpc/status.h rename to lib/jsonrpc/include/jsonrpc/status.h diff --git a/lib/jsonrpc/api.c b/lib/jsonrpc/src/jsonrpc/api.c similarity index 100% rename from lib/jsonrpc/api.c rename to lib/jsonrpc/src/jsonrpc/api.c diff --git a/lib/jsonrpc/impl/error.c b/lib/jsonrpc/src/jsonrpc/impl/error.c similarity index 100% rename from lib/jsonrpc/impl/error.c rename to lib/jsonrpc/src/jsonrpc/impl/error.c diff --git a/lib/jsonrpc/impl/error.h b/lib/jsonrpc/src/jsonrpc/impl/error.h similarity index 100% rename from lib/jsonrpc/impl/error.h rename to lib/jsonrpc/src/jsonrpc/impl/error.h diff --git a/lib/jsonrpc/impl/method.c b/lib/jsonrpc/src/jsonrpc/impl/method.c similarity index 100% rename from lib/jsonrpc/impl/method.c rename to lib/jsonrpc/src/jsonrpc/impl/method.c diff --git a/lib/jsonrpc/impl/method.h b/lib/jsonrpc/src/jsonrpc/impl/method.h similarity index 100% rename from lib/jsonrpc/impl/method.h rename to lib/jsonrpc/src/jsonrpc/impl/method.h diff --git a/lib/jsonrpc/impl/proxy.c b/lib/jsonrpc/src/jsonrpc/impl/proxy.c similarity index 100% rename from lib/jsonrpc/impl/proxy.c rename to lib/jsonrpc/src/jsonrpc/impl/proxy.c diff --git a/lib/jsonrpc/impl/proxy.h b/lib/jsonrpc/src/jsonrpc/impl/proxy.h similarity index 100% rename from lib/jsonrpc/impl/proxy.h rename to lib/jsonrpc/src/jsonrpc/impl/proxy.h diff --git a/lib/jsonrpc/impl/request.c b/lib/jsonrpc/src/jsonrpc/impl/request.c similarity index 100% rename from lib/jsonrpc/impl/request.c rename to lib/jsonrpc/src/jsonrpc/impl/request.c diff --git a/lib/jsonrpc/impl/request.h b/lib/jsonrpc/src/jsonrpc/impl/request.h similarity index 100% rename from lib/jsonrpc/impl/request.h rename to lib/jsonrpc/src/jsonrpc/impl/request.h diff --git a/lib/jsonrpc/impl/response.c b/lib/jsonrpc/src/jsonrpc/impl/response.c similarity index 100% rename from lib/jsonrpc/impl/response.c rename to lib/jsonrpc/src/jsonrpc/impl/response.c diff --git a/lib/jsonrpc/impl/response.h b/lib/jsonrpc/src/jsonrpc/impl/response.h similarity index 100% rename from lib/jsonrpc/impl/response.h rename to lib/jsonrpc/src/jsonrpc/impl/response.h diff --git a/lib/jsonrpc/impl/server.c b/lib/jsonrpc/src/jsonrpc/impl/server.c similarity index 100% rename from lib/jsonrpc/impl/server.c rename to lib/jsonrpc/src/jsonrpc/impl/server.c diff --git a/lib/jsonrpc/impl/server.h b/lib/jsonrpc/src/jsonrpc/impl/server.h similarity index 100% rename from lib/jsonrpc/impl/server.h rename to lib/jsonrpc/src/jsonrpc/impl/server.h diff --git a/lib/jsonrpc/impl/unused_param.h b/lib/jsonrpc/src/jsonrpc/impl/unused_param.h similarity index 100% rename from lib/jsonrpc/impl/unused_param.h rename to lib/jsonrpc/src/jsonrpc/impl/unused_param.h diff --git a/test/jsonrpc/test_is_request.cc b/lib/jsonrpc/test/jsonrpc/test_is_request.cc similarity index 100% rename from test/jsonrpc/test_is_request.cc rename to lib/jsonrpc/test/jsonrpc/test_is_request.cc diff --git a/test/jsonrpc/test_is_response.cc b/lib/jsonrpc/test/jsonrpc/test_is_response.cc similarity index 100% rename from test/jsonrpc/test_is_response.cc rename to lib/jsonrpc/test/jsonrpc/test_is_response.cc diff --git a/test/jsonrpc/test_proxy.cc b/lib/jsonrpc/test/jsonrpc/test_proxy.cc similarity index 100% rename from test/jsonrpc/test_proxy.cc rename to lib/jsonrpc/test/jsonrpc/test_proxy.cc diff --git a/test/jsonrpc/test_request.cc b/lib/jsonrpc/test/jsonrpc/test_request.cc similarity index 100% rename from test/jsonrpc/test_request.cc rename to lib/jsonrpc/test/jsonrpc/test_request.cc diff --git a/test/jsonrpc/test_response.cc b/lib/jsonrpc/test/jsonrpc/test_response.cc similarity index 100% rename from test/jsonrpc/test_response.cc rename to lib/jsonrpc/test/jsonrpc/test_response.cc diff --git a/test/jsonrpc/test_response_parser.cc b/lib/jsonrpc/test/jsonrpc/test_response_parser.cc similarity index 100% rename from test/jsonrpc/test_response_parser.cc rename to lib/jsonrpc/test/jsonrpc/test_response_parser.cc diff --git a/test/jsonrpc/test_server.cc b/lib/jsonrpc/test/jsonrpc/test_server.cc similarity index 100% rename from test/jsonrpc/test_server.cc rename to lib/jsonrpc/test/jsonrpc/test_server.cc diff --git a/test/jsonrpc/test_util.cc b/lib/jsonrpc/test/jsonrpc/test_util.cc similarity index 100% rename from test/jsonrpc/test_util.cc rename to lib/jsonrpc/test/jsonrpc/test_util.cc From 79318c47b8994060a864128f0e68d83c982bdc68 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sat, 29 Feb 2020 21:06:40 +0100 Subject: [PATCH 06/15] refactor: extracted timer function of adapter into separate library --- CMakeLists.txt | 1 + cmake/jsonrpc.cmake | 10 +- cmake/unit_tests.cmake | 8 +- cmake/webfuse_adapter.cmake | 6 +- cmake/wf_timer.cmake | 18 +++ lib/jsonrpc/include/jsonrpc/proxy.h | 6 +- lib/jsonrpc/src/jsonrpc/api.c | 2 +- lib/jsonrpc/src/jsonrpc/impl/proxy.c | 29 ++-- lib/jsonrpc/src/jsonrpc/impl/proxy.h | 12 +- lib/jsonrpc/test/jsonrpc/test_proxy.cc | 84 ++++------ lib/webfuse/adapter/impl/server_protocol.c | 11 +- lib/webfuse/adapter/impl/server_protocol.h | 4 +- lib/webfuse/adapter/impl/session.c | 4 +- lib/webfuse/adapter/impl/session.h | 4 +- lib/webfuse/adapter/impl/session_manager.c | 4 +- lib/webfuse/adapter/impl/session_manager.h | 4 +- .../adapter/impl/time/timeout_manager.c | 84 ---------- .../adapter/impl/time/timeout_manager.h | 29 ---- .../impl/time/timeout_manager_intern.h | 24 --- lib/webfuse/adapter/impl/time/timepoint.c | 31 ---- lib/webfuse/adapter/impl/time/timepoint.h | 31 ---- lib/webfuse/adapter/impl/time/timer.c | 65 -------- lib/webfuse/adapter/impl/time/timer.h | 48 ------ lib/webfuse/adapter/impl/time/timer_intern.h | 25 --- lib/wf/timer/include/wf/timer.h | 9 ++ lib/wf/timer/include/wf/timer/api.h | 8 + lib/wf/timer/include/wf/timer/manager.h | 29 ++++ lib/wf/timer/include/wf/timer/on_timer_fn.h | 19 +++ lib/wf/timer/include/wf/timer/timer.h | 38 +++++ lib/wf/timer/src/wf/timer/api.c | 59 +++++++ lib/wf/timer/src/wf/timer/impl/manager.c | 96 +++++++++++ lib/wf/timer/src/wf/timer/impl/manager.h | 36 +++++ lib/wf/timer/src/wf/timer/impl/timepoint.c | 31 ++++ lib/wf/timer/src/wf/timer/impl/timepoint.h | 31 ++++ lib/wf/timer/src/wf/timer/impl/timer.c | 67 ++++++++ lib/wf/timer/src/wf/timer/impl/timer.h | 59 +++++++ lib/wf/timer/test/wf/timer/test_timepoint.cc | 36 +++++ lib/wf/timer/test/wf/timer/test_timer.cc | 136 ++++++++++++++++ test/webfuse/tests/adapter/test_timepoint.cc | 36 ----- test/webfuse/tests/adapter/test_timer.cc | 149 ------------------ 40 files changed, 765 insertions(+), 618 deletions(-) create mode 100644 cmake/wf_timer.cmake delete mode 100644 lib/webfuse/adapter/impl/time/timeout_manager.c delete mode 100644 lib/webfuse/adapter/impl/time/timeout_manager.h delete mode 100644 lib/webfuse/adapter/impl/time/timeout_manager_intern.h delete mode 100644 lib/webfuse/adapter/impl/time/timepoint.c delete mode 100644 lib/webfuse/adapter/impl/time/timepoint.h delete mode 100644 lib/webfuse/adapter/impl/time/timer.c delete mode 100644 lib/webfuse/adapter/impl/time/timer.h delete mode 100644 lib/webfuse/adapter/impl/time/timer_intern.h create mode 100644 lib/wf/timer/include/wf/timer.h create mode 100644 lib/wf/timer/include/wf/timer/api.h create mode 100644 lib/wf/timer/include/wf/timer/manager.h create mode 100644 lib/wf/timer/include/wf/timer/on_timer_fn.h create mode 100644 lib/wf/timer/include/wf/timer/timer.h create mode 100644 lib/wf/timer/src/wf/timer/api.c create mode 100644 lib/wf/timer/src/wf/timer/impl/manager.c create mode 100644 lib/wf/timer/src/wf/timer/impl/manager.h create mode 100644 lib/wf/timer/src/wf/timer/impl/timepoint.c create mode 100644 lib/wf/timer/src/wf/timer/impl/timepoint.h create mode 100644 lib/wf/timer/src/wf/timer/impl/timer.c create mode 100644 lib/wf/timer/src/wf/timer/impl/timer.h create mode 100644 lib/wf/timer/test/wf/timer/test_timepoint.cc create mode 100644 lib/wf/timer/test/wf/timer/test_timer.cc delete mode 100644 test/webfuse/tests/adapter/test_timepoint.cc delete mode 100644 test/webfuse/tests/adapter/test_timer.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e87f52..a856e45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ add_compile_options( "-pthread" ) +include(wf_timer) include(jsonrpc) include(webfuse_core) include(webfuse_adapter) diff --git a/cmake/jsonrpc.cmake b/cmake/jsonrpc.cmake index 76653bb..a7a643f 100644 --- a/cmake/jsonrpc.cmake +++ b/cmake/jsonrpc.cmake @@ -10,10 +10,16 @@ add_library(jsonrpc STATIC lib/jsonrpc/src/jsonrpc/impl/error.c ) -target_include_directories(jsonrpc PUBLIC - lib/jsonrpc/include +target_link_libraries(jsonrpc PUBLIC wf_timer) + +target_include_directories(jsonrpc PRIVATE + lib/wf/timer/include lib/jsonrpc/src lib ) +target_include_directories(jsonrpc PUBLIC + lib/jsonrpc/include +) + set_target_properties(jsonrpc PROPERTIES C_VISIBILITY_PRESET hidden) diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index a6fbc8e..0ba1878 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -15,6 +15,8 @@ add_executable(alltests lib/jsonrpc/test/jsonrpc/test_server.cc lib/jsonrpc/test/jsonrpc/test_proxy.cc lib/jsonrpc/test/jsonrpc/test_response_parser.cc + lib/wf/timer/test/wf/timer/test_timepoint.cc + lib/wf/timer/test/wf/timer/test_timer.cc test/webfuse/utils/tempdir.cc test/webfuse/utils/file_utils.cc test/webfuse/utils/msleep.cc @@ -35,8 +37,6 @@ add_executable(alltests test/webfuse/tests/core/test_message_queue.cc test/webfuse/tests/adapter/test_server.cc test/webfuse/tests/adapter/test_server_config.cc - test/webfuse/tests/adapter/test_timepoint.cc - test/webfuse/tests/adapter/test_timer.cc test/webfuse/tests/adapter/test_credentials.cc test/webfuse/tests/adapter/test_authenticator.cc test/webfuse/tests/adapter/test_authenticators.cc @@ -53,6 +53,9 @@ add_executable(alltests target_include_directories(alltests PRIVATE lib/jsonrpc/include + lib/jsonrpc/src + lib/wf/timer/include + lib/wf/timer/src ${FUSE3_INCLUDE_DIRS} ${UUID_INCLUDE_DIRS} ) @@ -67,6 +70,7 @@ target_link_libraries(alltests PUBLIC webfuse-provider-static webfuse-core jsonrpc + wf_timer ${FUSE3_LIBRARIES} ${LWS_LIBRARIES} ${JANSSON_LIBRARIES} diff --git a/cmake/webfuse_adapter.cmake b/cmake/webfuse_adapter.cmake index 7362802..f6fb6af 100644 --- a/cmake/webfuse_adapter.cmake +++ b/cmake/webfuse_adapter.cmake @@ -19,9 +19,6 @@ add_library(webfuse-adapter-static STATIC lib/webfuse/adapter/impl/mountpoint_factory.c lib/webfuse/adapter/impl/uuid_mountpoint_factory.c lib/webfuse/adapter/impl/uuid_mountpoint.c - lib/webfuse/adapter/impl/time/timepoint.c - lib/webfuse/adapter/impl/time/timer.c - lib/webfuse/adapter/impl/time/timeout_manager.c lib/webfuse/adapter/impl/operation/lookup.c lib/webfuse/adapter/impl/operation/getattr.c lib/webfuse/adapter/impl/operation/readdir.c @@ -32,6 +29,7 @@ add_library(webfuse-adapter-static STATIC target_include_directories(webfuse-adapter-static PRIVATE lib + lib/wf/timer/include lib/jsonrpc/include ${FUSE3_INCLUDE_DIRS} ${UUID_INCLUDE_DIRS} @@ -65,7 +63,7 @@ set_target_properties(webfuse-adapter PROPERTIES SOVERSION 0) set_target_properties(webfuse-adapter PROPERTIES C_VISIBILITY_PRESET hidden) set_target_properties(webfuse-adapter PROPERTIES COMPILE_DEFINITIONS "WF_API=WF_EXPORT") -target_link_libraries(webfuse-adapter PRIVATE webfuse-adapter-static webfuse-core jsonrpc) +target_link_libraries(webfuse-adapter PRIVATE webfuse-adapter-static webfuse-core jsonrpc wf_timer) file(WRITE "${PROJECT_BINARY_DIR}/libwebfuse-adapter.pc" "prefix=\"${CMAKE_INSTALL_PREFIX}\" diff --git a/cmake/wf_timer.cmake b/cmake/wf_timer.cmake new file mode 100644 index 0000000..8bdecf2 --- /dev/null +++ b/cmake/wf_timer.cmake @@ -0,0 +1,18 @@ +# timer + +add_library(wf_timer STATIC + lib/wf/timer/src/wf/timer/api.c + lib/wf/timer/src/wf/timer/impl/manager.c + lib/wf/timer/src/wf/timer/impl/timepoint.c + lib/wf/timer/src/wf/timer/impl/timer.c +) + +target_include_directories(wf_timer PRIVATE + lib/wf/timer/src +) + +target_include_directories(wf_timer PUBLIC + lib/wf/timer/include +) + +set_target_properties(wf_timer PROPERTIES C_VISIBILITY_PRESET hidden) diff --git a/lib/jsonrpc/include/jsonrpc/proxy.h b/lib/jsonrpc/include/jsonrpc/proxy.h index d433253..3f2dca5 100644 --- a/lib/jsonrpc/include/jsonrpc/proxy.h +++ b/lib/jsonrpc/include/jsonrpc/proxy.h @@ -16,18 +16,16 @@ using std::size_t; #include #include -#include "webfuse/adapter/impl/time/timeout_manager.h" -#include "webfuse/adapter/impl/time/timer.h" - #ifdef __cplusplus extern "C" { #endif struct jsonrpc_proxy; +struct wf_timer_manager; extern JSONRPC_API struct jsonrpc_proxy * jsonrpc_proxy_create( - struct wf_impl_timeout_manager * manager, + struct wf_timer_manager * manager, int timeout, jsonrpc_send_fn * send, void * user_data); diff --git a/lib/jsonrpc/src/jsonrpc/api.c b/lib/jsonrpc/src/jsonrpc/api.c index a6d7d1c..817438c 100644 --- a/lib/jsonrpc/src/jsonrpc/api.c +++ b/lib/jsonrpc/src/jsonrpc/api.c @@ -9,7 +9,7 @@ struct jsonrpc_proxy * jsonrpc_proxy_create( - struct wf_impl_timeout_manager * manager, + struct wf_timer_manager * manager, int timeout, jsonrpc_send_fn * send, void * user_data) diff --git a/lib/jsonrpc/src/jsonrpc/impl/proxy.c b/lib/jsonrpc/src/jsonrpc/impl/proxy.c index 446cd91..fa4f3ad 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/proxy.c +++ b/lib/jsonrpc/src/jsonrpc/impl/proxy.c @@ -3,12 +3,14 @@ #include "jsonrpc/impl/error.h" #include "jsonrpc/status.h" +#include + #include #include struct jsonrpc_proxy * jsonrpc_impl_proxy_create( - struct wf_impl_timeout_manager * manager, + struct wf_timer_manager * manager, int timeout, jsonrpc_send_fn * send, void * user_data) @@ -29,10 +31,10 @@ void jsonrpc_impl_proxy_dispose( free(proxy); } -static void jsonrpc_impl_proxy_timeout( - struct wf_impl_timer * timer) +static void jsonrpc_impl_proxy_on_timeout( + struct wf_timer * timer, void * proxy_ptr) { - struct jsonrpc_proxy * proxy = timer->user_data; + struct jsonrpc_proxy * proxy = proxy_ptr; if (proxy->request.is_pending) { @@ -43,7 +45,7 @@ static void jsonrpc_impl_proxy_timeout( proxy->request.id = 0; proxy->request.user_data = NULL; proxy->request.finished = NULL; - wf_impl_timer_cancel(&proxy->request.timer); + wf_timer_cancel(timer); jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_TIMEOUT, "Timeout"); } @@ -95,7 +97,7 @@ static json_t * jsonrpc_impl_request_create( void jsonrpc_impl_proxy_init( struct jsonrpc_proxy * proxy, - struct wf_impl_timeout_manager * timeout_manager, + struct wf_timer_manager * timeout_manager, int timeout, jsonrpc_send_fn * send, void * user_data) @@ -104,8 +106,8 @@ void jsonrpc_impl_proxy_init( proxy->timeout = timeout; proxy->user_data = user_data; proxy->request.is_pending = false; - - wf_impl_timer_init(&proxy->request.timer, timeout_manager); + proxy->request.timer = wf_timer_create(timeout_manager, + &jsonrpc_impl_proxy_on_timeout, proxy); } void jsonrpc_impl_proxy_cleanup( @@ -120,12 +122,12 @@ void jsonrpc_impl_proxy_cleanup( proxy->request.finished = NULL; proxy->request.user_data = NULL; proxy->request.id = 0; - wf_impl_timer_cancel(&proxy->request.timer); + wf_timer_cancel(proxy->request.timer); jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); } - wf_impl_timer_cleanup(&proxy->request.timer); + wf_timer_dispose(proxy->request.timer); } void jsonrpc_impl_proxy_invoke( @@ -143,8 +145,7 @@ void jsonrpc_impl_proxy_invoke( proxy->request.finished = finished; proxy->request.user_data = user_data; proxy->request.id = 42; - wf_impl_timer_start(&proxy->request.timer, wf_impl_timepoint_in_msec(proxy->timeout), - &jsonrpc_impl_proxy_timeout, proxy); + wf_timer_start(proxy->request.timer, proxy->timeout); json_t * request = jsonrpc_impl_request_create(method_name, proxy->request.id, param_info, args); @@ -155,7 +156,7 @@ void jsonrpc_impl_proxy_invoke( proxy->request.finished = NULL; proxy->request.user_data = NULL; proxy->request.id = 0; - wf_impl_timer_cancel(&proxy->request.timer); + wf_timer_cancel(proxy->request.timer); jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); } @@ -203,7 +204,7 @@ void jsonrpc_impl_proxy_onresult( proxy->request.id = 0; proxy->request.user_data = NULL; proxy->request.finished = NULL; - wf_impl_timer_cancel(&proxy->request.timer); + wf_timer_cancel(proxy->request.timer); finished(user_data, response.result, response.error); } diff --git a/lib/jsonrpc/src/jsonrpc/impl/proxy.h b/lib/jsonrpc/src/jsonrpc/impl/proxy.h index 0556e0d..940f853 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/proxy.h +++ b/lib/jsonrpc/src/jsonrpc/impl/proxy.h @@ -4,21 +4,21 @@ #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 wf_timer_manager; +struct wf_timer; + struct jsonrpc_request { bool is_pending; jsonrpc_proxy_finished_fn * finished; void * user_data; int id; - struct wf_impl_timer timer; + struct wf_timer * timer; }; struct jsonrpc_proxy @@ -32,7 +32,7 @@ struct jsonrpc_proxy extern void jsonrpc_impl_proxy_init( struct jsonrpc_proxy * proxy, - struct wf_impl_timeout_manager * manager, + struct wf_timer_manager * manager, int timeout, jsonrpc_send_fn * send, void * user_data); @@ -43,7 +43,7 @@ jsonrpc_impl_proxy_cleanup( extern struct jsonrpc_proxy * jsonrpc_impl_proxy_create( - struct wf_impl_timeout_manager * manager, + struct wf_timer_manager * manager, int timeout, jsonrpc_send_fn * send, void * user_data); diff --git a/lib/jsonrpc/test/jsonrpc/test_proxy.cc b/lib/jsonrpc/test/jsonrpc/test_proxy.cc index 834a7ef..cc71ae8 100644 --- a/lib/jsonrpc/test/jsonrpc/test_proxy.cc +++ b/lib/jsonrpc/test/jsonrpc/test_proxy.cc @@ -1,6 +1,6 @@ #include #include "jsonrpc/proxy.h" -#include "webfuse/adapter/impl/time/timeout_manager.h" +#include "wf/timer/manager.h" #include "webfuse/utils/msleep.hpp" #include "webfuse/core/json_util.h" @@ -87,27 +87,25 @@ namespace TEST(jsonrpc_proxy, init) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext context; void * user_data = reinterpret_cast(&context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data); jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); ASSERT_FALSE(context.is_called); } TEST(jsonrpc_proxy, invoke) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); @@ -134,7 +132,7 @@ TEST(jsonrpc_proxy, invoke) ASSERT_FALSE(finished_context.is_called); jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); ASSERT_TRUE(finished_context.is_called); ASSERT_FALSE(nullptr == finished_context.error); @@ -142,12 +140,11 @@ TEST(jsonrpc_proxy, invoke) TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context(false); void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); @@ -160,17 +157,16 @@ TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails) ASSERT_FALSE(nullptr == finished_context.error); jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); } TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); @@ -189,17 +185,16 @@ TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending) ASSERT_EQ(WF_BAD_BUSY, wf_impl_jsonrpc_get_status(finished_context2.error)); jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); } TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); @@ -211,17 +206,16 @@ TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid) ASSERT_EQ(WF_BAD, wf_impl_jsonrpc_get_status(finished_context.error)); jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); } TEST(jsonrpc_proxy, on_result) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); @@ -246,17 +240,16 @@ TEST(jsonrpc_proxy, on_result) ASSERT_STREQ("okay", json_string_value(finished_context.result)); jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); } TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); @@ -278,17 +271,16 @@ TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id) ASSERT_FALSE(finished_context.is_called); jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); } TEST(jsonrpc_proxy, timeout) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 0, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, 0, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); @@ -298,23 +290,22 @@ TEST(jsonrpc_proxy, timeout) ASSERT_TRUE(json_is_object(send_context.response)); msleep(10); - wf_impl_timeout_manager_check(&timeout_manager); + wf_timer_manager_check(timer_manager); ASSERT_TRUE(finished_context.is_called); ASSERT_EQ(WF_BAD_TIMEOUT, wf_impl_jsonrpc_get_status(finished_context.error)); jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); } TEST(jsonrpc_proxy, cleanup_pending_request) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 10, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, 10, &jsonrpc_send, send_data); FinishedContext finished_context; void * finished_data = reinterpret_cast(&finished_context); @@ -324,26 +315,23 @@ TEST(jsonrpc_proxy, cleanup_pending_request) ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_FALSE(finished_context.is_called); - ASSERT_NE(nullptr, timeout_manager.timers); jsonrpc_proxy_dispose(proxy); ASSERT_TRUE(finished_context.is_called); - ASSERT_EQ(nullptr, timeout_manager.timers); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); } TEST(jsonrpc_proxy, notify) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42); @@ -365,24 +353,22 @@ TEST(jsonrpc_proxy, notify) json_t * id = json_object_get(send_context.response, "id"); ASSERT_EQ(nullptr, id); - jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); } TEST(jsonrpc_proxy, notify_dont_send_invalid_request) { - struct wf_impl_timeout_manager timeout_manager; - wf_impl_timeout_manager_init(&timeout_manager); + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); jsonrpc_proxy_notify(proxy, "foo", "?"); ASSERT_FALSE(send_context.is_called); jsonrpc_proxy_dispose(proxy); - wf_impl_timeout_manager_cleanup(&timeout_manager); + wf_timer_manager_dispose(timer_manager); } diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index 061fa5c..7bf17c9 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -13,6 +13,9 @@ #include "webfuse/adapter/impl/uuid_mountpoint_factory.h" #include "webfuse/core/status_intern.h" +#include "wf/timer/manager.h" +#include "wf/timer/timer.h" + static int wf_impl_server_protocol_callback( struct lws * wsi, enum lws_callback_reasons reason, @@ -28,7 +31,7 @@ static int wf_impl_server_protocol_callback( struct wf_server_protocol * protocol = ws_protocol->user; - wf_impl_timeout_manager_check(&protocol->timeout_manager); + wf_timer_manager_check(protocol->timer_manager); struct wf_impl_session * session = wf_impl_session_manager_get(&protocol->session_manager, wsi); switch (reason) @@ -42,7 +45,7 @@ static int wf_impl_server_protocol_callback( wsi, &protocol->authenticators, &protocol->mountpoint_factory, - &protocol->timeout_manager, + protocol->timer_manager, protocol->server); if (NULL != session) @@ -237,7 +240,7 @@ void wf_impl_server_protocol_init( wf_impl_mountpoint_factory_move(mountpoint_factory, &protocol->mountpoint_factory); - wf_impl_timeout_manager_init(&protocol->timeout_manager); + protocol->timer_manager = wf_timer_manager_create(); wf_impl_session_manager_init(&protocol->session_manager); wf_impl_authenticators_init(&protocol->authenticators); @@ -252,7 +255,7 @@ void wf_impl_server_protocol_cleanup( protocol->is_operational = false; jsonrpc_server_dispose(protocol->server); - wf_impl_timeout_manager_cleanup(&protocol->timeout_manager); + wf_timer_manager_dispose(protocol->timer_manager); wf_impl_authenticators_cleanup(&protocol->authenticators); wf_impl_session_manager_cleanup(&protocol->session_manager); wf_impl_mountpoint_factory_cleanup(&protocol->mountpoint_factory); diff --git a/lib/webfuse/adapter/impl/server_protocol.h b/lib/webfuse/adapter/impl/server_protocol.h index 0677c84..0a9fdbd 100644 --- a/lib/webfuse/adapter/impl/server_protocol.h +++ b/lib/webfuse/adapter/impl/server_protocol.h @@ -2,7 +2,6 @@ #define WF_ADAPTER_IMPL_SERVER_PROTOCOL_H #include "jsonrpc/proxy.h" -#include "webfuse/adapter/impl/time/timeout_manager.h" #include "webfuse/adapter/impl/authenticators.h" #include "webfuse/adapter/impl/mountpoint_factory.h" #include "webfuse/adapter/impl/session_manager.h" @@ -18,14 +17,15 @@ extern "C" #endif struct lws_protocols; +struct wf_timer_manager; struct wf_server_protocol { - struct wf_impl_timeout_manager timeout_manager; struct wf_impl_authenticators authenticators; struct wf_impl_mountpoint_factory mountpoint_factory; struct wf_impl_session_manager session_manager; struct jsonrpc_server * server; + struct wf_timer_manager * timer_manager; bool is_operational; }; diff --git a/lib/webfuse/adapter/impl/session.c b/lib/webfuse/adapter/impl/session.c index deafb92..870f6c0 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -44,7 +44,7 @@ static bool wf_impl_session_send( struct wf_impl_session * wf_impl_session_create( struct lws * wsi, struct wf_impl_authenticators * authenticators, - struct wf_impl_timeout_manager * timeout_manager, + struct wf_timer_manager * timer_manager, struct jsonrpc_server * server, struct wf_impl_mountpoint_factory * mountpoint_factory) { @@ -59,7 +59,7 @@ struct wf_impl_session * wf_impl_session_create( session->authenticators = authenticators; session->server = server; session->mountpoint_factory = mountpoint_factory; - session->rpc = jsonrpc_proxy_create(timeout_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); + session->rpc = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); wf_slist_init(&session->messages); } diff --git a/lib/webfuse/adapter/impl/session.h b/lib/webfuse/adapter/impl/session.h index cc95911..a9d15af 100644 --- a/lib/webfuse/adapter/impl/session.h +++ b/lib/webfuse/adapter/impl/session.h @@ -25,7 +25,7 @@ struct wf_message; struct wf_credentials; struct wf_impl_authenticators; struct wf_impl_mountpoint_factory; -struct wf_impl_timeout_manager; +struct timer_manager; struct wf_impl_session { @@ -43,7 +43,7 @@ struct wf_impl_session extern struct wf_impl_session * wf_impl_session_create( struct lws * wsi, struct wf_impl_authenticators * authenticators, - struct wf_impl_timeout_manager * timeout_manager, + struct wf_timer_manager * timer_manager, struct jsonrpc_server * server, struct wf_impl_mountpoint_factory * mountpoint_factory); diff --git a/lib/webfuse/adapter/impl/session_manager.c b/lib/webfuse/adapter/impl/session_manager.c index e7e109d..6474c06 100644 --- a/lib/webfuse/adapter/impl/session_manager.c +++ b/lib/webfuse/adapter/impl/session_manager.c @@ -28,11 +28,11 @@ struct wf_impl_session * wf_impl_session_manager_add( struct lws * wsi, struct wf_impl_authenticators * authenticators, struct wf_impl_mountpoint_factory * mountpoint_factory, - struct wf_impl_timeout_manager * timeout_manager, + struct wf_timer_manager * timer_manager, struct jsonrpc_server * server) { struct wf_impl_session * session = wf_impl_session_create( - wsi, authenticators, timeout_manager, server, mountpoint_factory); + wsi, authenticators, timer_manager, server, mountpoint_factory); if (NULL != session) { wf_slist_append(&manager->sessions, &session->item); diff --git a/lib/webfuse/adapter/impl/session_manager.h b/lib/webfuse/adapter/impl/session_manager.h index 51c8910..4f5f218 100644 --- a/lib/webfuse/adapter/impl/session_manager.h +++ b/lib/webfuse/adapter/impl/session_manager.h @@ -15,7 +15,7 @@ extern "C" #endif struct lws; -struct wf_impl_timeout_manager; +struct wf_timer_manager; struct jsonrpc_server; struct wf_impl_session_manager @@ -34,7 +34,7 @@ extern struct wf_impl_session * wf_impl_session_manager_add( struct lws * wsi, struct wf_impl_authenticators * authenticators, struct wf_impl_mountpoint_factory * mountpoint_factory, - struct wf_impl_timeout_manager * timeout_manager, + struct wf_timer_manager * timer_manager, struct jsonrpc_server * server); extern struct wf_impl_session * wf_impl_session_manager_get( diff --git a/lib/webfuse/adapter/impl/time/timeout_manager.c b/lib/webfuse/adapter/impl/time/timeout_manager.c deleted file mode 100644 index 155ffe6..0000000 --- a/lib/webfuse/adapter/impl/time/timeout_manager.c +++ /dev/null @@ -1,84 +0,0 @@ -#include "webfuse/adapter/impl/time/timeout_manager_intern.h" - -#include -#include "webfuse/adapter/impl/time/timer_intern.h" -#include "webfuse/adapter/impl/time/timepoint.h" - -void wf_impl_timeout_manager_init( - struct wf_impl_timeout_manager * manager) -{ - manager->timers = NULL; -} - -void wf_impl_timeout_manager_cleanup( - struct wf_impl_timeout_manager * manager) -{ - struct wf_impl_timer * timer = manager->timers; - while (NULL != timer) - { - struct wf_impl_timer * next = timer->next; - - wf_impl_timer_trigger(timer); - - timer = next; - } - - manager->timers = NULL; - -} - -void wf_impl_timeout_manager_check( - struct wf_impl_timeout_manager * manager) -{ - struct wf_impl_timer * timer = manager->timers; - while (NULL != timer) - { - struct wf_impl_timer * next = timer->next; - - if (wf_impl_timer_is_timeout(timer)) - { - wf_impl_timeout_manager_removetimer(manager, timer); - wf_impl_timer_trigger(timer); - } - - timer = next; - } -} - -void wf_impl_timeout_manager_addtimer( - struct wf_impl_timeout_manager * manager, - struct wf_impl_timer * timer) -{ - if (NULL != manager->timers) - { - manager->timers->prev = timer; - } - - timer->next = manager->timers; - timer->prev = NULL; - manager->timers = timer; -} - -void wf_impl_timeout_manager_removetimer( - struct wf_impl_timeout_manager * manager, - struct wf_impl_timer * timer) -{ - struct wf_impl_timer * prev = timer->prev; - struct wf_impl_timer * next = timer->next; - - if (NULL != prev) - { - prev->next = next; - } - - if (NULL != next) - { - next->prev = prev; - } - - if (manager->timers == timer) - { - manager->timers = next; - } -} - diff --git a/lib/webfuse/adapter/impl/time/timeout_manager.h b/lib/webfuse/adapter/impl/time/timeout_manager.h deleted file mode 100644 index 0e33c1b..0000000 --- a/lib/webfuse/adapter/impl/time/timeout_manager.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_H -#define WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_impl_timer; -struct wf_impl_timeout_manager -{ - struct wf_impl_timer * timers; -}; - -extern void wf_impl_timeout_manager_init( - struct wf_impl_timeout_manager * manager); - -extern void wf_impl_timeout_manager_cleanup( - struct wf_impl_timeout_manager * manager); - -extern void wf_impl_timeout_manager_check( - struct wf_impl_timeout_manager * manager); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/lib/webfuse/adapter/impl/time/timeout_manager_intern.h b/lib/webfuse/adapter/impl/time/timeout_manager_intern.h deleted file mode 100644 index 67ee0f8..0000000 --- a/lib/webfuse/adapter/impl/time/timeout_manager_intern.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_INTERN_H -#define WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_INTERN_H - -#include "webfuse/adapter/impl/time/timeout_manager.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -extern void wf_impl_timeout_manager_addtimer( - struct wf_impl_timeout_manager * manager, - struct wf_impl_timer * timer); - -extern void wf_impl_timeout_manager_removetimer( - struct wf_impl_timeout_manager * manager, - struct wf_impl_timer * timer); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/lib/webfuse/adapter/impl/time/timepoint.c b/lib/webfuse/adapter/impl/time/timepoint.c deleted file mode 100644 index 8425000..0000000 --- a/lib/webfuse/adapter/impl/time/timepoint.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "webfuse/adapter/impl/time/timepoint.h" - -#include - -#define WF_MSEC_PER_SEC ((wf_impl_timepoint) 1000) -#define WF_NSEC_PER_MSEC ((wf_impl_timepoint) 1000 * 1000) - -wf_impl_timepoint wf_impl_timepoint_now(void) -{ - struct timespec tp; - clock_gettime(CLOCK_MONOTONIC, &tp); - - wf_impl_timepoint const now = (tp.tv_sec * WF_MSEC_PER_SEC) + (tp.tv_nsec / WF_NSEC_PER_MSEC); - return now; -} - -wf_impl_timepoint wf_impl_timepoint_in_msec(wf_impl_timediff value) -{ - wf_impl_timepoint const now = wf_impl_timepoint_now(); - wf_impl_timepoint result = now + ((wf_impl_timepoint) value); - - return result; -} - -bool wf_impl_timepoint_is_elapsed(wf_impl_timepoint tp) -{ - wf_impl_timepoint const now = wf_impl_timepoint_now(); - wf_impl_timediff const diff = (wf_impl_timediff) (tp - now); - - return (0 > diff); -} diff --git a/lib/webfuse/adapter/impl/time/timepoint.h b/lib/webfuse/adapter/impl/time/timepoint.h deleted file mode 100644 index c9f6560..0000000 --- a/lib/webfuse/adapter/impl/time/timepoint.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_TIME_TIMEPOINT_H -#define WF_ADAPTER_IMPL_TIME_TIMEPOINT_H - -#ifndef __cplusplus -#include -#include -#else -#include -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - -typedef uint64_t wf_impl_timepoint; -typedef int64_t wf_impl_timediff; - -extern wf_impl_timepoint wf_impl_timepoint_now(void); - -extern wf_impl_timepoint wf_impl_timepoint_in_msec( - wf_impl_timediff value); - -extern bool wf_impl_timepoint_is_elapsed( - wf_impl_timepoint timepoint); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/lib/webfuse/adapter/impl/time/timer.c b/lib/webfuse/adapter/impl/time/timer.c deleted file mode 100644 index 9d1da17..0000000 --- a/lib/webfuse/adapter/impl/time/timer.c +++ /dev/null @@ -1,65 +0,0 @@ -#include "webfuse/adapter/impl/time/timer_intern.h" -#include "webfuse/adapter/impl/time/timeout_manager_intern.h" - -#include -#include - -void wf_impl_timer_init( - struct wf_impl_timer * timer, - struct wf_impl_timeout_manager * manager) -{ - timer->manager = manager; - timer->timeout = 0; - timer->timeout_handler = NULL; - timer->user_data = NULL; - timer->prev = NULL; - timer->next = NULL; -} - -void wf_impl_timer_cleanup( - struct wf_impl_timer * timer) -{ - memset(timer, 0, sizeof(struct wf_impl_timer)); -} - -void wf_impl_timer_start( - struct wf_impl_timer * timer, - wf_impl_timepoint absolute_timeout, - wf_impl_timer_timeout_fn * handler, - void * user_data) -{ - timer->timeout = absolute_timeout; - timer->timeout_handler = handler; - timer->user_data = user_data; - - wf_impl_timeout_manager_addtimer(timer->manager, timer); -} - -void wf_impl_timer_cancel( - struct wf_impl_timer * timer) -{ - wf_impl_timeout_manager_removetimer(timer->manager, timer); - - timer->timeout = 0; - timer->timeout_handler = NULL; - timer->user_data = NULL; -} - -bool wf_impl_timer_is_timeout( - struct wf_impl_timer * timer) -{ - return wf_impl_timepoint_is_elapsed(timer->timeout); -} - - -void wf_impl_timer_trigger( - struct wf_impl_timer * timer) -{ - if (NULL != timer->timeout_handler) - { - timer->prev = NULL; - timer->next = NULL; - - timer->timeout_handler(timer); - } -} diff --git a/lib/webfuse/adapter/impl/time/timer.h b/lib/webfuse/adapter/impl/time/timer.h deleted file mode 100644 index bbc840d..0000000 --- a/lib/webfuse/adapter/impl/time/timer.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_TIME_TIMER_H -#define WF_ADAPTER_IMPL_TIME_TIMER_H - -#include "webfuse/adapter/impl/time/timepoint.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_impl_timer; -struct wf_impl_timeout_manager; - -typedef void wf_impl_timer_timeout_fn(struct wf_impl_timer * timer); - -struct wf_impl_timer -{ - struct wf_impl_timeout_manager * manager; - wf_impl_timepoint timeout; - wf_impl_timer_timeout_fn * timeout_handler; - void * user_data; - struct wf_impl_timer * next; - struct wf_impl_timer * prev; -}; - -extern void wf_impl_timer_init( - struct wf_impl_timer * timer, - struct wf_impl_timeout_manager * manager); - -extern void wf_impl_timer_cleanup( - struct wf_impl_timer * timer); - -extern void wf_impl_timer_start( - struct wf_impl_timer * timer, - wf_impl_timepoint absolute_timeout, - wf_impl_timer_timeout_fn * handler, - void * user_data); - -extern void wf_impl_timer_cancel( - struct wf_impl_timer * timer); - -#ifdef __cplusplus -} -#endif - - - -#endif diff --git a/lib/webfuse/adapter/impl/time/timer_intern.h b/lib/webfuse/adapter/impl/time/timer_intern.h deleted file mode 100644 index 6f1f19f..0000000 --- a/lib/webfuse/adapter/impl/time/timer_intern.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_TIME_TIMER_INTERN_H -#define WF_ADAPTER_IMPL_TIME_TIMER_INTERN_H - -#ifndef __cplusplus -#include -#endif - -#include "webfuse/adapter/impl/time/timer.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -extern bool wf_impl_timer_is_timeout( - struct wf_impl_timer * timer); - -extern void wf_impl_timer_trigger( - struct wf_impl_timer * timer); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/lib/wf/timer/include/wf/timer.h b/lib/wf/timer/include/wf/timer.h new file mode 100644 index 0000000..341792b --- /dev/null +++ b/lib/wf/timer/include/wf/timer.h @@ -0,0 +1,9 @@ +#ifndef WF_TIMER_H +#define WF_TIMER_H + +#include +#include +#include +#include + +#endif diff --git a/lib/wf/timer/include/wf/timer/api.h b/lib/wf/timer/include/wf/timer/api.h new file mode 100644 index 0000000..c327ba7 --- /dev/null +++ b/lib/wf/timer/include/wf/timer/api.h @@ -0,0 +1,8 @@ +#ifndef WF_TIMER_API_H +#define WF_TIMER_API_H + +#ifndef WF_TIMER_API +#define WF_TIMER_API +#endif + +#endif diff --git a/lib/wf/timer/include/wf/timer/manager.h b/lib/wf/timer/include/wf/timer/manager.h new file mode 100644 index 0000000..e0febc0 --- /dev/null +++ b/lib/wf/timer/include/wf/timer/manager.h @@ -0,0 +1,29 @@ +#ifndef WF_TIMER_MANAGER_H +#define WF_TIMER_MANAGER_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_timer_manager; + +extern WF_TIMER_API struct wf_timer_manager * +wf_timer_manager_create(void); + +extern WF_TIMER_API void +wf_timer_manager_dispose( + struct wf_timer_manager * manager); + +extern WF_TIMER_API void +wf_timer_manager_check( + struct wf_timer_manager * manager); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/lib/wf/timer/include/wf/timer/on_timer_fn.h b/lib/wf/timer/include/wf/timer/on_timer_fn.h new file mode 100644 index 0000000..d250bae --- /dev/null +++ b/lib/wf/timer/include/wf/timer/on_timer_fn.h @@ -0,0 +1,19 @@ +#ifndef WF_TIMER_ON_TIMER_FN_H +#define WF_TIMER_ON_TIMER_FN_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_timer; + +typedef void wf_timer_on_timer_fn( + struct wf_timer * timer, + void * user_data); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/wf/timer/include/wf/timer/timer.h b/lib/wf/timer/include/wf/timer/timer.h new file mode 100644 index 0000000..686eb89 --- /dev/null +++ b/lib/wf/timer/include/wf/timer/timer.h @@ -0,0 +1,38 @@ +#ifndef WF_TIMER_TIMER_H +#define WF_TIMER_TIMER_H + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_timer; +struct wf_timer_manager; + +extern WF_TIMER_API struct wf_timer * +wf_timer_create( + struct wf_timer_manager * manager, + wf_timer_on_timer_fn * on_timer, + void * user_data); + +extern WF_TIMER_API void +wf_timer_dispose( + struct wf_timer * timer); + +extern WF_TIMER_API void +wf_timer_start( + struct wf_timer * timer, + int timeout_ms); + +extern WF_TIMER_API void +wf_timer_cancel( + struct wf_timer * timer); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/wf/timer/src/wf/timer/api.c b/lib/wf/timer/src/wf/timer/api.c new file mode 100644 index 0000000..d0399a9 --- /dev/null +++ b/lib/wf/timer/src/wf/timer/api.c @@ -0,0 +1,59 @@ +#include "wf/timer.h" + +#include "wf/timer/impl/manager.h" +#include "wf/timer/impl/timer.h" + +// manager + +struct wf_timer_manager * +wf_timer_manager_create(void) +{ + return wf_timer_impl_manager_create(); +} + +void +wf_timer_manager_dispose( + struct wf_timer_manager * manager) +{ + wf_timer_impl_manager_dispose(manager); +} + +void +wf_timer_manager_check( + struct wf_timer_manager * manager) +{ + wf_timer_impl_manager_check(manager); +} + +// timer + +struct wf_timer * +wf_timer_create( + struct wf_timer_manager * manager, + wf_timer_on_timer_fn * on_timer, + void * user_data) +{ + return wf_timer_impl_create(manager, on_timer, user_data); +} + +void +wf_timer_dispose( + struct wf_timer * timer) +{ + wf_timer_impl_dispose(timer); +} + +void +wf_timer_start( + struct wf_timer * timer, + int timeout_ms) +{ + wf_timer_impl_start(timer, timeout_ms); +} + +void +wf_timer_cancel( + struct wf_timer * timer) +{ + wf_timer_impl_cancel(timer); +} diff --git a/lib/wf/timer/src/wf/timer/impl/manager.c b/lib/wf/timer/src/wf/timer/impl/manager.c new file mode 100644 index 0000000..2b00f80 --- /dev/null +++ b/lib/wf/timer/src/wf/timer/impl/manager.c @@ -0,0 +1,96 @@ +#include "wf/timer/impl/manager.h" +#include "wf/timer/impl/timer.h" +#include "wf/timer/impl/timepoint.h" + +#include +#include + +struct wf_timer_manager +{ + struct wf_timer * timers; +}; + +struct wf_timer_manager * +wf_timer_impl_manager_create(void) +{ + struct wf_timer_manager * manager = malloc(sizeof(struct wf_timer_manager)); + if (NULL != manager) + { + manager->timers = NULL; + } + + return manager; +} + +void +wf_timer_impl_manager_dispose( + struct wf_timer_manager * manager) +{ + struct wf_timer * timer = manager->timers; + while (NULL != timer) + { + struct wf_timer * next = timer->next; + + wf_timer_impl_trigger(timer); + timer = next; + } + + free(manager); +} + + +void wf_timer_impl_manager_check( + struct wf_timer_manager * manager) +{ + struct wf_timer * timer = manager->timers; + while (NULL != timer) + { + struct wf_timer * next = timer->next; + + if (wf_timer_impl_is_timeout(timer)) + { + wf_timer_impl_manager_removetimer(manager, timer); + wf_timer_impl_trigger(timer); + } + + timer = next; + } +} + +void wf_timer_impl_manager_addtimer( + struct wf_timer_manager * manager, + struct wf_timer * timer) +{ + if (NULL != manager->timers) + { + manager->timers->prev = timer; + } + + timer->next = manager->timers; + timer->prev = NULL; + manager->timers = timer; +} + +void wf_timer_impl_manager_removetimer( + struct wf_timer_manager * manager, + struct wf_timer * timer) +{ + struct wf_timer * prev = timer->prev; + struct wf_timer * next = timer->next; + + if (NULL != prev) + { + prev->next = next; + } + + if (NULL != next) + { + next->prev = prev; + } + + if (manager->timers == timer) + { + manager->timers = next; + } +} + diff --git a/lib/wf/timer/src/wf/timer/impl/manager.h b/lib/wf/timer/src/wf/timer/impl/manager.h new file mode 100644 index 0000000..c4fc64a --- /dev/null +++ b/lib/wf/timer/src/wf/timer/impl/manager.h @@ -0,0 +1,36 @@ +#ifndef WF_TIMER_IMPL_MANAGER_H +#define WF_TIMER_IMPL_MANAGER_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_timer; +struct wf_timer_manager; + +extern struct wf_timer_manager * +wf_timer_impl_manager_create(void); + +extern void +wf_timer_impl_manager_dispose( + struct wf_timer_manager * manager); + +extern void +wf_timer_impl_manager_check( + struct wf_timer_manager * manager); + +extern void wf_timer_impl_manager_addtimer( + struct wf_timer_manager * manager, + struct wf_timer * timer); + +extern void wf_timer_impl_manager_removetimer( + struct wf_timer_manager * manager, + struct wf_timer * timer); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/lib/wf/timer/src/wf/timer/impl/timepoint.c b/lib/wf/timer/src/wf/timer/impl/timepoint.c new file mode 100644 index 0000000..877b9c9 --- /dev/null +++ b/lib/wf/timer/src/wf/timer/impl/timepoint.c @@ -0,0 +1,31 @@ +#include "wf/timer/impl/timepoint.h" + +#include + +#define WF_TIMER_MSEC_PER_SEC ((wf_timer_timepoint) 1000) +#define WF_TIMER_NSEC_PER_MSEC ((wf_timer_timepoint) 1000 * 1000) + +wf_timer_timepoint wf_timer_impl_timepoint_now(void) +{ + struct timespec tp; + clock_gettime(CLOCK_MONOTONIC, &tp); + + wf_timer_timepoint const now = (tp.tv_sec * WF_TIMER_MSEC_PER_SEC) + (tp.tv_nsec / WF_TIMER_NSEC_PER_MSEC); + return now; +} + +wf_timer_timepoint wf_timer_impl_timepoint_in_msec(wf_timer_timediff value) +{ + wf_timer_timepoint const now = wf_timer_impl_timepoint_now(); + wf_timer_timepoint result = now + ((wf_timer_timepoint) value); + + return result; +} + +bool wf_timer_impl_timepoint_is_elapsed(wf_timer_timepoint tp) +{ + wf_timer_timepoint const now = wf_timer_impl_timepoint_now(); + wf_timer_timediff const diff = (wf_timer_timediff) (tp - now); + + return (0 > diff); +} diff --git a/lib/wf/timer/src/wf/timer/impl/timepoint.h b/lib/wf/timer/src/wf/timer/impl/timepoint.h new file mode 100644 index 0000000..584aa92 --- /dev/null +++ b/lib/wf/timer/src/wf/timer/impl/timepoint.h @@ -0,0 +1,31 @@ +#ifndef WF_TIMER_IMPL_TIMEPOINT_H +#define WF_TIMER_IMPL_TIMEPOINT_H + +#ifndef __cplusplus +#include +#include +#else +#include +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef uint64_t wf_timer_timepoint; +typedef int64_t wf_timer_timediff; + +extern wf_timer_timepoint wf_timer_impl_timepoint_now(void); + +extern wf_timer_timepoint wf_timer_impl_timepoint_in_msec( + wf_timer_timediff value); + +extern bool wf_timer_impl_timepoint_is_elapsed( + wf_timer_timepoint timepoint); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/wf/timer/src/wf/timer/impl/timer.c b/lib/wf/timer/src/wf/timer/impl/timer.c new file mode 100644 index 0000000..5559451 --- /dev/null +++ b/lib/wf/timer/src/wf/timer/impl/timer.c @@ -0,0 +1,67 @@ +#include "wf/timer/impl/timer.h" +#include "wf/timer/impl/manager.h" +#include "wf/timer/impl/timepoint.h" + +#include +#include +#include + +struct wf_timer * +wf_timer_impl_create( + struct wf_timer_manager * manager, + wf_timer_on_timer_fn * on_timer, + void * user_data) +{ + struct wf_timer * timer = malloc(sizeof(struct wf_timer)); + timer->manager = manager; + timer->timeout = 0; + timer->on_timer = on_timer; + timer->user_data = user_data; + timer->prev = NULL; + timer->next = NULL; + + return timer; +} + +void +wf_timer_impl_dispose( + struct wf_timer * timer) +{ + free(timer); +} + +void wf_timer_impl_start( + struct wf_timer * timer, + int timeout_ms) +{ + timer->timeout = wf_timer_impl_timepoint_in_msec(timeout_ms); + + wf_timer_impl_manager_addtimer(timer->manager, timer); +} + +void wf_timer_impl_cancel( + struct wf_timer * timer) +{ + wf_timer_impl_manager_removetimer(timer->manager, timer); + + timer->timeout = 0; +} + +bool wf_timer_impl_is_timeout( + struct wf_timer * timer) +{ + return wf_timer_impl_timepoint_is_elapsed(timer->timeout); +} + + +void wf_timer_impl_trigger( + struct wf_timer * timer) +{ + if (0 != timer->on_timer) + { + timer->prev = NULL; + timer->next = NULL; + + timer->on_timer(timer, timer->user_data); + } +} diff --git a/lib/wf/timer/src/wf/timer/impl/timer.h b/lib/wf/timer/src/wf/timer/impl/timer.h new file mode 100644 index 0000000..2e18a8b --- /dev/null +++ b/lib/wf/timer/src/wf/timer/impl/timer.h @@ -0,0 +1,59 @@ +#ifndef WF_ADAPTER_IMPL_TIME_TIMER_H +#define WF_ADAPTER_IMPL_TIME_TIMER_H + +#include "wf/timer/on_timer_fn.h" +#include "wf/timer/impl/timepoint.h" + +#ifndef __cplusplus +#include +#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 diff --git a/lib/wf/timer/test/wf/timer/test_timepoint.cc b/lib/wf/timer/test/wf/timer/test_timepoint.cc new file mode 100644 index 0000000..bd7774a --- /dev/null +++ b/lib/wf/timer/test/wf/timer/test_timepoint.cc @@ -0,0 +1,36 @@ +#include + +#include "webfuse/utils/msleep.hpp" +#include "wf/timer/impl/timepoint.h" + +using webfuse_test::msleep; + +TEST(wf_timer_timepoint, now) +{ + wf_timer_timepoint start = wf_timer_impl_timepoint_now(); + msleep(42); + 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)); +} diff --git a/lib/wf/timer/test/wf/timer/test_timer.cc b/lib/wf/timer/test/wf/timer/test_timer.cc new file mode 100644 index 0000000..ffeb1d3 --- /dev/null +++ b/lib/wf/timer/test/wf/timer/test_timer.cc @@ -0,0 +1,136 @@ +#include + +#include + +#include "webfuse/utils/msleep.hpp" +#include "wf/timer/timer.h" +#include "wf/timer/manager.h" + +using std::size_t; +using webfuse_test::msleep; + +extern "C" +{ + void on_timeout(struct wf_timer * timer, void * user_data) + { + (void) timer; + + bool * triggered = reinterpret_cast(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(&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(&triggered)); + + wf_timer_start(timer, 250); + msleep(500); + 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(&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(&triggered)); + + wf_timer_start(timer, 250); + msleep(500); + 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(&triggered)); + wf_timer_start(timer[i], 0); + } + + msleep(10); + 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(&triggered[i])); + triggered[i] = false; + wf_timer_start(timer[i], (300 - (50 * i))); + } + + for(size_t i = 0; i < count; i++) + { + msleep(100); + 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); +} diff --git a/test/webfuse/tests/adapter/test_timepoint.cc b/test/webfuse/tests/adapter/test_timepoint.cc deleted file mode 100644 index 8b0db97..0000000 --- a/test/webfuse/tests/adapter/test_timepoint.cc +++ /dev/null @@ -1,36 +0,0 @@ -#include - -#include "webfuse/utils/msleep.hpp" -#include "webfuse/adapter/impl/time/timepoint.h" - -using webfuse_test::msleep; - -TEST(timepoint, now) -{ - wf_impl_timepoint start = wf_impl_timepoint_now(); - msleep(42); - wf_impl_timepoint end = wf_impl_timepoint_now(); - - ASSERT_LT(start, end); - ASSERT_LT(end, start + 500); -} - -TEST(timepoint, in_msec) -{ - wf_impl_timepoint now = wf_impl_timepoint_now(); - wf_impl_timepoint later = wf_impl_timepoint_in_msec(42); - - ASSERT_LT(now, later); - ASSERT_LT(later, now + 500); -} - -TEST(wf_impl_timepoint, elapsed) -{ - wf_impl_timepoint now; - - now = wf_impl_timepoint_now(); - ASSERT_TRUE(wf_impl_timepoint_is_elapsed(now - 1)); - - now =wf_impl_timepoint_now(); - ASSERT_FALSE(wf_impl_timepoint_is_elapsed(now + 500)); -} diff --git a/test/webfuse/tests/adapter/test_timer.cc b/test/webfuse/tests/adapter/test_timer.cc deleted file mode 100644 index d554f2d..0000000 --- a/test/webfuse/tests/adapter/test_timer.cc +++ /dev/null @@ -1,149 +0,0 @@ -#include - -#include - -#include "webfuse/utils/msleep.hpp" -#include "webfuse/adapter/impl/time/timer.h" -#include "webfuse/adapter/impl/time/timeout_manager.h" - -using std::size_t; -using webfuse_test::msleep; - -namespace -{ - void on_timeout(struct wf_impl_timer * timer) - { - bool * triggered = reinterpret_cast(timer->user_data); - *triggered = true; - } -} - -TEST(timer, init) -{ - struct wf_impl_timeout_manager manager; - struct wf_impl_timer timer; - - wf_impl_timeout_manager_init(&manager); - wf_impl_timer_init(&timer, &manager); - - wf_impl_timer_cleanup(&timer); - wf_impl_timeout_manager_cleanup(&manager); -} - -TEST(timer, trigger) -{ - struct wf_impl_timeout_manager manager; - struct wf_impl_timer timer; - - wf_impl_timeout_manager_init(&manager); - wf_impl_timer_init(&timer, &manager); - - bool triggered = false; - wf_impl_timer_start(&timer, wf_impl_timepoint_in_msec(250), &on_timeout, reinterpret_cast(&triggered)); - msleep(500); - wf_impl_timeout_manager_check(&manager); - - ASSERT_TRUE(triggered); - - wf_impl_timer_cleanup(&timer); - wf_impl_timeout_manager_cleanup(&manager); -} - -TEST(timer, trigger_on_cleanup) -{ - struct wf_impl_timeout_manager manager; - struct wf_impl_timer timer; - - wf_impl_timeout_manager_init(&manager); - wf_impl_timer_init(&timer, &manager); - - bool triggered = false; - wf_impl_timer_start(&timer, wf_impl_timepoint_in_msec(5 * 60 * 1000), &on_timeout, reinterpret_cast(&triggered)); - - wf_impl_timeout_manager_cleanup(&manager); - ASSERT_TRUE(triggered); - - wf_impl_timer_cleanup(&timer); -} - -TEST(timer, cancel) -{ - struct wf_impl_timeout_manager manager; - struct wf_impl_timer timer; - - wf_impl_timeout_manager_init(&manager); - wf_impl_timer_init(&timer, &manager); - - bool triggered = false; - wf_impl_timer_start(&timer, wf_impl_timepoint_in_msec(250), &on_timeout, &triggered); - msleep(500); - wf_impl_timer_cancel(&timer); - wf_impl_timeout_manager_check(&manager); - - ASSERT_FALSE(triggered); - - wf_impl_timer_cleanup(&timer); - wf_impl_timeout_manager_cleanup(&manager); -} - -TEST(timer, cancel_multiple_timers) -{ - static size_t const count = 5; - struct wf_impl_timeout_manager manager; - struct wf_impl_timer timer[count]; - - wf_impl_timeout_manager_init(&manager); - - bool triggered = false; - for(size_t i = 0; i < count; i++) - { - wf_impl_timer_init(&timer[i], &manager); - wf_impl_timer_start(&timer[i], wf_impl_timepoint_in_msec(0), &on_timeout, &triggered); - } - - msleep(10); - for(size_t i = 0; i < count; i++) - { - wf_impl_timer_cancel(&timer[i]); - } - - wf_impl_timeout_manager_check(&manager); - ASSERT_FALSE(triggered); - - for(size_t i = 0; i < count; i++) - { - wf_impl_timer_cleanup(&timer[0]); - } - wf_impl_timeout_manager_cleanup(&manager); -} - -TEST(timer, multiple_timers) -{ - static size_t const count = 5; - struct wf_impl_timeout_manager manager; - struct wf_impl_timer timer[count]; - bool triggered[count]; - - wf_impl_timeout_manager_init(&manager); - - for(size_t i = 0; i < count; i++) - { - wf_impl_timer_init(&timer[i], &manager); - triggered[i] = false; - wf_impl_timer_start(&timer[i], wf_impl_timepoint_in_msec(300 - (50 * i)), &on_timeout, &triggered[i]); - } - - for(size_t i = 0; i < count; i++) - { - msleep(100); - wf_impl_timeout_manager_check(&manager); - } - - for(size_t i = 0; i < count; i++) - { - ASSERT_TRUE(triggered[i]); - wf_impl_timer_cleanup(&timer[i]); - } - - wf_impl_timeout_manager_cleanup(&manager); -} From 97eb420ef211a8446ed7b931c3253e6f6897fffd Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sat, 29 Feb 2020 23:53:48 +0100 Subject: [PATCH 07/15] chore: removed dependency to lib --- cmake/jsonrpc.cmake | 1 - 1 file changed, 1 deletion(-) diff --git a/cmake/jsonrpc.cmake b/cmake/jsonrpc.cmake index a7a643f..e50e1c6 100644 --- a/cmake/jsonrpc.cmake +++ b/cmake/jsonrpc.cmake @@ -15,7 +15,6 @@ target_link_libraries(jsonrpc PUBLIC wf_timer) target_include_directories(jsonrpc PRIVATE lib/wf/timer/include lib/jsonrpc/src - lib ) target_include_directories(jsonrpc PUBLIC From af2343c67a46922391550fd047dbe3d02be52122 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 1 Mar 2020 01:13:24 +0100 Subject: [PATCH 08/15] use jsonrpc_proxy to send requests within client_protocol (provider) --- cmake/webfuse_provider.cmake | 6 +- lib/jsonrpc/src/jsonrpc/impl/proxy.c | 4 +- lib/webfuse/provider/impl/client_protocol.c | 112 ++++++++++++++------ lib/webfuse/provider/impl/client_protocol.h | 4 + 4 files changed, 88 insertions(+), 38 deletions(-) diff --git a/cmake/webfuse_provider.cmake b/cmake/webfuse_provider.cmake index 0d80d2d..8e829a4 100644 --- a/cmake/webfuse_provider.cmake +++ b/cmake/webfuse_provider.cmake @@ -20,7 +20,11 @@ add_library(webfuse-provider-static STATIC set_target_properties(webfuse-provider-static PROPERTIES OUTPUT_NAME webfuse-provider) set_target_properties(webfuse-provider-static PROPERTIES C_VISIBILITY_PRESET hidden) -target_include_directories(webfuse-provider-static PUBLIC lib) +target_include_directories(webfuse-provider-static PUBLIC + lib + lib/wf/timer/include + lib/jsonrpc/include +) add_library(webfuse-provider SHARED lib/webfuse/provider/api.c diff --git a/lib/jsonrpc/src/jsonrpc/impl/proxy.c b/lib/jsonrpc/src/jsonrpc/impl/proxy.c index fa4f3ad..caea4a4 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/proxy.c +++ b/lib/jsonrpc/src/jsonrpc/impl/proxy.c @@ -124,7 +124,7 @@ void jsonrpc_impl_proxy_cleanup( proxy->request.id = 0; wf_timer_cancel(proxy->request.timer); - jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); + jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad: cancelled pending request during shutdown"); } wf_timer_dispose(proxy->request.timer); @@ -158,7 +158,7 @@ void jsonrpc_impl_proxy_invoke( proxy->request.id = 0; wf_timer_cancel(proxy->request.timer); - jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad"); + jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad: requenst is not sent"); } if (NULL != request) diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index 59945bd..46d2133 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -15,6 +15,14 @@ #include "webfuse/provider/impl/url.h" #include "webfuse/core/protocol_names.h" +#include "wf/timer/manager.h" + +#include "jsonrpc/response.h" +#include "jsonrpc/request.h" +#include "jsonrpc/proxy.h" + +#define WF_DEFAULT_TIMEOUT (10 * 1000) + static void wfp_impl_client_protocol_respond( json_t * response, void * user_data) @@ -29,54 +37,65 @@ static void wfp_impl_client_protocol_respond( } } -static void wfp_impl_client_protocol_process_request( +static void wfp_impl_client_protocol_process( struct wfp_client_protocol * protocol, - char const * message, + char const * data, size_t length) { - json_t * request = json_loadb(message, length, 0, NULL); - if (NULL != request) + json_t * message = json_loadb(data, length, 0, NULL); + if (NULL != message) { - // FIXME: is_connected should be invoked, when filesystem added - if ((!protocol->is_connected) && (NULL != json_object_get(request, "result"))) + if (jsonrpc_is_response(message)) { - protocol->is_connected = true; - protocol->provider.connected(protocol->user_data); + jsonrpc_proxy_onresult(protocol->proxy, message); } - - - struct wfp_impl_invokation_context context = + if (jsonrpc_is_request(message)) { - .provider = &protocol->provider, - .user_data = protocol->user_data, - .request = &protocol->request - }; + struct wfp_impl_invokation_context context = + { + .provider = &protocol->provider, + .user_data = protocol->user_data, + .request = &protocol->request + }; - wfp_impl_provider_invoke(&context, request); - json_decref(request); + wfp_impl_provider_invoke(&context, message); + } + + json_decref(message); } } +static void +wfp_impl_client_protocol_on_authenticate_finished( + void * user_data, + json_t const * result, + json_t const * WF_UNUSED_PARAM(error)) +{ + struct wfp_client_protocol * protocol = user_data; + + if (NULL != result) + { + protocol->is_connected = true; + protocol->provider.connected(protocol->user_data); + } + else + { + // ToDo: handle error + } + +} + static void wfp_impl_client_protocol_add_filesystem( struct wfp_client_protocol * protocol) { - json_t * params = json_array(); - json_array_append_new(params, json_string("cprovider")); - - json_t * request = json_object(); - json_object_set_new(request, "method", json_string("add_filesystem")); - json_object_set_new(request, "params", params); - json_object_set_new(request, "id", json_integer(42)); - - struct wf_message * message = wf_message_create(request); - if (NULL != message) - { - wf_slist_append(&protocol->messages, &message->item); - lws_callback_on_writable(protocol->wsi); - } - - json_decref(request); + jsonrpc_proxy_invoke( + protocol->proxy, + &wfp_impl_client_protocol_on_authenticate_finished, + protocol, + "add_filesystem", + "s", + "cprovider"); } static int wfp_impl_client_protocol_callback( @@ -91,11 +110,12 @@ static int wfp_impl_client_protocol_callback( if (NULL != protocol) { + wf_timer_manager_check(protocol->timer_manager); + switch (reason) { case LWS_CALLBACK_CLIENT_ESTABLISHED: wfp_impl_client_protocol_add_filesystem(protocol); - // Defer is_connected until response received break; case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: protocol->is_connected = false; @@ -106,7 +126,7 @@ static int wfp_impl_client_protocol_callback( protocol->provider.disconnected(protocol->user_data); break; case LWS_CALLBACK_CLIENT_RECEIVE: - wfp_impl_client_protocol_process_request(protocol, in, len); + wfp_impl_client_protocol_process(protocol, in, len); break; case LWS_CALLBACK_SERVER_WRITEABLE: // fall-through @@ -133,6 +153,23 @@ static int wfp_impl_client_protocol_callback( return 0; } +static bool wfp_impl_client_protocol_send( + json_t * request, + void * user_data) +{ + bool result = false; + struct wfp_client_protocol * protocol = user_data; + + struct wf_message * message = wf_message_create(request); + if (NULL != message) + { + wf_slist_append(&protocol->messages, &message->item); + lws_callback_on_writable(protocol->wsi); + result = true; + } + + return result; +} void wfp_impl_client_protocol_init( struct wfp_client_protocol * protocol, @@ -147,6 +184,9 @@ void wfp_impl_client_protocol_init( protocol->request.respond = &wfp_impl_client_protocol_respond; protocol->request.user_data = protocol; + protocol->timer_manager = wf_timer_manager_create(); + protocol->proxy = jsonrpc_proxy_create(protocol->timer_manager, WF_DEFAULT_TIMEOUT, &wfp_impl_client_protocol_send, protocol); + protocol->user_data = user_data; wfp_impl_provider_init_from_prototype(&protocol->provider, provider); } @@ -154,6 +194,8 @@ void wfp_impl_client_protocol_init( void wfp_impl_client_protocol_cleanup( struct wfp_client_protocol * protocol) { + jsonrpc_proxy_dispose(protocol->proxy); + wf_timer_manager_dispose(protocol->timer_manager); wf_message_queue_cleanup(&protocol->messages); } diff --git a/lib/webfuse/provider/impl/client_protocol.h b/lib/webfuse/provider/impl/client_protocol.h index d6f537d..77a99d5 100644 --- a/lib/webfuse/provider/impl/client_protocol.h +++ b/lib/webfuse/provider/impl/client_protocol.h @@ -14,6 +14,8 @@ extern "C" struct wfp_client_config; struct lws_protocols; struct lws_context; +struct jsonrpc_proxy; +struct wf_timer_manager; struct wfp_client_protocol { @@ -22,6 +24,8 @@ struct wfp_client_protocol struct wfp_provider provider; void * user_data; struct lws * wsi; + struct wf_timer_manager * timer_manager; + struct jsonrpc_proxy * proxy; struct wf_slist messages; }; From f79b9c998a5709e0608fc393acfaddec02196ff0 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 1 Mar 2020 11:06:09 +0100 Subject: [PATCH 09/15] added error handling on authentication failure --- lib/webfuse/provider/impl/client_protocol.c | 42 +++++++++++++-------- lib/webfuse/provider/impl/client_protocol.h | 1 + 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index 46d2133..fdbcaf1 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -73,7 +73,8 @@ wfp_impl_client_protocol_on_authenticate_finished( json_t const * WF_UNUSED_PARAM(error)) { struct wfp_client_protocol * protocol = user_data; - + if (NULL == protocol->wsi) { return; } + if (NULL != result) { protocol->is_connected = true; @@ -81,9 +82,9 @@ wfp_impl_client_protocol_on_authenticate_finished( } else { - // ToDo: handle error - } - + protocol->is_shutdown_requested = true; + lws_callback_on_writable(protocol->wsi); + } } static void wfp_impl_client_protocol_add_filesystem( @@ -105,6 +106,7 @@ static int wfp_impl_client_protocol_callback( void * in, size_t len) { + int result = 0; struct lws_protocols const * ws_protocol = lws_get_protocol(wsi); struct wfp_client_protocol * protocol = (NULL != ws_protocol) ? ws_protocol->user: NULL; @@ -123,7 +125,8 @@ static int wfp_impl_client_protocol_callback( break; case LWS_CALLBACK_CLIENT_CLOSED: protocol->is_connected = false; - protocol->provider.disconnected(protocol->user_data); + protocol->provider.disconnected(protocol->user_data); + protocol->wsi = NULL; break; case LWS_CALLBACK_CLIENT_RECEIVE: wfp_impl_client_protocol_process(protocol, in, len); @@ -131,26 +134,32 @@ static int wfp_impl_client_protocol_callback( case LWS_CALLBACK_SERVER_WRITEABLE: // fall-through case LWS_CALLBACK_CLIENT_WRITEABLE: - if ((wsi == protocol->wsi) && (!wf_slist_empty(&protocol->messages))) + if (wsi == protocol->wsi) { - struct wf_slist_item * item = wf_slist_remove_first(&protocol->messages); - struct wf_message * message = wf_container_of(item, struct wf_message, item); - lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT); - wf_message_dispose(message); - - if (!wf_slist_empty(&protocol->messages)) + if (protocol->is_shutdown_requested) { - lws_callback_on_writable(wsi); - + result = 1; } - } + else if (!wf_slist_empty(&protocol->messages)) + { + struct wf_slist_item * item = wf_slist_remove_first(&protocol->messages); + struct wf_message * message = wf_container_of(item, struct wf_message, item); + lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT); + wf_message_dispose(message); + + if (!wf_slist_empty(&protocol->messages)) + { + lws_callback_on_writable(wsi); + } + } + } break; default: break; } } - return 0; + return result; } static bool wfp_impl_client_protocol_send( @@ -177,6 +186,7 @@ void wfp_impl_client_protocol_init( void * user_data) { protocol->is_connected = false; + protocol->is_shutdown_requested = false; wf_slist_init(&protocol->messages); protocol->wsi = NULL; diff --git a/lib/webfuse/provider/impl/client_protocol.h b/lib/webfuse/provider/impl/client_protocol.h index 77a99d5..d0e6660 100644 --- a/lib/webfuse/provider/impl/client_protocol.h +++ b/lib/webfuse/provider/impl/client_protocol.h @@ -20,6 +20,7 @@ struct wf_timer_manager; struct wfp_client_protocol { bool is_connected; + bool is_shutdown_requested; struct wfp_request request; struct wfp_provider provider; void * user_data; From 7856b5a99d158803c8f07ffd7d92462124e025f1 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 1 Mar 2020 13:42:46 +0100 Subject: [PATCH 10/15] feature: enabled authentication --- lib/jsonrpc/include/jsonrpc/proxy.h | 15 ++++- lib/jsonrpc/src/jsonrpc/impl/proxy.c | 6 ++ lib/webfuse/provider/impl/client_protocol.c | 65 +++++++++++++++++-- lib/webfuse/provider/impl/credentials.c | 12 ++++ lib/webfuse/provider/impl/credentials.h | 5 ++ lib/webfuse/provider/impl/provider.c | 9 ++- lib/webfuse/provider/impl/provider.h | 8 +++ .../tests/provider/test_client_protocol.cc | 2 +- 8 files changed, 115 insertions(+), 7 deletions(-) diff --git a/lib/jsonrpc/include/jsonrpc/proxy.h b/lib/jsonrpc/include/jsonrpc/proxy.h index 3f2dca5..a7573fa 100644 --- a/lib/jsonrpc/include/jsonrpc/proxy.h +++ b/lib/jsonrpc/include/jsonrpc/proxy.h @@ -33,7 +33,20 @@ jsonrpc_proxy_create( extern JSONRPC_API void jsonrpc_proxy_dispose( struct jsonrpc_proxy * proxy); - +//------------------------------------------------------------------------------ +/// \brief Invokes a method. +/// +/// Creates a method an sends it using the send function. +/// Proxy keeps track of method invokation. If no response is returned within +/// timeout, an error is propagated. +/// +/// \param proxy pointer to proxy instance +/// \param finished function which is called exactly once, either on success or +/// on failure. +/// \param method_name name of the method to invoke +/// \param param_info types of the param (s = string, i = integer, j = json) +/// \param ... params +//------------------------------------------------------------------------------ extern JSONRPC_API void jsonrpc_proxy_invoke( struct jsonrpc_proxy * proxy, jsonrpc_proxy_finished_fn * finished, diff --git a/lib/jsonrpc/src/jsonrpc/impl/proxy.c b/lib/jsonrpc/src/jsonrpc/impl/proxy.c index caea4a4..976da05 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/proxy.c +++ b/lib/jsonrpc/src/jsonrpc/impl/proxy.c @@ -77,6 +77,12 @@ static json_t * jsonrpc_impl_request_create( json_array_append_new(params, json_integer(value)); } break; + case 'j': + { + json_t * const value = va_arg(args, json_t *); + json_array_append_new(params, value); + } + break; default: fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type); json_decref(params); diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index fdbcaf1..1b434fc 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -8,6 +8,7 @@ #include "webfuse/provider/impl/client_config.h" #include "webfuse/provider/impl/provider.h" +#include "webfuse/provider/impl/credentials.h" #include "webfuse/core/util.h" #include "webfuse/core/message.h" #include "webfuse/core/message_queue.h" @@ -67,14 +68,14 @@ static void wfp_impl_client_protocol_process( } static void -wfp_impl_client_protocol_on_authenticate_finished( +wfp_impl_client_protocol_on_add_filesystem_finished( void * user_data, json_t const * result, json_t const * WF_UNUSED_PARAM(error)) { struct wfp_client_protocol * protocol = user_data; if (NULL == protocol->wsi) { return; } - + if (NULL != result) { protocol->is_connected = true; @@ -92,13 +93,69 @@ static void wfp_impl_client_protocol_add_filesystem( { jsonrpc_proxy_invoke( protocol->proxy, - &wfp_impl_client_protocol_on_authenticate_finished, + &wfp_impl_client_protocol_on_add_filesystem_finished, protocol, "add_filesystem", "s", "cprovider"); } +static void +wfp_impl_client_protocol_on_authenticate_finished( + void * user_data, + json_t const * result, + json_t const * WF_UNUSED_PARAM(error)) +{ + struct wfp_client_protocol * protocol = user_data; + if (NULL == protocol->wsi) { return; } + + if (NULL != result) + { + wfp_impl_client_protocol_add_filesystem(protocol); + } + else + { + protocol->is_shutdown_requested = true; + lws_callback_on_writable(protocol->wsi); + } +} + +static void wfp_impl_client_protocol_authenticate( + struct wfp_client_protocol * protocol) +{ + struct wfp_credentials credentials; + wfp_impl_credentials_init(&credentials); + + protocol->provider.get_credentials(&credentials, protocol->user_data); + + char const * cred_type = wfp_impl_credentials_get_type(&credentials); + json_t * creds = wfp_impl_credentials_get(&credentials); + json_incref(creds); + + jsonrpc_proxy_invoke( + protocol->proxy, + &wfp_impl_client_protocol_on_authenticate_finished, + protocol, + "authenticate", + "sj", + cred_type, creds); + + wfp_impl_credentials_cleanup(&credentials); +} + +static void wfp_impl_client_protocol_handshake( + struct wfp_client_protocol * protocol) +{ + if (wfp_impl_provider_is_authentication_enabled(&protocol->provider)) + { + wfp_impl_client_protocol_authenticate(protocol); + } + else + { + wfp_impl_client_protocol_add_filesystem(protocol); + } +} + static int wfp_impl_client_protocol_callback( struct lws * wsi, enum lws_callback_reasons reason, @@ -117,7 +174,7 @@ static int wfp_impl_client_protocol_callback( switch (reason) { case LWS_CALLBACK_CLIENT_ESTABLISHED: - wfp_impl_client_protocol_add_filesystem(protocol); + wfp_impl_client_protocol_handshake(protocol); break; case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: protocol->is_connected = false; diff --git a/lib/webfuse/provider/impl/credentials.c b/lib/webfuse/provider/impl/credentials.c index 8e788ca..f2d254f 100644 --- a/lib/webfuse/provider/impl/credentials.c +++ b/lib/webfuse/provider/impl/credentials.c @@ -32,3 +32,15 @@ void wfp_impl_credentials_add( { json_object_set_new(credentials->contents, key, json_string(value)); } + +char const * wfp_impl_credentials_get_type( + struct wfp_credentials * credentials) +{ + return credentials->type; +} + +json_t * wfp_impl_credentials_get( + struct wfp_credentials * credentials) +{ + return credentials->contents; +} diff --git a/lib/webfuse/provider/impl/credentials.h b/lib/webfuse/provider/impl/credentials.h index 6da40b9..304b067 100644 --- a/lib/webfuse/provider/impl/credentials.h +++ b/lib/webfuse/provider/impl/credentials.h @@ -30,6 +30,11 @@ extern void wfp_impl_credentials_add( char const * key, char const * value); +extern char const * wfp_impl_credentials_get_type( + struct wfp_credentials * credentials); + +extern json_t * wfp_impl_credentials_get( + struct wfp_credentials * credentials); #ifdef __cplusplus } diff --git a/lib/webfuse/provider/impl/provider.c b/lib/webfuse/provider/impl/provider.c index d3ba96c..14282d2 100644 --- a/lib/webfuse/provider/impl/provider.c +++ b/lib/webfuse/provider/impl/provider.c @@ -126,4 +126,11 @@ void wfp_impl_ontimer_default( (void) user_data; // empty -} \ No newline at end of file +} + + bool wfp_impl_provider_is_authentication_enabled( + struct wfp_provider * provider) +{ + return (NULL != provider->get_credentials); +} + diff --git a/lib/webfuse/provider/impl/provider.h b/lib/webfuse/provider/impl/provider.h index 8f6d115..a9b814d 100644 --- a/lib/webfuse/provider/impl/provider.h +++ b/lib/webfuse/provider/impl/provider.h @@ -1,9 +1,14 @@ #ifndef WF_PROVIDER_IMPL_PROVIDER_H #define WF_PROVIDER_IMPL_PROVIDER_H +#ifndef __cplusplus +#include +#endif + #include #include "webfuse/provider/client_config.h" + #ifdef __cplusplus extern "C" { @@ -42,6 +47,9 @@ extern void wfp_impl_provider_invoke( struct wfp_impl_invokation_context * context, json_t * request); +extern bool wfp_impl_provider_is_authentication_enabled( + struct wfp_provider * provider); + extern void wfp_impl_connected_default( void * user_data); diff --git a/test/webfuse/tests/provider/test_client_protocol.cc b/test/webfuse/tests/provider/test_client_protocol.cc index 53529c7..3563949 100644 --- a/test/webfuse/tests/provider/test_client_protocol.cc +++ b/test/webfuse/tests/provider/test_client_protocol.cc @@ -173,7 +173,7 @@ TEST(client_protocol, connect_with_username_authentication) EXPECT_CALL(provider, OnConnected()).Times(AtMost(1)); EXPECT_CALL(provider, OnDisconnected()).Times(1); - EXPECT_CALL(provider, GetCredentials(_)).WillOnce(Invoke(GetCredentials)).Times(1); + EXPECT_CALL(provider, GetCredentials(_)).Times(1).WillOnce(Invoke(GetCredentials)); fixture.Connect(); if (HasFatalFailure()) { return; } From 1b4034e08111781b27309dcb2445aa1791c2aac7 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 1 Mar 2020 14:00:49 +0100 Subject: [PATCH 11/15] fix: do not ignore return value of mkdtemp --- test/webfuse/tests/integration/server.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/webfuse/tests/integration/server.cc b/test/webfuse/tests/integration/server.cc index 1360934..847efa2 100644 --- a/test/webfuse/tests/integration/server.cc +++ b/test/webfuse/tests/integration/server.cc @@ -20,7 +20,12 @@ public: : is_shutdown_requested(false) { snprintf(base_dir, WF_PATH_MAX, "%s", "/tmp/webfuse_test_integration_XXXXXX"); - mkdtemp(base_dir); + char const * result = mkdtemp(base_dir); + if (NULL == result) + { + throw std::runtime_error("unable to create temp dir"); + } + config = wf_server_config_create(); wf_server_config_set_port(config, 8080); From 8a40919296fb0896baa1b707b36bd855f00e7e27 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 1 Mar 2020 14:39:58 +0100 Subject: [PATCH 12/15] fix: do not ignore return value of symlink --- lib/webfuse/adapter/impl/uuid_mountpoint.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/webfuse/adapter/impl/uuid_mountpoint.c b/lib/webfuse/adapter/impl/uuid_mountpoint.c index eafa444..4573329 100644 --- a/lib/webfuse/adapter/impl/uuid_mountpoint.c +++ b/lib/webfuse/adapter/impl/uuid_mountpoint.c @@ -60,8 +60,8 @@ static bool wf_impl_uuid_mountpoint_link_first_subdir( { if ((DT_DIR == entry->d_type) && ('.' != entry->d_name[0])) { - symlink(entry->d_name, link_path); - result = true; + int rc = symlink(entry->d_name, link_path); + result = (0 == rc); break; } @@ -114,7 +114,8 @@ wf_impl_uuid_mountpoint_create( mkdir(data->full_path, 0755); data->default_path = wf_create_string("%s/%s/default", root_path, filesystem); - symlink(data->id, data->default_path); + int rc = symlink(data->id, data->default_path); + (void) rc; // ignore missing symlink struct wf_mountpoint * mountpoint = wf_impl_mountpoint_create(data->full_path); wf_impl_mountpoint_set_userdata(mountpoint, data, &wf_impl_uuid_mountpoint_data_dispose); From 1a29b44ad67394b87ee08818f97b23a8e707e77f Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 1 Mar 2020 16:55:58 +0100 Subject: [PATCH 13/15] chore: prefixed jsonrpc by wf_ --- CMakeLists.txt | 2 +- cmake/jsonrpc.cmake | 24 --- cmake/unit_tests.cmake | 22 +-- cmake/webfuse_adapter.cmake | 4 +- cmake/webfuse_core.cmake | 2 +- cmake/webfuse_provider.cmake | 2 +- cmake/wf_jsonrpc.cmake | 24 +++ lib/jsonrpc/include/jsonrpc.h | 14 -- lib/jsonrpc/include/jsonrpc/api.h | 8 - .../include/jsonrpc/method_invoke_fn.h | 26 ---- lib/jsonrpc/include/jsonrpc/request.h | 53 ------- lib/jsonrpc/include/jsonrpc/server.h | 46 ------ lib/jsonrpc/include/jsonrpc/status.h | 11 -- lib/jsonrpc/src/jsonrpc/api.c | 144 ------------------ lib/jsonrpc/src/jsonrpc/impl/method.h | 34 ----- lib/jsonrpc/src/jsonrpc/impl/proxy.h | 84 ---------- lib/jsonrpc/src/jsonrpc/impl/request.h | 53 ------- lib/jsonrpc/src/jsonrpc/impl/server.c | 132 ---------------- lib/jsonrpc/src/jsonrpc/impl/server.h | 41 ----- lib/jsonrpc/src/jsonrpc/impl/unused_param.h | 10 -- lib/webfuse/adapter/impl/operation/close.c | 6 +- lib/webfuse/adapter/impl/operation/getattr.c | 6 +- lib/webfuse/adapter/impl/operation/lookup.c | 6 +- lib/webfuse/adapter/impl/operation/open.c | 6 +- lib/webfuse/adapter/impl/operation/read.c | 6 +- lib/webfuse/adapter/impl/operation/readdir.c | 14 +- lib/webfuse/adapter/impl/operations.c | 4 +- lib/webfuse/adapter/impl/operations.h | 4 +- lib/webfuse/adapter/impl/server_protocol.c | 26 ++-- lib/webfuse/adapter/impl/server_protocol.h | 6 +- lib/webfuse/adapter/impl/session.c | 23 +-- lib/webfuse/adapter/impl/session.h | 12 +- lib/webfuse/adapter/impl/session_manager.c | 2 +- lib/webfuse/adapter/impl/session_manager.h | 4 +- lib/webfuse/core/json_util.c | 12 +- lib/webfuse/provider/impl/client_protocol.c | 20 +-- lib/webfuse/provider/impl/client_protocol.h | 4 +- lib/wf/jsonrpc/include/wf/jsonrpc.h | 14 ++ lib/wf/jsonrpc/include/wf/jsonrpc/api.h | 8 + .../include/wf/jsonrpc/method_invoke_fn.h | 26 ++++ .../jsonrpc/include/wf}/jsonrpc/proxy.h | 36 ++--- .../include/wf}/jsonrpc/proxy_finished_fn.h | 6 +- lib/wf/jsonrpc/include/wf/jsonrpc/request.h | 54 +++++++ .../jsonrpc/include/wf}/jsonrpc/response.h | 8 +- .../jsonrpc/include/wf}/jsonrpc/send_fn.h | 8 +- lib/wf/jsonrpc/include/wf/jsonrpc/server.h | 46 ++++++ lib/wf/jsonrpc/include/wf/jsonrpc/status.h | 11 ++ lib/wf/jsonrpc/src/wf/jsonrpc/api.c | 144 ++++++++++++++++++ .../jsonrpc/src/wf}/jsonrpc/impl/error.c | 10 +- .../jsonrpc/src/wf}/jsonrpc/impl/error.h | 12 +- .../jsonrpc/src/wf}/jsonrpc/impl/method.c | 12 +- lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.h | 34 +++++ .../jsonrpc/src/wf}/jsonrpc/impl/proxy.c | 80 +++++----- lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.h | 84 ++++++++++ .../jsonrpc/src/wf}/jsonrpc/impl/request.c | 40 ++--- lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.h | 53 +++++++ .../jsonrpc/src/wf}/jsonrpc/impl/response.c | 20 +-- .../jsonrpc/src/wf}/jsonrpc/impl/response.h | 16 +- lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c | 132 ++++++++++++++++ lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.h | 41 +++++ .../src/wf/jsonrpc/impl/unused_param.h | 10 ++ .../test/wf}/jsonrpc/test_is_request.cc | 42 ++--- .../test/wf}/jsonrpc/test_is_response.cc | 38 ++--- .../jsonrpc/test/wf}/jsonrpc/test_proxy.cc | 94 ++++++------ .../jsonrpc/test/wf}/jsonrpc/test_request.cc | 32 ++-- .../jsonrpc/test/wf}/jsonrpc/test_response.cc | 26 ++-- .../test/wf}/jsonrpc/test_response_parser.cc | 18 +-- .../jsonrpc/test/wf}/jsonrpc/test_server.cc | 40 ++--- .../webfuse/tests/core}/test_util.cc | 0 69 files changed, 1047 insertions(+), 1045 deletions(-) delete mode 100644 cmake/jsonrpc.cmake create mode 100644 cmake/wf_jsonrpc.cmake delete mode 100644 lib/jsonrpc/include/jsonrpc.h delete mode 100644 lib/jsonrpc/include/jsonrpc/api.h delete mode 100644 lib/jsonrpc/include/jsonrpc/method_invoke_fn.h delete mode 100644 lib/jsonrpc/include/jsonrpc/request.h delete mode 100644 lib/jsonrpc/include/jsonrpc/server.h delete mode 100644 lib/jsonrpc/include/jsonrpc/status.h delete mode 100644 lib/jsonrpc/src/jsonrpc/api.c delete mode 100644 lib/jsonrpc/src/jsonrpc/impl/method.h delete mode 100644 lib/jsonrpc/src/jsonrpc/impl/proxy.h delete mode 100644 lib/jsonrpc/src/jsonrpc/impl/request.h delete mode 100644 lib/jsonrpc/src/jsonrpc/impl/server.c delete mode 100644 lib/jsonrpc/src/jsonrpc/impl/server.h delete mode 100644 lib/jsonrpc/src/jsonrpc/impl/unused_param.h create mode 100644 lib/wf/jsonrpc/include/wf/jsonrpc.h create mode 100644 lib/wf/jsonrpc/include/wf/jsonrpc/api.h create mode 100644 lib/wf/jsonrpc/include/wf/jsonrpc/method_invoke_fn.h rename lib/{jsonrpc/include => wf/jsonrpc/include/wf}/jsonrpc/proxy.h (63%) rename lib/{jsonrpc/include => wf/jsonrpc/include/wf}/jsonrpc/proxy_finished_fn.h (58%) create mode 100644 lib/wf/jsonrpc/include/wf/jsonrpc/request.h rename lib/{jsonrpc/include => wf/jsonrpc/include/wf}/jsonrpc/response.h (55%) rename lib/{jsonrpc/include => wf/jsonrpc/include/wf}/jsonrpc/send_fn.h (61%) create mode 100644 lib/wf/jsonrpc/include/wf/jsonrpc/server.h create mode 100644 lib/wf/jsonrpc/include/wf/jsonrpc/status.h create mode 100644 lib/wf/jsonrpc/src/wf/jsonrpc/api.c rename lib/{jsonrpc/src => wf/jsonrpc/src/wf}/jsonrpc/impl/error.c (65%) rename lib/{jsonrpc/src => wf/jsonrpc/src/wf}/jsonrpc/impl/error.h (53%) rename lib/{jsonrpc/src => wf/jsonrpc/src/wf}/jsonrpc/impl/method.c (53%) create mode 100644 lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.h rename lib/{jsonrpc/src => wf/jsonrpc/src/wf}/jsonrpc/impl/proxy.c (64%) create mode 100644 lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.h rename lib/{jsonrpc/src => wf/jsonrpc/src/wf}/jsonrpc/impl/request.c (58%) create mode 100644 lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.h rename lib/{jsonrpc/src => wf/jsonrpc/src/wf}/jsonrpc/impl/response.c (69%) rename lib/{jsonrpc/src => wf/jsonrpc/src/wf}/jsonrpc/impl/response.h (50%) create mode 100644 lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c create mode 100644 lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.h create mode 100644 lib/wf/jsonrpc/src/wf/jsonrpc/impl/unused_param.h rename lib/{jsonrpc/test => wf/jsonrpc/test/wf}/jsonrpc/test_is_request.cc (65%) rename lib/{jsonrpc/test => wf/jsonrpc/test/wf}/jsonrpc/test_is_response.cc (60%) rename lib/{jsonrpc/test => wf/jsonrpc/test/wf}/jsonrpc/test_proxy.cc (73%) rename lib/{jsonrpc/test => wf/jsonrpc/test/wf}/jsonrpc/test_request.cc (70%) rename lib/{jsonrpc/test => wf/jsonrpc/test/wf}/jsonrpc/test_response.cc (69%) rename lib/{jsonrpc/test => wf/jsonrpc/test/wf}/jsonrpc/test_response_parser.cc (75%) rename lib/{jsonrpc/test => wf/jsonrpc/test/wf}/jsonrpc/test_server.cc (72%) rename {lib/jsonrpc/test/jsonrpc => test/webfuse/tests/core}/test_util.cc (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a856e45..00daef9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,7 @@ add_compile_options( ) include(wf_timer) -include(jsonrpc) +include(wf_jsonrpc) include(webfuse_core) include(webfuse_adapter) include(webfuse_provider) diff --git a/cmake/jsonrpc.cmake b/cmake/jsonrpc.cmake deleted file mode 100644 index e50e1c6..0000000 --- a/cmake/jsonrpc.cmake +++ /dev/null @@ -1,24 +0,0 @@ -# jsonrpc - -add_library(jsonrpc STATIC - lib/jsonrpc/src/jsonrpc/api.c - lib/jsonrpc/src/jsonrpc/impl/proxy.c - lib/jsonrpc/src/jsonrpc/impl/server.c - lib/jsonrpc/src/jsonrpc/impl/method.c - lib/jsonrpc/src/jsonrpc/impl/request.c - lib/jsonrpc/src/jsonrpc/impl/response.c - lib/jsonrpc/src/jsonrpc/impl/error.c -) - -target_link_libraries(jsonrpc PUBLIC wf_timer) - -target_include_directories(jsonrpc PRIVATE - lib/wf/timer/include - lib/jsonrpc/src -) - -target_include_directories(jsonrpc PUBLIC - lib/jsonrpc/include -) - -set_target_properties(jsonrpc PROPERTIES C_VISIBILITY_PRESET hidden) diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index 0ba1878..b7887fa 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -7,14 +7,13 @@ include(GoogleTest) pkg_check_modules(GMOCK gmock) add_executable(alltests - lib/jsonrpc/test/jsonrpc/test_util.cc - lib/jsonrpc/test/jsonrpc/test_is_request.cc - lib/jsonrpc/test/jsonrpc/test_request.cc - lib/jsonrpc/test/jsonrpc/test_is_response.cc - lib/jsonrpc/test/jsonrpc/test_response.cc - lib/jsonrpc/test/jsonrpc/test_server.cc - lib/jsonrpc/test/jsonrpc/test_proxy.cc - lib/jsonrpc/test/jsonrpc/test_response_parser.cc + lib/wf/jsonrpc/test/wf/jsonrpc/test_is_request.cc + lib/wf/jsonrpc/test/wf/jsonrpc/test_request.cc + lib/wf/jsonrpc/test/wf/jsonrpc/test_is_response.cc + lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc + lib/wf/jsonrpc/test/wf/jsonrpc/test_server.cc + lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc + lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc lib/wf/timer/test/wf/timer/test_timepoint.cc lib/wf/timer/test/wf/timer/test_timer.cc test/webfuse/utils/tempdir.cc @@ -28,6 +27,7 @@ add_executable(alltests test/webfuse/mocks/mock_authenticator.cc test/webfuse/mocks/mock_request.cc test/webfuse/mocks/mock_provider_client.cc + test/webfuse//tests/core/test_util.cc test/webfuse/tests/core/test_container_of.cc test/webfuse/tests/core/test_string.cc test/webfuse/tests/core/test_slist.cc @@ -52,8 +52,8 @@ add_executable(alltests ) target_include_directories(alltests PRIVATE - lib/jsonrpc/include - lib/jsonrpc/src + lib/wf/jsonrpc/include + lib/wf/jsonrpc/src lib/wf/timer/include lib/wf/timer/src ${FUSE3_INCLUDE_DIRS} @@ -69,7 +69,7 @@ target_link_libraries(alltests PUBLIC webfuse-adapter-static webfuse-provider-static webfuse-core - jsonrpc + wf_jsonrpc wf_timer ${FUSE3_LIBRARIES} ${LWS_LIBRARIES} diff --git a/cmake/webfuse_adapter.cmake b/cmake/webfuse_adapter.cmake index f6fb6af..bb0f723 100644 --- a/cmake/webfuse_adapter.cmake +++ b/cmake/webfuse_adapter.cmake @@ -30,7 +30,7 @@ add_library(webfuse-adapter-static STATIC target_include_directories(webfuse-adapter-static PRIVATE lib lib/wf/timer/include - lib/jsonrpc/include + lib/wf/jsonrpc/include ${FUSE3_INCLUDE_DIRS} ${UUID_INCLUDE_DIRS} ) @@ -63,7 +63,7 @@ set_target_properties(webfuse-adapter PROPERTIES SOVERSION 0) set_target_properties(webfuse-adapter PROPERTIES C_VISIBILITY_PRESET hidden) set_target_properties(webfuse-adapter PROPERTIES COMPILE_DEFINITIONS "WF_API=WF_EXPORT") -target_link_libraries(webfuse-adapter PRIVATE webfuse-adapter-static webfuse-core jsonrpc wf_timer) +target_link_libraries(webfuse-adapter PRIVATE webfuse-adapter-static webfuse-core wf_jsonrpc wf_timer) file(WRITE "${PROJECT_BINARY_DIR}/libwebfuse-adapter.pc" "prefix=\"${CMAKE_INSTALL_PREFIX}\" diff --git a/cmake/webfuse_core.cmake b/cmake/webfuse_core.cmake index 8db1dad..dab6223 100644 --- a/cmake/webfuse_core.cmake +++ b/cmake/webfuse_core.cmake @@ -12,7 +12,7 @@ add_library(webfuse-core STATIC ) set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core) -target_include_directories(webfuse-core PUBLIC lib lib/jsonrpc/include) +target_include_directories(webfuse-core PUBLIC lib lib/wf/jsonrpc/include) set_target_properties(webfuse-core PROPERTIES C_VISIBILITY_PRESET hidden) install(DIRECTORY include/webfuse/core DESTINATION include/webfuse COMPONENT headers) diff --git a/cmake/webfuse_provider.cmake b/cmake/webfuse_provider.cmake index 8e829a4..a0c9e08 100644 --- a/cmake/webfuse_provider.cmake +++ b/cmake/webfuse_provider.cmake @@ -23,7 +23,7 @@ set_target_properties(webfuse-provider-static PROPERTIES C_VISIBILITY_PRESET hid target_include_directories(webfuse-provider-static PUBLIC lib lib/wf/timer/include - lib/jsonrpc/include + lib/wf/jsonrpc/include ) add_library(webfuse-provider SHARED diff --git a/cmake/wf_jsonrpc.cmake b/cmake/wf_jsonrpc.cmake new file mode 100644 index 0000000..fae8d74 --- /dev/null +++ b/cmake/wf_jsonrpc.cmake @@ -0,0 +1,24 @@ +# wf_jsonrpc + +add_library(wf_jsonrpc STATIC + lib/wf/jsonrpc/src/wf/jsonrpc/api.c + lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c + lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c + lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c + lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c + lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.c + lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.c +) + +target_link_libraries(wf_jsonrpc PUBLIC wf_timer) + +target_include_directories(wf_jsonrpc PRIVATE + lib/wf/timer/include + lib/wf/jsonrpc/src +) + +target_include_directories(wf_jsonrpc PUBLIC + lib/wf/jsonrpc/include +) + +set_target_properties(wf_jsonrpc PROPERTIES C_VISIBILITY_PRESET hidden) diff --git a/lib/jsonrpc/include/jsonrpc.h b/lib/jsonrpc/include/jsonrpc.h deleted file mode 100644 index b0e2999..0000000 --- a/lib/jsonrpc/include/jsonrpc.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef JSONRPC_H -#define JSONRPC_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#endif diff --git a/lib/jsonrpc/include/jsonrpc/api.h b/lib/jsonrpc/include/jsonrpc/api.h deleted file mode 100644 index 41b8563..0000000 --- a/lib/jsonrpc/include/jsonrpc/api.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef JSONRPC_API_H -#define JSONRPC_API_H - -#ifndef JSONRPC_API -#define JSONRPC_API -#endif - -#endif diff --git a/lib/jsonrpc/include/jsonrpc/method_invoke_fn.h b/lib/jsonrpc/include/jsonrpc/method_invoke_fn.h deleted file mode 100644 index 96fecea..0000000 --- a/lib/jsonrpc/include/jsonrpc/method_invoke_fn.h +++ /dev/null @@ -1,26 +0,0 @@ -#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/lib/jsonrpc/include/jsonrpc/request.h b/lib/jsonrpc/include/jsonrpc/request.h deleted file mode 100644 index 8c5cbae..0000000 --- a/lib/jsonrpc/include/jsonrpc/request.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef JSONRPC_REQUEST_H -#define JSONRPC_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 JSONRPC_API bool jsonrpc_is_request( - json_t * message); - -extern JSONRPC_API struct jsonrpc_request * jsonrpc_request_create( - int id, - jsonrpc_send_fn * send, - void * user_data); - -extern JSONRPC_API void jsonrpc_request_dispose( - struct jsonrpc_request * request); - -extern JSONRPC_API void * jsonrpc_request_get_userdata( - struct jsonrpc_request * request); - -extern JSONRPC_API void jsonrpc_respond( - struct jsonrpc_request * request, - json_t * result); - -extern JSONRPC_API void jsonrpc_respond_error( - struct jsonrpc_request * request, - int code, - char const * message); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/lib/jsonrpc/include/jsonrpc/server.h b/lib/jsonrpc/include/jsonrpc/server.h deleted file mode 100644 index 7c215ec..0000000 --- a/lib/jsonrpc/include/jsonrpc/server.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef JSONRPC_SERVER_H -#define JSONRPC_SERVER_H - -#ifndef __cplusplus -#include -#include -#else -#include -#endif - -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct jsonrpc_server; - -extern JSONRPC_API struct jsonrpc_server * -jsonrpc_server_create(void); - -extern JSONRPC_API void -jsonrpc_server_dispose( - struct jsonrpc_server * server); - -extern JSONRPC_API void jsonrpc_server_add( - struct jsonrpc_server * server, - char const * method_name, - jsonrpc_method_invoke_fn * invoke, - void * user_data); - -extern JSONRPC_API void jsonrpc_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/include/jsonrpc/status.h b/lib/jsonrpc/include/jsonrpc/status.h deleted file mode 100644 index 905a161..0000000 --- a/lib/jsonrpc/include/jsonrpc/status.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef JSONRPC_STATUS_H -#define JSONRPC_STATUS_H - -#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 - -#endif diff --git a/lib/jsonrpc/src/jsonrpc/api.c b/lib/jsonrpc/src/jsonrpc/api.c deleted file mode 100644 index 817438c..0000000 --- a/lib/jsonrpc/src/jsonrpc/api.c +++ /dev/null @@ -1,144 +0,0 @@ -#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_timer_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/src/jsonrpc/impl/method.h b/lib/jsonrpc/src/jsonrpc/impl/method.h deleted file mode 100644 index 595d35d..0000000 --- a/lib/jsonrpc/src/jsonrpc/impl/method.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef JSONRPC_IMPL_METHOD_H -#define JSONRPC_IMPL_METHOD_H - -#include "jsonrpc/method_invoke_fn.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct jsonrpc_method -{ - struct jsonrpc_method * next; - char * name; - jsonrpc_method_invoke_fn * invoke; - void * user_data; -}; - -extern struct jsonrpc_method * -jsonrpc_impl_method_create( - char const * method_name, - jsonrpc_method_invoke_fn * invoke, - void * user_data); - -extern void -jsonrpc_impl_method_dispose( - struct jsonrpc_method * method); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/lib/jsonrpc/src/jsonrpc/impl/proxy.h b/lib/jsonrpc/src/jsonrpc/impl/proxy.h deleted file mode 100644 index 940f853..0000000 --- a/lib/jsonrpc/src/jsonrpc/impl/proxy.h +++ /dev/null @@ -1,84 +0,0 @@ -#ifndef JSONRPC_IMPL_PROXY_H -#define JSONRPC_IMPL_PROXY_H - -#include "jsonrpc/proxy_finished_fn.h" -#include "jsonrpc/send_fn.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_timer_manager; -struct wf_timer; - -struct jsonrpc_request -{ - bool is_pending; - jsonrpc_proxy_finished_fn * finished; - void * user_data; - int id; - struct wf_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_timer_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_timer_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/src/jsonrpc/impl/request.h b/lib/jsonrpc/src/jsonrpc/impl/request.h deleted file mode 100644 index 234a508..0000000 --- a/lib/jsonrpc/src/jsonrpc/impl/request.h +++ /dev/null @@ -1,53 +0,0 @@ -#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/src/jsonrpc/impl/server.c b/lib/jsonrpc/src/jsonrpc/impl/server.c deleted file mode 100644 index 7da42a3..0000000 --- a/lib/jsonrpc/src/jsonrpc/impl/server.c +++ /dev/null @@ -1,132 +0,0 @@ -#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/src/jsonrpc/impl/server.h b/lib/jsonrpc/src/jsonrpc/impl/server.h deleted file mode 100644 index 6263c66..0000000 --- a/lib/jsonrpc/src/jsonrpc/impl/server.h +++ /dev/null @@ -1,41 +0,0 @@ -#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/src/jsonrpc/impl/unused_param.h b/lib/jsonrpc/src/jsonrpc/impl/unused_param.h deleted file mode 100644 index cde12d0..0000000 --- a/lib/jsonrpc/src/jsonrpc/impl/unused_param.h +++ /dev/null @@ -1,10 +0,0 @@ -#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/webfuse/adapter/impl/operation/close.c b/lib/webfuse/adapter/impl/operation/close.c index fc1b1a0..dcb65fb 100644 --- a/lib/webfuse/adapter/impl/operation/close.c +++ b/lib/webfuse/adapter/impl/operation/close.c @@ -4,7 +4,7 @@ #include #include -#include "jsonrpc/proxy.h" +#include "wf/jsonrpc/proxy.h" #include "webfuse/core/util.h" void wf_impl_operation_close( @@ -13,12 +13,12 @@ void wf_impl_operation_close( struct fuse_file_info * file_info) { struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { int handle = (int) (file_info->fh & INT_MAX); - jsonrpc_proxy_notify(rpc, "close", "siii", user_data->name, inode, handle, file_info->flags); + wf_jsonrpc_proxy_notify(rpc, "close", "siii", user_data->name, inode, handle, file_info->flags); } fuse_reply_err(request, 0); diff --git a/lib/webfuse/adapter/impl/operation/getattr.c b/lib/webfuse/adapter/impl/operation/getattr.c index f7be79d..767b06c 100644 --- a/lib/webfuse/adapter/impl/operation/getattr.c +++ b/lib/webfuse/adapter/impl/operation/getattr.c @@ -7,7 +7,7 @@ #include #include -#include "jsonrpc/proxy.h" +#include "wf/jsonrpc/proxy.h" #include "webfuse/core/json_util.h" #include "webfuse/core/util.h" @@ -83,7 +83,7 @@ void wf_impl_operation_getattr ( { struct fuse_ctx const * context = fuse_req_ctx(request); struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { @@ -94,7 +94,7 @@ void wf_impl_operation_getattr ( getattr_context->gid = context->gid; getattr_context->timeout = user_data->timeout; - jsonrpc_proxy_invoke(rpc, &wf_impl_operation_getattr_finished, getattr_context, "getattr", "si", user_data->name, inode); + wf_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_getattr_finished, getattr_context, "getattr", "si", user_data->name, inode); } else { diff --git a/lib/webfuse/adapter/impl/operation/lookup.c b/lib/webfuse/adapter/impl/operation/lookup.c index eaacf8d..596f7e4 100644 --- a/lib/webfuse/adapter/impl/operation/lookup.c +++ b/lib/webfuse/adapter/impl/operation/lookup.c @@ -10,7 +10,7 @@ #include -#include "jsonrpc/proxy.h" +#include "wf/jsonrpc/proxy.h" #include "webfuse/core/json_util.h" #include "webfuse/core/util.h" @@ -92,7 +92,7 @@ void wf_impl_operation_lookup ( { struct fuse_ctx const * context = fuse_req_ctx(request); struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { @@ -102,7 +102,7 @@ void wf_impl_operation_lookup ( lookup_context->gid = context->gid; lookup_context->timeout = user_data->timeout; - jsonrpc_proxy_invoke(rpc, &wf_impl_operation_lookup_finished, lookup_context, "lookup", "sis", user_data->name, (int) (parent & INT_MAX), name); + wf_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_lookup_finished, lookup_context, "lookup", "sis", user_data->name, (int) (parent & INT_MAX), name); } else { diff --git a/lib/webfuse/adapter/impl/operation/open.c b/lib/webfuse/adapter/impl/operation/open.c index 05fcec3..0c10c45 100644 --- a/lib/webfuse/adapter/impl/operation/open.c +++ b/lib/webfuse/adapter/impl/operation/open.c @@ -4,7 +4,7 @@ #include #include -#include "jsonrpc/proxy.h" +#include "wf/jsonrpc/proxy.h" #include "webfuse/core/util.h" #include "webfuse/core/status.h" #include "webfuse/core/json_util.h" @@ -49,11 +49,11 @@ void wf_impl_operation_open( struct fuse_file_info * file_info) { struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { - jsonrpc_proxy_invoke(rpc, &wf_impl_operation_open_finished, request, "open", "sii", user_data->name, inode, file_info->flags); + wf_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_open_finished, request, "open", "sii", user_data->name, inode, file_info->flags); } else { diff --git a/lib/webfuse/adapter/impl/operation/read.c b/lib/webfuse/adapter/impl/operation/read.c index b9cfa1a..87330fc 100644 --- a/lib/webfuse/adapter/impl/operation/read.c +++ b/lib/webfuse/adapter/impl/operation/read.c @@ -5,7 +5,7 @@ #include #include -#include "jsonrpc/proxy.h" +#include "wf/jsonrpc/proxy.h" #include "webfuse/core/base64.h" #include "webfuse/core/json_util.h" @@ -92,13 +92,13 @@ void wf_impl_operation_read( struct fuse_file_info * file_info) { struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { int const length = (size <= WF_MAX_READ_LENGTH) ? (int) size : WF_MAX_READ_LENGTH; int handle = (file_info->fh & INT_MAX); - jsonrpc_proxy_invoke(rpc, &wf_impl_operation_read_finished, request, "read", "siiii", user_data->name, (int) inode, handle, (int) offset, length); + wf_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_read_finished, request, "read", "siiii", user_data->name, (int) inode, handle, (int) offset, length); } else { diff --git a/lib/webfuse/adapter/impl/operation/readdir.c b/lib/webfuse/adapter/impl/operation/readdir.c index 76414ea..f188084 100644 --- a/lib/webfuse/adapter/impl/operation/readdir.c +++ b/lib/webfuse/adapter/impl/operation/readdir.c @@ -8,7 +8,7 @@ #include #include -#include "jsonrpc/proxy.h" +#include "wf/jsonrpc/proxy.h" #include "webfuse/core/util.h" #include "webfuse/core/json_util.h" @@ -139,16 +139,16 @@ void wf_impl_operation_readdir ( struct fuse_file_info * WF_UNUSED_PARAM(file_info)) { struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); if (NULL != rpc) { - struct wf_impl_operation_readdir_context * readdir_context = malloc(sizeof(struct wf_impl_operation_readdir_context)); - readdir_context->request = request; - readdir_context->size = size; - readdir_context->offset = offset; + struct wf_impl_operation_readdir_context * readdir_context = malloc(sizeof(struct wf_impl_operation_readdir_context)); + readdir_context->request = request; + readdir_context->size = size; + readdir_context->offset = offset; - jsonrpc_proxy_invoke(rpc, &wf_impl_operation_readdir_finished, readdir_context, "readdir", "si", user_data->name, inode); + wf_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_readdir_finished, readdir_context, "readdir", "si", user_data->name, inode); } else { diff --git a/lib/webfuse/adapter/impl/operations.c b/lib/webfuse/adapter/impl/operations.c index d3f9e96..9ddad3e 100644 --- a/lib/webfuse/adapter/impl/operations.c +++ b/lib/webfuse/adapter/impl/operations.c @@ -3,10 +3,10 @@ #include "webfuse/adapter/impl/session.h" #include -struct jsonrpc_proxy * wf_impl_operations_context_get_proxy( +struct wf_jsonrpc_proxy * wf_impl_operations_context_get_proxy( struct wf_impl_operations_context * context) { - struct jsonrpc_proxy * proxy = NULL; + struct wf_jsonrpc_proxy * proxy = NULL; struct wf_impl_session * session = context->session; if (NULL != session) diff --git a/lib/webfuse/adapter/impl/operations.h b/lib/webfuse/adapter/impl/operations.h index 50bcef6..4d23db2 100644 --- a/lib/webfuse/adapter/impl/operations.h +++ b/lib/webfuse/adapter/impl/operations.h @@ -8,7 +8,7 @@ extern "C" { #endif struct wf_impl_session; -struct jsonrpc_proxy; +struct wf_jsonrpc_proxy; struct wf_impl_operations_context { @@ -49,7 +49,7 @@ extern void wf_impl_operation_read( fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi); -extern struct jsonrpc_proxy * wf_impl_operations_context_get_proxy( +extern struct wf_jsonrpc_proxy * wf_impl_operations_context_get_proxy( struct wf_impl_operations_context * context); #ifdef __cplusplus diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index 7bf17c9..9085a14 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -9,10 +9,10 @@ #include "webfuse/core/protocol_names.h" #include "webfuse/adapter/impl/credentials.h" -#include "jsonrpc/request.h" #include "webfuse/adapter/impl/uuid_mountpoint_factory.h" #include "webfuse/core/status_intern.h" +#include "wf/jsonrpc/request.h" #include "wf/timer/manager.h" #include "wf/timer/timer.h" @@ -133,7 +133,7 @@ void wf_impl_server_protocol_init_lws( } static void wf_impl_server_protocol_authenticate( - struct jsonrpc_request * request, + struct wf_jsonrpc_request * request, char const * WF_UNUSED_PARAM(method_name), json_t * params, void * WF_UNUSED_PARAM(user_data)) @@ -149,7 +149,7 @@ static void wf_impl_server_protocol_authenticate( struct wf_credentials creds; wf_impl_credentials_init(&creds, type, creds_holder); - struct wf_impl_session * session = jsonrpc_request_get_userdata(request); + struct wf_impl_session * session = wf_jsonrpc_request_get_userdata(request); result = wf_impl_session_authenticate(session, &creds); wf_impl_credentials_cleanup(&creds); @@ -159,11 +159,11 @@ static void wf_impl_server_protocol_authenticate( if (result) { json_t * result = json_object(); - jsonrpc_respond(request, result); + wf_jsonrpc_respond(request, result); } else { - jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED, wf_status_tostring(WF_BAD_ACCESS_DENIED)); + wf_jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED, wf_status_tostring(WF_BAD_ACCESS_DENIED)); } } @@ -183,12 +183,12 @@ static bool wf_impl_server_protocol_check_name(char const * value) } static void wf_impl_server_protocol_add_filesystem( - struct jsonrpc_request * request, + struct wf_jsonrpc_request * request, char const * WF_UNUSED_PARAM(method_name), json_t * params, void * WF_UNUSED_PARAM(user_data)) { - struct wf_impl_session * session = jsonrpc_request_get_userdata(request); + struct wf_impl_session * session = wf_jsonrpc_request_get_userdata(request); wf_status status = (session->is_authenticated) ? WF_GOOD : WF_BAD_ACCESS_DENIED; char const * name = NULL; @@ -222,11 +222,11 @@ static void wf_impl_server_protocol_add_filesystem( { json_t * result = json_object(); json_object_set_new(result, "id", json_string(name)); - jsonrpc_respond(request, result); + wf_jsonrpc_respond(request, result); } else { - jsonrpc_respond_error(request, status, wf_status_tostring(status)); + wf_jsonrpc_respond_error(request, status, wf_status_tostring(status)); } @@ -244,9 +244,9 @@ void wf_impl_server_protocol_init( wf_impl_session_manager_init(&protocol->session_manager); wf_impl_authenticators_init(&protocol->authenticators); - 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); + protocol->server = wf_jsonrpc_server_create(); + wf_jsonrpc_server_add(protocol->server, "authenticate", &wf_impl_server_protocol_authenticate, protocol); + wf_jsonrpc_server_add(protocol->server, "add_filesystem", &wf_impl_server_protocol_add_filesystem, protocol); } void wf_impl_server_protocol_cleanup( @@ -254,7 +254,7 @@ void wf_impl_server_protocol_cleanup( { protocol->is_operational = false; - jsonrpc_server_dispose(protocol->server); + wf_jsonrpc_server_dispose(protocol->server); wf_timer_manager_dispose(protocol->timer_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 0a9fdbd..5d8052e 100644 --- a/lib/webfuse/adapter/impl/server_protocol.h +++ b/lib/webfuse/adapter/impl/server_protocol.h @@ -1,11 +1,11 @@ #ifndef WF_ADAPTER_IMPL_SERVER_PROTOCOL_H #define WF_ADAPTER_IMPL_SERVER_PROTOCOL_H -#include "jsonrpc/proxy.h" #include "webfuse/adapter/impl/authenticators.h" #include "webfuse/adapter/impl/mountpoint_factory.h" #include "webfuse/adapter/impl/session_manager.h" -#include "jsonrpc/server.h" +#include "wf/jsonrpc/proxy.h" +#include "wf/jsonrpc/server.h" #ifndef __cplusplus #include @@ -24,7 +24,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 wf_jsonrpc_server * server; struct wf_timer_manager * timer_manager; bool is_operational; }; diff --git a/lib/webfuse/adapter/impl/session.c b/lib/webfuse/adapter/impl/session.c index 870f6c0..e1fdd87 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -2,15 +2,16 @@ #include "webfuse/adapter/impl/authenticators.h" #include "webfuse/core/message_queue.h" #include "webfuse/core/message.h" -#include "jsonrpc/proxy.h" -#include "jsonrpc/request.h" -#include "jsonrpc/response.h" #include "webfuse/adapter/impl/mountpoint_factory.h" #include "webfuse/adapter/impl/mountpoint.h" #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 #include #include @@ -24,7 +25,7 @@ static bool wf_impl_session_send( struct wf_impl_session * session = user_data; struct wf_message * message = wf_message_create(request); - bool result = (session->is_authenticated || jsonrpc_is_response(request)) && (NULL != session->wsi); + bool result = (session->is_authenticated || wf_jsonrpc_is_response(request)) && (NULL != session->wsi); if (result) { @@ -45,7 +46,7 @@ struct wf_impl_session * wf_impl_session_create( struct lws * wsi, struct wf_impl_authenticators * authenticators, struct wf_timer_manager * timer_manager, - struct jsonrpc_server * server, + struct wf_jsonrpc_server * server, struct wf_impl_mountpoint_factory * mountpoint_factory) { @@ -59,7 +60,7 @@ struct wf_impl_session * wf_impl_session_create( session->authenticators = authenticators; session->server = server; session->mountpoint_factory = mountpoint_factory; - session->rpc = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); + session->rpc = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); wf_slist_init(&session->messages); } @@ -83,7 +84,7 @@ static void wf_impl_session_dispose_filesystems( void wf_impl_session_dispose( struct wf_impl_session * session) { - jsonrpc_proxy_dispose(session->rpc); + wf_jsonrpc_proxy_dispose(session->rpc); wf_message_queue_cleanup(&session->messages); wf_impl_session_dispose_filesystems(&session->filesystems); @@ -154,13 +155,13 @@ void wf_impl_session_receive( json_t * message = json_loadb(data, length, 0, NULL); if (NULL != message) { - if (jsonrpc_is_response(message)) + if (wf_jsonrpc_is_response(message)) { - jsonrpc_proxy_onresult(session->rpc, message); + wf_jsonrpc_proxy_onresult(session->rpc, message); } - else if (jsonrpc_is_request(message)) + else if (wf_jsonrpc_is_request(message)) { - jsonrpc_server_process(session->server, message, &wf_impl_session_send, session); + wf_jsonrpc_server_process(session->server, message, &wf_impl_session_send, session); } json_decref(message); diff --git a/lib/webfuse/adapter/impl/session.h b/lib/webfuse/adapter/impl/session.h index a9d15af..82b042f 100644 --- a/lib/webfuse/adapter/impl/session.h +++ b/lib/webfuse/adapter/impl/session.h @@ -10,11 +10,12 @@ using std::size_t; #endif #include "webfuse/core/message_queue.h" -#include "jsonrpc/proxy.h" -#include "jsonrpc/server.h" #include "webfuse/adapter/impl/filesystem.h" #include "webfuse/core/slist.h" +#include "wf/jsonrpc/proxy.h" +#include "wf/jsonrpc/server.h" + #ifdef __cplusplus extern "C" { @@ -25,7 +26,6 @@ struct wf_message; struct wf_credentials; struct wf_impl_authenticators; struct wf_impl_mountpoint_factory; -struct timer_manager; struct wf_impl_session { @@ -35,8 +35,8 @@ struct wf_impl_session struct wf_slist messages; struct wf_impl_authenticators * authenticators; struct wf_impl_mountpoint_factory * mountpoint_factory; - struct jsonrpc_server * server; - struct jsonrpc_proxy * rpc; + struct wf_jsonrpc_server * server; + struct wf_jsonrpc_proxy * rpc; struct wf_slist filesystems; }; @@ -44,7 +44,7 @@ extern struct wf_impl_session * wf_impl_session_create( struct lws * wsi, struct wf_impl_authenticators * authenticators, struct wf_timer_manager * timer_manager, - struct jsonrpc_server * server, + struct wf_jsonrpc_server * server, struct wf_impl_mountpoint_factory * mountpoint_factory); extern void wf_impl_session_dispose( diff --git a/lib/webfuse/adapter/impl/session_manager.c b/lib/webfuse/adapter/impl/session_manager.c index 6474c06..cc41fa0 100644 --- a/lib/webfuse/adapter/impl/session_manager.c +++ b/lib/webfuse/adapter/impl/session_manager.c @@ -29,7 +29,7 @@ struct wf_impl_session * wf_impl_session_manager_add( struct wf_impl_authenticators * authenticators, struct wf_impl_mountpoint_factory * mountpoint_factory, struct wf_timer_manager * timer_manager, - struct jsonrpc_server * server) + struct wf_jsonrpc_server * server) { struct wf_impl_session * session = wf_impl_session_create( wsi, authenticators, timer_manager, server, mountpoint_factory); diff --git a/lib/webfuse/adapter/impl/session_manager.h b/lib/webfuse/adapter/impl/session_manager.h index 4f5f218..691f133 100644 --- a/lib/webfuse/adapter/impl/session_manager.h +++ b/lib/webfuse/adapter/impl/session_manager.h @@ -16,7 +16,7 @@ extern "C" struct lws; struct wf_timer_manager; -struct jsonrpc_server; +struct wf_jsonrpc_server; struct wf_impl_session_manager { @@ -35,7 +35,7 @@ extern struct wf_impl_session * wf_impl_session_manager_add( struct wf_impl_authenticators * authenticators, struct wf_impl_mountpoint_factory * mountpoint_factory, struct wf_timer_manager * timer_manager, - struct jsonrpc_server * server); + struct wf_jsonrpc_server * server); extern struct wf_impl_session * wf_impl_session_manager_get( struct wf_impl_session_manager * manager, diff --git a/lib/webfuse/core/json_util.c b/lib/webfuse/core/json_util.c index dfe95cd..48fe956 100644 --- a/lib/webfuse/core/json_util.c +++ b/lib/webfuse/core/json_util.c @@ -1,5 +1,5 @@ #include "webfuse/core/json_util.h" -#include "jsonrpc/status.h" +#include "wf/jsonrpc/status.h" int wf_impl_json_get_int(json_t const * object, char const * key, int default_value) { @@ -18,15 +18,15 @@ static wf_status wf_impl_jsonrc_code_to_status(int code) { switch (code) { - case JSONRPC_GOOD: + case WF_JSONRPC_GOOD: return WF_GOOD; - case JSONRPC_BAD: + case WF_JSONRPC_BAD: return WF_BAD; - case JSONRPC_BAD_BUSY: + case WF_JSONRPC_BAD_BUSY: return WF_BAD_BUSY; - case JSONRPC_BAD_TIMEOUT: + case WF_JSONRPC_BAD_TIMEOUT: return WF_BAD_TIMEOUT; - case JSONRPC_BAD_FORMAT: + case WF_JSONRPC_BAD_FORMAT: return WF_BAD_FORMAT; default: return (wf_status) code; diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index 1b434fc..4f61b8a 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -18,9 +18,9 @@ #include "wf/timer/manager.h" -#include "jsonrpc/response.h" -#include "jsonrpc/request.h" -#include "jsonrpc/proxy.h" +#include "wf/jsonrpc/response.h" +#include "wf/jsonrpc/request.h" +#include "wf/jsonrpc/proxy.h" #define WF_DEFAULT_TIMEOUT (10 * 1000) @@ -46,12 +46,12 @@ static void wfp_impl_client_protocol_process( json_t * message = json_loadb(data, length, 0, NULL); if (NULL != message) { - if (jsonrpc_is_response(message)) + if (wf_jsonrpc_is_response(message)) { - jsonrpc_proxy_onresult(protocol->proxy, message); + wf_jsonrpc_proxy_onresult(protocol->proxy, message); } - if (jsonrpc_is_request(message)) + if (wf_jsonrpc_is_request(message)) { struct wfp_impl_invokation_context context = { @@ -91,7 +91,7 @@ wfp_impl_client_protocol_on_add_filesystem_finished( static void wfp_impl_client_protocol_add_filesystem( struct wfp_client_protocol * protocol) { - jsonrpc_proxy_invoke( + wf_jsonrpc_proxy_invoke( protocol->proxy, &wfp_impl_client_protocol_on_add_filesystem_finished, protocol, @@ -132,7 +132,7 @@ static void wfp_impl_client_protocol_authenticate( json_t * creds = wfp_impl_credentials_get(&credentials); json_incref(creds); - jsonrpc_proxy_invoke( + wf_jsonrpc_proxy_invoke( protocol->proxy, &wfp_impl_client_protocol_on_authenticate_finished, protocol, @@ -252,7 +252,7 @@ void wfp_impl_client_protocol_init( protocol->request.user_data = protocol; protocol->timer_manager = wf_timer_manager_create(); - protocol->proxy = jsonrpc_proxy_create(protocol->timer_manager, WF_DEFAULT_TIMEOUT, &wfp_impl_client_protocol_send, protocol); + protocol->proxy = wf_jsonrpc_proxy_create(protocol->timer_manager, WF_DEFAULT_TIMEOUT, &wfp_impl_client_protocol_send, protocol); protocol->user_data = user_data; wfp_impl_provider_init_from_prototype(&protocol->provider, provider); @@ -261,7 +261,7 @@ void wfp_impl_client_protocol_init( void wfp_impl_client_protocol_cleanup( struct wfp_client_protocol * protocol) { - jsonrpc_proxy_dispose(protocol->proxy); + wf_jsonrpc_proxy_dispose(protocol->proxy); wf_timer_manager_dispose(protocol->timer_manager); wf_message_queue_cleanup(&protocol->messages); } diff --git a/lib/webfuse/provider/impl/client_protocol.h b/lib/webfuse/provider/impl/client_protocol.h index d0e6660..af5c74b 100644 --- a/lib/webfuse/provider/impl/client_protocol.h +++ b/lib/webfuse/provider/impl/client_protocol.h @@ -14,7 +14,7 @@ extern "C" struct wfp_client_config; struct lws_protocols; struct lws_context; -struct jsonrpc_proxy; +struct wf_jsonrpc_proxy; struct wf_timer_manager; struct wfp_client_protocol @@ -26,7 +26,7 @@ struct wfp_client_protocol void * user_data; struct lws * wsi; struct wf_timer_manager * timer_manager; - struct jsonrpc_proxy * proxy; + struct wf_jsonrpc_proxy * proxy; struct wf_slist messages; }; diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc.h b/lib/wf/jsonrpc/include/wf/jsonrpc.h new file mode 100644 index 0000000..ab7f1dc --- /dev/null +++ b/lib/wf/jsonrpc/include/wf/jsonrpc.h @@ -0,0 +1,14 @@ +#ifndef WF_JSONRPC_H +#define WF_JSONRPC_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/api.h b/lib/wf/jsonrpc/include/wf/jsonrpc/api.h new file mode 100644 index 0000000..648bf0f --- /dev/null +++ b/lib/wf/jsonrpc/include/wf/jsonrpc/api.h @@ -0,0 +1,8 @@ +#ifndef WF_JSONRPC_API_H +#define WF_JSONRPC_API_H + +#ifndef WF_JSONRPC_API +#define WF_JSONRPC_API +#endif + +#endif diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/method_invoke_fn.h b/lib/wf/jsonrpc/include/wf/jsonrpc/method_invoke_fn.h new file mode 100644 index 0000000..d7a9cc8 --- /dev/null +++ b/lib/wf/jsonrpc/include/wf/jsonrpc/method_invoke_fn.h @@ -0,0 +1,26 @@ +#ifndef WF_JSONRPC_METHOD_INVOKE_FN_H +#define WF_JSONRPC_METHOD_INVOKE_FN_H + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_jsonrpc_request; + +typedef void wf_jsonrpc_method_invoke_fn( + struct wf_jsonrpc_request * request, + char const * method_name, + json_t * params, + void * user_data); + + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/lib/jsonrpc/include/jsonrpc/proxy.h b/lib/wf/jsonrpc/include/wf/jsonrpc/proxy.h similarity index 63% rename from lib/jsonrpc/include/jsonrpc/proxy.h rename to lib/wf/jsonrpc/include/wf/jsonrpc/proxy.h index a7573fa..66fd1a5 100644 --- a/lib/jsonrpc/include/jsonrpc/proxy.h +++ b/lib/wf/jsonrpc/include/wf/jsonrpc/proxy.h @@ -1,5 +1,5 @@ -#ifndef JSONRPC_PROXY_H -#define JSONRPC_PROXY_H +#ifndef WF_JSONRPC_PROXY_H +#define WF_JSONRPC_PROXY_H #ifndef __cplusplus #include @@ -12,26 +12,26 @@ using std::size_t; #endif #include -#include -#include -#include +#include +#include +#include #ifdef __cplusplus extern "C" { #endif -struct jsonrpc_proxy; +struct wf_jsonrpc_proxy; struct wf_timer_manager; -extern JSONRPC_API struct jsonrpc_proxy * -jsonrpc_proxy_create( +extern WF_JSONRPC_API struct wf_jsonrpc_proxy * +wf_jsonrpc_proxy_create( struct wf_timer_manager * manager, int timeout, - jsonrpc_send_fn * send, + wf_jsonrpc_send_fn * send, void * user_data); -extern JSONRPC_API void jsonrpc_proxy_dispose( - struct jsonrpc_proxy * proxy); +extern WF_JSONRPC_API void wf_jsonrpc_proxy_dispose( + struct wf_jsonrpc_proxy * proxy); //------------------------------------------------------------------------------ /// \brief Invokes a method. @@ -47,24 +47,24 @@ extern JSONRPC_API void jsonrpc_proxy_dispose( /// \param param_info types of the param (s = string, i = integer, j = json) /// \param ... params //------------------------------------------------------------------------------ -extern JSONRPC_API void jsonrpc_proxy_invoke( - struct jsonrpc_proxy * proxy, - jsonrpc_proxy_finished_fn * finished, +extern WF_JSONRPC_API void wf_jsonrpc_proxy_invoke( + struct wf_jsonrpc_proxy * proxy, + wf_jsonrpc_proxy_finished_fn * finished, void * user_data, char const * method_name, char const * param_info, ... ); -extern JSONRPC_API void jsonrpc_proxy_notify( - struct jsonrpc_proxy * proxy, +extern WF_JSONRPC_API void wf_jsonrpc_proxy_notify( + struct wf_jsonrpc_proxy * proxy, char const * method_name, char const * param_info, ... ); -extern JSONRPC_API void jsonrpc_proxy_onresult( - struct jsonrpc_proxy * proxy, +extern WF_JSONRPC_API void wf_jsonrpc_proxy_onresult( + struct wf_jsonrpc_proxy * proxy, json_t * message); #ifdef __cplusplus diff --git a/lib/jsonrpc/include/jsonrpc/proxy_finished_fn.h b/lib/wf/jsonrpc/include/wf/jsonrpc/proxy_finished_fn.h similarity index 58% rename from lib/jsonrpc/include/jsonrpc/proxy_finished_fn.h rename to lib/wf/jsonrpc/include/wf/jsonrpc/proxy_finished_fn.h index 4c96b2b..cd59c87 100644 --- a/lib/jsonrpc/include/jsonrpc/proxy_finished_fn.h +++ b/lib/wf/jsonrpc/include/wf/jsonrpc/proxy_finished_fn.h @@ -1,5 +1,5 @@ -#ifndef JSONRPC_PROXY_FINISHED_FN_H -#define JSONRPC_PROXY_FINISHED_FN_H +#ifndef WF_JSONRPC_PROXY_FINISHED_FN_H +#define WF_JSONRPC_PROXY_FINISHED_FN_H #include @@ -8,7 +8,7 @@ extern "C" { #endif -typedef void jsonrpc_proxy_finished_fn( +typedef void wf_jsonrpc_proxy_finished_fn( void * user_data, json_t const * result, json_t const * error); diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/request.h b/lib/wf/jsonrpc/include/wf/jsonrpc/request.h new file mode 100644 index 0000000..73295e6 --- /dev/null +++ b/lib/wf/jsonrpc/include/wf/jsonrpc/request.h @@ -0,0 +1,54 @@ +#ifndef WF_JSONRPC_REQUEST_H +#define WF_JSONRPC_REQUEST_H + +#ifndef __cplusplus +#include +#include +#include +#else +#include +#include +using std::size_t; +#endif + +#include +#include +#include "wf/jsonrpc/send_fn.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_jsonrpc_request; + +extern WF_JSONRPC_API bool wf_jsonrpc_is_request( + json_t * message); + +extern WF_JSONRPC_API struct wf_jsonrpc_request * +wf_jsonrpc_request_create( + int id, + wf_jsonrpc_send_fn * send, + void * user_data); + +extern WF_JSONRPC_API void wf_jsonrpc_request_dispose( + struct wf_jsonrpc_request * request); + +extern WF_JSONRPC_API void * wf_jsonrpc_request_get_userdata( + struct wf_jsonrpc_request * request); + +extern WF_JSONRPC_API void wf_jsonrpc_respond( + struct wf_jsonrpc_request * request, + json_t * result); + +extern WF_JSONRPC_API void wf_jsonrpc_respond_error( + struct wf_jsonrpc_request * request, + int code, + char const * message); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/jsonrpc/include/jsonrpc/response.h b/lib/wf/jsonrpc/include/wf/jsonrpc/response.h similarity index 55% rename from lib/jsonrpc/include/jsonrpc/response.h rename to lib/wf/jsonrpc/include/wf/jsonrpc/response.h index 64414ec..abe2011 100644 --- a/lib/jsonrpc/include/jsonrpc/response.h +++ b/lib/wf/jsonrpc/include/wf/jsonrpc/response.h @@ -1,19 +1,19 @@ -#ifndef JSONRPC_RESPONSE_H -#define JSONRPC_RESPONSE_H +#ifndef WF_JSONRPC_RESPONSE_H +#define WF_JSONRPC_RESPONSE_H #ifndef __cplusplus #include #endif #include -#include +#include #ifdef __cplusplus extern "C" { #endif -extern JSONRPC_API bool jsonrpc_is_response( +extern WF_JSONRPC_API bool wf_jsonrpc_is_response( json_t * message); #ifdef __cplusplus diff --git a/lib/jsonrpc/include/jsonrpc/send_fn.h b/lib/wf/jsonrpc/include/wf/jsonrpc/send_fn.h similarity index 61% rename from lib/jsonrpc/include/jsonrpc/send_fn.h rename to lib/wf/jsonrpc/include/wf/jsonrpc/send_fn.h index fe866af..9ea9ef4 100644 --- a/lib/jsonrpc/include/jsonrpc/send_fn.h +++ b/lib/wf/jsonrpc/include/wf/jsonrpc/send_fn.h @@ -1,19 +1,19 @@ -#ifndef JSONRPC_SEND_FN_H -#define JSONRPC_SEND_FN_H +#ifndef WF_JSONRPC_SEND_FN_H +#define WF_JSONRPC_SEND_FN_H #ifndef __cplusplus #include #endif #include -#include +#include #ifdef __cplusplus extern "C" { #endif -typedef bool jsonrpc_send_fn( +typedef bool wf_jsonrpc_send_fn( json_t * request, void * user_data); diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/server.h b/lib/wf/jsonrpc/include/wf/jsonrpc/server.h new file mode 100644 index 0000000..c4850a5 --- /dev/null +++ b/lib/wf/jsonrpc/include/wf/jsonrpc/server.h @@ -0,0 +1,46 @@ +#ifndef WF_JSONRPC_SERVER_H +#define WF_JSONRPC_SERVER_H + +#ifndef __cplusplus +#include +#include +#else +#include +#endif + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_jsonrpc_server; + +extern WF_JSONRPC_API struct wf_jsonrpc_server * +wf_jsonrpc_server_create(void); + +extern WF_JSONRPC_API void +wf_jsonrpc_server_dispose( + struct wf_jsonrpc_server * server); + +extern WF_JSONRPC_API void wf_jsonrpc_server_add( + struct wf_jsonrpc_server * server, + char const * method_name, + wf_jsonrpc_method_invoke_fn * invoke, + void * user_data); + +extern WF_JSONRPC_API void wf_jsonrpc_server_process( + struct wf_jsonrpc_server * server, + json_t * request, + wf_jsonrpc_send_fn * send, + void * user_data); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/status.h b/lib/wf/jsonrpc/include/wf/jsonrpc/status.h new file mode 100644 index 0000000..85da73d --- /dev/null +++ b/lib/wf/jsonrpc/include/wf/jsonrpc/status.h @@ -0,0 +1,11 @@ +#ifndef WF_JSONRPC_STATUS_H +#define WF_JSONRPC_STATUS_H + +#define WF_JSONRPC_GOOD 0 +#define WF_JSONRPC_BAD -1 +#define WF_JSONRPC_BAD_NOTIMPLEMENTED -2 +#define WF_JSONRPC_BAD_TIMEOUT -3 +#define WF_JSONRPC_BAD_BUSY -4 +#define WF_JSONRPC_BAD_FORMAT -5 + +#endif diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/api.c b/lib/wf/jsonrpc/src/wf/jsonrpc/api.c new file mode 100644 index 0000000..891bcce --- /dev/null +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/api.c @@ -0,0 +1,144 @@ +#include "wf/jsonrpc.h" + +#include "wf/jsonrpc/impl/proxy.h" +#include "wf/jsonrpc/impl/request.h" +#include "wf/jsonrpc/impl/response.h" +#include "wf/jsonrpc/impl/server.h" + +// proxy + +struct wf_jsonrpc_proxy * +wf_jsonrpc_proxy_create( + struct wf_timer_manager * manager, + int timeout, + wf_jsonrpc_send_fn * send, + void * user_data) +{ + return wf_jsonrpc_impl_proxy_create(manager, timeout, send, user_data); +} + +void wf_jsonrpc_proxy_dispose( + struct wf_jsonrpc_proxy * proxy) +{ + wf_jsonrpc_impl_proxy_dispose(proxy); +} + +void wf_jsonrpc_proxy_invoke( + struct wf_jsonrpc_proxy * proxy, + wf_jsonrpc_proxy_finished_fn * finished, + void * user_data, + char const * method_name, + char const * param_info, + ... +) +{ + va_list args; + va_start(args, param_info); + wf_jsonrpc_impl_proxy_invoke(proxy, finished, user_data, method_name, param_info, args); + va_end(args); +} + +void wf_jsonrpc_proxy_notify( + struct wf_jsonrpc_proxy * proxy, + char const * method_name, + char const * param_info, + ... +) +{ + va_list args; + va_start(args, param_info); + wf_jsonrpc_impl_proxy_notify(proxy, method_name, param_info, args); + va_end(args); +} + +void wf_jsonrpc_proxy_onresult( + struct wf_jsonrpc_proxy * proxy, + json_t * message) +{ + wf_jsonrpc_impl_proxy_onresult(proxy, message); +} + + +// request + +bool wf_jsonrpc_is_request( + json_t * message) +{ + return wf_jsonrpc_impl_is_request(message); +} + +struct wf_jsonrpc_request * wf_jsonrpc_request_create( + int id, + wf_jsonrpc_send_fn * send, + void * user_data) +{ + return wf_jsonrpc_impl_request_create(id, send, user_data); +} + +void wf_jsonrpc_request_dispose( + struct wf_jsonrpc_request * request) +{ + wf_jsonrpc_impl_request_dispose(request); +} + +void * wf_jsonrpc_request_get_userdata( + struct wf_jsonrpc_request * request) +{ + return wf_jsonrpc_impl_request_get_userdata(request); +} + +void wf_jsonrpc_respond( + struct wf_jsonrpc_request * request, + json_t * result) +{ + wf_jsonrpc_impl_respond(request, result); +} + +void wf_jsonrpc_respond_error( + struct wf_jsonrpc_request * request, + int code, + char const * message) +{ + wf_jsonrpc_impl_respond_error(request, code, message); +} + +// response + +bool wf_jsonrpc_is_response( + json_t * message) +{ + return wf_jsonrpc_impl_is_response(message); +} + +// server + +struct wf_jsonrpc_server * +wf_jsonrpc_server_create(void) +{ + return wf_jsonrpc_impl_server_create(); +} + +void +wf_jsonrpc_server_dispose( + struct wf_jsonrpc_server * server) +{ + wf_jsonrpc_impl_server_dispose(server); +} + +void wf_jsonrpc_server_add( + struct wf_jsonrpc_server * server, + char const * method_name, + wf_jsonrpc_method_invoke_fn * invoke, + void * user_data) +{ + wf_jsonrpc_impl_server_add(server, method_name, invoke, user_data); +} + +void wf_jsonrpc_server_process( + struct wf_jsonrpc_server * server, + json_t * request, + wf_jsonrpc_send_fn * send, + void * user_data) +{ + wf_jsonrpc_impl_server_process(server, request, send, user_data); +} diff --git a/lib/jsonrpc/src/jsonrpc/impl/error.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.c similarity index 65% rename from lib/jsonrpc/src/jsonrpc/impl/error.c rename to lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.c index 716f6de..eba2e86 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/error.c +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.c @@ -1,7 +1,7 @@ -#include "jsonrpc/impl/error.h" +#include "wf/jsonrpc/impl/error.h" json_t * -jsonrpc_impl_error( +wf_jsonrpc_impl_error( int code, char const * message) { @@ -13,13 +13,13 @@ jsonrpc_impl_error( } void -jsonrpc_impl_propate_error( - jsonrpc_proxy_finished_fn * finised, +wf_jsonrpc_impl_propate_error( + wf_jsonrpc_proxy_finished_fn * finised, void * user_data, int code, char const * message) { - json_t * error = jsonrpc_impl_error(code, message); + json_t * error = wf_jsonrpc_impl_error(code, message); finised(user_data, NULL, error); json_decref(error); diff --git a/lib/jsonrpc/src/jsonrpc/impl/error.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.h similarity index 53% rename from lib/jsonrpc/src/jsonrpc/impl/error.h rename to lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.h index 6485f05..fda0f89 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/error.h +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.h @@ -1,8 +1,8 @@ -#ifndef JSONRPC_IMPL_ERROR_H -#define JSONRPC_IMPL_ERROR_H +#ifndef WF_JSONRPC_IMPL_ERROR_H +#define WF_JSONRPC_IMPL_ERROR_H #include -#include "jsonrpc/proxy_finished_fn.h" +#include "wf/jsonrpc/proxy_finished_fn.h" #ifdef __cplusplus extern "C" @@ -10,13 +10,13 @@ extern "C" #endif extern json_t * -jsonrpc_impl_error( +wf_jsonrpc_impl_error( int code, char const * message); extern void -jsonrpc_impl_propate_error( - jsonrpc_proxy_finished_fn * finised, +wf_jsonrpc_impl_propate_error( + wf_jsonrpc_proxy_finished_fn * finised, void * user_data, int code, char const * message); diff --git a/lib/jsonrpc/src/jsonrpc/impl/method.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c similarity index 53% rename from lib/jsonrpc/src/jsonrpc/impl/method.c rename to lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c index 073e8b3..87b3ff1 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/method.c +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c @@ -1,13 +1,13 @@ -#include "jsonrpc/impl/method.h" +#include "wf/jsonrpc/impl/method.h" #include #include -struct jsonrpc_method * jsonrpc_impl_method_create( +struct wf_jsonrpc_method * wf_jsonrpc_impl_method_create( char const * method_name, - jsonrpc_method_invoke_fn * invoke, + wf_jsonrpc_method_invoke_fn * invoke, void * user_data) { - struct jsonrpc_method * method = malloc(sizeof(struct jsonrpc_method)); + struct wf_jsonrpc_method * method = malloc(sizeof(struct wf_jsonrpc_method)); if (NULL != method) { method->next = NULL; @@ -19,8 +19,8 @@ struct jsonrpc_method * jsonrpc_impl_method_create( return method; } -void jsonrpc_impl_method_dispose( - struct jsonrpc_method * method) +void wf_jsonrpc_impl_method_dispose( + struct wf_jsonrpc_method * method) { free(method->name); free(method); diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.h new file mode 100644 index 0000000..d0e42f0 --- /dev/null +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.h @@ -0,0 +1,34 @@ +#ifndef WF_JSONRPC_IMPL_METHOD_H +#define WF_JSONRPC_IMPL_METHOD_H + +#include "wf/jsonrpc/method_invoke_fn.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_jsonrpc_method +{ + struct wf_jsonrpc_method * next; + char * name; + wf_jsonrpc_method_invoke_fn * invoke; + void * user_data; +}; + +extern struct wf_jsonrpc_method * +wf_jsonrpc_impl_method_create( + char const * method_name, + wf_jsonrpc_method_invoke_fn * invoke, + void * user_data); + +extern void +wf_jsonrpc_impl_method_dispose( + struct wf_jsonrpc_method * method); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/lib/jsonrpc/src/jsonrpc/impl/proxy.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c similarity index 64% rename from lib/jsonrpc/src/jsonrpc/impl/proxy.c rename to lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c index 976da05..a2b9816 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/proxy.c +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c @@ -1,44 +1,44 @@ -#include "jsonrpc/impl/proxy.h" -#include "jsonrpc/impl/response.h" -#include "jsonrpc/impl/error.h" -#include "jsonrpc/status.h" +#include "wf/jsonrpc/impl/proxy.h" +#include "wf/jsonrpc/impl/response.h" +#include "wf/jsonrpc/impl/error.h" +#include "wf/jsonrpc/status.h" #include #include #include -struct jsonrpc_proxy * -jsonrpc_impl_proxy_create( +struct wf_jsonrpc_proxy * +wf_jsonrpc_impl_proxy_create( struct wf_timer_manager * manager, int timeout, - jsonrpc_send_fn * send, + wf_jsonrpc_send_fn * send, void * user_data) { - struct jsonrpc_proxy * proxy = malloc(sizeof(struct jsonrpc_proxy)); + struct wf_jsonrpc_proxy * proxy = malloc(sizeof(struct wf_jsonrpc_proxy)); if (NULL != proxy) { - jsonrpc_impl_proxy_init(proxy, manager, timeout, send, user_data); + wf_jsonrpc_impl_proxy_init(proxy, manager, timeout, send, user_data); } return proxy; } -void jsonrpc_impl_proxy_dispose( - struct jsonrpc_proxy * proxy) +void wf_jsonrpc_impl_proxy_dispose( + struct wf_jsonrpc_proxy * proxy) { - jsonrpc_impl_proxy_cleanup(proxy); + wf_jsonrpc_impl_proxy_cleanup(proxy); free(proxy); } -static void jsonrpc_impl_proxy_on_timeout( +static void wf_jsonrpc_impl_proxy_on_timeout( struct wf_timer * timer, void * proxy_ptr) { - struct jsonrpc_proxy * proxy = proxy_ptr; + struct wf_jsonrpc_proxy * proxy = proxy_ptr; if (proxy->request.is_pending) { - jsonrpc_proxy_finished_fn * finished = proxy->request.finished; + wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished; void * user_data = proxy->request.user_data; proxy->request.is_pending = false; @@ -47,11 +47,11 @@ static void jsonrpc_impl_proxy_on_timeout( proxy->request.finished = NULL; wf_timer_cancel(timer); - jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_TIMEOUT, "Timeout"); + wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD_TIMEOUT, "Timeout"); } } -static json_t * jsonrpc_impl_request_create( +static json_t * wf_jsonrpc_impl_request_create( char const * method, int id, char const * param_info, @@ -101,11 +101,11 @@ static json_t * jsonrpc_impl_request_create( return request; } -void jsonrpc_impl_proxy_init( - struct jsonrpc_proxy * proxy, +void wf_jsonrpc_impl_proxy_init( + struct wf_jsonrpc_proxy * proxy, struct wf_timer_manager * timeout_manager, int timeout, - jsonrpc_send_fn * send, + wf_jsonrpc_send_fn * send, void * user_data) { proxy->send = send; @@ -113,16 +113,16 @@ void jsonrpc_impl_proxy_init( proxy->user_data = user_data; proxy->request.is_pending = false; proxy->request.timer = wf_timer_create(timeout_manager, - &jsonrpc_impl_proxy_on_timeout, proxy); + &wf_jsonrpc_impl_proxy_on_timeout, proxy); } -void jsonrpc_impl_proxy_cleanup( - struct jsonrpc_proxy * proxy) +void wf_jsonrpc_impl_proxy_cleanup( + struct wf_jsonrpc_proxy * proxy) { if (proxy->request.is_pending) { void * user_data = proxy->request.user_data; - jsonrpc_proxy_finished_fn * finished = proxy->request.finished; + wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished; proxy->request.is_pending = false; proxy->request.finished = NULL; @@ -130,15 +130,15 @@ void jsonrpc_impl_proxy_cleanup( proxy->request.id = 0; wf_timer_cancel(proxy->request.timer); - jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad: cancelled pending request during shutdown"); + wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD, "Bad: cancelled pending request during shutdown"); } wf_timer_dispose(proxy->request.timer); } -void jsonrpc_impl_proxy_invoke( - struct jsonrpc_proxy * proxy, - jsonrpc_proxy_finished_fn * finished, +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, @@ -153,7 +153,7 @@ void jsonrpc_impl_proxy_invoke( proxy->request.id = 42; wf_timer_start(proxy->request.timer, proxy->timeout); - json_t * request = jsonrpc_impl_request_create(method_name, proxy->request.id, param_info, args); + json_t * request = wf_jsonrpc_impl_request_create(method_name, proxy->request.id, param_info, args); bool const is_send = ((NULL != request) && (proxy->send(request, proxy->user_data))); if (!is_send) @@ -164,7 +164,7 @@ void jsonrpc_impl_proxy_invoke( proxy->request.id = 0; wf_timer_cancel(proxy->request.timer); - jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad: requenst is not sent"); + wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD, "Bad: requenst is not sent"); } if (NULL != request) @@ -174,18 +174,18 @@ void jsonrpc_impl_proxy_invoke( } else { - jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_BUSY, "Busy"); + wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD_BUSY, "Busy"); } } -extern void jsonrpc_impl_proxy_notify( - struct jsonrpc_proxy * proxy, +extern void wf_jsonrpc_impl_proxy_notify( + struct wf_jsonrpc_proxy * proxy, char const * method_name, char const * param_info, va_list args ) { - json_t * request = jsonrpc_impl_request_create(method_name, 0, param_info, args); + json_t * request = wf_jsonrpc_impl_request_create(method_name, 0, param_info, args); if (NULL != request) { proxy->send(request, proxy->user_data); @@ -194,16 +194,16 @@ extern void jsonrpc_impl_proxy_notify( } -void jsonrpc_impl_proxy_onresult( - struct jsonrpc_proxy * proxy, +void wf_jsonrpc_impl_proxy_onresult( + struct wf_jsonrpc_proxy * proxy, json_t * message) { - struct jsonrpc_response response; - jsonrpc_impl_response_init(&response, message); + struct wf_jsonrpc_response response; + wf_jsonrpc_impl_response_init(&response, message); if ((proxy->request.is_pending) && (response.id == proxy->request.id)) { - jsonrpc_proxy_finished_fn * finished = proxy->request.finished; + wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished; void * user_data = proxy->request.user_data; proxy->request.is_pending = false; @@ -215,6 +215,6 @@ void jsonrpc_impl_proxy_onresult( finished(user_data, response.result, response.error); } - jsonrpc_impl_response_cleanup(&response); + wf_jsonrpc_impl_response_cleanup(&response); } diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.h new file mode 100644 index 0000000..60d2f7a --- /dev/null +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.h @@ -0,0 +1,84 @@ +#ifndef WF_JSONRPC_IMPL_PROXY_H +#define WF_JSONRPC_IMPL_PROXY_H + +#include "wf/jsonrpc/proxy_finished_fn.h" +#include "wf/jsonrpc/send_fn.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_timer_manager; +struct wf_timer; + +struct wf_jsonrpc_request +{ + bool is_pending; + wf_jsonrpc_proxy_finished_fn * finished; + void * user_data; + int id; + struct wf_timer * timer; +}; + +struct wf_jsonrpc_proxy +{ + struct wf_jsonrpc_request request; + int timeout; + wf_jsonrpc_send_fn * send; + void * user_data; +}; + +extern void +wf_jsonrpc_impl_proxy_init( + struct wf_jsonrpc_proxy * proxy, + struct wf_timer_manager * manager, + int timeout, + wf_jsonrpc_send_fn * send, + void * user_data); + +extern void +wf_jsonrpc_impl_proxy_cleanup( + struct wf_jsonrpc_proxy * proxy); + +extern struct wf_jsonrpc_proxy * +wf_jsonrpc_impl_proxy_create( + struct wf_timer_manager * manager, + int timeout, + wf_jsonrpc_send_fn * send, + void * user_data); + +extern void +wf_jsonrpc_impl_proxy_dispose( + struct wf_jsonrpc_proxy * proxy); + + +extern void +wf_jsonrpc_impl_proxy_invoke( + struct wf_jsonrpc_proxy * proxy, + wf_jsonrpc_proxy_finished_fn * finished, + void * user_data, + char const * method_name, + char const * param_info, + va_list args +); + +extern void +wf_jsonrpc_impl_proxy_notify( + struct wf_jsonrpc_proxy * proxy, + char const * method_name, + char const * param_info, + va_list args +); + +extern void +wf_jsonrpc_impl_proxy_onresult( + struct wf_jsonrpc_proxy * proxy, + json_t * message); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/jsonrpc/src/jsonrpc/impl/request.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c similarity index 58% rename from lib/jsonrpc/src/jsonrpc/impl/request.c rename to lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c index 91ffd96..e46f19e 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/request.c +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c @@ -1,16 +1,16 @@ -#include "jsonrpc/impl/request.h" -#include "jsonrpc/impl/error.h" +#include "wf/jsonrpc/impl/request.h" +#include "wf/jsonrpc/impl/error.h" #include -struct jsonrpc_request +struct wf_jsonrpc_request { int id; - jsonrpc_send_fn * send; + wf_jsonrpc_send_fn * send; void * user_data; }; bool -jsonrpc_impl_is_request( +wf_jsonrpc_impl_is_request( json_t * message) { json_t * id = json_object_get(message, "id"); @@ -22,13 +22,13 @@ jsonrpc_impl_is_request( } -struct jsonrpc_request * -jsonrpc_impl_request_create( +struct wf_jsonrpc_request * +wf_jsonrpc_impl_request_create( int id, - jsonrpc_send_fn * send, + wf_jsonrpc_send_fn * send, void * user_data) { - struct jsonrpc_request * request = malloc(sizeof(struct jsonrpc_request)); + struct wf_jsonrpc_request * request = malloc(sizeof(struct wf_jsonrpc_request)); if (NULL != request) { request->id = id; @@ -40,23 +40,23 @@ jsonrpc_impl_request_create( } void -jsonrpc_impl_request_dispose( - struct jsonrpc_request * request) +wf_jsonrpc_impl_request_dispose( + struct wf_jsonrpc_request * request) { free(request); } void * -jsonrpc_impl_request_get_userdata( - struct jsonrpc_request * request) +wf_jsonrpc_impl_request_get_userdata( + struct wf_jsonrpc_request * request) { return request->user_data; } void -jsonrpc_impl_respond( - struct jsonrpc_request * request, +wf_jsonrpc_impl_respond( + struct wf_jsonrpc_request * request, json_t * result) { json_t * response = json_object(); @@ -65,20 +65,20 @@ jsonrpc_impl_respond( request->send(response, request->user_data); json_decref(response); - jsonrpc_impl_request_dispose(request); + wf_jsonrpc_impl_request_dispose(request); } -void jsonrpc_impl_respond_error( - struct jsonrpc_request * request, +void wf_jsonrpc_impl_respond_error( + struct wf_jsonrpc_request * request, int code, char const * message) { json_t * response = json_object(); - json_object_set_new(response, "error", jsonrpc_impl_error(code, message)); + json_object_set_new(response, "error", wf_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_impl_request_dispose(request); + wf_jsonrpc_impl_request_dispose(request); } diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.h new file mode 100644 index 0000000..0d510e9 --- /dev/null +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.h @@ -0,0 +1,53 @@ +#ifndef WF_JSONRPC_IMPL_REQUEST_H +#define WF_JSONRPC_IMPL_REQUEST_H + +#ifndef __cplusplus +#include +#include +#include +#else +#include +#include +using std::size_t; +#endif + +#include +#include +#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 diff --git a/lib/jsonrpc/src/jsonrpc/impl/response.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.c similarity index 69% rename from lib/jsonrpc/src/jsonrpc/impl/response.c rename to lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.c index 9f3d04e..07171fb 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/response.c +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.c @@ -1,9 +1,9 @@ -#include "jsonrpc/impl/response.h" -#include "jsonrpc/impl/error.h" -#include "jsonrpc/status.h" +#include "wf/jsonrpc/impl/response.h" +#include "wf/jsonrpc/impl/error.h" +#include "wf/jsonrpc/status.h" bool -jsonrpc_impl_is_response( +wf_jsonrpc_impl_is_response( json_t * message) { json_t * id = json_object_get(message, "id"); @@ -16,8 +16,8 @@ jsonrpc_impl_is_response( void -jsonrpc_impl_response_init( - struct jsonrpc_response * result, +wf_jsonrpc_impl_response_init( + struct wf_jsonrpc_response * result, json_t * response) { result->id = -1; @@ -27,7 +27,7 @@ jsonrpc_impl_response_init( json_t * id_holder = json_object_get(response, "id"); if ((NULL == id_holder) || (!json_is_integer(id_holder))) { - result->error = jsonrpc_impl_error(JSONRPC_BAD_FORMAT, "invalid format: missing id"); + result->error = wf_jsonrpc_impl_error(WF_JSONRPC_BAD_FORMAT, "invalid format: missing id"); return; } @@ -47,14 +47,14 @@ jsonrpc_impl_response_init( } else { - result->error = jsonrpc_impl_error(JSONRPC_BAD_FORMAT, "invalid format: invalid error object"); + result->error = wf_jsonrpc_impl_error(WF_JSONRPC_BAD_FORMAT, "invalid format: invalid error object"); } } } void -jsonrpc_impl_response_cleanup( - struct jsonrpc_response * response) +wf_jsonrpc_impl_response_cleanup( + struct wf_jsonrpc_response * response) { if (NULL != response->result) { diff --git a/lib/jsonrpc/src/jsonrpc/impl/response.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.h similarity index 50% rename from lib/jsonrpc/src/jsonrpc/impl/response.h rename to lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.h index b39e30e..3e1e02e 100644 --- a/lib/jsonrpc/src/jsonrpc/impl/response.h +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.h @@ -1,5 +1,5 @@ -#ifndef JSONRPC_IMPL_RESPONSE_H -#define JSONRPC_IMPL_RESPONSE_H +#ifndef WF_JSONRPC_IMPL_RESPONSE_H +#define WF_JSONRPC_IMPL_RESPONSE_H #ifndef __cplusplus #include @@ -15,22 +15,22 @@ using std::size_t; extern "C" { #endif -struct jsonrpc_response +struct wf_jsonrpc_response { json_t * result; json_t * error; int id; }; -extern bool jsonrpc_impl_is_response( +extern bool wf_jsonrpc_impl_is_response( json_t * message); -extern void jsonrpc_impl_response_init( - struct jsonrpc_response * response, +extern void wf_jsonrpc_impl_response_init( + struct wf_jsonrpc_response * response, json_t * message); -extern void jsonrpc_impl_response_cleanup( - struct jsonrpc_response * response); +extern void wf_jsonrpc_impl_response_cleanup( + struct wf_jsonrpc_response * response); #ifdef __cplusplus } diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c new file mode 100644 index 0000000..e510daa --- /dev/null +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c @@ -0,0 +1,132 @@ +#include "wf/jsonrpc/impl/server.h" +#include "wf/jsonrpc/impl/method.h" +#include "wf/jsonrpc/impl/request.h" +#include "wf/jsonrpc/impl/unused_param.h" +#include "wf/jsonrpc/status.h" + +#include +#include + +struct wf_jsonrpc_server +{ + struct wf_jsonrpc_method * methods; +}; + +static void +wf_jsonrpc_impl_server_init( + struct wf_jsonrpc_server * server); + +static void +wf_jsonrpc_impl_server_cleanup( + struct wf_jsonrpc_server * server); + +struct wf_jsonrpc_server * +wf_jsonrpc_impl_server_create(void) +{ + struct wf_jsonrpc_server * server = malloc(sizeof(struct wf_jsonrpc_server)); + if (NULL != server) + { + wf_jsonrpc_impl_server_init(server); + } + + return server; +} + +void +wf_jsonrpc_impl_server_dispose( + struct wf_jsonrpc_server * server) +{ + wf_jsonrpc_impl_server_cleanup(server); + free(server); +} + + +static void wf_jsonrpc_impl_server_init( + struct wf_jsonrpc_server * server) +{ + server->methods = NULL; +} + +static void wf_jsonrpc_impl_server_cleanup( + struct wf_jsonrpc_server * server) +{ + struct wf_jsonrpc_method * current = server->methods; + while (NULL != current) + { + struct wf_jsonrpc_method * next = current->next; + wf_jsonrpc_impl_method_dispose(current); + current = next; + } + server->methods = NULL; +} + +void wf_jsonrpc_impl_server_add( + struct wf_jsonrpc_server * server, + char const * method_name, + wf_jsonrpc_method_invoke_fn * invoke, + void * user_data) +{ + struct wf_jsonrpc_method * method = wf_jsonrpc_impl_method_create(method_name, invoke, user_data); + method->next = server->methods; + server->methods = method; +} + +static void wf_jsonrpc_impl_server_invalid_method_invoke( + struct wf_jsonrpc_request * request, + char const * WF_JSONRPC_UNUSED_PARAM(method_name), + json_t * WF_JSONRPC_UNUSED_PARAM(params), + void * WF_JSONRPC_UNUSED_PARAM(user_data)) +{ + wf_jsonrpc_impl_respond_error(request, WF_JSONRPC_BAD_NOTIMPLEMENTED, "not implemented"); +} + +static struct wf_jsonrpc_method const wf_jsonrpc_impl_server_invalid_method = +{ + .next = NULL, + .name = "", + .invoke = &wf_jsonrpc_impl_server_invalid_method_invoke, + .user_data = NULL +}; + +static struct wf_jsonrpc_method const * +wf_jsonrpc_impl_server_get_method( + struct wf_jsonrpc_server * server, + char const * method_name) +{ + struct wf_jsonrpc_method const * current = server->methods; + while (NULL != current) + { + if (0 == strcmp(method_name, current->name)) + { + return current; + } + + current = current->next; + } + + return &wf_jsonrpc_impl_server_invalid_method; +} + +void wf_jsonrpc_impl_server_process( + struct wf_jsonrpc_server * server, + json_t * request_data, + wf_jsonrpc_send_fn * send, + void * user_data) +{ + json_t * method_holder = json_object_get(request_data, "method"); + json_t * params = json_object_get(request_data, "params"); + json_t * id_holder = json_object_get(request_data, "id"); + + if (json_is_string(method_holder) && + (json_is_array(params) || (json_is_object(params))) && + json_is_integer(id_holder)) + { + char const * method_name = json_string_value(method_holder); + int id = json_integer_value(id_holder); + struct wf_jsonrpc_request * request = wf_jsonrpc_impl_request_create(id, send, user_data); + struct wf_jsonrpc_method const * method = wf_jsonrpc_impl_server_get_method(server, method_name); + + method->invoke(request, method_name, params, method->user_data); + } +} + diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.h new file mode 100644 index 0000000..82c7421 --- /dev/null +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.h @@ -0,0 +1,41 @@ +#ifndef WF_JSONRPC_IMPL_SERVER_H +#define WF_JSONRPC_IMPL_SERVER_H + +#include +#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 diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/unused_param.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/unused_param.h new file mode 100644 index 0000000..41323c8 --- /dev/null +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/unused_param.h @@ -0,0 +1,10 @@ +#ifndef WF_JSONRPC_UTIL_H +#define WF_JSONRPC_UTIL_H + +#ifdef __GNUC__ +#define WF_JSONRPC_UNUSED_PARAM(param) param __attribute__((unused)) +#else +#define WF_JSONRPC_UNUSED_PARAM(param) +#endif + +#endif diff --git a/lib/jsonrpc/test/jsonrpc/test_is_request.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_is_request.cc similarity index 65% rename from lib/jsonrpc/test/jsonrpc/test_is_request.cc rename to lib/wf/jsonrpc/test/wf/jsonrpc/test_is_request.cc index 03d95f7..55e40c6 100644 --- a/lib/jsonrpc/test/jsonrpc/test_is_request.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_is_request.cc @@ -1,112 +1,112 @@ #include -#include "jsonrpc/request.h" +#include "wf/jsonrpc/request.h" -TEST(jsonrpc_is_request, request_with_object_params) +TEST(wf_jsonrpc_is_request, request_with_object_params) { json_t * request = json_object(); json_object_set_new(request, "method", json_string("method")); json_object_set_new(request, "params", json_object()); json_object_set_new(request, "id", json_integer(42)); - ASSERT_TRUE(jsonrpc_is_request(request)); + ASSERT_TRUE(wf_jsonrpc_is_request(request)); json_decref(request); } -TEST(jsonrpc_is_request, request_with_array_params) +TEST(wf_jsonrpc_is_request, request_with_array_params) { json_t * request = json_object(); json_object_set_new(request, "method", json_string("method")); json_object_set_new(request, "params", json_array()); json_object_set_new(request, "id", json_integer(42)); - ASSERT_TRUE(jsonrpc_is_request(request)); + ASSERT_TRUE(wf_jsonrpc_is_request(request)); json_decref(request); } -TEST(jsonrpc_is_request, null_request) +TEST(wf_jsonrpc_is_request, null_request) { - ASSERT_FALSE(jsonrpc_is_request(nullptr)); + ASSERT_FALSE(wf_jsonrpc_is_request(nullptr)); } -TEST(jsonrpc_is_request, invalid_request) +TEST(wf_jsonrpc_is_request, invalid_request) { json_t * request = json_array(); json_array_append_new(request, json_string("method")); json_array_append_new(request, json_object()); json_array_append_new(request, json_integer(42)); - ASSERT_FALSE(jsonrpc_is_request(request)); + ASSERT_FALSE(wf_jsonrpc_is_request(request)); json_decref(request); } -TEST(jsonrpc_is_request, invalid_request_without_id) +TEST(wf_jsonrpc_is_request, invalid_request_without_id) { json_t * request = json_object(); json_object_set_new(request, "method", json_string("method")); json_object_set_new(request, "params", json_object()); - ASSERT_FALSE(jsonrpc_is_request(request)); + ASSERT_FALSE(wf_jsonrpc_is_request(request)); json_decref(request); } -TEST(jsonrpc_is_request, invalid_request_due_to_invalid_id) +TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_id) { json_t * request = json_object(); json_object_set_new(request, "method", json_string("method")); json_object_set_new(request, "params", json_object()); json_object_set_new(request, "id", json_string("42")); - ASSERT_FALSE(jsonrpc_is_request(request)); + ASSERT_FALSE(wf_jsonrpc_is_request(request)); json_decref(request); } -TEST(jsonrpc_is_request, invalid_request_without_method) +TEST(wf_jsonrpc_is_request, invalid_request_without_method) { json_t * request = json_object(); json_object_set_new(request, "params", json_object()); json_object_set_new(request, "id", json_integer(42)); - ASSERT_FALSE(jsonrpc_is_request(request)); + ASSERT_FALSE(wf_jsonrpc_is_request(request)); json_decref(request); } -TEST(jsonrpc_is_request, invalid_request_due_to_invalid_method) +TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_method) { json_t * request = json_object(); json_object_set_new(request, "method", json_integer(42)); json_object_set_new(request, "params", json_object()); json_object_set_new(request, "id", json_integer(42)); - ASSERT_FALSE(jsonrpc_is_request(request)); + ASSERT_FALSE(wf_jsonrpc_is_request(request)); json_decref(request); } -TEST(jsonrpc_is_request, invalid_request_without_params) +TEST(wf_jsonrpc_is_request, invalid_request_without_params) { json_t * request = json_object(); json_object_set_new(request, "method", json_string("method")); json_object_set_new(request, "id", json_integer(42)); - ASSERT_FALSE(jsonrpc_is_request(request)); + ASSERT_FALSE(wf_jsonrpc_is_request(request)); json_decref(request); } -TEST(jsonrpc_is_request, invalid_request_due_to_invalid_params) +TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_params) { json_t * request = json_object(); json_object_set_new(request, "methdo", json_string("method")); json_object_set_new(request, "params", json_string("params")); json_object_set_new(request, "id", json_integer(42)); - ASSERT_FALSE(jsonrpc_is_request(request)); + ASSERT_FALSE(wf_jsonrpc_is_request(request)); json_decref(request); } diff --git a/lib/jsonrpc/test/jsonrpc/test_is_response.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_is_response.cc similarity index 60% rename from lib/jsonrpc/test/jsonrpc/test_is_response.cc rename to lib/wf/jsonrpc/test/wf/jsonrpc/test_is_response.cc index b32a284..690bb6c 100644 --- a/lib/jsonrpc/test/jsonrpc/test_is_response.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_is_response.cc @@ -1,94 +1,94 @@ #include -#include "jsonrpc/response.h" +#include "wf/jsonrpc/response.h" -TEST(jsonrpc_is_response, valid_result) +TEST(wf_jsonrpc_is_response, valid_result) { json_t * message = json_object(); json_object_set_new(message, "result", json_object()); json_object_set_new(message, "id", json_integer(42)); - ASSERT_TRUE(jsonrpc_is_response(message)); + ASSERT_TRUE(wf_jsonrpc_is_response(message)); json_decref(message); } -TEST(jsonrpc_is_response, valid_result_string) +TEST(wf_jsonrpc_is_response, valid_result_string) { json_t * message = json_object(); json_object_set_new(message, "result", json_string("also valid")); json_object_set_new(message, "id", json_integer(42)); - ASSERT_TRUE(jsonrpc_is_response(message)); + ASSERT_TRUE(wf_jsonrpc_is_response(message)); json_decref(message); } -TEST(jsonrpc_is_response, valid_error) +TEST(wf_jsonrpc_is_response, valid_error) { json_t * message = json_object(); json_object_set_new(message, "error", json_object()); json_object_set_new(message, "id", json_integer(42)); - ASSERT_TRUE(jsonrpc_is_response(message)); + ASSERT_TRUE(wf_jsonrpc_is_response(message)); json_decref(message); } -TEST(jsonrpc_is_response, invalid_null) +TEST(wf_jsonrpc_is_response, invalid_null) { - ASSERT_FALSE(jsonrpc_is_response(nullptr)); + ASSERT_FALSE(wf_jsonrpc_is_response(nullptr)); } -TEST(jsonrpc_is_response, invalid_message) +TEST(wf_jsonrpc_is_response, invalid_message) { json_t * message = json_array(); json_array_append_new(message, json_object()); json_array_append_new(message, json_integer(42)); - ASSERT_FALSE(jsonrpc_is_response(message)); + ASSERT_FALSE(wf_jsonrpc_is_response(message)); json_decref(message); } -TEST(jsonrpc_is_response, invalid_missing_id) +TEST(wf_jsonrpc_is_response, invalid_missing_id) { json_t * message = json_object(); json_object_set_new(message, "result", json_object()); - ASSERT_FALSE(jsonrpc_is_response(message)); + ASSERT_FALSE(wf_jsonrpc_is_response(message)); json_decref(message); } -TEST(jsonrpc_is_response, invalid_id_wrong_type) +TEST(wf_jsonrpc_is_response, invalid_id_wrong_type) { json_t * message = json_object(); json_object_set_new(message, "result", json_object()); json_object_set_new(message, "id", json_string("42")); - ASSERT_FALSE(jsonrpc_is_response(message)); + ASSERT_FALSE(wf_jsonrpc_is_response(message)); json_decref(message); } -TEST(jsonrpc_is_response, invalid_missing_result_and_error) +TEST(wf_jsonrpc_is_response, invalid_missing_result_and_error) { json_t * message = json_object(); json_object_set_new(message, "id", json_integer(42)); - ASSERT_FALSE(jsonrpc_is_response(message)); + ASSERT_FALSE(wf_jsonrpc_is_response(message)); json_decref(message); } -TEST(jsonrpc_is_response, invalid_error_wrong_type) +TEST(wf_jsonrpc_is_response, invalid_error_wrong_type) { json_t * message = json_object(); json_object_set_new(message, "error", json_array()); json_object_set_new(message, "id", json_integer(42)); - ASSERT_FALSE(jsonrpc_is_response(message)); + ASSERT_FALSE(wf_jsonrpc_is_response(message)); json_decref(message); } diff --git a/lib/jsonrpc/test/jsonrpc/test_proxy.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc similarity index 73% rename from lib/jsonrpc/test/jsonrpc/test_proxy.cc rename to lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc index cc71ae8..416c25f 100644 --- a/lib/jsonrpc/test/jsonrpc/test_proxy.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc @@ -1,5 +1,5 @@ #include -#include "jsonrpc/proxy.h" +#include "wf/jsonrpc/proxy.h" #include "wf/timer/manager.h" #include "webfuse/utils/msleep.hpp" #include "webfuse/core/json_util.h" @@ -85,31 +85,31 @@ namespace } } -TEST(jsonrpc_proxy, init) +TEST(wf_jsonrpc_proxy, init) { struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext context; void * user_data = reinterpret_cast(&context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data); + struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data); - jsonrpc_proxy_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); ASSERT_FALSE(context.is_called); } -TEST(jsonrpc_proxy, invoke) +TEST(wf_jsonrpc_proxy, invoke) { struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + 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(&finished_context); - jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -131,24 +131,24 @@ TEST(jsonrpc_proxy, invoke) ASSERT_FALSE(finished_context.is_called); - jsonrpc_proxy_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); ASSERT_TRUE(finished_context.is_called); ASSERT_FALSE(nullptr == finished_context.error); } -TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails) +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(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + 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(&finished_context); - jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -156,25 +156,25 @@ TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails) ASSERT_TRUE(finished_context.is_called); ASSERT_FALSE(nullptr == finished_context.error); - jsonrpc_proxy_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); } -TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending) +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(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + 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(&finished_context); - jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + wf_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", ""); + wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", ""); ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(json_is_object(send_context.response)); @@ -184,42 +184,42 @@ 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_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); } -TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid) +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(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + 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(&finished_context); - jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "?", "error"); + 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_BAD, wf_impl_jsonrpc_get_status(finished_context.error)); - jsonrpc_proxy_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); } -TEST(jsonrpc_proxy, on_result) +TEST(wf_jsonrpc_proxy, on_result) { struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + 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(&finished_context); - jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -231,7 +231,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); + wf_jsonrpc_proxy_onresult(proxy, response); json_decref(response); ASSERT_TRUE(finished_context.is_called); @@ -239,21 +239,21 @@ TEST(jsonrpc_proxy, on_result) ASSERT_TRUE(json_is_string(finished_context.result)); ASSERT_STREQ("okay", json_string_value(finished_context.result)); - jsonrpc_proxy_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); } -TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id) +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(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + 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(&finished_context); - jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); + 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)); @@ -265,26 +265,26 @@ 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); + wf_jsonrpc_proxy_onresult(proxy, response); json_decref(response); ASSERT_FALSE(finished_context.is_called); - jsonrpc_proxy_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); } -TEST(jsonrpc_proxy, timeout) +TEST(wf_jsonrpc_proxy, timeout) { struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, 0, &jsonrpc_send, send_data); + struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_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); + 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)); @@ -295,28 +295,28 @@ 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_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); } -TEST(jsonrpc_proxy, cleanup_pending_request) +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(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, 10, &jsonrpc_send, send_data); + struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_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); + 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); - jsonrpc_proxy_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); ASSERT_TRUE(finished_context.is_called); @@ -325,15 +325,15 @@ TEST(jsonrpc_proxy, cleanup_pending_request) -TEST(jsonrpc_proxy, notify) +TEST(wf_jsonrpc_proxy, notify) { struct wf_timer_manager * timer_manager = wf_timer_manager_create(); SendContext send_context; void * send_data = reinterpret_cast(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); - jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42); + wf_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42); ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(json_is_object(send_context.response)); @@ -353,22 +353,22 @@ TEST(jsonrpc_proxy, notify) json_t * id = json_object_get(send_context.response, "id"); ASSERT_EQ(nullptr, id); - jsonrpc_proxy_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); } -TEST(jsonrpc_proxy, notify_dont_send_invalid_request) +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(&send_context); - struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); - jsonrpc_proxy_notify(proxy, "foo", "?"); + wf_jsonrpc_proxy_notify(proxy, "foo", "?"); ASSERT_FALSE(send_context.is_called); - jsonrpc_proxy_dispose(proxy); + wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); } diff --git a/lib/jsonrpc/test/jsonrpc/test_request.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_request.cc similarity index 70% rename from lib/jsonrpc/test/jsonrpc/test_request.cc rename to lib/wf/jsonrpc/test/wf/jsonrpc/test_request.cc index a5f956e..eb15e91 100644 --- a/lib/jsonrpc/test/jsonrpc/test_request.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_request.cc @@ -1,6 +1,6 @@ #include -#include "jsonrpc/request.h" -#include "jsonrpc/status.h" +#include "wf/jsonrpc/request.h" +#include "wf/jsonrpc/status.h" namespace { @@ -23,29 +23,29 @@ bool jsonrpc_send( } -TEST(jsonrpc_request, create_dispose) +TEST(wf_jsonrpc_request, create_dispose) { Context context{nullptr}; void * user_data = reinterpret_cast(&context); - struct jsonrpc_request * request = - jsonrpc_request_create(42, &jsonrpc_send, user_data); + struct wf_jsonrpc_request * request = + wf_jsonrpc_request_create(42, &jsonrpc_send, user_data); ASSERT_NE(nullptr, request); - ASSERT_EQ(user_data, jsonrpc_request_get_userdata(request)); + ASSERT_EQ(user_data, wf_jsonrpc_request_get_userdata(request)); - jsonrpc_request_dispose(request); + wf_jsonrpc_request_dispose(request); } -TEST(jsonrpc_request, respond) +TEST(wf_jsonrpc_request, respond) { Context context{nullptr}; void * user_data = reinterpret_cast(&context); - struct jsonrpc_request * request = - jsonrpc_request_create(42, &jsonrpc_send, user_data); + struct wf_jsonrpc_request * request = + wf_jsonrpc_request_create(42, &jsonrpc_send, user_data); - jsonrpc_respond(request, json_string("okay")); + wf_jsonrpc_respond(request, json_string("okay")); ASSERT_NE(nullptr, context.response); @@ -66,15 +66,15 @@ TEST(jsonrpc_request, respond) json_decref(response); } -TEST(jsonrpc_request, respond_error) +TEST(wf_jsonrpc_request, respond_error) { Context context{nullptr}; void * user_data = reinterpret_cast(&context); - struct jsonrpc_request * request = - jsonrpc_request_create(42, &jsonrpc_send, user_data); + struct wf_jsonrpc_request * request = + wf_jsonrpc_request_create(42, &jsonrpc_send, user_data); - jsonrpc_respond_error(request, JSONRPC_BAD, "Bad"); + wf_jsonrpc_respond_error(request, WF_JSONRPC_BAD, "Bad"); ASSERT_NE(nullptr, context.response); @@ -93,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(JSONRPC_BAD, json_integer_value(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)); diff --git a/lib/jsonrpc/test/jsonrpc/test_response.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc similarity index 69% rename from lib/jsonrpc/test/jsonrpc/test_response.cc rename to lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc index bdf6e61..911319d 100644 --- a/lib/jsonrpc/test/jsonrpc/test_response.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc @@ -1,27 +1,27 @@ #include -#include "jsonrpc/impl/response.h" +#include "wf/jsonrpc/impl/response.h" #include "webfuse/core/status.h" #include "webfuse/core/json_util.h" -TEST(json_response, init_result) +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 jsonrpc_response response; - jsonrpc_impl_response_init(&response, message); + 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); - jsonrpc_impl_response_cleanup(&response); + wf_jsonrpc_impl_response_cleanup(&response); json_decref(message); } -TEST(json_response, init_error) +TEST(wf_json_response, init_error) { json_t * message = json_object(); json_t * err = json_object(); @@ -30,29 +30,29 @@ TEST(json_response, init_error) json_object_set_new(message, "error", err); json_object_set_new(message, "id", json_integer(23)); - struct jsonrpc_response response; - jsonrpc_impl_response_init(&response, message); + struct wf_jsonrpc_response response; + wf_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_impl_response_cleanup(&response); + wf_jsonrpc_impl_response_cleanup(&response); json_decref(message); } -TEST(json_response, init_format_error) +TEST(wf_json_response, init_format_error) { json_t * message = json_object(); json_object_set_new(message, "id", json_integer(12)); - struct jsonrpc_response response; - jsonrpc_impl_response_init(&response, message); + struct wf_jsonrpc_response response; + wf_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_impl_response_cleanup(&response); + wf_jsonrpc_impl_response_cleanup(&response); json_decref(message); } diff --git a/lib/jsonrpc/test/jsonrpc/test_response_parser.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc similarity index 75% rename from lib/jsonrpc/test/jsonrpc/test_response_parser.cc rename to lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc index f65fa6f..81a73bd 100644 --- a/lib/jsonrpc/test/jsonrpc/test_response_parser.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc @@ -1,46 +1,46 @@ #include #include -#include "jsonrpc/impl/response.h" +#include "wf/jsonrpc/impl/response.h" #include "webfuse/core/json_util.h" static void response_parse_str( std::string const & buffer, - struct jsonrpc_response * response) + struct wf_jsonrpc_response * response) { json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr); if (nullptr != message) { - jsonrpc_impl_response_init(response, message); + wf_jsonrpc_impl_response_init(response, message); json_decref(message); } } TEST(response_parser, test) { - struct jsonrpc_response response; + 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); - jsonrpc_impl_response_cleanup(&response); + 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); - jsonrpc_impl_response_cleanup(&response); + 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); - jsonrpc_impl_response_cleanup(&response); + wf_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_impl_response_cleanup(&response); + wf_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_impl_response_cleanup(&response); + wf_jsonrpc_impl_response_cleanup(&response); } diff --git a/lib/jsonrpc/test/jsonrpc/test_server.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_server.cc similarity index 72% rename from lib/jsonrpc/test/jsonrpc/test_server.cc rename to lib/wf/jsonrpc/test/wf/jsonrpc/test_server.cc index e8e879a..14f7ac5 100644 --- a/lib/jsonrpc/test/jsonrpc/test_server.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_server.cc @@ -1,7 +1,7 @@ #include -#include "jsonrpc/server.h" -#include "jsonrpc/request.h" -#include "jsonrpc/status.h" +#include "wf/jsonrpc/server.h" +#include "wf/jsonrpc/request.h" +#include "wf/jsonrpc/status.h" namespace { @@ -24,7 +24,7 @@ namespace } void sayHello( - struct jsonrpc_request * request, + struct wf_jsonrpc_request * request, char const * method_name, json_t * params, void * user_data) @@ -34,15 +34,15 @@ namespace (void) user_data; json_t * result = json_string("Hello"); - jsonrpc_respond(request, result); + wf_jsonrpc_respond(request, result); } } -TEST(jsonrpc_server, process_request) +TEST(wf_jsonrpc_server, process_request) { - struct jsonrpc_server * server = jsonrpc_server_create(); - jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); + 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(&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); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); ASSERT_TRUE(context.is_called); ASSERT_NE(nullptr, context.response); @@ -66,13 +66,13 @@ TEST(jsonrpc_server, process_request) json_decref(context.response); json_decref(request); - jsonrpc_server_dispose(server); + wf_jsonrpc_server_dispose(server); } -TEST(jsonrpc_server, invoke_unknown_method) +TEST(wf_jsonrpc_server, invoke_unknown_method) { - struct jsonrpc_server * server = jsonrpc_server_create(); - jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); + 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(&context); @@ -80,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); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); ASSERT_TRUE(context.is_called); ASSERT_NE(nullptr, context.response); @@ -95,29 +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(JSONRPC_BAD_NOTIMPLEMENTED, json_integer_value(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); - jsonrpc_server_dispose(server); + wf_jsonrpc_server_dispose(server); } -TEST(jsonrpc_server, skip_invalid_request) +TEST(wf_jsonrpc_server, skip_invalid_request) { - struct jsonrpc_server * server = jsonrpc_server_create(); + struct wf_jsonrpc_server * server = wf_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); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); ASSERT_FALSE(context.is_called); json_decref(request); - jsonrpc_server_dispose(server); + wf_jsonrpc_server_dispose(server); } diff --git a/lib/jsonrpc/test/jsonrpc/test_util.cc b/test/webfuse/tests/core/test_util.cc similarity index 100% rename from lib/jsonrpc/test/jsonrpc/test_util.cc rename to test/webfuse/tests/core/test_util.cc From 107e66b65a992d5da0e0d5e64abf0c3ccfe1d41d Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 1 Mar 2020 17:23:59 +0100 Subject: [PATCH 14/15] chore: replaced msleep by std::this_thread::sleep_for --- cmake/unit_tests.cmake | 1 - lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc | 9 ++++++--- lib/wf/timer/test/wf/timer/test_timepoint.cc | 8 +++++--- lib/wf/timer/test/wf/timer/test_timer.cc | 13 +++++++------ test/webfuse/tests/integration/provider.cc | 6 ++++-- test/webfuse/tests/integration/server.cc | 1 - test/webfuse/utils/msleep.cc | 19 ------------------- test/webfuse/utils/msleep.hpp | 12 ------------ 8 files changed, 22 insertions(+), 47 deletions(-) delete mode 100644 test/webfuse/utils/msleep.cc delete mode 100644 test/webfuse/utils/msleep.hpp diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index b7887fa..13b91f9 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -18,7 +18,6 @@ add_executable(alltests lib/wf/timer/test/wf/timer/test_timer.cc test/webfuse/utils/tempdir.cc test/webfuse/utils/file_utils.cc - test/webfuse/utils/msleep.cc test/webfuse/utils/die_if.cc test/webfuse/utils/timeout_watcher.cc test/webfuse/utils/path.c diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc index 416c25f..af38a5c 100644 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc @@ -1,10 +1,13 @@ #include #include "wf/jsonrpc/proxy.h" #include "wf/timer/manager.h" -#include "webfuse/utils/msleep.hpp" #include "webfuse/core/json_util.h" -using webfuse_test::msleep; +#include +#include + +using namespace std::chrono_literals; + #define WF_DEFAULT_TIMEOUT (10 * 1000) @@ -289,7 +292,7 @@ TEST(wf_jsonrpc_proxy, timeout) ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(json_is_object(send_context.response)); - msleep(10); + std::this_thread::sleep_for(10ms); wf_timer_manager_check(timer_manager); ASSERT_TRUE(finished_context.is_called); diff --git a/lib/wf/timer/test/wf/timer/test_timepoint.cc b/lib/wf/timer/test/wf/timer/test_timepoint.cc index bd7774a..723ff6a 100644 --- a/lib/wf/timer/test/wf/timer/test_timepoint.cc +++ b/lib/wf/timer/test/wf/timer/test_timepoint.cc @@ -1,14 +1,16 @@ #include -#include "webfuse/utils/msleep.hpp" #include "wf/timer/impl/timepoint.h" -using webfuse_test::msleep; +#include +#include + +using namespace std::chrono_literals; TEST(wf_timer_timepoint, now) { wf_timer_timepoint start = wf_timer_impl_timepoint_now(); - msleep(42); + std::this_thread::sleep_for(42ms); wf_timer_timepoint end = wf_timer_impl_timepoint_now(); ASSERT_LT(start, end); diff --git a/lib/wf/timer/test/wf/timer/test_timer.cc b/lib/wf/timer/test/wf/timer/test_timer.cc index ffeb1d3..e97c011 100644 --- a/lib/wf/timer/test/wf/timer/test_timer.cc +++ b/lib/wf/timer/test/wf/timer/test_timer.cc @@ -1,13 +1,14 @@ #include #include +#include +#include -#include "webfuse/utils/msleep.hpp" #include "wf/timer/timer.h" #include "wf/timer/manager.h" using std::size_t; -using webfuse_test::msleep; +using namespace std::chrono_literals; extern "C" { @@ -37,7 +38,7 @@ TEST(wf_timer, trigger) struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast(&triggered)); wf_timer_start(timer, 250); - msleep(500); + std::this_thread::sleep_for(500ms); wf_timer_manager_check(manager); ASSERT_TRUE(triggered); @@ -67,7 +68,7 @@ TEST(wf_timer, cancel) struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast(&triggered)); wf_timer_start(timer, 250); - msleep(500); + std::this_thread::sleep_for(500ms); wf_timer_cancel(timer); wf_timer_manager_check(manager); @@ -90,7 +91,7 @@ TEST(wf_timer, cancel_multiple_timers) wf_timer_start(timer[i], 0); } - msleep(10); + std::this_thread::sleep_for(10ms); for(size_t i = 0; i < count; i++) { wf_timer_cancel(timer[i]); @@ -122,7 +123,7 @@ TEST(wf_timer, multiple_timers) for(size_t i = 0; i < count; i++) { - msleep(100); + std::this_thread::sleep_for(100ms); wf_timer_manager_check(manager); } diff --git a/test/webfuse/tests/integration/provider.cc b/test/webfuse/tests/integration/provider.cc index 5142ca8..45e331a 100644 --- a/test/webfuse/tests/integration/provider.cc +++ b/test/webfuse/tests/integration/provider.cc @@ -3,10 +3,12 @@ #include "webfuse/provider/impl/client.h" #include #include +#include #include -#include "webfuse/utils/msleep.hpp" #include "webfuse/utils/static_filesystem.h" +using namespace std::chrono_literals; + namespace webfuse_test { @@ -29,7 +31,7 @@ public: } thread = std::thread(Run, this); - webfuse_test::msleep(200); + std::this_thread::sleep_for(200ms); } ~Private() diff --git a/test/webfuse/tests/integration/server.cc b/test/webfuse/tests/integration/server.cc index 847efa2..e48f074 100644 --- a/test/webfuse/tests/integration/server.cc +++ b/test/webfuse/tests/integration/server.cc @@ -6,7 +6,6 @@ #include #include "webfuse_adapter.h" #include "webfuse/adapter/impl/server.h" -#include "webfuse/utils/msleep.hpp" #define WF_PATH_MAX (100) diff --git a/test/webfuse/utils/msleep.cc b/test/webfuse/utils/msleep.cc deleted file mode 100644 index 659bb81..0000000 --- a/test/webfuse/utils/msleep.cc +++ /dev/null @@ -1,19 +0,0 @@ -#include "webfuse/utils/msleep.hpp" -#include - -namespace webfuse_test -{ - -void msleep(long millis) -{ - long const secs_per_msec = 1000; - long const msecs_per_nsec = (1000 * 1000); - - long const seconds = millis / secs_per_msec; - long const nanos = (millis % secs_per_msec) * msecs_per_nsec; - - struct timespec timeout = { seconds, nanos }; - while (0 != nanosleep(&timeout, &timeout)); -} - -} \ No newline at end of file diff --git a/test/webfuse/utils/msleep.hpp b/test/webfuse/utils/msleep.hpp deleted file mode 100644 index 47cb1e6..0000000 --- a/test/webfuse/utils/msleep.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef WF_TEST_MSLEEP_HPP -#define WF_TEST_MSLEEP_HPP - -namespace webfuse_test -{ - -extern void msleep(long millis); - -} - - -#endif From ab6a6198b197b1c7992c6f5d172ec3e1a80fff73 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 1 Mar 2020 17:44:12 +0100 Subject: [PATCH 15/15] chore: removed webfuse dependency in unit tests --- lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc | 13 +++++++++---- lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc | 12 ++++++------ .../jsonrpc/test/wf/jsonrpc/test_response_parser.cc | 3 +-- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc index af38a5c..66eafa9 100644 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc @@ -1,7 +1,7 @@ #include #include "wf/jsonrpc/proxy.h" +#include "wf/jsonrpc/status.h" #include "wf/timer/manager.h" -#include "webfuse/core/json_util.h" #include #include @@ -13,6 +13,11 @@ using namespace std::chrono_literals; 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 { @@ -185,7 +190,7 @@ TEST(wf_jsonrpc_proxy, invoke_fails_if_another_request_is_pending) ASSERT_FALSE(finished_context.is_called); ASSERT_TRUE(finished_context2.is_called); - ASSERT_EQ(WF_BAD_BUSY, wf_impl_jsonrpc_get_status(finished_context2.error)); + ASSERT_EQ(WF_JSONRPC_BAD_BUSY, jsonrpc_get_status(finished_context2.error)); wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); @@ -206,7 +211,7 @@ TEST(wf_jsonrpc_proxy, invoke_fails_if_request_is_invalid) ASSERT_FALSE(send_context.is_called); ASSERT_TRUE(finished_context.is_called); - ASSERT_EQ(WF_BAD, wf_impl_jsonrpc_get_status(finished_context.error)); + ASSERT_EQ(WF_JSONRPC_BAD, jsonrpc_get_status(finished_context.error)); wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); @@ -296,7 +301,7 @@ TEST(wf_jsonrpc_proxy, timeout) wf_timer_manager_check(timer_manager); ASSERT_TRUE(finished_context.is_called); - ASSERT_EQ(WF_BAD_TIMEOUT, wf_impl_jsonrpc_get_status(finished_context.error)); + ASSERT_EQ(WF_JSONRPC_BAD_TIMEOUT, jsonrpc_get_status(finished_context.error)); wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc index 911319d..e8515aa 100644 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc @@ -1,7 +1,6 @@ #include #include "wf/jsonrpc/impl/response.h" -#include "webfuse/core/status.h" -#include "webfuse/core/json_util.h" +#include "wf/jsonrpc/status.h" TEST(wf_json_response, init_result) { @@ -25,15 +24,16 @@ TEST(wf_json_response, init_error) { json_t * message = json_object(); json_t * err = json_object(); - json_object_set_new(err, "code", json_integer(WF_BAD_ACCESS_DENIED)); - json_object_set_new(err, "message", json_string("access denied")); + 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(WF_BAD_ACCESS_DENIED, wf_impl_jsonrpc_get_status(response.error)) << json_string_value(json_object_get(response.error, "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); @@ -49,7 +49,7 @@ TEST(wf_json_response, init_format_error) struct wf_jsonrpc_response response; wf_jsonrpc_impl_response_init(&response, message); - ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_get_status(response.error)); + 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); diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc index 81a73bd..e3301fa 100644 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc +++ b/lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc @@ -2,7 +2,6 @@ #include #include "wf/jsonrpc/impl/response.h" -#include "webfuse/core/json_util.h" static void response_parse_str( @@ -52,7 +51,7 @@ TEST(response_parser, test) // valid response response_parse_str("{\"result\": true, \"id\": 42}", &response); - ASSERT_EQ(WF_GOOD, response.error); + ASSERT_EQ(nullptr, response.error); ASSERT_EQ(42, response.id); ASSERT_NE(nullptr, response.result); wf_jsonrpc_impl_response_cleanup(&response);