2019-02-05 23:58:51 +00:00
|
|
|
#include "wsfs/server_protocol_intern.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
2019-02-09 02:08:02 +00:00
|
|
|
#include "wsfs/message.h"
|
2019-02-05 23:58:51 +00:00
|
|
|
#include "wsfs/filesystem.h"
|
|
|
|
#include "wsfs/util.h"
|
|
|
|
|
|
|
|
static int wsfs_server_protocol_callback(
|
|
|
|
struct lws * wsi,
|
|
|
|
enum lws_callback_reasons reason,
|
|
|
|
void * WSFS_UNUSED_PARAM(user),
|
|
|
|
void * WSFS_UNUSED_PARAM(in),
|
|
|
|
size_t WSFS_UNUSED_PARAM(len))
|
|
|
|
{
|
|
|
|
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
|
|
|
|
struct wsfs_server_protocol * protocol = ws_protocol->user;
|
|
|
|
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case LWS_CALLBACK_PROTOCOL_INIT:
|
|
|
|
{
|
|
|
|
lws_sock_file_fd_type fd;
|
|
|
|
fd.filefd = wsfs_filesystem_get_fd(&protocol->filesystem);
|
|
|
|
if (!lws_adopt_descriptor_vhost(lws_get_vhost(wsi), LWS_ADOPT_RAW_FILE_DESC, fd, ws_protocol->name, NULL))
|
|
|
|
{
|
|
|
|
puts("error: unable to adopt fd");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-02-09 02:08:02 +00:00
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
|
|
|
{
|
|
|
|
if (NULL == protocol->wsi)
|
|
|
|
{
|
|
|
|
protocol->wsi = wsi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CLOSED:
|
|
|
|
{
|
|
|
|
if (wsi == protocol->wsi)
|
|
|
|
{
|
|
|
|
protocol->wsi = NULL;
|
|
|
|
wsfs_message_queue_cleanup(&protocol->queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
|
|
{
|
|
|
|
if ((wsi == protocol->wsi) && (!wsfs_message_queue_empty(&protocol->queue)))
|
|
|
|
{
|
|
|
|
struct wsfs_message * message = wsfs_message_queue_pop(&protocol->queue);
|
|
|
|
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
|
|
|
|
wsfs_message_dispose(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-02-05 23:58:51 +00:00
|
|
|
case LWS_CALLBACK_RAW_RX_FILE:
|
|
|
|
{
|
|
|
|
wsfs_filesystem_process_request(&protocol->filesystem);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-02-09 02:08:02 +00:00
|
|
|
|
|
|
|
if ((wsi == protocol->wsi) && (!wsfs_message_queue_empty(&protocol->queue)))
|
|
|
|
{
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
}
|
|
|
|
|
2019-02-05 23:58:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-09 02:08:02 +00:00
|
|
|
static bool wsfs_server_protocol_invoke(
|
|
|
|
void * user_data,
|
|
|
|
json_t const * request)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
struct wsfs_server_protocol * protocol = user_data;
|
|
|
|
|
|
|
|
if (NULL != protocol->wsi)
|
|
|
|
{
|
|
|
|
size_t length = json_dumpb(request, NULL, 0, JSON_COMPACT);
|
|
|
|
if (0 < length)
|
|
|
|
{
|
|
|
|
struct wsfs_message * message = wsfs_message_create(length);
|
|
|
|
json_dumpb(request, message->data, length, JSON_COMPACT);
|
|
|
|
|
|
|
|
wsfs_message_queue_push(&protocol->queue, message);
|
|
|
|
lws_callback_on_writable(protocol->wsi);
|
|
|
|
|
|
|
|
// ToDo: add timeout
|
|
|
|
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-05 23:58:51 +00:00
|
|
|
struct wsfs_server_protocol * wsfs_server_protocol_create(
|
|
|
|
char * mount_point)
|
|
|
|
{
|
|
|
|
struct wsfs_server_protocol * protocol = malloc(sizeof(struct wsfs_server_protocol));
|
|
|
|
if (NULL != protocol)
|
|
|
|
{
|
2019-02-09 02:08:02 +00:00
|
|
|
if (!wsfs_server_protocol_init(protocol, mount_point))
|
|
|
|
{
|
|
|
|
free(protocol);
|
|
|
|
protocol = NULL;
|
|
|
|
}
|
2019-02-05 23:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return protocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wsfs_server_protocol_dispose(
|
|
|
|
struct wsfs_server_protocol * protocol)
|
|
|
|
{
|
|
|
|
wsfs_server_protocol_cleanup(protocol);
|
|
|
|
free(protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wsfs_server_protocol_init_lws(
|
|
|
|
struct wsfs_server_protocol * protocol,
|
|
|
|
struct lws_protocols * lws_protocol)
|
|
|
|
{
|
|
|
|
lws_protocol->callback = &wsfs_server_protocol_callback;
|
|
|
|
lws_protocol->per_session_data_size = 0;
|
|
|
|
lws_protocol->user = protocol;
|
|
|
|
}
|
|
|
|
|
2019-02-09 02:08:02 +00:00
|
|
|
bool wsfs_server_protocol_init(
|
2019-02-05 23:58:51 +00:00
|
|
|
struct wsfs_server_protocol * protocol,
|
|
|
|
char * mount_point)
|
|
|
|
{
|
2019-02-09 02:08:02 +00:00
|
|
|
protocol->wsi = NULL;
|
|
|
|
wsfs_message_queue_init(&protocol->queue);
|
|
|
|
|
|
|
|
wsfs_jsonrpc_server_init(&protocol->rpc);
|
|
|
|
wsfs_jsonrpc_server_add(&protocol->rpc, "lookup", &wsfs_server_protocol_invoke, protocol);
|
|
|
|
wsfs_jsonrpc_server_add(&protocol->rpc, "getattr", &wsfs_server_protocol_invoke, protocol);
|
|
|
|
wsfs_jsonrpc_server_add(&protocol->rpc, "readdir", &wsfs_server_protocol_invoke, protocol);
|
|
|
|
wsfs_jsonrpc_server_add(&protocol->rpc, "open", &wsfs_server_protocol_invoke, protocol);
|
|
|
|
wsfs_jsonrpc_server_add(&protocol->rpc, "close", &wsfs_server_protocol_invoke, protocol);
|
|
|
|
wsfs_jsonrpc_server_add(&protocol->rpc, "read", &wsfs_server_protocol_invoke, protocol);
|
|
|
|
|
|
|
|
return wsfs_filesystem_init(&protocol->filesystem, &protocol->rpc, mount_point);
|
2019-02-05 23:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wsfs_server_protocol_cleanup(
|
|
|
|
struct wsfs_server_protocol * protocol)
|
|
|
|
{
|
|
|
|
wsfs_filesystem_cleanup(&protocol->filesystem);
|
2019-02-09 02:08:02 +00:00
|
|
|
wsfs_jsonrpc_server_cleanup(&protocol->rpc);
|
|
|
|
wsfs_message_queue_cleanup(&protocol->queue);
|
|
|
|
protocol->wsi = NULL;
|
2019-02-05 23:58:51 +00:00
|
|
|
}
|