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:
144
lib/jsonrpc/api.c
Normal file
144
lib/jsonrpc/api.c
Normal file
@@ -0,0 +1,144 @@
|
||||
#include "jsonrpc.h"
|
||||
|
||||
#include "jsonrpc/impl/proxy.h"
|
||||
#include "jsonrpc/impl/request.h"
|
||||
#include "jsonrpc/impl/response.h"
|
||||
#include "jsonrpc/impl/server.h"
|
||||
|
||||
// proxy
|
||||
|
||||
struct jsonrpc_proxy *
|
||||
jsonrpc_proxy_create(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
return jsonrpc_impl_proxy_create(manager, timeout, send, user_data);
|
||||
}
|
||||
|
||||
void jsonrpc_proxy_dispose(
|
||||
struct jsonrpc_proxy * proxy)
|
||||
{
|
||||
jsonrpc_impl_proxy_dispose(proxy);
|
||||
}
|
||||
|
||||
void jsonrpc_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;
|
||||
va_start(args, param_info);
|
||||
jsonrpc_impl_proxy_invoke(proxy, finished, user_data, method_name, param_info, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void jsonrpc_proxy_notify(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
jsonrpc_impl_proxy_notify(proxy, method_name, param_info, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void jsonrpc_proxy_onresult(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
json_t * message)
|
||||
{
|
||||
jsonrpc_impl_proxy_onresult(proxy, message);
|
||||
}
|
||||
|
||||
|
||||
// request
|
||||
|
||||
bool jsonrpc_is_request(
|
||||
json_t * message)
|
||||
{
|
||||
return jsonrpc_impl_is_request(message);
|
||||
}
|
||||
|
||||
struct jsonrpc_request * jsonrpc_request_create(
|
||||
int id,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
return jsonrpc_impl_request_create(id, send, user_data);
|
||||
}
|
||||
|
||||
void jsonrpc_request_dispose(
|
||||
struct jsonrpc_request * request)
|
||||
{
|
||||
jsonrpc_impl_request_dispose(request);
|
||||
}
|
||||
|
||||
void * jsonrpc_request_get_userdata(
|
||||
struct jsonrpc_request * request)
|
||||
{
|
||||
return jsonrpc_impl_request_get_userdata(request);
|
||||
}
|
||||
|
||||
void jsonrpc_respond(
|
||||
struct jsonrpc_request * request,
|
||||
json_t * result)
|
||||
{
|
||||
jsonrpc_impl_respond(request, result);
|
||||
}
|
||||
|
||||
void jsonrpc_respond_error(
|
||||
struct jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
jsonrpc_impl_respond_error(request, code, message);
|
||||
}
|
||||
|
||||
// response
|
||||
|
||||
bool jsonrpc_is_response(
|
||||
json_t * message)
|
||||
{
|
||||
return jsonrpc_impl_is_response(message);
|
||||
}
|
||||
|
||||
// server
|
||||
|
||||
struct jsonrpc_server *
|
||||
jsonrpc_server_create(void)
|
||||
{
|
||||
return jsonrpc_impl_server_create();
|
||||
}
|
||||
|
||||
void
|
||||
jsonrpc_server_dispose(
|
||||
struct jsonrpc_server * server)
|
||||
{
|
||||
jsonrpc_impl_server_dispose(server);
|
||||
}
|
||||
|
||||
void jsonrpc_server_add(
|
||||
struct jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
jsonrpc_impl_server_add(server, method_name, invoke, user_data);
|
||||
}
|
||||
|
||||
void jsonrpc_server_process(
|
||||
struct jsonrpc_server * server,
|
||||
json_t * request,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
jsonrpc_impl_server_process(server, request, send, user_data);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "jsonrpc/error.h"
|
||||
#include "jsonrpc/impl/error.h"
|
||||
|
||||
json_t *
|
||||
jsonrpc_error(
|
||||
jsonrpc_impl_error(
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
@@ -13,13 +13,13 @@ jsonrpc_error(
|
||||
}
|
||||
|
||||
void
|
||||
jsonrpc_propate_error(
|
||||
jsonrpc_impl_propate_error(
|
||||
jsonrpc_proxy_finished_fn * finised,
|
||||
void * user_data,
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * error = jsonrpc_error(code, message);
|
||||
json_t * error = jsonrpc_impl_error(code, message);
|
||||
finised(user_data, NULL, error);
|
||||
|
||||
json_decref(error);
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef JSONRPC_ERROR_H
|
||||
#define JSONRPC_ERROR_H
|
||||
#ifndef JSONRPC_IMPL_ERROR_H
|
||||
#define JSONRPC_IMPL_ERROR_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "jsonrpc/proxy.h"
|
||||
#include "jsonrpc/proxy_finished_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -10,12 +10,12 @@ extern "C"
|
||||
#endif
|
||||
|
||||
extern json_t *
|
||||
jsonrpc_error(
|
||||
jsonrpc_impl_error(
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
extern void
|
||||
jsonrpc_propate_error(
|
||||
jsonrpc_impl_propate_error(
|
||||
jsonrpc_proxy_finished_fn * finised,
|
||||
void * user_data,
|
||||
int code,
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "jsonrpc/method.h"
|
||||
#include "jsonrpc/impl/method.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct jsonrpc_method * jsonrpc_method_create(
|
||||
struct jsonrpc_method * jsonrpc_impl_method_create(
|
||||
char const * method_name,
|
||||
jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
@@ -19,7 +19,7 @@ struct jsonrpc_method * jsonrpc_method_create(
|
||||
return method;
|
||||
}
|
||||
|
||||
void jsonrpc_method_dispose(
|
||||
void jsonrpc_impl_method_dispose(
|
||||
struct jsonrpc_method * method)
|
||||
{
|
||||
free(method->name);
|
||||
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
|
||||
@@ -1,11 +1,35 @@
|
||||
#include "jsonrpc/proxy.h"
|
||||
#include "jsonrpc/response.h"
|
||||
#include "jsonrpc/error.h"
|
||||
#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>
|
||||
|
||||
static void jsonrpc_proxy_timeout(
|
||||
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;
|
||||
@@ -21,11 +45,11 @@ static void jsonrpc_proxy_timeout(
|
||||
proxy->request.finished = NULL;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
jsonrpc_propate_error(finished, user_data, JSONRPC_BAD_TIMEOUT, "Timeout");
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_TIMEOUT, "Timeout");
|
||||
}
|
||||
}
|
||||
|
||||
static json_t * jsonrpc_request_create(
|
||||
static json_t * jsonrpc_impl_request_create(
|
||||
char const * method,
|
||||
int id,
|
||||
char const * param_info,
|
||||
@@ -69,7 +93,7 @@ static json_t * jsonrpc_request_create(
|
||||
return request;
|
||||
}
|
||||
|
||||
void jsonrpc_proxy_init(
|
||||
void jsonrpc_impl_proxy_init(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
struct wf_impl_timeout_manager * timeout_manager,
|
||||
int timeout,
|
||||
@@ -84,7 +108,7 @@ void jsonrpc_proxy_init(
|
||||
wf_impl_timer_init(&proxy->request.timer, timeout_manager);
|
||||
}
|
||||
|
||||
void jsonrpc_proxy_cleanup(
|
||||
void jsonrpc_impl_proxy_cleanup(
|
||||
struct jsonrpc_proxy * proxy)
|
||||
{
|
||||
if (proxy->request.is_pending)
|
||||
@@ -98,19 +122,19 @@ void jsonrpc_proxy_cleanup(
|
||||
proxy->request.id = 0;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
jsonrpc_propate_error(finished, user_data, JSONRPC_BAD, "Bad");
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad");
|
||||
}
|
||||
|
||||
wf_impl_timer_cleanup(&proxy->request.timer);
|
||||
}
|
||||
|
||||
void jsonrpc_proxy_invoke(
|
||||
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)
|
||||
@@ -120,12 +144,9 @@ void jsonrpc_proxy_invoke(
|
||||
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_proxy_timeout, proxy);
|
||||
&jsonrpc_impl_proxy_timeout, proxy);
|
||||
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
json_t * request = jsonrpc_request_create(method_name, proxy->request.id, param_info, args);
|
||||
va_end(args);
|
||||
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)
|
||||
@@ -136,7 +157,7 @@ void jsonrpc_proxy_invoke(
|
||||
proxy->request.id = 0;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
|
||||
jsonrpc_propate_error(finished, user_data, JSONRPC_BAD, "Bad");
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad");
|
||||
}
|
||||
|
||||
if (NULL != request)
|
||||
@@ -146,21 +167,18 @@ void jsonrpc_proxy_invoke(
|
||||
}
|
||||
else
|
||||
{
|
||||
jsonrpc_propate_error(finished, user_data, JSONRPC_BAD_BUSY, "Busy");
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_BUSY, "Busy");
|
||||
}
|
||||
}
|
||||
|
||||
extern void jsonrpc_proxy_notify(
|
||||
extern void jsonrpc_impl_proxy_notify(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
va_list args
|
||||
)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
json_t * request = jsonrpc_request_create(method_name, 0, param_info, args);
|
||||
va_end(args);
|
||||
json_t * request = jsonrpc_impl_request_create(method_name, 0, param_info, args);
|
||||
if (NULL != request)
|
||||
{
|
||||
proxy->send(request, proxy->user_data);
|
||||
@@ -169,12 +187,12 @@ extern void jsonrpc_proxy_notify(
|
||||
}
|
||||
|
||||
|
||||
void jsonrpc_proxy_onresult(
|
||||
void jsonrpc_impl_proxy_onresult(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
json_t * message)
|
||||
{
|
||||
struct jsonrpc_response response;
|
||||
jsonrpc_response_init(&response, message);
|
||||
jsonrpc_impl_response_init(&response, message);
|
||||
|
||||
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
|
||||
{
|
||||
@@ -190,6 +208,6 @@ void jsonrpc_proxy_onresult(
|
||||
finished(user_data, response.result, response.error);
|
||||
}
|
||||
|
||||
jsonrpc_response_cleanup(&response);
|
||||
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
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "jsonrpc/request.h"
|
||||
#include "webfuse/core/status_intern.h"
|
||||
#include "jsonrpc/impl/request.h"
|
||||
#include "jsonrpc/impl/error.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct jsonrpc_request
|
||||
@@ -9,7 +9,8 @@ struct jsonrpc_request
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
bool jsonrpc_is_request(
|
||||
bool
|
||||
jsonrpc_impl_is_request(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
@@ -21,7 +22,8 @@ bool jsonrpc_is_request(
|
||||
}
|
||||
|
||||
|
||||
struct jsonrpc_request * jsonrpc_request_create(
|
||||
struct jsonrpc_request *
|
||||
jsonrpc_impl_request_create(
|
||||
int id,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
@@ -37,20 +39,23 @@ struct jsonrpc_request * jsonrpc_request_create(
|
||||
return request;
|
||||
}
|
||||
|
||||
void jsonrpc_request_dispose(
|
||||
void
|
||||
jsonrpc_impl_request_dispose(
|
||||
struct jsonrpc_request * request)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
void * jsonrpc_request_get_userdata(
|
||||
void *
|
||||
jsonrpc_impl_request_get_userdata(
|
||||
struct jsonrpc_request * request)
|
||||
{
|
||||
return request->user_data;
|
||||
}
|
||||
|
||||
|
||||
void jsonrpc_respond(
|
||||
void
|
||||
jsonrpc_impl_respond(
|
||||
struct jsonrpc_request * request,
|
||||
json_t * result)
|
||||
{
|
||||
@@ -60,23 +65,20 @@ void jsonrpc_respond(
|
||||
|
||||
request->send(response, request->user_data);
|
||||
json_decref(response);
|
||||
jsonrpc_request_dispose(request);
|
||||
jsonrpc_impl_request_dispose(request);
|
||||
}
|
||||
|
||||
void jsonrpc_respond_error(
|
||||
void jsonrpc_impl_respond_error(
|
||||
struct jsonrpc_request * request,
|
||||
wf_status status)
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "code", json_integer(status));
|
||||
json_object_set_new(err, "message", json_string(wf_status_tostring(status)));
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "error", err);
|
||||
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_request_dispose(request);
|
||||
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
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "jsonrpc/response.h"
|
||||
#include "jsonrpc/error.h"
|
||||
#include "jsonrpc/impl/response.h"
|
||||
#include "jsonrpc/impl/error.h"
|
||||
#include "jsonrpc/status.h"
|
||||
|
||||
extern bool jsonrpc_is_response(
|
||||
bool
|
||||
jsonrpc_impl_is_response(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
@@ -14,7 +15,8 @@ extern bool jsonrpc_is_response(
|
||||
}
|
||||
|
||||
|
||||
void jsonrpc_response_init(
|
||||
void
|
||||
jsonrpc_impl_response_init(
|
||||
struct jsonrpc_response * result,
|
||||
json_t * response)
|
||||
{
|
||||
@@ -25,7 +27,7 @@ void jsonrpc_response_init(
|
||||
json_t * id_holder = json_object_get(response, "id");
|
||||
if ((NULL == id_holder) || (!json_is_integer(id_holder)))
|
||||
{
|
||||
result->error = jsonrpc_error(JSONRPC_BAD_FORMAT, "invalid format: missing id");
|
||||
result->error = jsonrpc_impl_error(JSONRPC_BAD_FORMAT, "invalid format: missing id");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -45,12 +47,13 @@ void jsonrpc_response_init(
|
||||
}
|
||||
else
|
||||
{
|
||||
result->error = jsonrpc_error(JSONRPC_BAD_FORMAT, "invalid format: invalid error object");
|
||||
result->error = jsonrpc_impl_error(JSONRPC_BAD_FORMAT, "invalid format: invalid error object");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void jsonrpc_response_cleanup(
|
||||
void
|
||||
jsonrpc_impl_response_cleanup(
|
||||
struct jsonrpc_response * response)
|
||||
{
|
||||
if (NULL != response->result)
|
||||
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
|
||||
@@ -1,95 +0,0 @@
|
||||
#include "jsonrpc/server.h"
|
||||
#include "jsonrpc/method.h"
|
||||
#include "jsonrpc/request.h"
|
||||
#include "webfuse/core/util.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void jsonrpc_server_init(
|
||||
struct jsonrpc_server * server)
|
||||
{
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
void jsonrpc_server_cleanup(
|
||||
struct jsonrpc_server * server)
|
||||
{
|
||||
struct jsonrpc_method * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
struct jsonrpc_method * next = current->next;
|
||||
jsonrpc_method_dispose(current);
|
||||
current = next;
|
||||
}
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
void jsonrpc_server_add(
|
||||
struct jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
struct jsonrpc_method * method = jsonrpc_method_create(method_name, invoke, user_data);
|
||||
method->next = server->methods;
|
||||
server->methods = method;
|
||||
}
|
||||
|
||||
static void jsonrpc_server_invalid_method_invoke(
|
||||
struct jsonrpc_request * request,
|
||||
char const * WF_UNUSED_PARAM(method_name),
|
||||
json_t * WF_UNUSED_PARAM(params),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED);
|
||||
}
|
||||
|
||||
static struct jsonrpc_method const jsonrpc_server_invalid_method =
|
||||
{
|
||||
.next = NULL,
|
||||
.name = "<invalid>",
|
||||
.invoke = &jsonrpc_server_invalid_method_invoke,
|
||||
.user_data = NULL
|
||||
};
|
||||
|
||||
static struct jsonrpc_method const * jsonrpc_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_server_invalid_method;
|
||||
}
|
||||
|
||||
void jsonrpc_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_request_create(id, send, user_data);
|
||||
struct jsonrpc_method const * method = jsonrpc_server_get_method(server, method_name);
|
||||
|
||||
method->invoke(request, method_name, params, method->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user