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); -}