Skip to content

Commit 6cb2db0

Browse files
committed
Added HTTP Client and Server Example
1 parent a205625 commit 6cb2db0

File tree

8 files changed

+2672
-14
lines changed

8 files changed

+2672
-14
lines changed

.circleci/config.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ jobs:
2222
command: lcov --directory . --capture --output-file coverage.info && lcov --remove coverage.info '/usr/*' --output-file coverage.info
2323
- codecov/upload:
2424
file: coverage.info
25+
- run:
26+
name: Run example
27+
command: ./example-warehouse
2528
"build-linux-clang":
2629
docker:
2730
- image: "debian:buster"
@@ -35,8 +38,11 @@ jobs:
3538
name: Build
3639
command: cmake -DCOMPILE_TESTS=ON -DCOMPILE_EXAMPLES=ON . && cmake --build .
3740
- run:
38-
nmae: Run tests
41+
name: Run tests
3942
command: ./jsonrpccpp-test -s
43+
- run:
44+
name: Run example
45+
command: ./example-warehouse
4046
"build-osx-clang":
4147
macos:
4248
xcode: "10.2.1"
@@ -50,8 +56,11 @@ jobs:
5056
name: Build
5157
command: cmake -DCOMPILE_TESTS=ON -DCOMPILE_EXAMPLES=ON . && cmake --build .
5258
- run:
53-
nmae: Run tests
59+
name: Run tests
5460
command: ./jsonrpccpp-test -s
61+
- run:
62+
name: Run example
63+
command: ./example-warehouse
5564
workflows:
5665
version: 2
5766
build:

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ if (COMPILE_TESTS)
2929
endif ()
3030

3131
if (COMPILE_EXAMPLES)
32+
find_package(Threads)
3233
add_executable(example-warehouse examples/warehouse/main.cpp examples/warehouse/warehouseapp.cpp examples/warehouse/types.h examples/inmemoryconnector.hpp)
33-
target_link_libraries(example-warehouse json-rpc-cxx)
34+
target_link_libraries(example-warehouse json-rpc-cxx Threads::Threads)
3435
target_include_directories(example-warehouse PRIVATE vendor examples)
36+
add_test(NAME example COMMAND example-warehouse)
3537
endif ()
3638

3739

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ build_script:
66
- cmake -DCOMPILE_TESTS=ON -DCOMPILE_EXAMPLES=ON .
77
- cmake --build .
88
- Debug\jsonrpccpp-test.exe -s
9+
- Debug\example-warehouse.exe

examples/cpphttplibconnector.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
3+
#include <cpp-httplib/httplib.h>
4+
#include <jsonrpccxx/iclientconnector.hpp>
5+
#include <jsonrpccxx/server.hpp>
6+
#include <string>
7+
8+
class CppHttpLibClientConnector : public jsonrpccxx::IClientConnector {
9+
public:
10+
explicit CppHttpLibClientConnector(const std::string &host, int port) : httpClient(host.c_str(), port) {}
11+
std::string Send(const std::string &request) override {
12+
auto res = httpClient.Post("/jsonrpc", request, "application/json");
13+
if (!res || res->status != 200) {
14+
throw jsonrpccxx::JsonRpcException(-32003, "client connector error, received status != 200");
15+
}
16+
return res->body;
17+
}
18+
19+
private:
20+
httplib::Client httpClient;
21+
};
22+
23+
class CppHttpLibServerConnector {
24+
public:
25+
explicit CppHttpLibServerConnector(jsonrpccxx::JsonRpcServer &server, int port) : server(server), port(port) {
26+
httpServer.Post("/jsonrpc", [&](const httplib::Request &req, httplib::Response &res) {
27+
res.status = 200;
28+
res.set_content(server.HandleRequest(req.body), "application/json");
29+
});
30+
}
31+
virtual ~CppHttpLibServerConnector() { StopListening(); }
32+
33+
bool StartListening() {
34+
if (httpServer.is_running())
35+
return false;
36+
this->thread = std::thread([this]() { this->httpServer.listen("localhost", port); });
37+
return true;
38+
}
39+
40+
void StopListening() {
41+
if (httpServer.is_running()) {
42+
httpServer.stop();
43+
this->thread.join();
44+
}
45+
}
46+
47+
private:
48+
std::thread thread;
49+
jsonrpccxx::JsonRpcServer &server;
50+
httplib::Server httpServer;
51+
int port;
52+
};

examples/inmemoryconnector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <jsonrpccxx/iclientconnector.hpp>
33
#include <jsonrpccxx/server.hpp>
44

5+
//This class is server and client connector at the same time.
56
class InMemoryConnector : public jsonrpccxx::IClientConnector {
67
public:
78
explicit InMemoryConnector(jsonrpccxx::JsonRpcServer &server) : server(server) {}

examples/warehouse/main.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "inmemoryconnector.hpp"
2+
#include "cpphttplibconnector.hpp"
23
#include "warehouseapp.hpp"
34

45
#include <iostream>
@@ -18,17 +19,9 @@ class WareHouseClient {
1819
JsonRpcClient &client;
1920
};
2021

21-
int main() {
22-
JsonRpc2Server rpcServer;
23-
InMemoryConnector connector(rpcServer);
24-
JsonRpcClient client(connector, version::v2);
25-
26-
// Bindings
22+
void doWarehouseStuff(IClientConnector &clientConnector) {
23+
JsonRpcClient client(clientConnector, version::v2);
2724
WareHouseClient appClient(client);
28-
WarehouseServer app;
29-
rpcServer.Add("GetProduct", GetHandle(&WarehouseServer::GetProduct, app), {"id"});
30-
rpcServer.Add("AddProduct", GetHandle(&WarehouseServer::AddProduct, app), {"product"});
31-
3225
Product p = {"0xff", 22.4, "Product 1", category::cash_carry};
3326
cout << "Adding product: " << std::boolalpha << appClient.AddProduct(p) << "\n";
3427

@@ -38,7 +31,27 @@ int main() {
3831
appClient.GetProduct("0xff2");
3932
} catch (JsonRpcException &e) {
4033
cerr << "Error finding product: " << e.what() << "\n";
41-
return 1;
4234
}
35+
}
36+
37+
int main() {
38+
JsonRpc2Server rpcServer;
39+
40+
// Bindings
41+
WarehouseServer app;
42+
rpcServer.Add("GetProduct", GetHandle(&WarehouseServer::GetProduct, app), {"id"});
43+
rpcServer.Add("AddProduct", GetHandle(&WarehouseServer::AddProduct, app), {"product"});
44+
45+
cout << "Running in-memory example" << "\n";
46+
InMemoryConnector inMemoryConnector(rpcServer);
47+
doWarehouseStuff(inMemoryConnector);
48+
49+
cout << "Running http example" << "\n";
50+
CppHttpLibServerConnector httpServer(rpcServer, 8484);
51+
cout << "Starting http server: " << std::boolalpha << httpServer.StartListening() << "\n";
52+
CppHttpLibClientConnector httpClient("localhost", 8484);
53+
std::this_thread::sleep_for(0.5s);
54+
doWarehouseStuff(httpClient);
55+
4356
return 0;
4457
}

vendor/cpp-httplib/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 yhirose
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)