1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00
falk-werner_webfuse/lib/webfuse/adapter/impl/jsonrpc/server.c

96 lines
2.9 KiB
C
Raw Normal View History

2019-03-26 22:04:53 +00:00
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include "webfuse/adapter/impl/jsonrpc/method.h"
2019-03-26 22:04:53 +00:00
#include "webfuse/adapter/impl/jsonrpc/request.h"
#include "webfuse/core/util.h"
2019-03-26 22:04:53 +00:00
#include <string.h>
2019-03-26 22:04:53 +00:00
void wf_impl_jsonrpc_server_init(
struct wf_impl_jsonrpc_server * server)
2019-03-26 22:04:53 +00:00
{
server->methods = NULL;
}
void wf_impl_jsonrpc_server_cleanup(
struct wf_impl_jsonrpc_server * server)
{
struct wf_impl_jsonrpc_method * current = server->methods;
while (NULL != current)
2019-03-26 22:04:53 +00:00
{
struct wf_impl_jsonrpc_method * next = current->next;
wf_impl_jsonrpc_method_dispose(current);
current = next;
2019-03-26 22:04:53 +00:00
}
server->methods = NULL;
}
void wf_impl_jsonrpc_server_add(
struct wf_impl_jsonrpc_server * server,
char const * method_name,
2019-03-26 22:04:53 +00:00
wf_impl_jsonrpc_method_invoke_fn * invoke,
void * user_data)
{
struct wf_impl_jsonrpc_method * method = wf_impl_jsonrpc_method_create(method_name, invoke, user_data);
2019-03-26 22:04:53 +00:00
method->next = server->methods;
server->methods = method;
}
static void wf_impl_jsonrpc_server_invalid_method_invoke(
struct wf_impl_jsonrpc_request * request,
char const * WF_UNUSED_PARAM(method_name),
json_t * WF_UNUSED_PARAM(params),
void * WF_UNUSED_PARAM(user_data))
2019-03-26 22:04:53 +00:00
{
wf_impl_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED);
2019-03-26 22:04:53 +00:00
}
static struct wf_impl_jsonrpc_method const wf_impl_jsonrpc_server_invalid_method =
2019-03-26 22:04:53 +00:00
{
.next = NULL,
.name = "<invalid>",
.invoke = &wf_impl_jsonrpc_server_invalid_method_invoke,
.user_data = NULL
};
static struct wf_impl_jsonrpc_method const * wf_impl_jsonrpc_server_get_method(
struct wf_impl_jsonrpc_server * server,
char const * method_name)
{
struct wf_impl_jsonrpc_method const * current = server->methods;
while (NULL != current)
{
if (0 == strcmp(method_name, current->name))
2019-03-26 22:04:53 +00:00
{
return current;
2019-03-26 22:04:53 +00:00
}
current = current->next;
}
2019-03-26 22:04:53 +00:00
return &wf_impl_jsonrpc_server_invalid_method;
}
2019-03-26 22:04:53 +00:00
void wf_impl_jsonrpc_server_process(
2019-03-26 22:04:53 +00:00
struct wf_impl_jsonrpc_server * server,
json_t * request_data,
wf_impl_jsonrpc_send_fn * send,
void * user_data)
2019-03-26 22:04:53 +00:00
{
json_t * method_holder = json_object_get(request_data, "method");
json_t * params = json_object_get(request_data, "params");
json_t * id_holder = json_object_get(request_data, "id");
2019-03-26 22:04:53 +00:00
if (json_is_string(method_holder) &&
(json_is_array(params) || (json_is_object(params))) &&
json_is_integer(id_holder))
2019-03-26 22:04:53 +00:00
{
char const * method_name = json_string_value(method_holder);
int id = json_integer_value(id_holder);
struct wf_impl_jsonrpc_request * request = wf_impl_jsonrpc_request_create(id, send, user_data);
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_get_method(server, method_name);
2019-03-26 22:04:53 +00:00
method->invoke(request, method_name, params, method->user_data);
2019-03-26 22:04:53 +00:00
}
}