mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
chore: cleanup jsonrpc library
This commit is contained in:
27
lib/jsonrpc/impl/error.c
Normal file
27
lib/jsonrpc/impl/error.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "jsonrpc/impl/error.h"
|
||||
|
||||
json_t *
|
||||
jsonrpc_impl_error(
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * error = json_object();
|
||||
json_object_set_new(error, "code", json_integer(code));
|
||||
json_object_set_new(error, "message", json_string(message));
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void
|
||||
jsonrpc_impl_propate_error(
|
||||
jsonrpc_proxy_finished_fn * finised,
|
||||
void * user_data,
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * error = jsonrpc_impl_error(code, message);
|
||||
finised(user_data, NULL, error);
|
||||
|
||||
json_decref(error);
|
||||
}
|
||||
|
||||
29
lib/jsonrpc/impl/error.h
Normal file
29
lib/jsonrpc/impl/error.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef JSONRPC_IMPL_ERROR_H
|
||||
#define JSONRPC_IMPL_ERROR_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "jsonrpc/proxy_finished_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern json_t *
|
||||
jsonrpc_impl_error(
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_propate_error(
|
||||
jsonrpc_proxy_finished_fn * finised,
|
||||
void * user_data,
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
27
lib/jsonrpc/impl/method.c
Normal file
27
lib/jsonrpc/impl/method.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "jsonrpc/impl/method.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct jsonrpc_method * jsonrpc_impl_method_create(
|
||||
char const * method_name,
|
||||
jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
struct jsonrpc_method * method = malloc(sizeof(struct jsonrpc_method));
|
||||
if (NULL != method)
|
||||
{
|
||||
method->next = NULL;
|
||||
method->name = strdup(method_name);
|
||||
method->invoke = invoke;
|
||||
method->user_data = user_data;
|
||||
}
|
||||
|
||||
return method;
|
||||
}
|
||||
|
||||
void jsonrpc_impl_method_dispose(
|
||||
struct jsonrpc_method * method)
|
||||
{
|
||||
free(method->name);
|
||||
free(method);
|
||||
}
|
||||
34
lib/jsonrpc/impl/method.h
Normal file
34
lib/jsonrpc/impl/method.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef JSONRPC_IMPL_METHOD_H
|
||||
#define JSONRPC_IMPL_METHOD_H
|
||||
|
||||
#include "jsonrpc/method_invoke_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct jsonrpc_method
|
||||
{
|
||||
struct jsonrpc_method * next;
|
||||
char * name;
|
||||
jsonrpc_method_invoke_fn * invoke;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
extern struct jsonrpc_method *
|
||||
jsonrpc_impl_method_create(
|
||||
char const * method_name,
|
||||
jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_method_dispose(
|
||||
struct jsonrpc_method * method);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
213
lib/jsonrpc/impl/proxy.c
Normal file
213
lib/jsonrpc/impl/proxy.c
Normal file
@@ -0,0 +1,213 @@
|
||||
#include "jsonrpc/impl/proxy.h"
|
||||
#include "jsonrpc/impl/response.h"
|
||||
#include "jsonrpc/impl/error.h"
|
||||
#include "jsonrpc/status.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct jsonrpc_proxy *
|
||||
jsonrpc_impl_proxy_create(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
struct jsonrpc_proxy * proxy = malloc(sizeof(struct jsonrpc_proxy));
|
||||
if (NULL != proxy)
|
||||
{
|
||||
jsonrpc_impl_proxy_init(proxy, manager, timeout, send, user_data);
|
||||
}
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
void jsonrpc_impl_proxy_dispose(
|
||||
struct jsonrpc_proxy * proxy)
|
||||
{
|
||||
jsonrpc_impl_proxy_cleanup(proxy);
|
||||
free(proxy);
|
||||
}
|
||||
|
||||
static void jsonrpc_impl_proxy_timeout(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
struct jsonrpc_proxy * proxy = timer->user_data;
|
||||
|
||||
if (proxy->request.is_pending)
|
||||
{
|
||||
jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
void * user_data = proxy->request.user_data;
|
||||
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.id = 0;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.finished = NULL;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_TIMEOUT, "Timeout");
|
||||
}
|
||||
}
|
||||
|
||||
static json_t * jsonrpc_impl_request_create(
|
||||
char const * method,
|
||||
int id,
|
||||
char const * param_info,
|
||||
va_list args)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string(method));
|
||||
json_t * params = json_array();
|
||||
|
||||
for (char const * param_type = param_info; '\0' != *param_type; param_type++)
|
||||
{
|
||||
switch(*param_type)
|
||||
{
|
||||
case 's':
|
||||
{
|
||||
char const * const value = va_arg(args, char const *);
|
||||
json_array_append_new(params, json_string(value));
|
||||
}
|
||||
break;
|
||||
case 'i':
|
||||
{
|
||||
int const value = va_arg(args, int);
|
||||
json_array_append_new(params, json_integer(value));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
|
||||
json_decref(params);
|
||||
json_decref(request);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
json_object_set_new(request, "params", params);
|
||||
if (0 != id)
|
||||
{
|
||||
json_object_set_new(request, "id", json_integer(id));
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void jsonrpc_impl_proxy_init(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
struct wf_impl_timeout_manager * timeout_manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
proxy->send = send;
|
||||
proxy->timeout = timeout;
|
||||
proxy->user_data = user_data;
|
||||
proxy->request.is_pending = false;
|
||||
|
||||
wf_impl_timer_init(&proxy->request.timer, timeout_manager);
|
||||
}
|
||||
|
||||
void jsonrpc_impl_proxy_cleanup(
|
||||
struct jsonrpc_proxy * proxy)
|
||||
{
|
||||
if (proxy->request.is_pending)
|
||||
{
|
||||
void * user_data = proxy->request.user_data;
|
||||
jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.finished = NULL;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.id = 0;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad");
|
||||
}
|
||||
|
||||
wf_impl_timer_cleanup(&proxy->request.timer);
|
||||
}
|
||||
|
||||
void jsonrpc_impl_proxy_invoke(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args
|
||||
)
|
||||
{
|
||||
if (!proxy->request.is_pending)
|
||||
{
|
||||
proxy->request.is_pending = true;
|
||||
proxy->request.finished = finished;
|
||||
proxy->request.user_data = user_data;
|
||||
proxy->request.id = 42;
|
||||
wf_impl_timer_start(&proxy->request.timer, wf_impl_timepoint_in_msec(proxy->timeout),
|
||||
&jsonrpc_impl_proxy_timeout, proxy);
|
||||
|
||||
json_t * request = jsonrpc_impl_request_create(method_name, proxy->request.id, param_info, args);
|
||||
|
||||
bool const is_send = ((NULL != request) && (proxy->send(request, proxy->user_data)));
|
||||
if (!is_send)
|
||||
{
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.finished = NULL;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.id = 0;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad");
|
||||
}
|
||||
|
||||
if (NULL != request)
|
||||
{
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_BUSY, "Busy");
|
||||
}
|
||||
}
|
||||
|
||||
extern void jsonrpc_impl_proxy_notify(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args
|
||||
)
|
||||
{
|
||||
json_t * request = jsonrpc_impl_request_create(method_name, 0, param_info, args);
|
||||
if (NULL != request)
|
||||
{
|
||||
proxy->send(request, proxy->user_data);
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void jsonrpc_impl_proxy_onresult(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
json_t * message)
|
||||
{
|
||||
struct jsonrpc_response response;
|
||||
jsonrpc_impl_response_init(&response, message);
|
||||
|
||||
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
|
||||
{
|
||||
jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
void * user_data = proxy->request.user_data;
|
||||
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.id = 0;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.finished = NULL;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
finished(user_data, response.result, response.error);
|
||||
}
|
||||
|
||||
jsonrpc_impl_response_cleanup(&response);
|
||||
}
|
||||
|
||||
84
lib/jsonrpc/impl/proxy.h
Normal file
84
lib/jsonrpc/impl/proxy.h
Normal file
@@ -0,0 +1,84 @@
|
||||
#ifndef JSONRPC_IMPL_PROXY_H
|
||||
#define JSONRPC_IMPL_PROXY_H
|
||||
|
||||
#include "jsonrpc/proxy_finished_fn.h"
|
||||
#include "jsonrpc/send_fn.h"
|
||||
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
#include "webfuse/adapter/impl/time/timer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct jsonrpc_request
|
||||
{
|
||||
bool is_pending;
|
||||
jsonrpc_proxy_finished_fn * finished;
|
||||
void * user_data;
|
||||
int id;
|
||||
struct wf_impl_timer timer;
|
||||
};
|
||||
|
||||
struct jsonrpc_proxy
|
||||
{
|
||||
struct jsonrpc_request request;
|
||||
int timeout;
|
||||
jsonrpc_send_fn * send;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_proxy_init(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_proxy_cleanup(
|
||||
struct jsonrpc_proxy * proxy);
|
||||
|
||||
extern struct jsonrpc_proxy *
|
||||
jsonrpc_impl_proxy_create(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_proxy_dispose(
|
||||
struct jsonrpc_proxy * proxy);
|
||||
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_proxy_invoke(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args
|
||||
);
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_proxy_notify(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args
|
||||
);
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_proxy_onresult(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
json_t * message);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
84
lib/jsonrpc/impl/request.c
Normal file
84
lib/jsonrpc/impl/request.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "jsonrpc/impl/request.h"
|
||||
#include "jsonrpc/impl/error.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct jsonrpc_request
|
||||
{
|
||||
int id;
|
||||
jsonrpc_send_fn * send;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
bool
|
||||
jsonrpc_impl_is_request(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
json_t * method = json_object_get(message, "method");
|
||||
json_t * params = json_object_get(message, "params");
|
||||
|
||||
return (json_is_integer(id) && json_is_string(method) &&
|
||||
(json_is_array(params) || json_is_object(params)));
|
||||
}
|
||||
|
||||
|
||||
struct jsonrpc_request *
|
||||
jsonrpc_impl_request_create(
|
||||
int id,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
struct jsonrpc_request * request = malloc(sizeof(struct jsonrpc_request));
|
||||
if (NULL != request)
|
||||
{
|
||||
request->id = id;
|
||||
request->send = send;
|
||||
request->user_data = user_data;
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void
|
||||
jsonrpc_impl_request_dispose(
|
||||
struct jsonrpc_request * request)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
void *
|
||||
jsonrpc_impl_request_get_userdata(
|
||||
struct jsonrpc_request * request)
|
||||
{
|
||||
return request->user_data;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
jsonrpc_impl_respond(
|
||||
struct jsonrpc_request * request,
|
||||
json_t * result)
|
||||
{
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", result);
|
||||
json_object_set_new(response, "id", json_integer(request->id));
|
||||
|
||||
request->send(response, request->user_data);
|
||||
json_decref(response);
|
||||
jsonrpc_impl_request_dispose(request);
|
||||
}
|
||||
|
||||
void jsonrpc_impl_respond_error(
|
||||
struct jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "error", jsonrpc_impl_error(code, message));
|
||||
json_object_set_new(response, "id", json_integer(request->id));
|
||||
|
||||
request->send(response, request->user_data);
|
||||
json_decref(response);
|
||||
jsonrpc_impl_request_dispose(request);
|
||||
}
|
||||
|
||||
53
lib/jsonrpc/impl/request.h
Normal file
53
lib/jsonrpc/impl/request.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef JSONRPC_IMPL_REQUEST_H
|
||||
#define JSONRPC_IMPL_REQUEST_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include <jsonrpc/api.h>
|
||||
#include "jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct jsonrpc_request;
|
||||
|
||||
extern bool jsonrpc_impl_is_request(
|
||||
json_t * message);
|
||||
|
||||
extern struct jsonrpc_request * jsonrpc_impl_request_create(
|
||||
int id,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void jsonrpc_impl_request_dispose(
|
||||
struct jsonrpc_request * request);
|
||||
|
||||
extern void * jsonrpc_impl_request_get_userdata(
|
||||
struct jsonrpc_request * request);
|
||||
|
||||
extern void jsonrpc_impl_respond(
|
||||
struct jsonrpc_request * request,
|
||||
json_t * result);
|
||||
|
||||
extern void jsonrpc_impl_respond_error(
|
||||
struct jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
68
lib/jsonrpc/impl/response.c
Normal file
68
lib/jsonrpc/impl/response.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "jsonrpc/impl/response.h"
|
||||
#include "jsonrpc/impl/error.h"
|
||||
#include "jsonrpc/status.h"
|
||||
|
||||
bool
|
||||
jsonrpc_impl_is_response(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
json_t * err = json_object_get(message, "error");
|
||||
json_t * result = json_object_get(message, "result");
|
||||
|
||||
return (json_is_integer(id) &&
|
||||
(json_is_object(err) || (NULL != result)));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
jsonrpc_impl_response_init(
|
||||
struct jsonrpc_response * result,
|
||||
json_t * response)
|
||||
{
|
||||
result->id = -1;
|
||||
result->result = NULL;
|
||||
result->error = NULL;
|
||||
|
||||
json_t * id_holder = json_object_get(response, "id");
|
||||
if ((NULL == id_holder) || (!json_is_integer(id_holder)))
|
||||
{
|
||||
result->error = jsonrpc_impl_error(JSONRPC_BAD_FORMAT, "invalid format: missing id");
|
||||
return;
|
||||
}
|
||||
|
||||
result->id = json_integer_value(id_holder);
|
||||
result->result = json_object_get(response, "result");
|
||||
if (NULL != result->result)
|
||||
{
|
||||
json_incref(result->result);
|
||||
}
|
||||
else
|
||||
{
|
||||
json_t * error = json_object_get(response, "error");
|
||||
if ((json_is_object(error)) && (json_is_integer(json_object_get(error, "code"))))
|
||||
{
|
||||
result->error = error;
|
||||
json_incref(result->error);
|
||||
}
|
||||
else
|
||||
{
|
||||
result->error = jsonrpc_impl_error(JSONRPC_BAD_FORMAT, "invalid format: invalid error object");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
jsonrpc_impl_response_cleanup(
|
||||
struct jsonrpc_response * response)
|
||||
{
|
||||
if (NULL != response->result)
|
||||
{
|
||||
json_decref(response->result);
|
||||
}
|
||||
|
||||
if (NULL != response->error)
|
||||
{
|
||||
json_decref(response->error);
|
||||
}
|
||||
}
|
||||
40
lib/jsonrpc/impl/response.h
Normal file
40
lib/jsonrpc/impl/response.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef JSONRPC_IMPL_RESPONSE_H
|
||||
#define JSONRPC_IMPL_RESPONSE_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct jsonrpc_response
|
||||
{
|
||||
json_t * result;
|
||||
json_t * error;
|
||||
int id;
|
||||
};
|
||||
|
||||
extern bool jsonrpc_impl_is_response(
|
||||
json_t * message);
|
||||
|
||||
extern void jsonrpc_impl_response_init(
|
||||
struct jsonrpc_response * response,
|
||||
json_t * message);
|
||||
|
||||
extern void jsonrpc_impl_response_cleanup(
|
||||
struct jsonrpc_response * response);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
132
lib/jsonrpc/impl/server.c
Normal file
132
lib/jsonrpc/impl/server.c
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "jsonrpc/impl/server.h"
|
||||
#include "jsonrpc/impl/method.h"
|
||||
#include "jsonrpc/impl/request.h"
|
||||
#include "jsonrpc/impl/unused_param.h"
|
||||
#include "jsonrpc/status.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct jsonrpc_server
|
||||
{
|
||||
struct jsonrpc_method * methods;
|
||||
};
|
||||
|
||||
static void
|
||||
jsonrpc_impl_server_init(
|
||||
struct jsonrpc_server * server);
|
||||
|
||||
static void
|
||||
jsonrpc_impl_server_cleanup(
|
||||
struct jsonrpc_server * server);
|
||||
|
||||
struct jsonrpc_server *
|
||||
jsonrpc_impl_server_create(void)
|
||||
{
|
||||
struct jsonrpc_server * server = malloc(sizeof(struct jsonrpc_server));
|
||||
if (NULL != server)
|
||||
{
|
||||
jsonrpc_impl_server_init(server);
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
void
|
||||
jsonrpc_impl_server_dispose(
|
||||
struct jsonrpc_server * server)
|
||||
{
|
||||
jsonrpc_impl_server_cleanup(server);
|
||||
free(server);
|
||||
}
|
||||
|
||||
|
||||
static void jsonrpc_impl_server_init(
|
||||
struct jsonrpc_server * server)
|
||||
{
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
static void jsonrpc_impl_server_cleanup(
|
||||
struct jsonrpc_server * server)
|
||||
{
|
||||
struct jsonrpc_method * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
struct jsonrpc_method * next = current->next;
|
||||
jsonrpc_impl_method_dispose(current);
|
||||
current = next;
|
||||
}
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
void jsonrpc_impl_server_add(
|
||||
struct jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
struct jsonrpc_method * method = jsonrpc_impl_method_create(method_name, invoke, user_data);
|
||||
method->next = server->methods;
|
||||
server->methods = method;
|
||||
}
|
||||
|
||||
static void jsonrpc_impl_server_invalid_method_invoke(
|
||||
struct jsonrpc_request * request,
|
||||
char const * JSONRPC_UNUSED_PARAM(method_name),
|
||||
json_t * JSONRPC_UNUSED_PARAM(params),
|
||||
void * JSONRPC_UNUSED_PARAM(user_data))
|
||||
{
|
||||
jsonrpc_impl_respond_error(request, JSONRPC_BAD_NOTIMPLEMENTED, "not implemented");
|
||||
}
|
||||
|
||||
static struct jsonrpc_method const jsonrpc_impl_server_invalid_method =
|
||||
{
|
||||
.next = NULL,
|
||||
.name = "<invalid>",
|
||||
.invoke = &jsonrpc_impl_server_invalid_method_invoke,
|
||||
.user_data = NULL
|
||||
};
|
||||
|
||||
static struct jsonrpc_method const *
|
||||
jsonrpc_impl_server_get_method(
|
||||
struct jsonrpc_server * server,
|
||||
char const * method_name)
|
||||
{
|
||||
struct jsonrpc_method const * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
if (0 == strcmp(method_name, current->name))
|
||||
{
|
||||
return current;
|
||||
}
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
return &jsonrpc_impl_server_invalid_method;
|
||||
}
|
||||
|
||||
void jsonrpc_impl_server_process(
|
||||
struct jsonrpc_server * server,
|
||||
json_t * request_data,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
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");
|
||||
|
||||
if (json_is_string(method_holder) &&
|
||||
(json_is_array(params) || (json_is_object(params))) &&
|
||||
json_is_integer(id_holder))
|
||||
{
|
||||
char const * method_name = json_string_value(method_holder);
|
||||
int id = json_integer_value(id_holder);
|
||||
struct jsonrpc_request * request = jsonrpc_impl_request_create(id, send, user_data);
|
||||
struct jsonrpc_method const * method = jsonrpc_impl_server_get_method(server, method_name);
|
||||
|
||||
method->invoke(request, method_name, params, method->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
41
lib/jsonrpc/impl/server.h
Normal file
41
lib/jsonrpc/impl/server.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef JSONRPC_IMPL_SERVER_H
|
||||
#define JSONRPC_IMPL_SERVER_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "jsonrpc/method_invoke_fn.h"
|
||||
#include "jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct jsonrpc_server;
|
||||
|
||||
extern struct jsonrpc_server *
|
||||
jsonrpc_impl_server_create(void);
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_server_dispose(
|
||||
struct jsonrpc_server * server);
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_server_add(
|
||||
struct jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
jsonrpc_impl_server_process(
|
||||
struct jsonrpc_server * server,
|
||||
json_t * request,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
10
lib/jsonrpc/impl/unused_param.h
Normal file
10
lib/jsonrpc/impl/unused_param.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef JSONRPC_UTIL_H
|
||||
#define JSONRPC_UTIL_H
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define JSONRPC_UNUSED_PARAM(param) param __attribute__((unused))
|
||||
#else
|
||||
#define JSONRPC_UNUSED_PARAM(param)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user