• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/grpc.h>
20 #include <grpcpp/channel.h>
21 #include <grpcpp/client_context.h>
22 #include <grpcpp/create_channel.h>
23 #include <grpcpp/security/credentials.h>
24 
25 #include <chrono>
26 #include <iostream>
27 #include <memory>
28 #include <random>
29 #include <string>
30 #include <thread>
31 
32 #include "absl/flags/parse.h"
33 #include "helper.h"
34 #ifdef BAZEL_BUILD
35 #include "examples/protos/route_guide.grpc.pb.h"
36 #else
37 #include "route_guide.grpc.pb.h"
38 #endif
39 
40 using grpc::Channel;
41 using grpc::ClientContext;
42 using grpc::ClientReader;
43 using grpc::ClientReaderWriter;
44 using grpc::ClientWriter;
45 using grpc::Status;
46 using routeguide::Feature;
47 using routeguide::Point;
48 using routeguide::Rectangle;
49 using routeguide::RouteGuide;
50 using routeguide::RouteNote;
51 using routeguide::RouteSummary;
52 
MakePoint(long latitude,long longitude)53 Point MakePoint(long latitude, long longitude) {
54   Point p;
55   p.set_latitude(latitude);
56   p.set_longitude(longitude);
57   return p;
58 }
59 
MakeFeature(const std::string & name,long latitude,long longitude)60 Feature MakeFeature(const std::string& name, long latitude, long longitude) {
61   Feature f;
62   f.set_name(name);
63   f.mutable_location()->CopyFrom(MakePoint(latitude, longitude));
64   return f;
65 }
66 
MakeRouteNote(const std::string & message,long latitude,long longitude)67 RouteNote MakeRouteNote(const std::string& message, long latitude,
68                         long longitude) {
69   RouteNote n;
70   n.set_message(message);
71   n.mutable_location()->CopyFrom(MakePoint(latitude, longitude));
72   return n;
73 }
74 
75 class RouteGuideClient {
76  public:
RouteGuideClient(std::shared_ptr<Channel> channel,const std::string & db)77   RouteGuideClient(std::shared_ptr<Channel> channel, const std::string& db)
78       : stub_(RouteGuide::NewStub(channel)) {
79     routeguide::ParseDb(db, &feature_list_);
80   }
81 
GetFeature()82   void GetFeature() {
83     Point point;
84     Feature feature;
85     point = MakePoint(409146138, -746188906);
86     GetOneFeature(point, &feature);
87     point = MakePoint(0, 0);
88     GetOneFeature(point, &feature);
89   }
90 
ListFeatures()91   void ListFeatures() {
92     routeguide::Rectangle rect;
93     Feature feature;
94     ClientContext context;
95 
96     rect.mutable_lo()->set_latitude(400000000);
97     rect.mutable_lo()->set_longitude(-750000000);
98     rect.mutable_hi()->set_latitude(420000000);
99     rect.mutable_hi()->set_longitude(-730000000);
100     std::cout << "Looking for features between 40, -75 and 42, -73"
101               << std::endl;
102 
103     std::unique_ptr<ClientReader<Feature> > reader(
104         stub_->ListFeatures(&context, rect));
105     while (reader->Read(&feature)) {
106       std::cout << "Found feature called " << feature.name() << " at "
107                 << feature.location().latitude() / kCoordFactor_ << ", "
108                 << feature.location().longitude() / kCoordFactor_ << std::endl;
109     }
110     Status status = reader->Finish();
111     if (status.ok()) {
112       std::cout << "ListFeatures rpc succeeded." << std::endl;
113     } else {
114       std::cout << "ListFeatures rpc failed." << std::endl;
115     }
116   }
117 
RecordRoute()118   void RecordRoute() {
119     Point point;
120     RouteSummary stats;
121     ClientContext context;
122     const int kPoints = 10;
123     unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
124 
125     std::default_random_engine generator(seed);
126     std::uniform_int_distribution<int> feature_distribution(
127         0, feature_list_.size() - 1);
128     std::uniform_int_distribution<int> delay_distribution(500, 1500);
129 
130     std::unique_ptr<ClientWriter<Point> > writer(
131         stub_->RecordRoute(&context, &stats));
132     for (int i = 0; i < kPoints; i++) {
133       const Feature& f = feature_list_[feature_distribution(generator)];
134       std::cout << "Visiting point " << f.location().latitude() / kCoordFactor_
135                 << ", " << f.location().longitude() / kCoordFactor_
136                 << std::endl;
137       if (!writer->Write(f.location())) {
138         // Broken stream.
139         break;
140       }
141       std::this_thread::sleep_for(
142           std::chrono::milliseconds(delay_distribution(generator)));
143     }
144     writer->WritesDone();
145     Status status = writer->Finish();
146     if (status.ok()) {
147       std::cout << "Finished trip with " << stats.point_count() << " points\n"
148                 << "Passed " << stats.feature_count() << " features\n"
149                 << "Travelled " << stats.distance() << " meters\n"
150                 << "It took " << stats.elapsed_time() << " seconds"
151                 << std::endl;
152     } else {
153       std::cout << "RecordRoute rpc failed." << std::endl;
154     }
155   }
156 
RouteChat()157   void RouteChat() {
158     ClientContext context;
159 
160     std::shared_ptr<ClientReaderWriter<RouteNote, RouteNote> > stream(
161         stub_->RouteChat(&context));
162 
163     std::thread writer([stream]() {
164       std::vector<RouteNote> notes{MakeRouteNote("First message", 0, 0),
165                                    MakeRouteNote("Second message", 0, 1),
166                                    MakeRouteNote("Third message", 1, 0),
167                                    MakeRouteNote("Fourth message", 0, 0)};
168       for (const RouteNote& note : notes) {
169         std::cout << "Sending message " << note.message() << " at "
170                   << note.location().latitude() << ", "
171                   << note.location().longitude() << std::endl;
172         stream->Write(note);
173       }
174       stream->WritesDone();
175     });
176 
177     RouteNote server_note;
178     while (stream->Read(&server_note)) {
179       std::cout << "Got message " << server_note.message() << " at "
180                 << server_note.location().latitude() << ", "
181                 << server_note.location().longitude() << std::endl;
182     }
183     writer.join();
184     Status status = stream->Finish();
185     if (!status.ok()) {
186       std::cout << "RouteChat rpc failed." << std::endl;
187     }
188   }
189 
190  private:
GetOneFeature(const Point & point,Feature * feature)191   bool GetOneFeature(const Point& point, Feature* feature) {
192     ClientContext context;
193     Status status = stub_->GetFeature(&context, point, feature);
194     if (!status.ok()) {
195       std::cout << "GetFeature rpc failed." << std::endl;
196       return false;
197     }
198     if (!feature->has_location()) {
199       std::cout << "Server returns incomplete feature." << std::endl;
200       return false;
201     }
202     if (feature->name().empty()) {
203       std::cout << "Found no feature at "
204                 << feature->location().latitude() / kCoordFactor_ << ", "
205                 << feature->location().longitude() / kCoordFactor_ << std::endl;
206     } else {
207       std::cout << "Found feature called " << feature->name() << " at "
208                 << feature->location().latitude() / kCoordFactor_ << ", "
209                 << feature->location().longitude() / kCoordFactor_ << std::endl;
210     }
211     return true;
212   }
213 
214   const float kCoordFactor_ = 10000000.0;
215   std::unique_ptr<RouteGuide::Stub> stub_;
216   std::vector<Feature> feature_list_;
217 };
218 
main(int argc,char ** argv)219 int main(int argc, char** argv) {
220   absl::ParseCommandLine(argc, argv);
221   // Expect only arg: --db_path=path/to/route_guide_db.json.
222   std::string db = routeguide::GetDbFileContent(argc, argv);
223   RouteGuideClient guide(
224       grpc::CreateChannel("localhost:50051",
225                           grpc::InsecureChannelCredentials()),
226       db);
227 
228   std::cout << "-------------- GetFeature --------------" << std::endl;
229   guide.GetFeature();
230   std::cout << "-------------- ListFeatures --------------" << std::endl;
231   guide.ListFeatures();
232   std::cout << "-------------- RecordRoute --------------" << std::endl;
233   guide.RecordRoute();
234   std::cout << "-------------- RouteChat --------------" << std::endl;
235   guide.RouteChat();
236 
237   return 0;
238 }
239