forked from jsonrpcx/json-rpc-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
57 lines (46 loc) · 1.79 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "inmemoryconnector.hpp"
#include "cpphttplibconnector.hpp"
#include "warehouseapp.hpp"
#include <iostream>
#include <jsonrpccxx/client.hpp>
#include <jsonrpccxx/server.hpp>
using namespace jsonrpccxx;
using namespace std;
class WareHouseClient {
public:
explicit WareHouseClient(JsonRpcClient &client) : client(client) {}
bool AddProduct(const Product &p) { return client.CallMethod<bool>(1, "AddProduct", {p}); }
Product GetProduct(const std::string &id) { return client.CallMethod<Product>(1, "GetProduct", {id}); }
private:
JsonRpcClient &client;
};
void doWarehouseStuff(IClientConnector &clientConnector) {
JsonRpcClient client(clientConnector, version::v2);
WareHouseClient appClient(client);
Product p = {"0xff", 22.4, "Product 1", category::cash_carry};
cout << "Adding product: " << std::boolalpha << appClient.AddProduct(p) << "\n";
Product p2 = appClient.GetProduct("0xff");
cout << "Found product: " << p2.name << "\n";
try {
appClient.GetProduct("0xff2");
} catch (JsonRpcException &e) {
cerr << "Error finding product: " << e.what() << "\n";
}
}
int main() {
JsonRpc2Server rpcServer;
// Bindings
WarehouseServer app;
rpcServer.Add("GetProduct", GetHandle(&WarehouseServer::GetProduct, app), {"id"});
rpcServer.Add("AddProduct", GetHandle(&WarehouseServer::AddProduct, app), {"product"});
cout << "Running in-memory example" << "\n";
InMemoryConnector inMemoryConnector(rpcServer);
doWarehouseStuff(inMemoryConnector);
cout << "Running http example" << "\n";
CppHttpLibServerConnector httpServer(rpcServer, 8484);
cout << "Starting http server: " << std::boolalpha << httpServer.StartListening() << "\n";
CppHttpLibClientConnector httpClient("localhost", 8484);
std::this_thread::sleep_for(0.5s);
doWarehouseStuff(httpClient);
return 0;
}