1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

use jsonrpc_proxy to send requests within client_protocol (provider)

This commit is contained in:
Falk Werner 2020-03-01 01:13:24 +01:00
parent 97eb420ef2
commit af2343c67a
4 changed files with 88 additions and 38 deletions

View File

@ -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 OUTPUT_NAME webfuse-provider)
set_target_properties(webfuse-provider-static PROPERTIES C_VISIBILITY_PRESET hidden) 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 add_library(webfuse-provider SHARED
lib/webfuse/provider/api.c lib/webfuse/provider/api.c

View File

@ -124,7 +124,7 @@ void jsonrpc_impl_proxy_cleanup(
proxy->request.id = 0; proxy->request.id = 0;
wf_timer_cancel(proxy->request.timer); 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); wf_timer_dispose(proxy->request.timer);
@ -158,7 +158,7 @@ void jsonrpc_impl_proxy_invoke(
proxy->request.id = 0; proxy->request.id = 0;
wf_timer_cancel(proxy->request.timer); 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) if (NULL != request)

View File

@ -15,6 +15,14 @@
#include "webfuse/provider/impl/url.h" #include "webfuse/provider/impl/url.h"
#include "webfuse/core/protocol_names.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( static void wfp_impl_client_protocol_respond(
json_t * response, json_t * response,
void * user_data) void * user_data)
@ -29,23 +37,21 @@ 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, struct wfp_client_protocol * protocol,
char const * message, char const * data,
size_t length) size_t length)
{ {
json_t * request = json_loadb(message, length, 0, NULL); json_t * message = json_loadb(data, length, 0, NULL);
if (NULL != request) if (NULL != message)
{ {
// FIXME: is_connected should be invoked, when filesystem added if (jsonrpc_is_response(message))
if ((!protocol->is_connected) && (NULL != json_object_get(request, "result")))
{ {
protocol->is_connected = true; jsonrpc_proxy_onresult(protocol->proxy, message);
protocol->provider.connected(protocol->user_data);
} }
if (jsonrpc_is_request(message))
{
struct wfp_impl_invokation_context context = struct wfp_impl_invokation_context context =
{ {
.provider = &protocol->provider, .provider = &protocol->provider,
@ -53,30 +59,43 @@ static void wfp_impl_client_protocol_process_request(
.request = &protocol->request .request = &protocol->request
}; };
wfp_impl_provider_invoke(&context, request); wfp_impl_provider_invoke(&context, message);
json_decref(request);
} }
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( static void wfp_impl_client_protocol_add_filesystem(
struct wfp_client_protocol * protocol) struct wfp_client_protocol * protocol)
{ {
json_t * params = json_array(); jsonrpc_proxy_invoke(
json_array_append_new(params, json_string("cprovider")); protocol->proxy,
&wfp_impl_client_protocol_on_authenticate_finished,
json_t * request = json_object(); protocol,
json_object_set_new(request, "method", json_string("add_filesystem")); "add_filesystem",
json_object_set_new(request, "params", params); "s",
json_object_set_new(request, "id", json_integer(42)); "cprovider");
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);
} }
static int wfp_impl_client_protocol_callback( static int wfp_impl_client_protocol_callback(
@ -91,11 +110,12 @@ static int wfp_impl_client_protocol_callback(
if (NULL != protocol) if (NULL != protocol)
{ {
wf_timer_manager_check(protocol->timer_manager);
switch (reason) switch (reason)
{ {
case LWS_CALLBACK_CLIENT_ESTABLISHED: case LWS_CALLBACK_CLIENT_ESTABLISHED:
wfp_impl_client_protocol_add_filesystem(protocol); wfp_impl_client_protocol_add_filesystem(protocol);
// Defer is_connected until response received
break; break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
protocol->is_connected = false; protocol->is_connected = false;
@ -106,7 +126,7 @@ static int wfp_impl_client_protocol_callback(
protocol->provider.disconnected(protocol->user_data); protocol->provider.disconnected(protocol->user_data);
break; break;
case LWS_CALLBACK_CLIENT_RECEIVE: case LWS_CALLBACK_CLIENT_RECEIVE:
wfp_impl_client_protocol_process_request(protocol, in, len); wfp_impl_client_protocol_process(protocol, in, len);
break; break;
case LWS_CALLBACK_SERVER_WRITEABLE: case LWS_CALLBACK_SERVER_WRITEABLE:
// fall-through // fall-through
@ -133,6 +153,23 @@ static int wfp_impl_client_protocol_callback(
return 0; 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( void wfp_impl_client_protocol_init(
struct wfp_client_protocol * protocol, 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.respond = &wfp_impl_client_protocol_respond;
protocol->request.user_data = protocol; 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; protocol->user_data = user_data;
wfp_impl_provider_init_from_prototype(&protocol->provider, provider); 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( void wfp_impl_client_protocol_cleanup(
struct wfp_client_protocol * protocol) struct wfp_client_protocol * protocol)
{ {
jsonrpc_proxy_dispose(protocol->proxy);
wf_timer_manager_dispose(protocol->timer_manager);
wf_message_queue_cleanup(&protocol->messages); wf_message_queue_cleanup(&protocol->messages);
} }

View File

@ -14,6 +14,8 @@ extern "C"
struct wfp_client_config; struct wfp_client_config;
struct lws_protocols; struct lws_protocols;
struct lws_context; struct lws_context;
struct jsonrpc_proxy;
struct wf_timer_manager;
struct wfp_client_protocol struct wfp_client_protocol
{ {
@ -22,6 +24,8 @@ struct wfp_client_protocol
struct wfp_provider provider; struct wfp_provider provider;
void * user_data; void * user_data;
struct lws * wsi; struct lws * wsi;
struct wf_timer_manager * timer_manager;
struct jsonrpc_proxy * proxy;
struct wf_slist messages; struct wf_slist messages;
}; };