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