3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
33 #include "examples/protos/route_guide.grpc.pb.h"
35 #include "route_guide.grpc.pb.h"
39 using grpc::ClientContext;
40 using grpc::ClientReader;
41 using grpc::ClientReaderWriter;
42 using grpc::ClientWriter;
44 using routeguide::Feature;
45 using routeguide::Point;
46 using routeguide::Rectangle;
47 using routeguide::RouteGuide;
48 using routeguide::RouteNote;
49 using routeguide::RouteSummary;
51 Point MakePoint(long latitude, long longitude) {
53 p.set_latitude(latitude);
54 p.set_longitude(longitude);
58 Feature MakeFeature(const std::string& name, long latitude, long longitude) {
61 f.mutable_location()->CopyFrom(MakePoint(latitude, longitude));
65 RouteNote MakeRouteNote(const std::string& message, long latitude,
68 n.set_message(message);
69 n.mutable_location()->CopyFrom(MakePoint(latitude, longitude));
73 class RouteGuideClient {
75 RouteGuideClient(std::shared_ptr<Channel> channel, const std::string& db)
76 : stub_(RouteGuide::NewStub(channel)) {
77 routeguide::ParseDb(db, &feature_list_);
83 point = MakePoint(409146138, -746188906);
84 GetOneFeature(point, &feature);
85 point = MakePoint(0, 0);
86 GetOneFeature(point, &feature);
90 routeguide::Rectangle rect;
92 ClientContext context;
94 rect.mutable_lo()->set_latitude(400000000);
95 rect.mutable_lo()->set_longitude(-750000000);
96 rect.mutable_hi()->set_latitude(420000000);
97 rect.mutable_hi()->set_longitude(-730000000);
98 std::cout << "Looking for features between 40, -75 and 42, -73"
101 std::unique_ptr<ClientReader<Feature> > reader(
102 stub_->ListFeatures(&context, rect));
103 while (reader->Read(&feature)) {
104 std::cout << "Found feature called " << feature.name() << " at "
105 << feature.location().latitude() / kCoordFactor_ << ", "
106 << feature.location().longitude() / kCoordFactor_ << std::endl;
108 Status status = reader->Finish();
110 std::cout << "ListFeatures rpc succeeded." << std::endl;
112 std::cout << "ListFeatures rpc failed." << std::endl;
119 ClientContext context;
120 const int kPoints = 10;
121 unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
123 std::default_random_engine generator(seed);
124 std::uniform_int_distribution<int> feature_distribution(
125 0, feature_list_.size() - 1);
126 std::uniform_int_distribution<int> delay_distribution(500, 1500);
128 std::unique_ptr<ClientWriter<Point> > writer(
129 stub_->RecordRoute(&context, &stats));
130 for (int i = 0; i < kPoints; i++) {
131 const Feature& f = feature_list_[feature_distribution(generator)];
132 std::cout << "Visiting point " << f.location().latitude() / kCoordFactor_
133 << ", " << f.location().longitude() / kCoordFactor_
135 if (!writer->Write(f.location())) {
139 std::this_thread::sleep_for(
140 std::chrono::milliseconds(delay_distribution(generator)));
142 writer->WritesDone();
143 Status status = writer->Finish();
145 std::cout << "Finished trip with " << stats.point_count() << " points\n"
146 << "Passed " << stats.feature_count() << " features\n"
147 << "Travelled " << stats.distance() << " meters\n"
148 << "It took " << stats.elapsed_time() << " seconds"
151 std::cout << "RecordRoute rpc failed." << std::endl;
156 ClientContext context;
158 std::shared_ptr<ClientReaderWriter<RouteNote, RouteNote> > stream(
159 stub_->RouteChat(&context));
161 std::thread writer([stream]() {
162 std::vector<RouteNote> notes{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() << " at "
168 << note.location().latitude() << ", "
169 << note.location().longitude() << std::endl;
172 stream->WritesDone();
175 RouteNote server_note;
176 while (stream->Read(&server_note)) {
177 std::cout << "Got message " << server_note.message() << " at "
178 << server_note.location().latitude() << ", "
179 << server_note.location().longitude() << std::endl;
182 Status status = stream->Finish();
184 std::cout << "RouteChat rpc failed." << std::endl;
189 bool GetOneFeature(const Point& point, Feature* feature) {
190 ClientContext context;
191 Status status = stub_->GetFeature(&context, point, feature);
193 std::cout << "GetFeature rpc failed." << std::endl;
196 if (!feature->has_location()) {
197 std::cout << "Server returns incomplete feature." << std::endl;
200 if (feature->name().empty()) {
201 std::cout << "Found no feature at "
202 << feature->location().latitude() / kCoordFactor_ << ", "
203 << feature->location().longitude() / kCoordFactor_ << std::endl;
205 std::cout << "Found feature called " << feature->name() << " at "
206 << feature->location().latitude() / kCoordFactor_ << ", "
207 << feature->location().longitude() / kCoordFactor_ << std::endl;
212 const float kCoordFactor_ = 10000000.0;
213 std::unique_ptr<RouteGuide::Stub> stub_;
214 std::vector<Feature> feature_list_;
217 int main(int argc, char** argv) {
218 // Expect only arg: --db_path=path/to/route_guide_db.json.
219 std::string db = routeguide::GetDbFileContent(argc, argv);
220 RouteGuideClient guide(
221 grpc::CreateChannel("localhost:50051",
222 grpc::InsecureChannelCredentials()),
225 std::cout << "-------------- GetFeature --------------" << std::endl;
227 std::cout << "-------------- ListFeatures --------------" << std::endl;
228 guide.ListFeatures();
229 std::cout << "-------------- RecordRoute --------------" << std::endl;
231 std::cout << "-------------- RouteChat --------------" << std::endl;