-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_client.hpp
More file actions
158 lines (138 loc) · 4.57 KB
/
query_client.hpp
File metadata and controls
158 lines (138 loc) · 4.57 KB
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
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef QUERY_CLIENT_HPP
#define QUERY_CLIENT_HPP
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#ifdef BAZEL_BUILD
#include "examples/protos/query.grpc.pb.h"
#else
#include "query.grpc.pb.h"
#endif
using grpc::Channel;
using grpc::ClientContext;
using grpc::ClientAsyncResponseReader;
using grpc::CompletionQueue;
using grpc::Status;
using query::Query;
using query::ColorReply;
using query::ColorRequest;
using query::ReadyReply;
using query::ReadyRequest;
using query::StartReply;
using query::StartRequest;
using std::string;
class QueryClient {
public:
QueryClient(std::shared_ptr<Channel> channel): stub_(Query::NewStub(channel)) {}
// Assembles the client's payload, sends it and presents the response back
// from the server.
std::string AskColor(const std::string& color) {
// Data we are sending to the server.
ColorRequest request;
request.set_color(color);
// Container for the data we expect from the server.
ColorReply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
Status status;
CompletionQueue cq;
// The actual RPC.
//Status status = stub_->AskColor(&context, request, &reply);
//stub_->AsyncAskColor()
std::unique_ptr<ClientAsyncResponseReader<ColorReply> > rpc(
stub_->PrepareAsyncAskColor(&context, request, &cq));
rpc->StartCall();
rpc->Finish(&reply, &status, (void*)1);
void* got_tag;
bool ok = false;
GPR_ASSERT(cq.Next(&got_tag, &ok));
GPR_ASSERT(got_tag == (void*)1);
GPR_ASSERT(ok);
// Act upon its status.
if (status.ok()) {
return reply.reply();
} else {
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
std::string SayReady(const std::string& port){
// Data we are sending to the server.
ReadyRequest request;
request.set_port(port);
ReadyReply reply;
Status status;
do {
ClientContext context;
status = stub_->SayReady(&context, request, &reply);
} while (!status.ok());
// Act upon its status.
if (status.ok()) {
return reply.reply();
} else {
std::cout << "Here1: " << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
std::string SayStart(const std::string& start){
StartRequest request;
request.set_start(start);
StartReply reply;
ClientContext context;
Status status = stub_->SayStart(&context, request, &reply);
if (status.ok()) {
return reply.reply();
} else {
std::cout << "Here2: " << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
private:
std::unique_ptr<Query::Stub> stub_;
};
/*
int main(int argc, char** argv) {
// Instantiate the client. It requires a channel, out of which the actual RPCs
// are created. This channel models a connection to an endpoint specified by
// the argument "--target=" which is the only expected argument.
// We indicate that the channel isn't authenticated (use of
// InsecureChannelCredentials()).
string target_str;
string id = string(argv[1]);
if (id.size()==1) {
target_str = "localhost:5000" + id;
} else if (id.size()==2) {
target_str = "localhost:500" + id;
} else if (id.size()==3) {
target_str = "localhost:50" + id;
}
QueryClient query(
grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
std::string color("Blue");
std::string reply = query.AskColor(color);
std::cout << "Asker received: " << reply << std::endl;
return 0;
} */
#endif