| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 | 
							- /*
 
-  *
 
-  * Copyright 2015, Google Inc.
 
-  * All rights reserved.
 
-  *
 
-  * Redistribution and use in source and binary forms, with or without
 
-  * modification, are permitted provided that the following conditions are
 
-  * met:
 
-  *
 
-  *     * Redistributions of source code must retain the above copyright
 
-  * notice, this list of conditions and the following disclaimer.
 
-  *     * Redistributions in binary form must reproduce the above
 
-  * copyright notice, this list of conditions and the following disclaimer
 
-  * in the documentation and/or other materials provided with the
 
-  * distribution.
 
-  *     * Neither the name of Google Inc. nor the names of its
 
-  * contributors may be used to endorse or promote products derived from
 
-  * this software without specific prior written permission.
 
-  *
 
-  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
-  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
-  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
-  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
-  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
-  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
-  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
-  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
-  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
-  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
-  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-  *
 
-  */
 
- #include <chrono>
 
- #include <fstream>
 
- #include <iostream>
 
- #include <memory>
 
- #include <random>
 
- #include <sstream>
 
- #include <string>
 
- #include <thread>
 
- #include <grpc/grpc.h>
 
- #include <grpc++/channel_arguments.h>
 
- #include <grpc++/channel_interface.h>
 
- #include <grpc++/client_context.h>
 
- #include <grpc++/create_channel.h>
 
- #include <grpc++/status.h>
 
- #include <grpc++/stream.h>
 
- #include "route_guide.pb.h"
 
- using grpc::ChannelArguments;
 
- using grpc::ChannelInterface;
 
- using grpc::ClientContext;
 
- using grpc::ClientReader;
 
- using grpc::ClientReaderWriter;
 
- using grpc::ClientWriter;
 
- using grpc::Status;
 
- using examples::Point;
 
- using examples::Feature;
 
- using examples::Rectangle;
 
- using examples::RouteSummary;
 
- using examples::RouteNote;
 
- using examples::RouteGuide;
 
- Point MakePoint(long latitude, long longitude) {
 
-   Point p;
 
-   p.set_latitude(latitude);
 
-   p.set_longitude(longitude);
 
-   return p;
 
- }
 
- Feature MakeFeature(const std::string& name,
 
-                     long latitude, long longitude) {
 
-   Feature f;
 
-   f.set_name(name);
 
-   f.mutable_location()->CopyFrom(MakePoint(latitude, longitude));
 
-   return f;
 
- }
 
- RouteNote MakeRouteNote(const std::string& message,
 
-                         long latitude, long longitude) {
 
-   RouteNote n;
 
-   n.set_message(message);
 
-   n.mutable_location()->CopyFrom(MakePoint(latitude, longitude));
 
-   return n;
 
- }
 
- bool ParseDb(const std::string& stream, std::vector<Feature>* feature_list) {
 
-   // TODO
 
- }
 
