20 #include <condition_variable>
37 #include "examples/protos/route_guide.grpc.pb.h"
39 #include "route_guide.grpc.pb.h"
48 using routeguide::RouteGuide;
54 p.set_latitude(latitude);
55 p.set_longitude(longitude);
62 f.mutable_location()->CopyFrom(
MakePoint(latitude, longitude));
70 n.mutable_location()->CopyFrom(
MakePoint(latitude, longitude));
94 rect.mutable_lo()->set_latitude(400000000);
95 rect.mutable_lo()->set_longitude(-750000000);
96 rect.mutable_hi()->set_latitude(420000000);
97 rect.mutable_hi()->set_longitude(-730000000);
98 std::cout <<
"Looking for features between 40, -75 and 42, -73"
103 Reader(RouteGuide::Stub*
stub,
float coord_factor,
105 : coord_factor_(coord_factor) {
107 StartRead(&feature_);
110 void OnReadDone(
bool ok)
override {
112 std::cout <<
"Found feature called " << feature_.name() <<
" at "
113 << feature_.location().latitude() / coord_factor_ <<
", "
114 << feature_.location().longitude() / coord_factor_
116 StartRead(&feature_);
119 void OnDone(
const Status& s)
override {
120 std::unique_lock<std::mutex>
l(
mu_);
126 std::unique_lock<std::mutex>
l(
mu_);
136 std::condition_variable
cv_;
143 std::cout <<
"ListFeatures rpc succeeded." << std::endl;
145 std::cout <<
"ListFeatures rpc failed." << std::endl;
152 Recorder(RouteGuide::Stub*
stub,
float coord_factor,
153 const std::vector<Feature>* feature_list)
154 : coord_factor_(coord_factor),
158 feature_distribution_(0, feature_list->size() - 1),
159 delay_distribution_(500, 1500) {
168 void OnWriteDone(
bool ok)
override {
172 std::chrono::milliseconds(delay_distribution_(
generator_)),
173 [
this](
bool ) { NextWrite(); });
175 void OnDone(
const Status& s)
override {
176 std::unique_lock<std::mutex>
l(
mu_);
182 std::unique_lock<std::mutex>
l(
mu_);
190 if (points_remaining_ != 0) {
192 (*feature_list_)[feature_distribution_(
generator_)];
193 std::cout <<
"Visiting point "
194 <<
f.location().latitude() / coord_factor_ <<
", "
195 <<
f.location().longitude() / coord_factor_ << std::endl;
196 StartWrite(&
f.location());
205 int points_remaining_ = 10;
210 std::uniform_int_distribution<int> feature_distribution_;
211 std::uniform_int_distribution<int> delay_distribution_;
214 std::condition_variable
cv_;
222 std::cout <<
"Finished trip with " <<
stats.point_count() <<
" points\n"
223 <<
"Passed " <<
stats.feature_count() <<
" features\n"
224 <<
"Travelled " <<
stats.distance() <<
" meters\n"
225 <<
"It took " <<
stats.elapsed_time() <<
" seconds"
228 std::cout <<
"RecordRoute rpc failed." << std::endl;
235 explicit Chatter(RouteGuide::Stub*
stub)
240 notes_iterator_(notes_.begin()) {
243 StartRead(&server_note_);
246 void OnWriteDone(
bool )
override { NextWrite(); }
247 void OnReadDone(
bool ok)
override {
249 std::cout <<
"Got message " << server_note_.message() <<
" at "
250 << server_note_.location().latitude() <<
", "
251 << server_note_.location().longitude() << std::endl;
252 StartRead(&server_note_);
255 void OnDone(
const Status& s)
override {
256 std::unique_lock<std::mutex>
l(
mu_);
262 std::unique_lock<std::mutex>
l(
mu_);
269 if (notes_iterator_ != notes_.end()) {
270 const auto&
note = *notes_iterator_;
271 std::cout <<
"Sending message " <<
note.message() <<
" at "
272 <<
note.location().latitude() <<
", "
273 <<
note.location().longitude() << std::endl;
281 const std::vector<RouteNote> notes_;
282 std::vector<RouteNote>::const_iterator notes_iterator_;
285 std::condition_variable
cv_;
290 Chatter chatter(
stub_.get());
293 std::cout <<
"RouteChat rpc failed." << std::endl;
302 std::condition_variable
cv;
304 stub_->async()->GetFeature(
309 std::cout <<
"GetFeature rpc failed." << std::endl;
311 }
else if (!feature->has_location()) {
312 std::cout <<
"Server returns incomplete feature." << std::endl;
314 }
else if (feature->name().empty()) {
315 std::cout <<
"Found no feature at "
316 << feature->location().latitude() / kCoordFactor_ <<
", "
317 << feature->location().longitude() / kCoordFactor_
321 std::cout <<
"Found feature called " << feature->name() <<
" at "
322 << feature->location().latitude() / kCoordFactor_ <<
", "
323 << feature->location().longitude() / kCoordFactor_
327 std::lock_guard<std::mutex> lock(
mu);
332 std::unique_lock<std::mutex> lock(
mu);
338 std::unique_ptr<RouteGuide::Stub>
stub_;
342 int main(
int argc,
char** argv) {
350 std::cout <<
"-------------- GetFeature --------------" << std::endl;
352 std::cout <<
"-------------- ListFeatures --------------" << std::endl;
354 std::cout <<
"-------------- RecordRoute --------------" << std::endl;
356 std::cout <<
"-------------- RouteChat --------------" << std::endl;