-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.cpp
69 lines (57 loc) · 2.13 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
58
59
60
61
62
63
64
65
66
67
68
69
#include "inmemoryconnector.hpp"
#include "cpphttplibconnector.hpp"
#include "warehouseapp.hpp"
#include <iostream>
#include <vector>
#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}); }
vector<Product> AllProducts() { return client.CallMethod<vector<Product>>(1, "AllProducts", {}); }
private:
JsonRpcClient &client;
};
void doWarehouseStuff(IClientConnector &clientConnector) {
JsonRpcClient client(clientConnector, version::v2);
WareHouseClient appClient(client);
Product p;
p.id = "0xff";
p.price = 22.4;
p.name = "Product 1";
p.cat = 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";
}
auto all = appClient.AllProducts();
for (const auto &p: all) {
cout << p.name << endl;
}
}
int main() {
JsonRpc2Server rpcServer;
// Bindings
WarehouseServer app;
rpcServer.Add("GetProduct", GetHandle(&WarehouseServer::GetProduct, app), {"id"});
rpcServer.Add("AddProduct", GetHandle(&WarehouseServer::AddProduct, app), {"product"});
rpcServer.Add("AllProducts", GetHandle(&WarehouseServer::AllProducts, app), {});
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;
}