diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d01b1c..3f15d3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ add_library(webfuse-core STATIC lib/webfuse/core/status.c lib/webfuse/core/string.c lib/webfuse/core/path.c + lib/webfuse/core/lws_log.c ) set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core) diff --git a/lib/webfuse/adapter/impl/server.c b/lib/webfuse/adapter/impl/server.c index 7a3462a..15e18f4 100644 --- a/lib/webfuse/adapter/impl/server.c +++ b/lib/webfuse/adapter/impl/server.c @@ -10,8 +10,8 @@ #include "webfuse/adapter/impl/server_config.h" #include "webfuse/adapter/impl/server_protocol.h" +#include "webfuse/core/lws_log.h" -#define WF_DISABLE_LWS_LOG 0 #define WF_SERVER_PROTOCOL_COUNT 3 struct wf_server @@ -33,7 +33,7 @@ static bool wf_impl_server_tls_enabled( static struct lws_context * wf_impl_server_context_create( struct wf_server * server) { - lws_set_log_level(WF_DISABLE_LWS_LOG, NULL); + wf_lwslog_disable(); memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WF_SERVER_PROTOCOL_COUNT); server->ws_protocols[0].name = "http"; @@ -125,6 +125,12 @@ void wf_impl_server_dispose( free(server); } +bool wf_impl_server_is_operational( + struct wf_server * server) +{ + return server->protocol.is_operational; +} + void wf_impl_server_service( struct wf_server * server, int timeout_ms) diff --git a/lib/webfuse/adapter/impl/server.h b/lib/webfuse/adapter/impl/server.h index db26e69..1f04cc7 100644 --- a/lib/webfuse/adapter/impl/server.h +++ b/lib/webfuse/adapter/impl/server.h @@ -1,6 +1,10 @@ #ifndef WF_ADAPTER_IMPL_SERVER_H #define WF_ADAPTER_IMPL_SERVER_H +#ifndef __cplusplus +#include +#endif + #ifdef __cplusplus extern "C" { @@ -15,6 +19,9 @@ extern struct wf_server * wf_impl_server_create( extern void wf_impl_server_dispose( struct wf_server * server); +extern bool wf_impl_server_is_operational( + struct wf_server * server); + extern void wf_impl_server_service( struct wf_server * server, int timeout_ms); diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index fa5c837..65f6047 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -30,6 +30,9 @@ static int wf_impl_server_protocol_callback( switch (reason) { + case LWS_CALLBACK_PROTOCOL_INIT: + protocol->is_operational = true; + break; case LWS_CALLBACK_ESTABLISHED: session = wf_impl_session_manager_add( &protocol->session_manager, @@ -206,6 +209,7 @@ void wf_impl_server_protocol_init( char * mount_point) { protocol->mount_point = strdup(mount_point); + protocol->is_operational = false; wf_impl_timeout_manager_init(&protocol->timeout_manager); wf_impl_session_manager_init(&protocol->session_manager); @@ -220,6 +224,8 @@ void wf_impl_server_protocol_cleanup( struct wf_server_protocol * protocol) { free(protocol->mount_point); + protocol->is_operational = false; + wf_impl_jsonrpc_server_cleanup(&protocol->server); wf_impl_timeout_manager_cleanup(&protocol->timeout_manager); wf_impl_authenticators_cleanup(&protocol->authenticators); diff --git a/lib/webfuse/adapter/impl/server_protocol.h b/lib/webfuse/adapter/impl/server_protocol.h index 2ae0fe6..8e4cd8d 100644 --- a/lib/webfuse/adapter/impl/server_protocol.h +++ b/lib/webfuse/adapter/impl/server_protocol.h @@ -7,6 +7,10 @@ #include "webfuse/adapter/impl/session_manager.h" #include "webfuse/adapter/impl/jsonrpc/server.h" +#ifndef __cplusplus +#include +#endif + #ifdef __cplusplus extern "C" { @@ -21,6 +25,7 @@ struct wf_server_protocol struct wf_impl_authenticators authenticators; struct wf_impl_session_manager session_manager; struct wf_impl_jsonrpc_server server; + bool is_operational; }; extern void wf_impl_server_protocol_init( diff --git a/lib/webfuse/core/lws_log.c b/lib/webfuse/core/lws_log.c new file mode 100644 index 0000000..506e530 --- /dev/null +++ b/lib/webfuse/core/lws_log.c @@ -0,0 +1,18 @@ +#include "webfuse/core/lws_log.h" +#include +#include + +#define WF_LWSLOG_DISABLE 0 + +static bool wf_lwslog_is_diabled = false; + +void wf_lwslog_disable(void) +{ + if (!wf_lwslog_is_diabled) + { + lws_set_log_level(WF_LWSLOG_DISABLE, NULL); + wf_lwslog_is_diabled = true; + } +} + + diff --git a/lib/webfuse/core/lws_log.h b/lib/webfuse/core/lws_log.h new file mode 100644 index 0000000..6bbbdda --- /dev/null +++ b/lib/webfuse/core/lws_log.h @@ -0,0 +1,15 @@ +#ifndef WF_LWS_LOG_H +#define WF_LWS_LOG_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern void wf_lwslog_disable(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/webfuse/provider/impl/client.c b/lib/webfuse/provider/impl/client.c index 3a503e0..b88207a 100644 --- a/lib/webfuse/provider/impl/client.c +++ b/lib/webfuse/provider/impl/client.c @@ -10,9 +10,9 @@ #include "webfuse/provider/impl/client_protocol.h" #include "webfuse/provider/impl/client_config.h" #include "webfuse/provider/impl/url.h" +#include "webfuse/core/lws_log.h" #define WFP_PROTOCOL ("fs") -#define WFP_DISABLE_LWS_LOG 0 #define WFP_CLIENT_PROTOCOL_COUNT 2 struct wfp_client @@ -29,7 +29,7 @@ struct wfp_client struct wfp_client * wfp_impl_client_create( struct wfp_client_config * config) { - lws_set_log_level(WFP_DISABLE_LWS_LOG, NULL); + wf_lwslog_disable(); struct wfp_client * client = malloc(sizeof(struct wfp_client)); if (NULL != client) @@ -99,6 +99,12 @@ void wfp_impl_client_disconnect( // ToDo: implement me } +bool wfp_impl_client_is_connected( + struct wfp_client * client) +{ + return client->protocol.is_connected; +} + void wfp_impl_client_service( struct wfp_client * client, int timeout_ms) diff --git a/lib/webfuse/provider/impl/client.h b/lib/webfuse/provider/impl/client.h index 01e6d73..4ade5f2 100644 --- a/lib/webfuse/provider/impl/client.h +++ b/lib/webfuse/provider/impl/client.h @@ -1,6 +1,10 @@ #ifndef WF_PROVIDER_IMPL_CLIENT_H #define WF_PROVIDER_IMPL_CLIENT_H +#ifndef __cplusplus +#include +#endif + #ifdef __cplusplus extern "C" { @@ -27,13 +31,12 @@ extern void wfp_impl_client_connect( extern void wfp_impl_client_disconnect( struct wfp_client * client); -extern void wfp_impl_client_settimeout( - struct wfp_client * client, - unsigned int timepoint); - extern void wfp_impl_client_dispose( struct wfp_client * client); +extern bool wfp_impl_client_is_connected( + struct wfp_client * client); + extern void wfp_impl_client_service( struct wfp_client * client, int timeout_ms); diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index 788c314..2bd00d6 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -84,13 +84,16 @@ static int wfp_impl_client_protocol_callback( { case LWS_CALLBACK_CLIENT_ESTABLISHED: wfp_impl_client_protocol_add_filesystem(protocol); + protocol->is_connected = true; protocol->provider.connected(protocol->user_data); break; case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: + protocol->is_connected = false; protocol->provider.disconnected(protocol->user_data); break; case LWS_CALLBACK_CLIENT_CLOSED: - protocol->provider.connected(protocol->user_data); + protocol->is_connected = false; + protocol->provider.disconnected(protocol->user_data); break; case LWS_CALLBACK_CLIENT_RECEIVE: wfp_impl_client_protocol_process_request(protocol, in, len); @@ -126,6 +129,7 @@ void wfp_impl_client_protocol_init( struct wfp_provider const * provider, void * user_data) { + protocol->is_connected = 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 54929c3..9087d94 100644 --- a/lib/webfuse/provider/impl/client_protocol.h +++ b/lib/webfuse/provider/impl/client_protocol.h @@ -16,6 +16,7 @@ struct lws_protocols; struct wfp_client_protocol { + bool is_connected; struct wfp_request request; struct wfp_provider provider; void * user_data; diff --git a/test/integration/provider.cc b/test/integration/provider.cc index f430f57..0fe3456 100644 --- a/test/integration/provider.cc +++ b/test/integration/provider.cc @@ -1,5 +1,6 @@ #include "integration/provider.hpp" #include "webfuse_provider.h" +#include "webfuse/provider/impl/client.h" #include #include #include @@ -11,40 +12,33 @@ namespace webfuse_test class Provider::Private { public: - Private(char const * url_) - : url(url_) - , is_running(false) - , is_shutdown_requested(false) + Private(char const * url) + : is_shutdown_requested(false) { + config = wfp_client_config_create(); + + fs = wfp_static_filesystem_create(config); + wfp_static_filesystem_add_text(fs, "hello.txt", 0x444, "Hello, World"); + + client = wfp_client_create(config); + wfp_client_connect(client, url); + while (!wfp_impl_client_is_connected(client)) + { + wfp_client_service(client, 100); + } + + thread = std::thread(Run, this); } ~Private() { - Stop(); - } + RequestShutdown(); + thread.join(); - void Start() - { - std::lock_guard lock(run_lock); + wfp_client_dispose(client); - if (!is_running) - { - thread = std::thread(Run, this); - is_running = true; - msleep(200); - } - } - - void Stop() - { - std::lock_guard lock(run_lock); - - if (is_running) - { - RequestShutdown(); - thread.join(); - is_running = false; - } + wfp_static_filesystem_dispose(fs); + wfp_client_config_dispose(config); } bool IsShutdownRequested() @@ -52,11 +46,6 @@ public: std::lock_guard lock(shutdown_lock); return is_shutdown_requested; } - - char const * GetUrl() const - { - return url.c_str(); - } private: void RequestShutdown() { @@ -66,36 +55,20 @@ private: static void Run(Provider::Private * context) { - wfp_client_config * config = wfp_client_config_create(); - wfp_static_filesystem * fs = wfp_static_filesystem_create(config); - wfp_static_filesystem_add_text(fs, "hello.txt", 0x444, "Hello, World"); - - wfp_client * client = wfp_client_create(config); - if (nullptr != client) + while (!context->IsShutdownRequested()) { - wfp_client_connect(client, context->GetUrl()); - - while (!context->IsShutdownRequested()) - { - wfp_client_service(client, 100); - } - - wfp_client_dispose(client); + wfp_client_service(context->client, 100); } - - wfp_static_filesystem_dispose(fs); - wfp_client_config_dispose(config); } - std::string url; - - bool is_running; - std::mutex run_lock; - - bool is_shutdown_requested; std::mutex shutdown_lock; - std::thread thread; + bool is_shutdown_requested; + + wfp_client_config * config; + wfp_static_filesystem * fs; +public: + wfp_client * client; }; Provider::Provider(char const * url) @@ -108,14 +81,4 @@ Provider::~Provider() delete d; } -void Provider::Start(void) -{ - d->Start(); -} - -void Provider::Stop(void) -{ - d->Stop(); -} - } \ No newline at end of file diff --git a/test/integration/provider.hpp b/test/integration/provider.hpp index 7c967b0..08a7b44 100644 --- a/test/integration/provider.hpp +++ b/test/integration/provider.hpp @@ -9,8 +9,6 @@ class Provider public: Provider(char const * url); ~Provider(); - void Start(void); - void Stop(void); private: class Private; Private * d; diff --git a/test/integration/server.cc b/test/integration/server.cc index ccd7f18..0659efe 100644 --- a/test/integration/server.cc +++ b/test/integration/server.cc @@ -5,6 +5,7 @@ #include #include #include "webfuse_adapter.h" +#include "webfuse/adapter/impl/server.h" #include "msleep.hpp" #define WF_PATH_MAX (100) @@ -16,41 +17,33 @@ class Server::Private { public: Private() - : is_running(false) - , is_shutdown_requested(false) + : is_shutdown_requested(false) { snprintf(base_dir, WF_PATH_MAX, "%s", "/tmp/webfuse_test_integration_XXXXXX"); mkdtemp(base_dir); + + config = wf_server_config_create(); + wf_server_config_set_port(config, 8080); + wf_server_config_set_mountpoint(config, base_dir); + + server = wf_server_create(config); + + while (!wf_impl_server_is_operational(server)) + { + wf_server_service(server, 100); + } + + thread = std::thread(Run, this); + } ~Private() { - Stop(); + RequestShutdown(); + thread.join(); rmdir(base_dir); - } - - void Start() - { - std::lock_guard lock(run_lock); - - if (!is_running) - { - thread = std::thread(Run, this); - is_running = true; - msleep(200); - } - } - - void Stop() - { - std::lock_guard lock(run_lock); - - if (is_running) - { - RequestShutdown(); - thread.join(); - is_running = false; - } + wf_server_dispose(server); + wf_server_config_dispose(config); } bool IsShutdownRequested() @@ -59,11 +52,6 @@ public: return is_shutdown_requested; } - char const * GetBaseDir() - { - return base_dir; - } - private: void RequestShutdown() { @@ -73,33 +61,22 @@ private: static void Run(Server::Private * context) { - wf_server_config * config = wf_server_config_create(); - wf_server_config_set_port(config, 8080); - wf_server_config_set_mountpoint(config, context->GetBaseDir()); - - wf_server * server = wf_server_create(config); - if (nullptr != server) + while (!context->IsShutdownRequested()) { - while (!context->IsShutdownRequested()) - { - wf_server_service(server, 100); - } - - wf_server_dispose(server); + wf_server_service(context->server, 100); } - - wf_server_config_dispose(config); } - char base_dir[WF_PATH_MAX]; - bool is_running; - std::mutex run_lock; - - bool is_shutdown_requested; std::mutex shutdown_lock; - std::thread thread; + bool is_shutdown_requested; + + +public: + char base_dir[WF_PATH_MAX]; + wf_server_config * config; + wf_server * server; }; Server::Server() @@ -113,19 +90,9 @@ Server::~Server() delete d; } -void Server::Start(void) -{ - d->Start(); -} - -void Server::Stop(void) -{ - d->Stop(); -} - char const * Server::GetBaseDir(void) const { - return d->GetBaseDir(); + return d->base_dir; } diff --git a/test/integration/test_integration.cc b/test/integration/test_integration.cc index f39859a..c89caf8 100644 --- a/test/integration/test_integration.cc +++ b/test/integration/test_integration.cc @@ -2,12 +2,17 @@ #include "integration/server.hpp" #include "integration/provider.hpp" +#include + #include #include #include #include +#include +#include "webfuse/core/lws_log.h" + using webfuse_test::Server; using webfuse_test::Provider; @@ -17,34 +22,35 @@ namespace { public: IntegrationTest() - : provider("ws://localhost:8080/") { - + json_object_seed(0); + wf_lwslog_disable(); } + protected: void SetUp() { - server.Start(); - provider.Start(); + server = new Server(); + provider = new Provider("ws://localhost:8080/"); } void TearDown() { - provider.Stop(); - server.Stop(); + delete provider; + delete server; } char const * GetBaseDir() const { - return server.GetBaseDir(); + return server->GetBaseDir(); } private: - Server server; - Provider provider; + Server * server; + Provider * provider; }; } -TEST_F(IntegrationTest, DISABLED_HasMountpoint) +TEST_F(IntegrationTest, HasMountpoint) { struct stat buffer; int rc = stat(GetBaseDir(), &buffer); @@ -53,7 +59,7 @@ TEST_F(IntegrationTest, DISABLED_HasMountpoint) ASSERT_TRUE(S_ISDIR(buffer.st_mode)); } -TEST_F(IntegrationTest, DISABLED_ProvidesTextFile) +TEST_F(IntegrationTest, ProvidesTextFile) { std::string file_name = std::string(GetBaseDir()) + "/cprovider/default/hello.txt";