- class RouteGuideClient {
 
-  public:
 
-   RouteGuideClient(std::shared_ptr<ChannelInterface> channel)
 
-       : stub_(RouteGuide::NewStub(channel)) {}
 
-   void GetFeature() {
 
-     Point point;
 
-     Feature feature;
 
-     point = MakePoint(409146138, -746188906);
 
-     GetOneFeature(point, &feature);
 
-     point = MakePoint(0, 0);
 
-     GetOneFeature(point, &feature);
 
-   }
 
-   void ListFeatures() {
 
-     Rectangle rect;
 
-     Feature feature;
 
-     ClientContext context;
 
-     rect.mutable_lo()->set_latitude(400000000);
 
-     rect.mutable_lo()->set_longitude(-750000000);
 
-     rect.mutable_hi()->set_latitude(420000000);
 
-     rect.mutable_hi()->set_longitude(-730000000);
 
-     std::cout << "Looking for features between 40, -75 and 42, -73"
 
-               << std::endl;
 
-     std::unique_ptr<ClientReader<Feature> > reader(
 
-         stub_->ListFeatures(&context, rect));
 
-     while (reader->Read(&feature)) {
 
-       std::cout << "Found feature called "
 
-                 << feature.name() << " at "
 
-                 << feature.location().latitude()/kCoordFactor_ << ", "
 
-                 << feature.location().latitude()/kCoordFactor_ << std::endl;
 
-     }
 
-     Status status = reader->Finish();
 
-     if (status.IsOk()) {
 
-       std::cout << "ListFeatures rpc succeeded." << std::endl;
 
-     } else {
 
-       std::cout << "ListFeatures rpc failed." << std::endl;
 
-     }
 
-   }
 
-   void RecordRoute() {
 
-     Point point;
 
-     RouteSummary stats;
 
-     ClientContext context;
 
-     const int kPoints = 10;
 
-     std::default_random_engine generator;
 
-     std::uniform_int_distribution<int> feature_distribution(
 
-         0, feature_list_.size() - 1);
 
-     std::uniform_int_distribution<int> delay_distribution(
 
-         500, 1500);
 
-     std::unique_ptr<ClientWriter<Point> > writer(
 
-         stub_->RecordRoute(&context, &stats));
 
-     for (int i = 0; i < kPoints; i++) {
 
-       const Feature& f = feature_list_[feature_distribution(generator)];
 
-       std::cout << "Visiting point "
 
-                 << f.location().latitude()/kCoordFactor_ << ", "
 
-                 << f.location().longitude()/kCoordFactor_ << std::endl;
 
-       if (!writer->Write(f.location())) {
 
-         // Broken stream.
 
-         break;
 
-       }
 
-       std::this_thread::sleep_for(std::chrono::milliseconds(
 
-           delay_distribution(generator)));
 
-     }
 
-     writer->WritesDone();
 
-     Status status = writer->Finish();
 
-     if (status.IsOk()) {
 
-       std::cout << "Finished trip with " << stats.point_count() << " points\n"
 
-                 << "Passed " << stats.feature_count() << " features\n"
 
-                 << "Travelled " << stats.distance() << " meters\n"
 
-                 << "It took " << stats.elapsed_time() << " seconds"
 
-                 << std::endl;
 
-     } else {
 
-       std::cout << "RecordRoute rpc failed." << std::endl;
 
-     }
 
-   }
 
-   void RouteChat() {
 
-     ClientContext context;
 
-     std::shared_ptr<ClientReaderWriter<RouteNote, RouteNote> > stream(
 
-         stub_->RouteChat(&context));
 
-     std::thread writer([stream]() {
 
-       std::vector<RouteNote> notes{
 
-         MakeRouteNote("First message", 0, 0),
 
-         MakeRouteNote("Second message", 0, 1),
 
-         MakeRouteNote("Third message", 1, 0),
 
-         MakeRouteNote("Fourth message", 0, 0)};
 
-       for (const RouteNote& note : notes) {
 
-         std::cout << "Sending message " << note.message()
 
-                   << " at " << note.location().latitude() << ", "
 
-                   << note.location().longitude() << std::endl;
 
-         stream->Write(note);
 
-       }
 
-       stream->WritesDone();
 
-     });
 
-     RouteNote server_note;
 
-     while (stream->Read(&server_note)) {
 
-       std::cout << "Got message " << server_note.message()
 
-                 << " at " << server_note.location().latitude() << ", "
 
-                 << server_note.location().longitude() << std::endl;
 
-     }
 
-     writer.join();
 
-     Status status = stream->Finish();
 
-     if (!status.IsOk()) {
 
-       std::cout << "RouteChat rpc failed." << std::endl;
 
-     }
 
-   }
 
-   void Shutdown() { stub_.reset(); }
 
-   void FillFeatureList(const std::string& db_path) {
 
-     if (db_path.empty()) {
 
-       return;
 
-     }
 
-     std::ifstream db_file(db_path);
 
-     if (!db_file.is_open()) {
 
-       std::cout << "Failed to open " << db_path << std::endl;
 
-     }
 
-     std::stringstream db;
 
-     db << db_file.rdbuf();
 
-     ParseDb(db.str(), &feature_list_);
 
-   }
 
-  private:
 
-   bool GetOneFeature(const Point& point, Feature* feature) {
 
-     ClientContext context;
 
-     Status status = stub_->GetFeature(&context, point, feature);
 
-     if (!status.IsOk()) {
 
-       std::cout << "GetFeature rpc failed." << std::endl;
 
-       return false;
 
-     }
 
-     if (!feature->has_location()) {
 
-       std::cout << "Server returns incomplete feature." << std::endl;
 
-       return false;
 
-     }
 
-     if (feature->name().empty()) {
 
-       std::cout << "Found no feature at "
 
-                 << feature->location().latitude()/kCoordFactor_ << ", "
 
-                 << feature->location().longitude()/kCoordFactor_ << std::endl;
 
-     } else {
 
-       std::cout << "Found feature called " << feature->name()  << " at "
 
-                 << feature->location().latitude()/kCoordFactor_ << ", "
 
-                 << feature->location().longitude()/kCoordFactor_ << std::endl;
 
-     }
 
-     return true;
 
-   }
 
-   const float kCoordFactor_ = 10000000.0;
 
-   std::unique_ptr<RouteGuide::Stub> stub_;
 
-   std::vector<Feature> feature_list_;
 
- };
 
- int main(int argc, char** argv) {
 
-   grpc_init();
 
-   RouteGuideClient guide(
 
-       grpc::CreateChannel("localhost:50051", ChannelArguments()));
 
-   std::string db_path;
 
-   std::string arg_str("--db_path");
 
-   if (argc > 1) {
 
-     std::string argv_1 = argv[1];
 
-     size_t start_position = argv_1.find(arg_str);
 
-     if (start_position != std::string::npos) {
 
-       start_position += arg_str.size();
 
-       if (argv_1[start_position] == ' ' ||
 
-           argv_1[start_position] == '=') {
 
-         db_path = argv_1.substr(start_position + 1);
 
-       }
 
-     }
 
-   }
 
-   guide.FillFeatureList(db_path);
 
-   guide.GetFeature();
 
-   guide.ListFeatures();
 
-   guide.RecordRoute();
 
-   guide.RouteChat();
 
-   guide.Shutdown();
 
-   grpc_shutdown();
 
- }
 
 
  |