-
Notifications
You must be signed in to change notification settings - Fork 20
/
calculator_async_client.cc
297 lines (263 loc) · 10.6 KB
/
calculator_async_client.cc
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <calculator.grpc.pb.h>
#include <grpc++/grpc++.h>
#include <memory>
#include <thread>
#include <iostream>
using namespace grpc;
using namespace calculator;
enum class MessageID : uint8_t {
ADD = 0, SUB = 1, MUL = 2, DIV = 3, MOD = 4
};
class CalculatorClient {
public:
CalculatorClient(std::shared_ptr<Channel> channel) : _stub{Calculator::NewStub(channel)} {}
void Add(int32_t addend_1, int32_t addend_2) {
// Prepare request
AddRequest request;
request.set_addend_1(addend_1);
request.set_addend_2(addend_2);
// Create an AsyncClientCall object to store RPC data
auto* call = new AsyncClientCall<AddRequest, AddResponse>;
call->request = request;
// Create an RPC object
call->rpc = _stub->PrepareAsyncAdd(&call->context, request, &_queue);
// Initiate the RPC call
call->rpc->StartCall();
// Request to update the server's response and the call status upon completion of the RPC
auto* tag = new Tag;
tag->call = (void*)call;
tag->id = MessageID::ADD;
call->rpc->Finish(&call->response, &call->status, (void*)tag);
}
void Sub(int32_t minuend, int32_t subtrahend) {
// Prepare request
SubRequest request;
request.set_minuend(minuend);
request.set_subtrahend(subtrahend);
// Create an AsyncClientCall object to store RPC data
auto* call = new AsyncClientCall<SubRequest, SubResponse>;
call->request = request;
// Create an RPC object
call->rpc = _stub->PrepareAsyncSub(&call->context, request, &_queue);
// Initiate the RPC call
call->rpc->StartCall();
// Request to update the server's response and the call status upon completion of the RPC
auto* tag = new Tag;
tag->call = (void*)call;
tag->id = MessageID::SUB;
call->rpc->Finish(&call->response, &call->status, (void*)tag);
}
void Mul(int32_t multiplicand, int32_t multiplier) {
// Prepare request
MulRequest request;
request.set_multiplicand(multiplicand);
request.set_multiplier(multiplier);
// Create an AsyncClientCall object to store RPC data
auto* call = new AsyncClientCall<MulRequest, MulResponse>;
call->request = request;
// Create an RPC object
call->rpc = _stub->PrepareAsyncMul(&call->context, request, &_queue);
// Initiate the RPC call
call->rpc->StartCall();
// Request to update the server's response and the call status upon completion of the RPC
auto* tag = new Tag;
tag->call = (void*)call;
tag->id = MessageID::MUL;
call->rpc->Finish(&call->response, &call->status, (void*)tag);
}
void Div(int32_t dividend, int32_t divisor) {
// Prepare request
DivRequest request;
request.set_dividend(dividend);
request.set_divisor(divisor);
// Create an AsyncClientCall object to store RPC data
auto* call = new AsyncClientCall<DivRequest, DivResponse>;
call->request = request;
// Create an RPC object
call->rpc = _stub->PrepareAsyncDiv(&call->context, request, &_queue);
// Initiate the RPC call
call->rpc->StartCall();
// Request to update the server's response and the call status upon completion of the RPC
auto* tag = new Tag;
tag->call = (void*)call;
tag->id = MessageID::DIV;
call->rpc->Finish(&call->response, &call->status, (void*)tag);
}
void Mod(int32_t dividend, int32_t divisor) {
// Prepare request
ModRequest request;
request.set_dividend(dividend);
request.set_divisor(divisor);
// Create an AsyncClientCall object to store RPC data
auto* call = new AsyncClientCall<ModRequest, ModResponse>;
call->request = request;
// Create an RPC object
call->rpc = _stub->PrepareAsyncMod(&call->context, request, &_queue);
// Initiate the RPC call
call->rpc->StartCall();
// Request to update the server's response and the call status upon completion of the RPC
auto* tag = new Tag;
tag->call = (void*)call;
tag->id = MessageID::MOD;
call->rpc->Finish(&call->response, &call->status, (void*)tag);
}
void AsyncCompleteRPC() {
void* tag;
bool ok = false;
while (_queue.Next(&tag, &ok)) {
auto* tag_ptr = static_cast<Tag*>(tag);
if (!ok || !tag_ptr) {
std::cerr << "Something went wrong" << std::endl;
abort();
}
std::string err;
switch (tag_ptr->id) {
case MessageID::ADD: {
auto* call = static_cast<AsyncClientCall<AddRequest, AddResponse>*>(tag_ptr->call);
if (call) {
if (call->status.ok()) {
std::cout << call->request.addend_1()
<< " + "
<< call->request.addend_2()
<< " = "
<< call->response.sum() << std::endl;
} else {
err = std::to_string(call->status.error_code()) + ": " + call->status.error_message();
}
} else {
err = "A client call was deleted";
}
delete call;
break;
}
case MessageID::SUB: {
auto* call = static_cast<AsyncClientCall<SubRequest, SubResponse>*>(tag_ptr->call);
if (call) {
if (call->status.ok()) {
std::cout << call->request.minuend()
<< " - "
<< call->request.subtrahend()
<< " = "
<< call->response.difference() << std::endl;
} else {
err = std::to_string(call->status.error_code()) + ": " + call->status.error_message();
}
} else {
err = "A client call was deleted";
}
delete call;
break;
}
case MessageID::MUL: {
auto* call = static_cast<AsyncClientCall<MulRequest, MulResponse>*>(tag_ptr->call);
if (call) {
if (call->status.ok()) {
std::cout << call->request.multiplicand()
<< " * "
<< call->request.multiplier()
<< " = "
<< call->response.product() << std::endl;
} else {
err = std::to_string(call->status.error_code()) + ": " + call->status.error_message();
}
} else {
err = "A client call was deleted";
}
delete call;
break;
}
case MessageID::DIV: {
auto* call = static_cast<AsyncClientCall<DivRequest, DivResponse>*>(tag_ptr->call);
if (call) {
if (call->status.ok()) {
std::cout << call->request.dividend()
<< " / "
<< call->request.divisor()
<< " = "
<< call->response.quotient() << std::endl;
} else {
err = std::to_string(call->status.error_code()) + ": " + call->status.error_message();
}
} else {
err = "A client call was deleted";
}
delete call;
break;
}
case MessageID::MOD: {
auto* call = static_cast<AsyncClientCall<ModRequest, ModResponse>*>(tag_ptr->call);
if (call) {
if (call->status.ok()) {
std::cout << call->request.dividend()
<< " % "
<< call->request.divisor()
<< " = "
<< call->response.result() << std::endl;
} else {
err = std::to_string(call->status.error_code()) + ": " + call->status.error_message();
}
} else {
err = "A client call was deleted";
}
delete call;
break;
}
}
delete tag_ptr;
if (!err.empty()) {
throw std::runtime_error(err);
}
}
}
private:
template <class RequestType, class ResponseType>
struct AsyncClientCall {
RequestType request;
ResponseType response;
ClientContext context;
Status status;
std::unique_ptr<ClientAsyncResponseReader<ResponseType>> rpc;
};
struct Tag {
void* call;
MessageID id;
};
std::unique_ptr<Calculator::Stub> _stub;
CompletionQueue _queue;
};
void RunClient() {
std::string server_address{"localhost:2511"};
CalculatorClient client{grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials())};
std::thread thread{&CalculatorClient::AsyncCompleteRPC, &client};
int rand = 4;
for (int i = 0; i < 100; ++i) {
switch (static_cast<MessageID>(i % 5)) {
case MessageID::ADD: {
client.Add(i, rand);
break;
}
case MessageID::SUB: {
client.Sub(i, rand);
break;
}
case MessageID::MUL: {
client.Mul(i, rand);
break;
}
case MessageID::DIV: {
client.Div(i, rand);
break;
}
case MessageID::MOD: {
client.Mod(i, rand);
break;
}
}
}
std::cout << "Press Ctrl + C to quit..." << std::endl << std::endl;
thread.join();
}
int main(int argc, char** argv) {
RunClient();
return 0;
}