route_guide_server.cc 5.8 KB

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