1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

chore: cleanup jsonrpc library

This commit is contained in:
Falk Werner 2020-02-29 02:32:03 +01:00
parent e3a3427ca8
commit 69a1faaa3f
38 changed files with 792 additions and 345 deletions

View File

@ -21,7 +21,7 @@ set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(C_WARNINGS -Wall -Wextra)
set(C_WARNINGS -Wall -Wextra -Werror)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
include_directories(

View File

@ -1,12 +1,13 @@
# jsonrpc
add_library(jsonrpc STATIC
lib/jsonrpc/proxy.c
lib/jsonrpc/server.c
lib/jsonrpc/method.c
lib/jsonrpc/request.c
lib/jsonrpc/response.c
lib/jsonrpc/error.c
lib/jsonrpc/api.c
lib/jsonrpc/impl/proxy.c
lib/jsonrpc/impl/server.c
lib/jsonrpc/impl/method.c
lib/jsonrpc/impl/request.c
lib/jsonrpc/impl/response.c
lib/jsonrpc/impl/error.c
)
target_include_directories(jsonrpc PUBLIC lib)

View File

@ -14,6 +14,7 @@ add_executable(alltests
test/jsonrpc/test_response.cc
test/jsonrpc/test_server.cc
test/jsonrpc/test_proxy.cc
test/jsonrpc/test_response_parser.cc
test/webfuse/utils/tempdir.cc
test/webfuse/utils/file_utils.cc
test/webfuse/utils/msleep.cc
@ -32,7 +33,6 @@ add_executable(alltests
test/webfuse/tests/core/test_status.cc
test/webfuse/tests/core/test_message.cc
test/webfuse/tests/core/test_message_queue.cc
test/webfuse/tests/adapter/test_response_parser.cc
test/webfuse/tests/adapter/test_server.cc
test/webfuse/tests/adapter/test_server_config.cc
test/webfuse/tests/adapter/test_timepoint.cc

14
include/jsonrpc.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef JSONRPC_H
#define JSONRPC_H
#include <jsonrpc/api.h>
#include <jsonrpc/method_invoke_fn.h>
#include <jsonrpc/proxy.h>
#include <jsonrpc/proxy_finished_fn.h>
#include <jsonrpc/request.h>
#include <jsonrpc/response.h>
#include <jsonrpc/send_fn.h>
#include <jsonrpc/server.h>
#include <jsonrpc/status.h>
#endif

8
include/jsonrpc/api.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef JSONRPC_API_H
#define JSONRPC_API_H
#ifndef JSONRPC_API
#define JSONRPC_API
#endif
#endif

View File

@ -0,0 +1,26 @@
#ifndef JSONRPC_METHOD_INVOKE_FN_H
#define JSONRPC_METHOD_INVOKE_FN_H
#include <jsonrpc/api.h>
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct jsonrpc_request;
typedef void jsonrpc_method_invoke_fn(
struct jsonrpc_request * request,
char const * method_name,
json_t * params,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -12,7 +12,10 @@ using std::size_t;
#endif
#include <jansson.h>
#include "jsonrpc/send_fn.h"
#include <jsonrpc/api.h>
#include <jsonrpc/send_fn.h>
#include <jsonrpc/proxy_finished_fn.h>
#include "webfuse/adapter/impl/time/timeout_manager.h"
#include "webfuse/adapter/impl/time/timer.h"
@ -20,40 +23,20 @@ using std::size_t;
extern "C" {
#endif
typedef void jsonrpc_proxy_finished_fn(
void * user_data,
json_t const * result,
json_t const * error);
struct jsonrpc_proxy;
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_proxy_init(
struct jsonrpc_proxy * proxy,
extern JSONRPC_API struct jsonrpc_proxy *
jsonrpc_proxy_create(
struct wf_impl_timeout_manager * manager,
int timeout,
jsonrpc_send_fn * send,
void * user_data);
extern void jsonrpc_proxy_cleanup(
extern JSONRPC_API void jsonrpc_proxy_dispose(
struct jsonrpc_proxy * proxy);
extern void jsonrpc_proxy_invoke(
extern JSONRPC_API void jsonrpc_proxy_invoke(
struct jsonrpc_proxy * proxy,
jsonrpc_proxy_finished_fn * finished,
void * user_data,
@ -62,14 +45,14 @@ extern void jsonrpc_proxy_invoke(
...
);
extern void jsonrpc_proxy_notify(
extern JSONRPC_API void jsonrpc_proxy_notify(
struct jsonrpc_proxy * proxy,
char const * method_name,
char const * param_info,
...
);
extern void jsonrpc_proxy_onresult(
extern JSONRPC_API void jsonrpc_proxy_onresult(
struct jsonrpc_proxy * proxy,
json_t * message);

View File

@ -0,0 +1,20 @@
#ifndef JSONRPC_PROXY_FINISHED_FN_H
#define JSONRPC_PROXY_FINISHED_FN_H
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef void jsonrpc_proxy_finished_fn(
void * user_data,
json_t const * result,
json_t const * error);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -12,7 +12,7 @@ using std::size_t;
#endif
#include <jansson.h>
#include "webfuse/core/status.h"
#include <jsonrpc/api.h>
#include "jsonrpc/send_fn.h"
#ifdef __cplusplus
@ -22,27 +22,28 @@ extern "C"
struct jsonrpc_request;
extern bool jsonrpc_is_request(
extern JSONRPC_API bool jsonrpc_is_request(
json_t * message);
extern struct jsonrpc_request * jsonrpc_request_create(
extern JSONRPC_API struct jsonrpc_request * jsonrpc_request_create(
int id,
jsonrpc_send_fn * send,
void * user_data);
extern void jsonrpc_request_dispose(
extern JSONRPC_API void jsonrpc_request_dispose(
struct jsonrpc_request * request);
extern void * jsonrpc_request_get_userdata(
extern JSONRPC_API void * jsonrpc_request_get_userdata(
struct jsonrpc_request * request);
extern void jsonrpc_respond(
extern JSONRPC_API void jsonrpc_respond(
struct jsonrpc_request * request,
json_t * result);
extern void jsonrpc_respond_error(
extern JSONRPC_API void jsonrpc_respond_error(
struct jsonrpc_request * request,
wf_status status);
int code,
char const * message);
#ifdef __cplusplus

View File

@ -1,40 +1,23 @@
#ifndef WF_JSONRPC_RESPONSE_H
#define WF_JSONRPC_RESPONSE_H
#ifndef JSONRPC_RESPONSE_H
#define JSONRPC_RESPONSE_H
#ifndef __cplusplus
#include <stdbool.h>
#include <stddef.h>
#else
#include <cstddef>
using std::size_t;
#endif
#include <jansson.h>
#include <jsonrpc/api.h>
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
struct jsonrpc_response
{
json_t * result;
json_t * error;
int id;
};
extern bool jsonrpc_is_response(
extern JSONRPC_API bool jsonrpc_is_response(
json_t * message);
extern void jsonrpc_response_init(
struct jsonrpc_response * response,
json_t * message);
extern void jsonrpc_response_cleanup(
struct jsonrpc_response * response);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -6,6 +6,7 @@
#endif
#include <jansson.h>
#include <jsonrpc/api.h>
#ifdef __cplusplus
extern "C"

View File

@ -9,32 +9,31 @@
#endif
#include <jansson.h>
#include "jsonrpc/send_fn.h"
#include "jsonrpc/method.h"
#include <jsonrpc/api.h>
#include <jsonrpc/send_fn.h>
#include <jsonrpc/method_invoke_fn.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct jsonrpc_server
{
struct jsonrpc_method * methods;
};
struct jsonrpc_server;
extern void jsonrpc_server_init(
extern JSONRPC_API struct jsonrpc_server *
jsonrpc_server_create(void);
extern JSONRPC_API void
jsonrpc_server_dispose(
struct jsonrpc_server * server);
extern void jsonrpc_server_cleanup(
struct jsonrpc_server * server);
extern void jsonrpc_server_add(
extern JSONRPC_API void jsonrpc_server_add(
struct jsonrpc_server * server,
char const * method_name,
jsonrpc_method_invoke_fn * invoke,
void * user_data);
extern void jsonrpc_server_process(
extern JSONRPC_API void jsonrpc_server_process(
struct jsonrpc_server * server,
json_t * request,
jsonrpc_send_fn * send,

View File

@ -3,6 +3,7 @@
#define JSONRPC_GOOD 0
#define JSONRPC_BAD -1
#define JSONRPC_BAD_NOTIMPLEMENTED -2
#define JSONRPC_BAD_TIMEOUT -3
#define JSONRPC_BAD_BUSY -4
#define JSONRPC_BAD_FORMAT -5

144
lib/jsonrpc/api.c Normal file
View 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);
}

View File

@ -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);

View File

@ -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,

View File

@ -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);

View File

@ -1,21 +1,13 @@
#ifndef JSONRPC_METHOD_H
#define JSONRPC_METHOD_H
#ifndef JSONRPC_IMPL_METHOD_H
#define JSONRPC_IMPL_METHOD_H
#include <jansson.h>
#include "jsonrpc/method_invoke_fn.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct jsonrpc_request;
typedef void jsonrpc_method_invoke_fn(
struct jsonrpc_request * request,
char const * method_name,
json_t * params,
void * user_data);
struct jsonrpc_method
{
struct jsonrpc_method * next;
@ -24,12 +16,14 @@ struct jsonrpc_method
void * user_data;
};
extern struct jsonrpc_method * jsonrpc_method_create(
extern struct jsonrpc_method *
jsonrpc_impl_method_create(
char const * method_name,
jsonrpc_method_invoke_fn * invoke,
void * user_data);
extern void jsonrpc_method_dispose(
extern void
jsonrpc_impl_method_dispose(
struct jsonrpc_method * method);
#ifdef __cplusplus

View File

@ -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
View 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

View File

@ -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);
}

View 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

View File

@ -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)

View 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
View 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
View 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

View 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

View File

@ -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);
}
}

View File

@ -11,7 +11,7 @@ struct jsonrpc_proxy * wf_impl_operations_context_get_proxy(
struct wf_impl_session * session = context->session;
if (NULL != session)
{
proxy = &session->rpc;
proxy = session->rpc;
}
return proxy;

View File

@ -11,6 +11,7 @@
#include "webfuse/adapter/impl/credentials.h"
#include "jsonrpc/request.h"
#include "webfuse/adapter/impl/uuid_mountpoint_factory.h"
#include "webfuse/core/status_intern.h"
static int wf_impl_server_protocol_callback(
struct lws * wsi,
@ -42,7 +43,7 @@ static int wf_impl_server_protocol_callback(
&protocol->authenticators,
&protocol->mountpoint_factory,
&protocol->timeout_manager,
&protocol->server);
protocol->server);
if (NULL != session)
{
@ -159,7 +160,7 @@ static void wf_impl_server_protocol_authenticate(
}
else
{
jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED);
jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED, wf_status_tostring(WF_BAD_ACCESS_DENIED));
}
}
@ -222,7 +223,7 @@ static void wf_impl_server_protocol_add_filesystem(
}
else
{
jsonrpc_respond_error(request, status);
jsonrpc_respond_error(request, status, wf_status_tostring(status));
}
@ -240,9 +241,9 @@ void wf_impl_server_protocol_init(
wf_impl_session_manager_init(&protocol->session_manager);
wf_impl_authenticators_init(&protocol->authenticators);
jsonrpc_server_init(&protocol->server);
jsonrpc_server_add(&protocol->server, "authenticate", &wf_impl_server_protocol_authenticate, protocol);
jsonrpc_server_add(&protocol->server, "add_filesystem", &wf_impl_server_protocol_add_filesystem, protocol);
protocol->server = jsonrpc_server_create();
jsonrpc_server_add(protocol->server, "authenticate", &wf_impl_server_protocol_authenticate, protocol);
jsonrpc_server_add(protocol->server, "add_filesystem", &wf_impl_server_protocol_add_filesystem, protocol);
}
void wf_impl_server_protocol_cleanup(
@ -250,7 +251,7 @@ void wf_impl_server_protocol_cleanup(
{
protocol->is_operational = false;
jsonrpc_server_cleanup(&protocol->server);
jsonrpc_server_dispose(protocol->server);
wf_impl_timeout_manager_cleanup(&protocol->timeout_manager);
wf_impl_authenticators_cleanup(&protocol->authenticators);
wf_impl_session_manager_cleanup(&protocol->session_manager);

View File

@ -25,7 +25,7 @@ struct wf_server_protocol
struct wf_impl_authenticators authenticators;
struct wf_impl_mountpoint_factory mountpoint_factory;
struct wf_impl_session_manager session_manager;
struct jsonrpc_server server;
struct jsonrpc_server * server;
bool is_operational;
};

View File

@ -59,7 +59,7 @@ struct wf_impl_session * wf_impl_session_create(
session->authenticators = authenticators;
session->server = server;
session->mountpoint_factory = mountpoint_factory;
jsonrpc_proxy_init(&session->rpc, timeout_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session);
session->rpc = jsonrpc_proxy_create(timeout_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session);
wf_slist_init(&session->messages);
}
@ -83,15 +83,10 @@ static void wf_impl_session_dispose_filesystems(
void wf_impl_session_dispose(
struct wf_impl_session * session)
{
jsonrpc_proxy_cleanup(&session->rpc);
jsonrpc_proxy_dispose(session->rpc);
wf_message_queue_cleanup(&session->messages);
wf_impl_session_dispose_filesystems(&session->filesystems);
session->is_authenticated = false;
session->wsi = NULL;
session->authenticators = NULL;
session->mountpoint_factory = NULL;
session->server = NULL;
free(session);
}
@ -161,7 +156,7 @@ void wf_impl_session_receive(
{
if (jsonrpc_is_response(message))
{
jsonrpc_proxy_onresult(&session->rpc, message);
jsonrpc_proxy_onresult(session->rpc, message);
}
else if (jsonrpc_is_request(message))
{

View File

@ -36,7 +36,7 @@ struct wf_impl_session
struct wf_impl_authenticators * authenticators;
struct wf_impl_mountpoint_factory * mountpoint_factory;
struct jsonrpc_server * server;
struct jsonrpc_proxy rpc;
struct jsonrpc_proxy * rpc;
struct wf_slist filesystems;
};

View File

@ -92,10 +92,9 @@ TEST(jsonrpc_proxy, init)
SendContext context;
void * user_data = reinterpret_cast<void*>(&context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
ASSERT_FALSE(context.is_called);
@ -108,12 +107,11 @@ TEST(jsonrpc_proxy, invoke)
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
@ -135,7 +133,7 @@ TEST(jsonrpc_proxy, invoke)
ASSERT_FALSE(finished_context.is_called);
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
ASSERT_TRUE(finished_context.is_called);
@ -149,12 +147,11 @@ TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails)
SendContext send_context(false);
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
@ -162,7 +159,7 @@ TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails)
ASSERT_TRUE(finished_context.is_called);
ASSERT_FALSE(nullptr == finished_context.error);
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
@ -173,16 +170,15 @@ TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
FinishedContext finished_context2;
void * finished_data2 = reinterpret_cast<void*>(&finished_context2);
jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data2, "foo", "");
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", "");
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
@ -192,7 +188,7 @@ TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
ASSERT_TRUE(finished_context2.is_called);
ASSERT_EQ(WF_BAD_BUSY, wf_impl_jsonrpc_get_status(finished_context2.error));
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
@ -203,19 +199,18 @@ TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid)
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "?", "error");
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "?", "error");
ASSERT_FALSE(send_context.is_called);
ASSERT_TRUE(finished_context.is_called);
ASSERT_EQ(WF_BAD, wf_impl_jsonrpc_get_status(finished_context.error));
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
@ -226,12 +221,11 @@ TEST(jsonrpc_proxy, on_result)
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
@ -243,7 +237,7 @@ TEST(jsonrpc_proxy, on_result)
json_object_set_new(response, "result", json_string("okay"));
json_object_set(response, "id", id);
jsonrpc_proxy_onresult(&proxy, response);
jsonrpc_proxy_onresult(proxy, response);
json_decref(response);
ASSERT_TRUE(finished_context.is_called);
@ -251,7 +245,7 @@ TEST(jsonrpc_proxy, on_result)
ASSERT_TRUE(json_is_string(finished_context.result));
ASSERT_STREQ("okay", json_string_value(finished_context.result));
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
@ -262,12 +256,11 @@ TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id)
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
@ -279,12 +272,12 @@ TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id)
json_object_set_new(response, "result", json_string("okay"));
json_object_set_new(response, "id", json_integer(1 + json_integer_value(id)));
jsonrpc_proxy_onresult(&proxy, response);
jsonrpc_proxy_onresult(proxy, response);
json_decref(response);
ASSERT_FALSE(finished_context.is_called);
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
@ -295,12 +288,11 @@ TEST(jsonrpc_proxy, timeout)
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, 0, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 0, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
@ -311,7 +303,7 @@ TEST(jsonrpc_proxy, timeout)
ASSERT_TRUE(finished_context.is_called);
ASSERT_EQ(WF_BAD_TIMEOUT, wf_impl_jsonrpc_get_status(finished_context.error));
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
@ -322,12 +314,11 @@ TEST(jsonrpc_proxy, cleanup_pending_request)
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, 10, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 10, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
@ -335,7 +326,7 @@ TEST(jsonrpc_proxy, cleanup_pending_request)
ASSERT_FALSE(finished_context.is_called);
ASSERT_NE(nullptr, timeout_manager.timers);
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
ASSERT_TRUE(finished_context.is_called);
ASSERT_EQ(nullptr, timeout_manager.timers);
@ -352,10 +343,9 @@ TEST(jsonrpc_proxy, notify)
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
jsonrpc_proxy_notify(&proxy, "foo", "si", "bar", 42);
jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
@ -376,7 +366,7 @@ TEST(jsonrpc_proxy, notify)
ASSERT_EQ(nullptr, id);
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
@ -387,13 +377,12 @@ TEST(jsonrpc_proxy, notify_dont_send_invalid_request)
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct jsonrpc_proxy proxy;
jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
jsonrpc_proxy_notify(&proxy, "foo", "?");
jsonrpc_proxy_notify(proxy, "foo", "?");
ASSERT_FALSE(send_context.is_called);
jsonrpc_proxy_cleanup(&proxy);
jsonrpc_proxy_dispose(proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}

View File

@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include "jsonrpc/request.h"
#include "jsonrpc/status.h"
namespace
{
@ -73,7 +74,7 @@ TEST(jsonrpc_request, respond_error)
struct jsonrpc_request * request =
jsonrpc_request_create(42, &jsonrpc_send, user_data);
jsonrpc_respond_error(request, WF_BAD);
jsonrpc_respond_error(request, JSONRPC_BAD, "Bad");
ASSERT_NE(nullptr, context.response);
@ -92,7 +93,7 @@ TEST(jsonrpc_request, respond_error)
json_t * err_code = json_object_get(err, "code");
ASSERT_TRUE(json_is_integer(err_code));
ASSERT_EQ(WF_BAD, json_integer_value(err_code));
ASSERT_EQ(JSONRPC_BAD, json_integer_value(err_code));
json_t * err_message = json_object_get(err, "message");
ASSERT_TRUE(json_is_string(err_message));

View File

@ -1,5 +1,5 @@
#include <gtest/gtest.h>
#include "jsonrpc/response.h"
#include "jsonrpc/impl/response.h"
#include "webfuse/core/status.h"
#include "webfuse/core/json_util.h"
@ -10,14 +10,14 @@ TEST(json_response, init_result)
json_object_set_new(message, "id", json_integer(11));
struct jsonrpc_response response;
jsonrpc_response_init(&response, message);
jsonrpc_impl_response_init(&response, message);
ASSERT_EQ(nullptr, response.error);
ASSERT_TRUE(json_is_integer(response.result));
ASSERT_EQ(47, json_integer_value(response.result));
ASSERT_EQ(11, response.id);
jsonrpc_response_cleanup(&response);
jsonrpc_impl_response_cleanup(&response);
json_decref(message);
}
@ -31,13 +31,13 @@ TEST(json_response, init_error)
json_object_set_new(message, "id", json_integer(23));
struct jsonrpc_response response;
jsonrpc_response_init(&response, message);
jsonrpc_impl_response_init(&response, message);
ASSERT_EQ(WF_BAD_ACCESS_DENIED, wf_impl_jsonrpc_get_status(response.error)) << json_string_value(json_object_get(response.error, "message"));
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id);
jsonrpc_response_cleanup(&response);
jsonrpc_impl_response_cleanup(&response);
json_decref(message);
}
@ -47,12 +47,12 @@ TEST(json_response, init_format_error)
json_object_set_new(message, "id", json_integer(12));
struct jsonrpc_response response;
jsonrpc_response_init(&response, message);
jsonrpc_impl_response_init(&response, message);
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_get_status(response.error));
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(12, response.id);
jsonrpc_response_cleanup(&response);
jsonrpc_impl_response_cleanup(&response);
json_decref(message);
}

View File

@ -1,7 +1,7 @@
#include <string>
#include <gtest/gtest.h>
#include "jsonrpc/response.h"
#include "jsonrpc/impl/response.h"
#include "webfuse/core/json_util.h"
@ -12,7 +12,7 @@ static void response_parse_str(
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
if (nullptr != message)
{
jsonrpc_response_init(response, message);
jsonrpc_impl_response_init(response, message);
json_decref(message);
}
}
@ -26,21 +26,21 @@ TEST(response_parser, test)
ASSERT_NE(nullptr, response.error);
ASSERT_EQ(-1, response.id);
ASSERT_EQ(nullptr, response.result);
jsonrpc_response_cleanup(&response);
jsonrpc_impl_response_cleanup(&response);
// empty
response_parse_str("{}", &response);
ASSERT_NE(nullptr, response.error);
ASSERT_EQ(-1, response.id);
ASSERT_EQ(nullptr, response.result);
jsonrpc_response_cleanup(&response);
jsonrpc_impl_response_cleanup(&response);
// no data
response_parse_str("{\"id\":42}", &response);
ASSERT_NE(nullptr, response.error);
ASSERT_EQ(42, response.id);
ASSERT_EQ(nullptr, response.result);
jsonrpc_response_cleanup(&response);
jsonrpc_impl_response_cleanup(&response);
// custom error code
response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response);
@ -48,12 +48,12 @@ TEST(response_parser, test)
ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code")));
ASSERT_EQ(42, response.id);
ASSERT_EQ(nullptr, response.result);
jsonrpc_response_cleanup(&response);
jsonrpc_impl_response_cleanup(&response);
// valid response
response_parse_str("{\"result\": true, \"id\": 42}", &response);
ASSERT_EQ(WF_GOOD, response.error);
ASSERT_EQ(42, response.id);
ASSERT_NE(nullptr, response.result);
jsonrpc_response_cleanup(&response);
jsonrpc_impl_response_cleanup(&response);
}

View File

@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include "jsonrpc/server.h"
#include "jsonrpc/request.h"
#include "jsonrpc/status.h"
namespace
{
@ -40,9 +41,8 @@ namespace
TEST(jsonrpc_server, process_request)
{
struct jsonrpc_server server;
jsonrpc_server_init(&server);
jsonrpc_server_add(&server, "sayHello", &sayHello, nullptr);
struct jsonrpc_server * server = jsonrpc_server_create();
jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
Context context{nullptr, false};
void * user_data = reinterpret_cast<void*>(&context);
@ -50,7 +50,7 @@ TEST(jsonrpc_server, process_request)
json_object_set_new(request, "method", json_string("sayHello"));
json_object_set_new(request, "params", json_array());
json_object_set_new(request, "id", json_integer(23));
jsonrpc_server_process(&server, request, &jsonrpc_send, user_data);
jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_TRUE(context.is_called);
ASSERT_NE(nullptr, context.response);
@ -66,14 +66,13 @@ TEST(jsonrpc_server, process_request)
json_decref(context.response);
json_decref(request);
jsonrpc_server_cleanup(&server);
jsonrpc_server_dispose(server);
}
TEST(jsonrpc_server, invoke_unknown_method)
{
struct jsonrpc_server server;
jsonrpc_server_init(&server);
jsonrpc_server_add(&server, "sayHello", &sayHello, nullptr);
struct jsonrpc_server * server = jsonrpc_server_create();
jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
Context context{nullptr, false};
void * user_data = reinterpret_cast<void*>(&context);
@ -81,7 +80,7 @@ TEST(jsonrpc_server, invoke_unknown_method)
json_object_set_new(request, "method", json_string("greet"));
json_object_set_new(request, "params", json_array());
json_object_set_new(request, "id", json_integer(42));
jsonrpc_server_process(&server, request, &jsonrpc_send, user_data);
jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_TRUE(context.is_called);
ASSERT_NE(nullptr, context.response);
@ -96,30 +95,29 @@ TEST(jsonrpc_server, invoke_unknown_method)
json_t * err_code = json_object_get(err, "code");
ASSERT_TRUE(json_is_integer(err_code));
ASSERT_EQ(WF_BAD_NOTIMPLEMENTED, json_integer_value(err_code));
ASSERT_EQ(JSONRPC_BAD_NOTIMPLEMENTED, json_integer_value(err_code));
json_t * err_message = json_object_get(err, "message");
ASSERT_TRUE(json_is_string(err_message));
json_decref(context.response);
json_decref(request);
jsonrpc_server_cleanup(&server);
jsonrpc_server_dispose(server);
}
TEST(jsonrpc_server, skip_invalid_request)
{
struct jsonrpc_server server;
jsonrpc_server_init(&server);
struct jsonrpc_server * server = jsonrpc_server_create();
Context context{nullptr, false};
void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object();
json_object_set_new(request, "method", json_string("sayHello"));
json_object_set_new(request, "params", json_array());
jsonrpc_server_process(&server, request, &jsonrpc_send, user_data);
jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_FALSE(context.is_called);
json_decref(request);
jsonrpc_server_cleanup(&server);
jsonrpc_server_dispose(server);
}