-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith_client.cpp
134 lines (98 loc) · 3.06 KB
/
arith_client.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Copyright (c) 2023-present DeepGrace (complex dot invoke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/deepgrace/arpc
//
#define BOOST_ASIO_HAS_IO_URING
#define BOOST_ASIO_DISABLE_EPOLL
#include <chrono>
#include <random>
#include <thread>
#include <iostream>
#include <arpc.hpp>
#include <arith.pb.h>
namespace net = boost::asio;
namespace gp = google::protobuf;
using steady_t = std::chrono::steady_clock;
using time_point_t = typename steady_t::time_point;
struct task
{
arpc::controller controller;
pb::request request;
pb::response response;
arpc::Closure* done;
time_point_t time_begin;
time_point_t time_end;
};
class client
{
public:
client(net::io_context& ioc, const std::string& host, const std::string& port) : ioc(ioc), work(ioc), host(host), port(port), dist(0, 100)
{
channel = new arpc::channel(ioc);
service = new pb::service::Stub(channel, pb::service::STUB_OWNS_CHANNEL);
}
void invoke()
{
auto t = std::make_shared<task>();
auto& controller = t->controller;
controller.host(host);
controller.port(port);
controller.timeout(80);
auto& request = t->request;
auto op = pb::opcode(dist(generator) % 5);
request.set_op(op);
request.set_lhs(dist(generator));
request.set_rhs(dist(generator));
std::cout << "requqest: " << request.DebugString();
t->time_begin = steady_t::now();
t->done = gp::NewCallback(this, &client::done, t);
service->compute(&t->controller, &t->request, &t->response, t->done);
}
void done(std::shared_ptr<task> t)
{
t->time_end = steady_t::now();
auto& controller = t->controller;
auto period = t->time_end - t->time_begin;
auto number = std::chrono::duration_cast<std::chrono::microseconds>(period).count();
if (controller.Failed())
std::cerr << "ErrorCode: " << controller.ErrorCode() << " ErrorText: " << controller.ErrorText() << std::endl;
std::cout << "response " << t->response.DebugString();
std::cout << "it takes " << number << " us" << std::endl;
}
~client()
{
delete service;
}
private:
net::io_context& ioc;
net::io_context::work work;
arpc::channel* channel;
pb::service* service;
std::string host;
std::string port;
std::default_random_engine generator;
std::uniform_int_distribution<int> dist;
};
int main(int argc, char* argv[])
{
if (argc != 3)
{
std::cout << "Usage: " << argv[0] << " <host> <port>" << std::endl;
return 1;
}
std::string host(argv[1]);
std::string port(argv[2]);
net::io_context ioc;
client c(ioc, host, port);
std::thread t([&]{ ioc.run(); });
constexpr size_t size = 20;
char buff[size];
while (fgets(buff, size, stdin))
c.invoke();
t.join();
return 0;
}