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