|
|
@ -41,12 +41,12 @@ using grpc::ClientReader; |
|
|
|
using grpc::ClientReaderWriter; |
|
|
|
using grpc::ClientReaderWriter; |
|
|
|
using grpc::ClientWriter; |
|
|
|
using grpc::ClientWriter; |
|
|
|
using grpc::Status; |
|
|
|
using grpc::Status; |
|
|
|
using routeguide::Point; |
|
|
|
|
|
|
|
using routeguide::Feature; |
|
|
|
using routeguide::Feature; |
|
|
|
|
|
|
|
using routeguide::Point; |
|
|
|
using routeguide::Rectangle; |
|
|
|
using routeguide::Rectangle; |
|
|
|
using routeguide::RouteSummary; |
|
|
|
|
|
|
|
using routeguide::RouteNote; |
|
|
|
|
|
|
|
using routeguide::RouteGuide; |
|
|
|
using routeguide::RouteGuide; |
|
|
|
|
|
|
|
using routeguide::RouteNote; |
|
|
|
|
|
|
|
using routeguide::RouteSummary; |
|
|
|
|
|
|
|
|
|
|
|
Point MakePoint(long latitude, long longitude) { |
|
|
|
Point MakePoint(long latitude, long longitude) { |
|
|
|
Point p; |
|
|
|
Point p; |
|
|
@ -55,16 +55,15 @@ Point MakePoint(long latitude, long longitude) { |
|
|
|
return p; |
|
|
|
return p; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Feature MakeFeature(const std::string& name, |
|
|
|
Feature MakeFeature(const std::string& name, long latitude, long longitude) { |
|
|
|
long latitude, long longitude) { |
|
|
|
|
|
|
|
Feature f; |
|
|
|
Feature f; |
|
|
|
f.set_name(name); |
|
|
|
f.set_name(name); |
|
|
|
f.mutable_location()->CopyFrom(MakePoint(latitude, longitude)); |
|
|
|
f.mutable_location()->CopyFrom(MakePoint(latitude, longitude)); |
|
|
|
return f; |
|
|
|
return f; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
RouteNote MakeRouteNote(const std::string& message, |
|
|
|
RouteNote MakeRouteNote(const std::string& message, long latitude, |
|
|
|
long latitude, long longitude) { |
|
|
|
long longitude) { |
|
|
|
RouteNote n; |
|
|
|
RouteNote n; |
|
|
|
n.set_message(message); |
|
|
|
n.set_message(message); |
|
|
|
n.mutable_location()->CopyFrom(MakePoint(latitude, longitude)); |
|
|
|
n.mutable_location()->CopyFrom(MakePoint(latitude, longitude)); |
|
|
@ -102,10 +101,9 @@ class RouteGuideClient { |
|
|
|
std::unique_ptr<ClientReader<Feature> > reader( |
|
|
|
std::unique_ptr<ClientReader<Feature> > reader( |
|
|
|
stub_->ListFeatures(&context, rect)); |
|
|
|
stub_->ListFeatures(&context, rect)); |
|
|
|
while (reader->Read(&feature)) { |
|
|
|
while (reader->Read(&feature)) { |
|
|
|
std::cout << "Found feature called " |
|
|
|
std::cout << "Found feature called " << feature.name() << " at " |
|
|
|
<< feature.name() << " at " |
|
|
|
<< feature.location().latitude() / kCoordFactor_ << ", " |
|
|
|
<< feature.location().latitude()/kCoordFactor_ << ", " |
|
|
|
<< feature.location().longitude() / kCoordFactor_ << std::endl; |
|
|
|
<< feature.location().longitude()/kCoordFactor_ << std::endl; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
Status status = reader->Finish(); |
|
|
|
Status status = reader->Finish(); |
|
|
|
if (status.ok()) { |
|
|
|
if (status.ok()) { |
|
|
@ -125,22 +123,21 @@ class RouteGuideClient { |
|
|
|
std::default_random_engine generator(seed); |
|
|
|
std::default_random_engine generator(seed); |
|
|
|
std::uniform_int_distribution<int> feature_distribution( |
|
|
|
std::uniform_int_distribution<int> feature_distribution( |
|
|
|
0, feature_list_.size() - 1); |
|
|
|
0, feature_list_.size() - 1); |
|
|
|
std::uniform_int_distribution<int> delay_distribution( |
|
|
|
std::uniform_int_distribution<int> delay_distribution(500, 1500); |
|
|
|
500, 1500); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<ClientWriter<Point> > writer( |
|
|
|
std::unique_ptr<ClientWriter<Point> > writer( |
|
|
|
stub_->RecordRoute(&context, &stats)); |
|
|
|
stub_->RecordRoute(&context, &stats)); |
|
|
|
for (int i = 0; i < kPoints; i++) { |
|
|
|
for (int i = 0; i < kPoints; i++) { |
|
|
|
const Feature& f = feature_list_[feature_distribution(generator)]; |
|
|
|
const Feature& f = feature_list_[feature_distribution(generator)]; |
|
|
|
std::cout << "Visiting point " |
|
|
|
std::cout << "Visiting point " << f.location().latitude() / kCoordFactor_ |
|
|
|
<< f.location().latitude()/kCoordFactor_ << ", " |
|
|
|
<< ", " << f.location().longitude() / kCoordFactor_ |
|
|
|
<< f.location().longitude()/kCoordFactor_ << std::endl; |
|
|
|
<< std::endl; |
|
|
|
if (!writer->Write(f.location())) { |
|
|
|
if (!writer->Write(f.location())) { |
|
|
|
// Broken stream.
|
|
|
|
// Broken stream.
|
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds( |
|
|
|
std::this_thread::sleep_for( |
|
|
|
delay_distribution(generator))); |
|
|
|
std::chrono::milliseconds(delay_distribution(generator))); |
|
|
|
} |
|
|
|
} |
|
|
|
writer->WritesDone(); |
|
|
|
writer->WritesDone(); |
|
|
|
Status status = writer->Finish(); |
|
|
|
Status status = writer->Finish(); |
|
|
@ -162,14 +159,13 @@ class RouteGuideClient { |
|
|
|
stub_->RouteChat(&context)); |
|
|
|
stub_->RouteChat(&context)); |
|
|
|
|
|
|
|
|
|
|
|
std::thread writer([stream]() { |
|
|
|
std::thread writer([stream]() { |
|
|
|
std::vector<RouteNote> notes{ |
|
|
|
std::vector<RouteNote> notes{MakeRouteNote("First message", 0, 0), |
|
|
|
MakeRouteNote("First message", 0, 0), |
|
|
|
MakeRouteNote("Second message", 0, 1), |
|
|
|
MakeRouteNote("Second message", 0, 1), |
|
|
|
MakeRouteNote("Third message", 1, 0), |
|
|
|
MakeRouteNote("Third message", 1, 0), |
|
|
|
MakeRouteNote("Fourth message", 0, 0)}; |
|
|
|
MakeRouteNote("Fourth message", 0, 0)}; |
|
|
|
|
|
|
|
for (const RouteNote& note : notes) { |
|
|
|
for (const RouteNote& note : notes) { |
|
|
|
std::cout << "Sending message " << note.message() |
|
|
|
std::cout << "Sending message " << note.message() << " at " |
|
|
|
<< " at " << note.location().latitude() << ", " |
|
|
|
<< note.location().latitude() << ", " |
|
|
|
<< note.location().longitude() << std::endl; |
|
|
|
<< note.location().longitude() << std::endl; |
|
|
|
stream->Write(note); |
|
|
|
stream->Write(note); |
|
|
|
} |
|
|
|
} |
|
|
@ -178,8 +174,8 @@ class RouteGuideClient { |
|
|
|
|
|
|
|
|
|
|
|
RouteNote server_note; |
|
|
|
RouteNote server_note; |
|
|
|
while (stream->Read(&server_note)) { |
|
|
|
while (stream->Read(&server_note)) { |
|
|
|
std::cout << "Got message " << server_note.message() |
|
|
|
std::cout << "Got message " << server_note.message() << " at " |
|
|
|
<< " at " << server_note.location().latitude() << ", " |
|
|
|
<< server_note.location().latitude() << ", " |
|
|
|
<< server_note.location().longitude() << std::endl; |
|
|
|
<< server_note.location().longitude() << std::endl; |
|
|
|
} |
|
|
|
} |
|
|
|
writer.join(); |
|
|
|
writer.join(); |
|
|
@ -190,7 +186,6 @@ class RouteGuideClient { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
|
|
|
|
|
|
|
|
bool GetOneFeature(const Point& point, Feature* feature) { |
|
|
|
bool GetOneFeature(const Point& point, Feature* feature) { |
|
|
|
ClientContext context; |
|
|
|
ClientContext context; |
|
|
|
Status status = stub_->GetFeature(&context, point, feature); |
|
|
|
Status status = stub_->GetFeature(&context, point, feature); |
|
|
@ -204,12 +199,12 @@ class RouteGuideClient { |
|
|
|
} |
|
|
|
} |
|
|
|
if (feature->name().empty()) { |
|
|
|
if (feature->name().empty()) { |
|
|
|
std::cout << "Found no feature at " |
|
|
|
std::cout << "Found no feature at " |
|
|
|
<< feature->location().latitude()/kCoordFactor_ << ", " |
|
|
|
<< feature->location().latitude() / kCoordFactor_ << ", " |
|
|
|
<< feature->location().longitude()/kCoordFactor_ << std::endl; |
|
|
|
<< feature->location().longitude() / kCoordFactor_ << std::endl; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
std::cout << "Found feature called " << feature->name() << " at " |
|
|
|
std::cout << "Found feature called " << feature->name() << " at " |
|
|
|
<< feature->location().latitude()/kCoordFactor_ << ", " |
|
|
|
<< feature->location().latitude() / kCoordFactor_ << ", " |
|
|
|
<< feature->location().longitude()/kCoordFactor_ << std::endl; |
|
|
|
<< feature->location().longitude() / kCoordFactor_ << std::endl; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|