• 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 <algorithm>
20 #include <chrono>
21 #include <cmath>
22 #include <iostream>
23 #include <memory>
24 #include <string>
25 
26 #include <grpc/grpc.h>
27 #include <grpcpp/server.h>
28 #include <grpcpp/server_builder.h>
29 #include <grpcpp/server_context.h>
30 #include <grpcpp/security/server_credentials.h>
31 #include "helper.h"
32 #include "route_guide.grpc.pb.h"
33 
34 using grpc::Server;
35 using grpc::ServerBuilder;
36 using grpc::ServerContext;
37 using grpc::ServerReader;
38 using grpc::ServerReaderWriter;
39 using grpc::ServerWriter;
40 using grpc::Status;
41 using routeguide::Point;
42 using routeguide::Feature;
43 using routeguide::Rectangle;
44 using routeguide::RouteSummary;
45 using routeguide::RouteNote;
46 using routeguide::RouteGuide;
47 using std::chrono::system_clock;
48 
49 
ConvertToRadians(float num)50 float ConvertToRadians(float num) {
51   return num * 3.1415926 /180;
52 }
53 
54 // The formula is based on http://mathforum.org/library/drmath/view/51879.html
GetDistance(const Point & start,const Point & end)55 float GetDistance(const Point& start, const Point& end) {
56   const float kCoordFactor = 10000000.0;
57   float lat_1 = start.latitude() / kCoordFactor;
58   float lat_2 = end.latitude() / kCoordFactor;
59   float lon_1 = start.longitude() / kCoordFactor;
60   float lon_2 = end.longitude() / kCoordFactor;
61   float lat_rad_1 = ConvertToRadians(lat_1);
62   float lat_rad_2 = ConvertToRadians(lat_2);
63   float delta_lat_rad = ConvertToRadians(lat_2-lat_1);
64   float delta_lon_rad = ConvertToRadians(lon_2-lon_1);
65 
66   float a = pow(sin(delta_lat_rad/2), 2) + cos(lat_rad_1) * cos(lat_rad_2) *
67             pow(sin(delta_lon_rad/2), 2);
68   float c = 2 * atan2(sqrt(a), sqrt(1-a));
69   int R = 6371000; // metres
70 
71   return R * c;
72 }
73 
GetFeatureName(const Point & point,const std::vector<Feature> & feature_list)74 std::string GetFeatureName(const Point& point,
75                            const std::vector<Feature>& feature_list) {
76   for (const Feature& f : feature_list) {
77     if (f.location().latitude() == point.latitude() &&
78         f.location().longitude() == point.longitude()) {
79       return f.name();
80     }
81   }
82   return "";
83 }
84 
85 class RouteGuideImpl final : public RouteGuide::Service {
86  public:
RouteGuideImpl(const std::string & db)87   explicit RouteGuideImpl(const std::string& db) {
88     routeguide::ParseDb(db, &feature_list_);
89   }
90 
GetFeature(ServerContext * context,const Point * point,Feature * feature)91   Status GetFeature(ServerContext* context, const Point* point,
92                     Feature* feature) override {
93     feature->set_name(GetFeatureName(*point, feature_list_));
94     feature->mutable_location()->CopyFrom(*point);
95     return Status::OK;
96   }
97 
ListFeatures(ServerContext * context,const routeguide::Rectangle * rectangle,ServerWriter<Feature> * writer)98   Status ListFeatures(ServerContext* context,
99                       const routeguide::Rectangle* rectangle,
100                       ServerWriter<Feature>* writer) override {
101     auto lo = rectangle->lo();
102     auto hi = rectangle->hi();
103     long left = (std::min)(lo.longitude(), hi.longitude());
104     long right = (std::max)(lo.longitude(), hi.longitude());
105     long top = (std::max)(lo.latitude(), hi.latitude());
106     long bottom = (std::min)(lo.latitude(), hi.latitude());
107     for (const Feature& f : feature_list_) {
108       if (f.location().longitude() >= left &&
109           f.location().longitude() <= right &&
110           f.location().latitude() >= bottom &&
111           f.location().latitude() <= top) {
112         writer->Write(f);
113       }
114     }
115     return Status::OK;
116   }
117 
RecordRoute(ServerContext * context,ServerReader<Point> * reader,RouteSummary * summary)118   Status RecordRoute(ServerContext* context, ServerReader<Point>* reader,
119                      RouteSummary* summary) override {
120     Point point;
121     int point_count = 0;
122     int feature_count = 0;
123     float distance = 0.0;
124     Point previous;
125 
126     system_clock::time_point start_time = system_clock::now();
127     while (reader->Read(&point)) {
128       point_count++;
129       if (!GetFeatureName(point, feature_list_).empty()) {
130         feature_count++;
131       }
132       if (point_count != 1) {
133         distance += GetDistance(previous, point);
134       }
135       previous = point;
136     }
137     system_clock::time_point end_time = system_clock::now();
138     summary->set_point_count(point_count);
139     summary->set_feature_count(feature_count);
140     summary->set_distance(static_cast<long>(distance));
141     auto secs = std::chrono::duration_cast<std::chrono::seconds>(
142         end_time - start_time);
143     summary->set_elapsed_time(secs.count());
144 
145     return Status::OK;
146   }
147 
RouteChat(ServerContext * context,ServerReaderWriter<RouteNote,RouteNote> * stream)148   Status RouteChat(ServerContext* context,
149                    ServerReaderWriter<RouteNote, RouteNote>* stream) override {
150     std::vector<RouteNote> received_notes;
151     RouteNote note;
152     while (stream->Read(&note)) {
153       for (const RouteNote& n : received_notes) {
154         if (n.location().latitude() == note.location().latitude() &&
155             n.location().longitude() == note.location().longitude()) {
156           stream->Write(n);
157         }
158       }
159       received_notes.push_back(note);
160     }
161 
162     return Status::OK;
163   }
164 
165  private:
166 
167   std::vector<Feature> feature_list_;
168 };
169 
RunServer(const std::string & db_path)170 void RunServer(const std::string& db_path) {
171   std::string server_address("0.0.0.0:50051");
172   RouteGuideImpl service(db_path);
173 
174   ServerBuilder builder;
175   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
176   builder.RegisterService(&service);
177   std::unique_ptr<Server> server(builder.BuildAndStart());
178   std::cout << "Server listening on " << server_address << std::endl;
179   server->Wait();
180 }
181 
main(int argc,char ** argv)182 int main(int argc, char** argv) {
183   // Expect only arg: --db_path=path/to/route_guide_db.json.
184   std::string db = routeguide::GetDbFileContent(argc, argv);
185   RunServer(db);
186 
187   return 0;
188 }
189