|
|
@ -1,12 +1,73 @@
|
|
|
|
#include "webfuse/adapter/impl/client_protocol.h"
|
|
|
|
#include "webfuse/adapter/impl/client_protocol.h"
|
|
|
|
#include "webfuse/adapter/client_callback.h"
|
|
|
|
#include "webfuse/adapter/client_callback.h"
|
|
|
|
|
|
|
|
#include "webfuse/adapter/impl/credentials.h"
|
|
|
|
#include "webfuse/core/protocol_names.h"
|
|
|
|
#include "webfuse/core/protocol_names.h"
|
|
|
|
#include "webfuse/core/url.h"
|
|
|
|
#include "webfuse/core/url.h"
|
|
|
|
#include "webfuse/core/util.h"
|
|
|
|
#include "webfuse/core/util.h"
|
|
|
|
|
|
|
|
#include "webfuse/core/timer/manager.h"
|
|
|
|
|
|
|
|
#include "webfuse/core/jsonrpc/response.h"
|
|
|
|
|
|
|
|
#include "webfuse/core/jsonrpc/proxy.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "webfuse/core/message.h"
|
|
|
|
|
|
|
|
#include "webfuse/core/message_queue.h"
|
|
|
|
|
|
|
|
#include "webfuse/core/container_of.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <libwebsockets.h>
|
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
|
|
wf_impl_client_protocol_process(
|
|
|
|
|
|
|
|
struct wf_client_protocol * protocol,
|
|
|
|
|
|
|
|
char const * data,
|
|
|
|
|
|
|
|
size_t length)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
json_t * message = json_loadb(data, length, 0, NULL);
|
|
|
|
|
|
|
|
if (NULL != message)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (wf_jsonrpc_is_response(message))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
wf_jsonrpc_proxy_onresult(protocol->proxy, message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
|
|
|
wf_impl_client_protocol_send(
|
|
|
|
|
|
|
|
json_t * request,
|
|
|
|
|
|
|
|
void * user_data)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
struct wf_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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
|
|
wf_impl_client_protocol_on_authenticate_finished(
|
|
|
|
|
|
|
|
void * user_data,
|
|
|
|
|
|
|
|
json_t const * result,
|
|
|
|
|
|
|
|
json_t const * WF_UNUSED_PARAM(error))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
struct wf_client_protocol * protocol = user_data;
|
|
|
|
|
|
|
|
int const reason = (NULL != result) ? WF_CLIENT_AUTHENTICATED : WF_CLIENT_AUTHENTICATION_FAILED;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protocol->callback(protocol->user_data, reason, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int wf_impl_client_protocol_lws_callback(
|
|
|
|
static int wf_impl_client_protocol_lws_callback(
|
|
|
|
struct lws * wsi,
|
|
|
|
struct lws * wsi,
|
|
|
|
enum lws_callback_reasons reason,
|
|
|
|
enum lws_callback_reasons reason,
|
|
|
@ -20,6 +81,8 @@ static int wf_impl_client_protocol_lws_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:
|
|
|
@ -35,6 +98,8 @@ static int wf_impl_client_protocol_lws_callback(
|
|
|
|
protocol->callback(protocol->user_data, WF_CLIENT_DISCONNECTED, NULL);
|
|
|
|
protocol->callback(protocol->user_data, WF_CLIENT_DISCONNECTED, NULL);
|
|
|
|
protocol->wsi = NULL;
|
|
|
|
protocol->wsi = NULL;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
|
|
|
|
|
|
|
wf_impl_client_protocol_process(protocol, in, len);
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
|
|
// fall-through
|
|
|
|
// fall-through
|
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
|
|
@ -44,6 +109,18 @@ static int wf_impl_client_protocol_lws_callback(
|
|
|
|
{
|
|
|
|
{
|
|
|
|
result = 1;
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
break;
|
|
|
@ -65,6 +142,11 @@ wf_impl_client_protocol_init(
|
|
|
|
protocol->callback = callback;
|
|
|
|
protocol->callback = callback;
|
|
|
|
protocol->user_data = user_data;
|
|
|
|
protocol->user_data = user_data;
|
|
|
|
protocol->callback(protocol->user_data, WF_CLIENT_INIT, NULL);
|
|
|
|
protocol->callback(protocol->user_data, WF_CLIENT_INIT, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wf_slist_init(&protocol->messages);
|
|
|
|
|
|
|
|
protocol->timer_manager = wf_timer_manager_create();
|
|
|
|
|
|
|
|
protocol->proxy = wf_jsonrpc_proxy_create(protocol->timer_manager, WF_DEFAULT_TIMEOUT, &wf_impl_client_protocol_send, protocol);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
void
|
|
|
@ -72,6 +154,9 @@ wf_impl_client_protocol_cleanup(
|
|
|
|
struct wf_client_protocol * protocol)
|
|
|
|
struct wf_client_protocol * protocol)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
protocol->callback(protocol->user_data, WF_CLIENT_CLEANUP, NULL);
|
|
|
|
protocol->callback(protocol->user_data, WF_CLIENT_CLEANUP, NULL);
|
|
|
|
|
|
|
|
wf_jsonrpc_proxy_dispose(protocol->proxy);
|
|
|
|
|
|
|
|
wf_timer_manager_dispose(protocol->timer_manager);
|
|
|
|
|
|
|
|
wf_message_queue_cleanup(&protocol->messages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
void
|
|
|
@ -141,3 +226,23 @@ wf_impl_client_protocol_disconnect(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
|
|
wf_impl_client_protocol_authenticate(
|
|
|
|
|
|
|
|
struct wf_client_protocol * protocol)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
struct wf_credentials creds;
|
|
|
|
|
|
|
|
wf_impl_credentials_init_default(&creds);
|
|
|
|
|
|
|
|
protocol->callback(protocol->user_data, WF_CLIENT_AUTHENTICATE_GET_CREDENTIALS, &creds);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json_incref(creds.data);
|
|
|
|
|
|
|
|
wf_jsonrpc_proxy_invoke(
|
|
|
|
|
|
|
|
protocol->proxy,
|
|
|
|
|
|
|
|
&wf_impl_client_protocol_on_authenticate_finished,
|
|
|
|
|
|
|
|
protocol,
|
|
|
|
|
|
|
|
"authenticate",
|
|
|
|
|
|
|
|
"sj",
|
|
|
|
|
|
|
|
creds.type, creds.data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wf_impl_credentials_cleanup(&creds);
|
|
|
|
|
|
|
|
}
|
|
|
|