OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <chrono> |
| 35 #include <iostream> |
| 36 #include <memory> |
| 37 #include <random> |
| 38 #include <string> |
| 39 #include <thread> |
| 40 |
| 41 #include <grpc/grpc.h> |
| 42 #include <grpc++/channel.h> |
| 43 #include <grpc++/client_context.h> |
| 44 #include <grpc++/create_channel.h> |
| 45 #include <grpc++/security/credentials.h> |
| 46 #include "helper.h" |
| 47 #include "route_guide.grpc.pb.h" |
| 48 |
| 49 using grpc::Channel; |
| 50 using grpc::ClientContext; |
| 51 using grpc::ClientReader; |
| 52 using grpc::ClientReaderWriter; |
| 53 using grpc::ClientWriter; |
| 54 using grpc::Status; |
| 55 using routeguide::Point; |
| 56 using routeguide::Feature; |
| 57 using routeguide::Rectangle; |
| 58 using routeguide::RouteSummary; |
| 59 using routeguide::RouteNote; |
| 60 using routeguide::RouteGuide; |
| 61 |
| 62 Point MakePoint(long latitude, long longitude) { |
| 63 Point p; |
| 64 p.set_latitude(latitude); |
| 65 p.set_longitude(longitude); |
| 66 return p; |
| 67 } |
| 68 |
| 69 Feature MakeFeature(const std::string& name, |
| 70 long latitude, long longitude) { |
| 71 Feature f; |
| 72 f.set_name(name); |
| 73 f.mutable_location()->CopyFrom(MakePoint(latitude, longitude)); |
| 74 return f; |
| 75 } |
| 76 |
| 77 RouteNote MakeRouteNote(const std::string& message, |
| 78 long latitude, long longitude) { |
| 79 RouteNote n; |
| 80 n.set_message(message); |
| 81 n.mutable_location()->CopyFrom(MakePoint(latitude, longitude)); |
| 82 return n; |
| 83 } |
| 84 |
| 85 class RouteGuideClient { |
| 86 public: |
| 87 RouteGuideClient(std::shared_ptr<Channel> channel, const std::string& db) |
| 88 : stub_(RouteGuide::NewStub(channel)) { |
| 89 routeguide::ParseDb(db, &feature_list_); |
| 90 } |
| 91 |
| 92 void GetFeature() { |
| 93 Point point; |
| 94 Feature feature; |
| 95 point = MakePoint(409146138, -746188906); |
| 96 GetOneFeature(point, &feature); |
| 97 point = MakePoint(0, 0); |
| 98 GetOneFeature(point, &feature); |
| 99 } |
| 100 |
| 101 void ListFeatures() { |
| 102 routeguide::Rectangle rect; |
| 103 Feature feature; |
| 104 ClientContext context; |
| 105 |
| 106 rect.mutable_lo()->set_latitude(400000000); |
| 107 rect.mutable_lo()->set_longitude(-750000000); |
| 108 rect.mutable_hi()->set_latitude(420000000); |
| 109 rect.mutable_hi()->set_longitude(-730000000); |
| 110 std::cout << "Looking for features between 40, -75 and 42, -73" |
| 111 << std::endl; |
| 112 |
| 113 std::unique_ptr<ClientReader<Feature> > reader( |
| 114 stub_->ListFeatures(&context, rect)); |
| 115 while (reader->Read(&feature)) { |
| 116 std::cout << "Found feature called " |
| 117 << feature.name() << " at " |
| 118 << feature.location().latitude()/kCoordFactor_ << ", " |
| 119 << feature.location().longitude()/kCoordFactor_ << std::endl; |
| 120 } |
| 121 Status status = reader->Finish(); |
| 122 if (status.ok()) { |
| 123 std::cout << "ListFeatures rpc succeeded." << std::endl; |
| 124 } else { |
| 125 std::cout << "ListFeatures rpc failed." << std::endl; |
| 126 } |
| 127 } |
| 128 |
| 129 void RecordRoute() { |
| 130 Point point; |
| 131 RouteSummary stats; |
| 132 ClientContext context; |
| 133 const int kPoints = 10; |
| 134 unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); |
| 135 |
| 136 std::default_random_engine generator(seed); |
| 137 std::uniform_int_distribution<int> feature_distribution( |
| 138 0, feature_list_.size() - 1); |
| 139 std::uniform_int_distribution<int> delay_distribution( |
| 140 500, 1500); |
| 141 |
| 142 std::unique_ptr<ClientWriter<Point> > writer( |
| 143 stub_->RecordRoute(&context, &stats)); |
| 144 for (int i = 0; i < kPoints; i++) { |
| 145 const Feature& f = feature_list_[feature_distribution(generator)]; |
| 146 std::cout << "Visiting point " |
| 147 << f.location().latitude()/kCoordFactor_ << ", " |
| 148 << f.location().longitude()/kCoordFactor_ << std::endl; |
| 149 if (!writer->Write(f.location())) { |
| 150 // Broken stream. |
| 151 break; |
| 152 } |
| 153 std::this_thread::sleep_for(std::chrono::milliseconds( |
| 154 delay_distribution(generator))); |
| 155 } |
| 156 writer->WritesDone(); |
| 157 Status status = writer->Finish(); |
| 158 if (status.ok()) { |
| 159 std::cout << "Finished trip with " << stats.point_count() << " points\n" |
| 160 << "Passed " << stats.feature_count() << " features\n" |
| 161 << "Travelled " << stats.distance() << " meters\n" |
| 162 << "It took " << stats.elapsed_time() << " seconds" |
| 163 << std::endl; |
| 164 } else { |
| 165 std::cout << "RecordRoute rpc failed." << std::endl; |
| 166 } |
| 167 } |
| 168 |
| 169 void RouteChat() { |
| 170 ClientContext context; |
| 171 |
| 172 std::shared_ptr<ClientReaderWriter<RouteNote, RouteNote> > stream( |
| 173 stub_->RouteChat(&context)); |
| 174 |
| 175 std::thread writer([stream]() { |
| 176 std::vector<RouteNote> notes{ |
| 177 MakeRouteNote("First message", 0, 0), |
| 178 MakeRouteNote("Second message", 0, 1), |
| 179 MakeRouteNote("Third message", 1, 0), |
| 180 MakeRouteNote("Fourth message", 0, 0)}; |
| 181 for (const RouteNote& note : notes) { |
| 182 std::cout << "Sending message " << note.message() |
| 183 << " at " << note.location().latitude() << ", " |
| 184 << note.location().longitude() << std::endl; |
| 185 stream->Write(note); |
| 186 } |
| 187 stream->WritesDone(); |
| 188 }); |
| 189 |
| 190 RouteNote server_note; |
| 191 while (stream->Read(&server_note)) { |
| 192 std::cout << "Got message " << server_note.message() |
| 193 << " at " << server_note.location().latitude() << ", " |
| 194 << server_note.location().longitude() << std::endl; |
| 195 } |
| 196 writer.join(); |
| 197 Status status = stream->Finish(); |
| 198 if (!status.ok()) { |
| 199 std::cout << "RouteChat rpc failed." << std::endl; |
| 200 } |
| 201 } |
| 202 |
| 203 private: |
| 204 |
| 205 bool GetOneFeature(const Point& point, Feature* feature) { |
| 206 ClientContext context; |
| 207 Status status = stub_->GetFeature(&context, point, feature); |
| 208 if (!status.ok()) { |
| 209 std::cout << "GetFeature rpc failed." << std::endl; |
| 210 return false; |
| 211 } |
| 212 if (!feature->has_location()) { |
| 213 std::cout << "Server returns incomplete feature." << std::endl; |
| 214 return false; |
| 215 } |
| 216 if (feature->name().empty()) { |
| 217 std::cout << "Found no feature at " |
| 218 << feature->location().latitude()/kCoordFactor_ << ", " |
| 219 << feature->location().longitude()/kCoordFactor_ << std::endl; |
| 220 } else { |
| 221 std::cout << "Found feature called " << feature->name() << " at " |
| 222 << feature->location().latitude()/kCoordFactor_ << ", " |
| 223 << feature->location().longitude()/kCoordFactor_ << std::endl; |
| 224 } |
| 225 return true; |
| 226 } |
| 227 |
| 228 const float kCoordFactor_ = 10000000.0; |
| 229 std::unique_ptr<RouteGuide::Stub> stub_; |
| 230 std::vector<Feature> feature_list_; |
| 231 }; |
| 232 |
| 233 int main(int argc, char** argv) { |
| 234 // Expect only arg: --db_path=path/to/route_guide_db.json. |
| 235 std::string db = routeguide::GetDbFileContent(argc, argv); |
| 236 RouteGuideClient guide( |
| 237 grpc::CreateChannel("localhost:50051", |
| 238 grpc::InsecureChannelCredentials()), |
| 239 db); |
| 240 |
| 241 std::cout << "-------------- GetFeature --------------" << std::endl; |
| 242 guide.GetFeature(); |
| 243 std::cout << "-------------- ListFeatures --------------" << std::endl; |
| 244 guide.ListFeatures(); |
| 245 std::cout << "-------------- RecordRoute --------------" << std::endl; |
| 246 guide.RecordRoute(); |
| 247 std::cout << "-------------- RouteChat --------------" << std::endl; |
| 248 guide.RouteChat(); |
| 249 |
| 250 return 0; |
| 251 } |
OLD | NEW |