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