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