initial commit
This commit is contained in:
33
3rd_party/jsonrpcpp-1.1.1/.gitignore
vendored
Normal file
33
3rd_party/jsonrpcpp-1.1.1/.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
jsonrpctest
|
||||
.vscode
|
||||
build
|
||||
29
3rd_party/jsonrpcpp-1.1.1/.travis.yml
vendored
Normal file
29
3rd_party/jsonrpcpp-1.1.1/.travis.yml
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
language: cpp
|
||||
dist: trusty
|
||||
sudo: required
|
||||
group: edge
|
||||
|
||||
compiler:
|
||||
- gcc
|
||||
matrix:
|
||||
include:
|
||||
# build on ubuntu
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
env:
|
||||
- MATRIX_EVAL="CC=gcc-4.9 && CXX=g++-4.9"
|
||||
addons:
|
||||
apt:
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
packages:
|
||||
- g++-4.9
|
||||
# build on osx
|
||||
- os: osx
|
||||
osx_image: xcode9.1
|
||||
before_install:
|
||||
- eval "${MATRIX_EVAL}"
|
||||
script:
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake .. && make
|
||||
65
3rd_party/jsonrpcpp-1.1.1/CMakeLists.txt
vendored
Normal file
65
3rd_party/jsonrpcpp-1.1.1/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
cmake_minimum_required(VERSION 3.0.0)
|
||||
|
||||
project(jsonrpcpp VERSION 1.1.1 LANGUAGES CXX)
|
||||
set(PROJECT_DESCRIPTION "C++ JSON-RPC 2.0 library")
|
||||
set(PROJECT_URL "https://github.com/badaix/jsonrpcpp")
|
||||
|
||||
option(BUILD_SHARED_LIBS "Build jsonrpcpp as a shared library" ON)
|
||||
option(BUILD_STATIC_LIBS "Build jsonrpcpp as a static library" ON)
|
||||
option(BUILD_TESTS "Build tests (run tests with make test)" ON)
|
||||
|
||||
if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
|
||||
message(FATAL_ERROR "One or both of BUILD_SHARED_LIBS or BUILD_STATIC_LIBS"
|
||||
"must be set to ON to build")
|
||||
endif()
|
||||
|
||||
if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
|
||||
SET(CMAKE_INSTALL_LIBDIR lib CACHE PATH "Output directory for libraries")
|
||||
endif()
|
||||
|
||||
if(NOT DEFINED CMAKE_INSTALL_INCLUDEDIR)
|
||||
SET(CMAKE_INSTALL_INCLUDEDIR include CACHE
|
||||
PATH "Output directory for header files")
|
||||
endif()
|
||||
|
||||
include_directories(lib)
|
||||
set(JSONRPCPP_SOURCES lib/jsonrp.cpp)
|
||||
|
||||
if (BUILD_SHARED_LIBS)
|
||||
add_library(jsonrpcpp SHARED "${JSONRPCPP_SOURCES}")
|
||||
target_compile_features(jsonrpcpp PUBLIC cxx_std_11)
|
||||
if(WIN32)
|
||||
install(TARGETS jsonrpcpp RUNTIME DESTINATION "${CMAKE_INSTALL_LIBDIR}")
|
||||
else()
|
||||
install(TARGETS jsonrpcpp LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
|
||||
endif()
|
||||
|
||||
endif (BUILD_SHARED_LIBS)
|
||||
|
||||
if (BUILD_STATIC_LIBS)
|
||||
add_library(jsonrpcpp-static STATIC "${JSONRPCPP_SOURCES}")
|
||||
set_target_properties(jsonrpcpp-static PROPERTIES OUTPUT_NAME jsonrpcpp)
|
||||
target_compile_features(jsonrpcpp-static PUBLIC cxx_std_11)
|
||||
install(TARGETS jsonrpcpp-static ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")
|
||||
endif (BUILD_STATIC_LIBS)
|
||||
|
||||
if (BUILD_TESTS)
|
||||
if (NOT BUILD_STATIC_LIBS)
|
||||
message(FATAL_ERROR "Tests can only be built against static libraries "
|
||||
"(set BUILD_STATIC_LIBS=ON)")
|
||||
endif (NOT BUILD_STATIC_LIBS)
|
||||
add_executable(jsonrpctest jsonrpctest.cpp)
|
||||
target_link_libraries(jsonrpctest jsonrpcpp-static)
|
||||
target_compile_features(jsonrpctest PUBLIC cxx_std_11)
|
||||
endif (BUILD_TESTS)
|
||||
|
||||
install(FILES lib/jsonrp.hpp lib/json.hpp
|
||||
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/jsonrpcpp")
|
||||
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/jsonrpcpp.pc.cmake"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/jsonrpcpp.pc"
|
||||
@ONLY)
|
||||
|
||||
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/jsonrpcpp.pc"
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
|
||||
21
3rd_party/jsonrpcpp-1.1.1/LICENSE
vendored
Normal file
21
3rd_party/jsonrpcpp-1.1.1/LICENSE
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Johannes Pohl
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
19
3rd_party/jsonrpcpp-1.1.1/Makefile
vendored
Normal file
19
3rd_party/jsonrpcpp-1.1.1/Makefile
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
BIN = jsonrpctest
|
||||
|
||||
CXX = clang++
|
||||
STRIP = strip
|
||||
CXXFLAGS = -std=c++0x -Wall -O3 -Ilib -isystem lib/externals
|
||||
|
||||
OBJ = jsonrpctest.o lib/jsonrp.o
|
||||
|
||||
|
||||
all: $(OBJ)
|
||||
$(CXX) $(CXXFLAGS) -o $(BIN) $(OBJ) $(LDFLAGS)
|
||||
$(STRIP) $(BIN)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -rf $(BIN) $(OBJ) *~
|
||||
|
||||
75
3rd_party/jsonrpcpp-1.1.1/README.md
vendored
Normal file
75
3rd_party/jsonrpcpp-1.1.1/README.md
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
# jsonrpc++
|
||||
|
||||
C++ [JSON-RPC 2.0](http://www.jsonrpc.org/specification) library
|
||||
|
||||
[](https://travis-ci.org/badaix/jsonrpcpp)
|
||||
[](https://github.com/badaix/jsonrpcpp/releases)
|
||||
|
||||
When grown up, this will be a leightweight JSON-RPC 2.0 C++ library.
|
||||
|
||||
### What it is
|
||||
jsonrpc++ parses and constructs JSON RPC 2.0 objects, like
|
||||
* [Request](http://www.jsonrpc.org/specification#request_object)
|
||||
* [Notification](http://www.jsonrpc.org/specification#notification)
|
||||
* [Parameter](http://www.jsonrpc.org/specification#parameter_structures)
|
||||
* [Response](http://www.jsonrpc.org/specification#response_object)
|
||||
* [Error](http://www.jsonrpc.org/specification#error_object)
|
||||
* [Batch](http://www.jsonrpc.org/specification#batch)
|
||||
|
||||
|
||||
#### Example: Parsing a request
|
||||
````c++
|
||||
jsonrpcpp::entity_ptr entity = jsonrpcpp::Parser::parse(R"({"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3})");
|
||||
if (entity->is_request())
|
||||
{
|
||||
jsonrpcpp::request_ptr request = dynamic_pointer_cast<jsonrpcpp::Request>(entity);
|
||||
if (request->method == "subtract")
|
||||
{
|
||||
int result = request->params.get<int>("minuend") - request->params.get<int>("subtrahend");
|
||||
jsonrpcpp::Response response(*request, result);
|
||||
cout << " Response: " << response.to_json().dump() << "\n";
|
||||
//will print: {"jsonrpc": "2.0", "result": 19, "id": 3}
|
||||
}
|
||||
else
|
||||
throw jsonrpcpp::MethodNotFoundException(*request);
|
||||
}
|
||||
````
|
||||
|
||||
### What it not is
|
||||
jsonrpc++ is completely transport agnostic, i.e. it doesn't care about transportation of the messages and there are no TCP client or server components shipped with this lib.
|
||||
|
||||
As JSON backbone [JSON for Modern C++](https://nlohmann.github.io/json/) is used.
|
||||
|
||||
|
||||
## Some code
|
||||
````c++
|
||||
jsonrpcpp::entity_ptr entity = jsonrpcpp::Parser::parse(R"({"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3})");
|
||||
if (entity && entity->is_request())
|
||||
{
|
||||
jsonrpcpp::request_ptr request = dynamic_pointer_cast<jsonrpcpp::Request>(entity);
|
||||
cout << " Request: " << request->method << ", id: " << request->id << ", has params: " << !request->params.is_null() << "\n";
|
||||
if (request->method == "subtract")
|
||||
{
|
||||
int result;
|
||||
if (request->params.is_array())
|
||||
result = request->params.get<int>(0) - request->params.get<int>(1);
|
||||
else
|
||||
result = request->params.get<int>("minuend") - request->params.get<int>("subtrahend");
|
||||
|
||||
jsonrpcpp::Response response(*request, result);
|
||||
cout << " Response: " << response.to_json().dump() << "\n";
|
||||
}
|
||||
else if (request->method == "sum")
|
||||
{
|
||||
int result = 0;
|
||||
for (const auto& summand: request->params.param_array)
|
||||
result += summand.get<int>();
|
||||
jsonrpcpp::Response response(*request, result);
|
||||
cout << " Response: " << response.to_json().dump() << "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
throw jsonrpcpp::MethodNotFoundException(*request);
|
||||
}
|
||||
}
|
||||
````
|
||||
12
3rd_party/jsonrpcpp-1.1.1/cmake/jsonrpcpp.pc.cmake
vendored
Normal file
12
3rd_party/jsonrpcpp-1.1.1/cmake/jsonrpcpp.pc.cmake
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
prefix=@CMAKE_INSTALL_PREFIX@
|
||||
exec_prefix=${prefix}
|
||||
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
|
||||
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
|
||||
|
||||
Name: @PROJECT_NAME@
|
||||
Description: @PROJECT_DESCRIPTION@
|
||||
URL: @PROJECT_URL@
|
||||
Version: @PROJECT_VERSION@
|
||||
|
||||
Libs: -L${libdir} -ljsonrpcpp
|
||||
Cflags: -I${includedir}
|
||||
244
3rd_party/jsonrpcpp-1.1.1/jsonrpctest.cpp
vendored
Normal file
244
3rd_party/jsonrpcpp-1.1.1/jsonrpctest.cpp
vendored
Normal file
@@ -0,0 +1,244 @@
|
||||
/***
|
||||
This file is part of jsonrpc++
|
||||
Copyright (C) 2017 Johannes Pohl
|
||||
|
||||
This software may be modified and distributed under the terms
|
||||
of the MIT license. See the LICENSE file for details.
|
||||
***/
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include "jsonrp.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
jsonrpcpp::Parser parser;
|
||||
|
||||
|
||||
jsonrpcpp::Response getRespone(jsonrpcpp::request_ptr request)
|
||||
{
|
||||
//cout << " Request: " << request->method << ", id: " << request->id << ", has params: " << !request->params.is_null() << "\n";
|
||||
if (request->method == "subtract")
|
||||
{
|
||||
if (request->params)
|
||||
{
|
||||
int result;
|
||||
if (request->params.is_array())
|
||||
result = request->params.get<int>(0) - request->params.get<int>(1);
|
||||
else
|
||||
result = request->params.get<int>("minuend") - request->params.get<int>("subtrahend");
|
||||
|
||||
return jsonrpcpp::Response(*request, result);
|
||||
}
|
||||
else
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
else if (request->method == "sum")
|
||||
{
|
||||
int result = 0;
|
||||
for (const auto& summand: request->params.param_array)
|
||||
result += summand.get<int>();
|
||||
return jsonrpcpp::Response(*request, result);
|
||||
}
|
||||
else if (request->method == "get_data")
|
||||
{
|
||||
return jsonrpcpp::Response(*request, Json({"hello", 5}));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw jsonrpcpp::MethodNotFoundException(*request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void test(const std::string& json_str)
|
||||
{
|
||||
try
|
||||
{
|
||||
cout << "--> " << json_str << "\n";
|
||||
jsonrpcpp::entity_ptr entity = parser.parse(json_str);
|
||||
if (entity)
|
||||
{
|
||||
//cout << " Json: " << entity->to_json().dump() << "\n";
|
||||
if (entity->is_response())
|
||||
{
|
||||
cout << "<-- " << entity->to_json().dump() << "\n";
|
||||
}
|
||||
if (entity->is_request())
|
||||
{
|
||||
jsonrpcpp::Response response = getRespone(dynamic_pointer_cast<jsonrpcpp::Request>(entity));
|
||||
cout << "<-- " << response.to_json().dump() << "\n";
|
||||
}
|
||||
else if (entity->is_notification())
|
||||
{
|
||||
jsonrpcpp::notification_ptr notification = dynamic_pointer_cast<jsonrpcpp::Notification>(entity);
|
||||
cout << "Notification: " << notification->method << ", has params: " << !notification->params.is_null() << "\n";
|
||||
}
|
||||
else if (entity->is_batch())
|
||||
{
|
||||
jsonrpcpp::batch_ptr batch = dynamic_pointer_cast<jsonrpcpp::Batch>(entity);
|
||||
jsonrpcpp::Batch responseBatch;
|
||||
//cout << " Batch\n";
|
||||
for (const auto& batch_entity: batch->entities)
|
||||
{
|
||||
//cout << batch_entity->type_str() << ": \t" << batch_entity->to_json() << "\n";
|
||||
if (batch_entity->is_request())
|
||||
{
|
||||
try
|
||||
{
|
||||
jsonrpcpp::Response response = getRespone(dynamic_pointer_cast<jsonrpcpp::Request>(batch_entity));
|
||||
responseBatch.add(response); //<jsonrpcpp::Response>
|
||||
}
|
||||
catch(const jsonrpcpp::RequestException& e)
|
||||
{
|
||||
responseBatch.add(e); //<jsonrpcpp::RequestException>
|
||||
}
|
||||
}
|
||||
else if (batch_entity->is_exception())
|
||||
{
|
||||
responseBatch.add_ptr(batch_entity);
|
||||
}
|
||||
else if (batch_entity->is_error())
|
||||
{
|
||||
jsonrpcpp::error_ptr error = dynamic_pointer_cast<jsonrpcpp::Error>(batch_entity);
|
||||
responseBatch.add(jsonrpcpp::RequestException(*error));
|
||||
}
|
||||
}
|
||||
if (!responseBatch.entities.empty())
|
||||
cout << "<-- " << responseBatch.to_json().dump() << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(const jsonrpcpp::RequestException& e)
|
||||
{
|
||||
cout << "<-- " << e.to_json().dump() << "\n";
|
||||
//cout << " Response: " << jsonrpcpp::Response(e).to_json().dump() << "\n";
|
||||
//cerr << "RequestException: " << e.what() << "\n";
|
||||
}
|
||||
catch(const jsonrpcpp::ParseErrorException& e)
|
||||
{
|
||||
cout << "<-- " << e.to_json().dump() << "\n";
|
||||
}
|
||||
catch(const jsonrpcpp::RpcException& e)
|
||||
{
|
||||
cerr << "RpcException: " << e.what() << "\n";
|
||||
cout << "<-- " << jsonrpcpp::ParseErrorException(e.what()).to_json().dump() << "\n";
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
|
||||
|
||||
void test(const jsonrpcpp::Entity& entity)
|
||||
{
|
||||
test(entity.to_json().dump());
|
||||
}
|
||||
|
||||
|
||||
void update(const jsonrpcpp::Parameter& params)
|
||||
{
|
||||
cout << "Notification callback: update, has params: " << !params.is_null() << "\n";
|
||||
}
|
||||
|
||||
/*
|
||||
void foobar(const jsonrpcpp::Notification& notification, const jsonrpcpp::Parameter& params)
|
||||
{
|
||||
cout << "Notification callback: " << notification.method << ", has params: " << !notification.params.is_null() << "\n";
|
||||
}
|
||||
*/
|
||||
|
||||
void foobar(const jsonrpcpp::Parameter& params)
|
||||
{
|
||||
cout << "Notification callback: foobar, has params: " << !params.is_null() << "\n";
|
||||
}
|
||||
|
||||
|
||||
jsonrpcpp::response_ptr sum(const jsonrpcpp::Id& id, const jsonrpcpp::Parameter& params)
|
||||
{
|
||||
int result = 0;
|
||||
for (const auto& summand: params.param_array)
|
||||
result += summand.get<int>();
|
||||
cout << "Request callback: sum, result: " << result << "\n";
|
||||
return make_shared<jsonrpcpp::Response>(id, result);
|
||||
}
|
||||
|
||||
|
||||
//examples taken from: http://www.jsonrpc.org/specification#examples
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
parser.register_notification_callback("update", update);
|
||||
parser.register_notification_callback("foobar", foobar);
|
||||
parser.register_request_callback("sum", sum);
|
||||
|
||||
cout << "rpc call with positional parameters:\n\n";
|
||||
test(R"({"jsonrpc": "2.0", "method": "sum", "params": [1, 2, 3, 4, 5], "id": 1})");
|
||||
test(jsonrpcpp::Request(1, "sum", Json({1, 2, 3, 4, 5})));
|
||||
|
||||
test(R"({"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1})");
|
||||
test(jsonrpcpp::Request(1, "subtract", Json({42, 23})));
|
||||
test(R"({"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2})");
|
||||
test(jsonrpcpp::Request(2, "subtract", Json({23, 42})));
|
||||
|
||||
cout << "\n\nrpc call with named parameters:\n\n";
|
||||
test(R"({"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3})");
|
||||
test(jsonrpcpp::Request(3, "subtract", Json({{"subtrahend", 23}, {"minuend", 42}})));
|
||||
test(R"({"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4})");
|
||||
test(jsonrpcpp::Request(4, "subtract", Json({{"minuend", 42}, {"subtrahend", 23}})));
|
||||
|
||||
cout << "\n\na Notification:\n\n";
|
||||
test(R"({"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]})");
|
||||
test(jsonrpcpp::Notification("update", Json({1, 2, 3, 4, 5})));
|
||||
test(R"({"jsonrpc": "2.0", "method": "foobar"})");
|
||||
test(jsonrpcpp::Notification("foobar"));
|
||||
|
||||
cout << "\n\nrpc call of non-existent method:\n\n";
|
||||
test(R"({"jsonrpc": "2.0", "method": "foobar", "id": "1"})");
|
||||
test(jsonrpcpp::Request("1", "foobar"));
|
||||
|
||||
cout << "\n\nrpc call with invalid JSON:\n\n";
|
||||
test(R"({"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz])");
|
||||
|
||||
cout << "\n\nrpc call with invalid Request object:\n\n";
|
||||
test(R"({"jsonrpc": "2.0", "method": 1, "params": "bar"})");
|
||||
|
||||
cout << "\n\nrpc call Batch, invalid JSON:\n\n";
|
||||
test(R"( [
|
||||
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
|
||||
{"jsonrpc": "2.0", "method"
|
||||
])");
|
||||
|
||||
cout << "\n\nrpc call with an empty Array:\n\n";
|
||||
test(R"([])");
|
||||
|
||||
cout << "\n\nrpc call with an invalid Batch (but not empty):\n\n";
|
||||
test(R"([1])");
|
||||
|
||||
cout << "\n\nrpc call with invalid Batch:\n\n";
|
||||
test(R"([1,2,3])");
|
||||
|
||||
cout << "\n\nrpc call Batch:\n\n";
|
||||
test(R"( [
|
||||
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
|
||||
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
|
||||
{"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
|
||||
{"foo": "boo"},
|
||||
{"jsonrpc": "2.0", "method": 1, "params": "bar"},
|
||||
{"jsonrpc": "2.0", "method": 1, "params": "bar", "id": 4},
|
||||
{"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
|
||||
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}
|
||||
])");
|
||||
|
||||
cout << "\n\nrpc call Batch (all notifications):\n\n";
|
||||
test(R"( [
|
||||
{"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]},
|
||||
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}
|
||||
])");
|
||||
|
||||
}
|
||||
|
||||
|
||||
1017
3rd_party/jsonrpcpp-1.1.1/lib/jsonrp.cpp
vendored
Normal file
1017
3rd_party/jsonrpcpp-1.1.1/lib/jsonrp.cpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
461
3rd_party/jsonrpcpp-1.1.1/lib/jsonrp.hpp
vendored
Normal file
461
3rd_party/jsonrpcpp-1.1.1/lib/jsonrp.hpp
vendored
Normal file
@@ -0,0 +1,461 @@
|
||||
/***
|
||||
__ ____ __ __ _ ____ ____ ___ _ _
|
||||
_( )/ ___) / \ ( ( \( _ \( _ \ / __)( ) ( )
|
||||
/ \) \\___ \( O )/ / ) / ) __/( (__(_ _)(_ _)
|
||||
\____/(____/ \__/ \_)__)(__\_)(__) \___)(_) (_)
|
||||
version 1.1.1
|
||||
https://github.com/badaix/jsonrpcpp
|
||||
|
||||
This file is part of jsonrpc++
|
||||
Copyright (C) 2017 Johannes Pohl
|
||||
|
||||
This software may be modified and distributed under the terms
|
||||
of the MIT license. See the LICENSE file for details.
|
||||
***/
|
||||
|
||||
/// http://patorjk.com/software/taag/#p=display&f=Graceful&t=JSONRPC%2B%2B
|
||||
|
||||
#ifndef JSON_RPC_H
|
||||
#define JSON_RPC_H
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <exception>
|
||||
#include "json.hpp"
|
||||
|
||||
|
||||
using Json = nlohmann::json;
|
||||
|
||||
namespace jsonrpcpp
|
||||
{
|
||||
|
||||
|
||||
class Entity;
|
||||
class Request;
|
||||
class Notification;
|
||||
class Parameter;
|
||||
class Response;
|
||||
class Error;
|
||||
class Batch;
|
||||
|
||||
|
||||
typedef std::shared_ptr<Entity> entity_ptr;
|
||||
typedef std::shared_ptr<Request> request_ptr;
|
||||
typedef std::shared_ptr<Notification> notification_ptr;
|
||||
typedef std::shared_ptr<Parameter> parameter_ptr;
|
||||
typedef std::shared_ptr<Response> response_ptr;
|
||||
typedef std::shared_ptr<Error> error_ptr;
|
||||
typedef std::shared_ptr<Batch> batch_ptr;
|
||||
|
||||
|
||||
|
||||
class Entity
|
||||
{
|
||||
public:
|
||||
enum class entity_t : uint8_t
|
||||
{
|
||||
unknown,
|
||||
exception,
|
||||
id,
|
||||
error,
|
||||
response,
|
||||
request,
|
||||
notification,
|
||||
batch
|
||||
};
|
||||
|
||||
Entity(entity_t type);
|
||||
virtual ~Entity();
|
||||
|
||||
bool is_exception();
|
||||
bool is_id();
|
||||
bool is_error();
|
||||
bool is_response();
|
||||
bool is_request();
|
||||
bool is_notification();
|
||||
bool is_batch();
|
||||
|
||||
virtual std::string type_str() const;
|
||||
|
||||
virtual Json to_json() const = 0;
|
||||
virtual void parse_json(const Json& json) = 0;
|
||||
|
||||
virtual void parse(const std::string& json_str);
|
||||
virtual void parse(const char* json_str);
|
||||
|
||||
protected:
|
||||
entity_t entity;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class NullableEntity : public Entity
|
||||
{
|
||||
public:
|
||||
NullableEntity(entity_t type);
|
||||
NullableEntity(entity_t type, std::nullptr_t);
|
||||
virtual ~NullableEntity();
|
||||
#ifdef _MSC_VER
|
||||
virtual operator bool() const
|
||||
#else
|
||||
virtual explicit operator bool() const
|
||||
#endif
|
||||
{
|
||||
return !isNull;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool isNull;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Id : public Entity
|
||||
{
|
||||
public:
|
||||
enum class value_t : uint8_t
|
||||
{
|
||||
null,
|
||||
string,
|
||||
integer
|
||||
};
|
||||
|
||||
Id();
|
||||
Id(int id);
|
||||
Id(const char* id);
|
||||
Id(const std::string& id);
|
||||
Id(const Json& json_id);
|
||||
|
||||
virtual Json to_json() const;
|
||||
virtual void parse_json(const Json& json);
|
||||
|
||||
friend std::ostream& operator<< (std::ostream &out, const Id &id)
|
||||
{
|
||||
out << id.to_json();
|
||||
return out;
|
||||
}
|
||||
|
||||
value_t type;
|
||||
int int_id;
|
||||
std::string string_id;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Parameter : public NullableEntity
|
||||
{
|
||||
public:
|
||||
enum class value_t : uint8_t
|
||||
{
|
||||
null,
|
||||
array,
|
||||
map
|
||||
};
|
||||
|
||||
Parameter(std::nullptr_t);
|
||||
Parameter(const Json& json = nullptr);
|
||||
Parameter(const std::string& key1, const Json& value1,
|
||||
const std::string& key2 = "", const Json& value2 = nullptr,
|
||||
const std::string& key3 = "", const Json& value3 = nullptr,
|
||||
const std::string& key4 = "", const Json& value4 = nullptr);
|
||||
|
||||
virtual Json to_json() const;
|
||||
virtual void parse_json(const Json& json);
|
||||
|
||||
bool is_array() const;
|
||||
bool is_map() const;
|
||||
bool is_null() const;
|
||||
|
||||
Json get(const std::string& key) const;
|
||||
Json get(size_t idx) const;
|
||||
bool has(const std::string& key) const;
|
||||
bool has(size_t idx) const;
|
||||
|
||||
template<typename T>
|
||||
T get(const std::string& key) const
|
||||
{
|
||||
return get(key).get<T>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(size_t idx) const
|
||||
{
|
||||
return get(idx).get<T>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(const std::string& key, const T& default_value) const
|
||||
{
|
||||
if (!has(key))
|
||||
return default_value;
|
||||
else
|
||||
return get<T>(key);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(size_t idx, const T& default_value) const
|
||||
{
|
||||
if (!has(idx))
|
||||
return default_value;
|
||||
else
|
||||
return get<T>(idx);
|
||||
}
|
||||
|
||||
value_t type;
|
||||
std::vector<Json> param_array;
|
||||
std::map<std::string, Json> param_map;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Error : public NullableEntity
|
||||
{
|
||||
public:
|
||||
Error(const Json& json = nullptr);
|
||||
Error(std::nullptr_t);
|
||||
Error(const std::string& message, int code, const Json& data = nullptr);
|
||||
|
||||
virtual Json to_json() const;
|
||||
virtual void parse_json(const Json& json);
|
||||
|
||||
int code;
|
||||
std::string message;
|
||||
Json data;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/// JSON-RPC 2.0 request
|
||||
/**
|
||||
* Simple jsonrpc 2.0 parser with getters
|
||||
* Currently no named parameters are supported, but only array parameters
|
||||
*/
|
||||
class Request : public Entity
|
||||
{
|
||||
public:
|
||||
Request(const Json& json = nullptr);
|
||||
Request(const Id& id, const std::string& method, const Parameter& params = nullptr);
|
||||
|
||||
virtual Json to_json() const;
|
||||
virtual void parse_json(const Json& json);
|
||||
|
||||
std::string method;
|
||||
Parameter params;
|
||||
Id id;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class RpcException : public std::exception
|
||||
{
|
||||
char* text_;
|
||||
public:
|
||||
RpcException(const char* text);
|
||||
RpcException(const std::string& text);
|
||||
RpcException(const RpcException& e);
|
||||
|
||||
virtual ~RpcException() throw();
|
||||
virtual const char* what() const noexcept;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class ParseErrorException : public RpcException, public Entity
|
||||
{
|
||||
public:
|
||||
Error error;
|
||||
|
||||
ParseErrorException(const Error& error);
|
||||
ParseErrorException(const ParseErrorException& e);
|
||||
ParseErrorException(const std::string& data);
|
||||
virtual Json to_json() const;
|
||||
|
||||
protected:
|
||||
virtual void parse_json(const Json& json);
|
||||
};
|
||||
|
||||
|
||||
|
||||
// -32600 Invalid Request The JSON sent is not a valid Request object.
|
||||
// -32601 Method not found The method does not exist / is not available.
|
||||
// -32602 Invalid params Invalid method parameter(s).
|
||||
// -32603 Internal error Internal JSON-RPC error.
|
||||
|
||||
class RequestException : public RpcException, public Entity
|
||||
{
|
||||
public:
|
||||
Error error;
|
||||
Id id;
|
||||
|
||||
RequestException(const Error& error, const Id& requestId = Id());
|
||||
RequestException(const RequestException& e);
|
||||
virtual Json to_json() const;
|
||||
|
||||
protected:
|
||||
virtual void parse_json(const Json& json);
|
||||
};
|
||||
|
||||
|
||||
|
||||
class InvalidRequestException : public RequestException
|
||||
{
|
||||
public:
|
||||
InvalidRequestException(const Id& requestId = Id());
|
||||
InvalidRequestException(const Request& request);
|
||||
InvalidRequestException(const char* data, const Id& requestId = Id());
|
||||
InvalidRequestException(const std::string& data, const Id& requestId = Id());
|
||||
};
|
||||
|
||||
|
||||
|
||||
class MethodNotFoundException : public RequestException
|
||||
{
|
||||
public:
|
||||
MethodNotFoundException(const Id& requestId = Id());
|
||||
MethodNotFoundException(const Request& request);
|
||||
MethodNotFoundException(const char* data, const Id& requestId = Id());
|
||||
MethodNotFoundException(const std::string& data, const Id& requestId = Id());
|
||||
};
|
||||
|
||||
|
||||
|
||||
class InvalidParamsException : public RequestException
|
||||
{
|
||||
public:
|
||||
InvalidParamsException(const Id& requestId = Id());
|
||||
InvalidParamsException(const Request& request);
|
||||
InvalidParamsException(const char* data, const Id& requestId = Id());
|
||||
InvalidParamsException(const std::string& data, const Id& requestId = Id());
|
||||
};
|
||||
|
||||
|
||||
|
||||
class InternalErrorException : public RequestException
|
||||
{
|
||||
public:
|
||||
InternalErrorException(const Id& requestId = Id());
|
||||
InternalErrorException(const Request& request);
|
||||
InternalErrorException(const char* data, const Id& requestId = Id());
|
||||
InternalErrorException(const std::string& data, const Id& requestId = Id());
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Response : public Entity
|
||||
{
|
||||
public:
|
||||
Id id;
|
||||
Json result;
|
||||
Error error;
|
||||
|
||||
Response(const Json& json = nullptr);
|
||||
Response(const Id& id, const Json& result);
|
||||
Response(const Id& id, const Error& error);
|
||||
Response(const Request& request, const Json& result);
|
||||
Response(const Request& request, const Error& error);
|
||||
Response(const RequestException& exception);
|
||||
|
||||
virtual Json to_json() const;
|
||||
virtual void parse_json(const Json& json);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Notification : public Entity
|
||||
{
|
||||
public:
|
||||
std::string method;
|
||||
Parameter params;
|
||||
Notification(const Json& json = nullptr);
|
||||
Notification(const char* method, const Parameter& params = nullptr);
|
||||
Notification(const std::string& method, const Parameter& params);
|
||||
|
||||
virtual Json to_json() const;
|
||||
virtual void parse_json(const Json& json);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
typedef std::function<void(const Parameter& params)> notification_callback;
|
||||
typedef std::function<jsonrpcpp::response_ptr(const Id& id, const Parameter& params)> request_callback;
|
||||
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
Parser();
|
||||
virtual ~Parser();
|
||||
|
||||
entity_ptr parse(const std::string& json_str);
|
||||
entity_ptr parse_json(const Json& json);
|
||||
|
||||
void register_notification_callback(const std::string& notification, notification_callback callback);
|
||||
void register_request_callback(const std::string& request, request_callback callback);
|
||||
|
||||
static entity_ptr do_parse(const std::string& json_str);
|
||||
static entity_ptr do_parse_json(const Json& json);
|
||||
static bool is_request(const std::string& json_str);
|
||||
static bool is_request(const Json& json);
|
||||
static bool is_notification(const std::string& json_str);
|
||||
static bool is_notification(const Json& json);
|
||||
static bool is_response(const std::string& json_str);
|
||||
static bool is_response(const Json& json);
|
||||
static bool is_batch(const std::string& json_str);
|
||||
static bool is_batch(const Json& json);
|
||||
|
||||
private:
|
||||
std::map<std::string, notification_callback> notification_callbacks_;
|
||||
std::map<std::string, request_callback> request_callbacks_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Batch : public Entity
|
||||
{
|
||||
public:
|
||||
std::vector<entity_ptr> entities;
|
||||
|
||||
Batch(const Json& json = nullptr);
|
||||
|
||||
virtual Json to_json() const;
|
||||
virtual void parse_json(const Json& json);
|
||||
|
||||
template<typename T>
|
||||
void add(const T& entity)
|
||||
{
|
||||
entities.push_back(std::make_shared<T>(entity));
|
||||
}
|
||||
|
||||
void add_ptr(const entity_ptr& entity)
|
||||
{
|
||||
entities.push_back(entity);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} //namespace jsonrpc
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user