-
Notifications
You must be signed in to change notification settings - Fork 20
/
sample_async_client.cc
151 lines (128 loc) · 4.69 KB
/
sample_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
#include <sample.grpc.pb.h>
#include <grpc++/grpc++.h>
#include <memory>
#include <thread>
#include <iostream>
using grpc::Channel;
using grpc::ClientAsyncResponseReader;
using grpc::ClientContext;
using grpc::CompletionQueue;
using grpc::Status;
using sample::SampleRequest;
using sample::SampleResponse;
using sample::SampleService;
namespace ns1 {
class SampleClient {
public:
SampleClient(std::shared_ptr<Channel> channel) : _stub{SampleService::NewStub(channel)} {}
std::string SampleMethod(const std::string& request_sample_field) {
// Prepare request
SampleRequest request;
request.set_request_sample_field(request_sample_field);
// Create an RPC object
SampleResponse response;
ClientContext context;
CompletionQueue queue;
Status status;
std::unique_ptr<ClientAsyncResponseReader<SampleResponse>> rpc;
rpc = _stub->PrepareAsyncSampleMethod(&context, request, &queue);
// Initiate the RPC call
rpc->StartCall();
// Request to update the server's response and the call status upon completion of the RPC
rpc->Finish(&response, &status, (void*)1);
// Complete the RPC call
void* tag;
bool ok = false;
if (queue.Next(&tag, &ok) && ok && tag == (void*)1) {
if (status.ok()) {
return response.response_sample_field();
} else {
std::cerr << status.error_code() << ": " << status.error_message() << std::endl;
return "RPC failed";
}
} else {
std::cerr << "Something went wrong" << std::endl;
abort();
}
}
private:
std::unique_ptr<SampleService::Stub> _stub;
};
void RunClient() {
std::string server_address{"localhost:2511"};
SampleClient client{grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials())};
std::string request_sample_field{"world"};
std::string response_sample_field = client.SampleMethod(request_sample_field);
std::cout << "Client received: " << response_sample_field << std::endl;
}
}
namespace ns2 {
class SampleClient {
public:
SampleClient(std::shared_ptr<Channel> channel) : _stub{SampleService::NewStub(channel)} {}
void SampleMethod(const std::string& request_sample_field) {
// Prepare request
SampleRequest request;
request.set_request_sample_field(request_sample_field);
// Create an AsyncClientCall object to store RPC data
auto* call = new AsyncClientCall;
// Create an RPC object
call->rpc = _stub->PrepareAsyncSampleMethod(&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
call->rpc->Finish(&call->response, &call->status, (void*)call);
}
void AsyncCompleteRPC() {
void* tag;
bool ok = false;
while (_queue.Next(&tag, &ok)) {
if (!ok) {
std::cerr << "Something went wrong" << std::endl;
abort();
}
std::string err;
auto* call = static_cast<AsyncClientCall*>(tag);
if (call) {
if (call->status.ok()) {
std::cout << "Client received: " << call->response.response_sample_field() << std::endl;
} else {
std::cerr << call->status.error_code() << ": " << call->status.error_message() << std::endl;
std::cout << "Client received: RPC failed" << std::endl;
}
} else {
err = "A client call was deleted";
}
delete call;
if (!err.empty()) {
throw std::runtime_error(err);
}
}
}
private:
struct AsyncClientCall {
SampleResponse response;
ClientContext context;
Status status;
std::unique_ptr<ClientAsyncResponseReader<SampleResponse>> rpc;
};
std::unique_ptr<SampleService::Stub> _stub;
CompletionQueue _queue;
};
void RunClient() {
std::string server_address{"localhost:2511"};
SampleClient client{grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials())};
std::thread thread{&SampleClient::AsyncCompleteRPC, &client};
for (int i = 0; i < 100; ++i) {
std::string request_sample_field{"world " + std::to_string(i)};
client.SampleMethod(request_sample_field);
}
std::cout << "Press Ctrl + C to quit..." << std::endl << std::endl;
thread.join();
}
}
int main(int argc, char** argv) {
ns1::RunClient();
ns2::RunClient();
return 0;
}