server.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <iostream>
  2. #include <memory>
  3. #include <string>
  4. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  5. #include <grpcpp/grpcpp.h>
  6. #include <grpcpp/health_check_service_interface.h>
  7. #include "grpc_ex.grpc.pb.h"
  8. using grpc::CallbackServerContext;
  9. using grpc::Server;
  10. using grpc::ServerBuilder;
  11. using grpc::ServerUnaryReactor;
  12. using grpc::Status;
  13. using grpc_ex::MainGateway;
  14. using grpc_ex::Main_Ctrl_Takeoff_Reply;
  15. using grpc_ex::Main_Ctrl_Takeoff_Request;
  16. using grpc_ex::Main_Ctrl_Land_Reply;
  17. using grpc_ex::Main_Ctrl_Land_Request;
  18. using grpc_ex::Main_Ctrl_RTL_Reply;
  19. using grpc_ex::Main_Ctrl_RTL_Request;
  20. using grpc_ex::Main_Ctrl_PositionGlobal_Reply;
  21. using grpc_ex::Main_Ctrl_PositionGlobal_Request;
  22. using grpc_ex::Main_Ctrl_PositionNED_Reply;
  23. using grpc_ex::Main_Ctrl_PositionNED_Request;
  24. class GreeterServiceImpl final : public MainGateway::CallbackService {
  25. ServerUnaryReactor* Ctrl_Takeoff(CallbackServerContext* context,
  26. const Main_Ctrl_Takeoff_Request* request,
  27. Main_Ctrl_Takeoff_Reply* reply) override {
  28. reply->set_message("CMD : Takeoff : Accept");
  29. std::cout << "Takeoff\t" << request->alt_m() << std::endl;
  30. ServerUnaryReactor* reactor = context->DefaultReactor();
  31. reactor->Finish(Status::OK);
  32. return reactor;
  33. }
  34. ServerUnaryReactor* Ctrl_Land(CallbackServerContext* context,
  35. const Main_Ctrl_Land_Request* request,
  36. Main_Ctrl_Land_Reply* reply) override {
  37. reply->set_message("CMD : Land : Accept");
  38. ServerUnaryReactor* reactor = context->DefaultReactor();
  39. reactor->Finish(Status::OK);
  40. return reactor;
  41. }
  42. ServerUnaryReactor* Ctrl_RTL(CallbackServerContext* context,
  43. const Main_Ctrl_RTL_Request* request,
  44. Main_Ctrl_RTL_Reply* reply) override {
  45. reply->set_message("CMD : RTL : Accept");
  46. ServerUnaryReactor* reactor = context->DefaultReactor();
  47. reactor->Finish(Status::OK);
  48. return reactor;
  49. }
  50. ServerUnaryReactor* Ctrl_PositionGlobal(CallbackServerContext* context,
  51. const Main_Ctrl_PositionGlobal_Request* request,
  52. Main_Ctrl_PositionGlobal_Reply* reply) override {
  53. reply->set_message("CMD : PositionGlobal : Accept");
  54. ServerUnaryReactor* reactor = context->DefaultReactor();
  55. reactor->Finish(Status::OK);
  56. return reactor;
  57. }
  58. ServerUnaryReactor* Ctrl_PositionNED(CallbackServerContext* context,
  59. const Main_Ctrl_PositionNED_Request* request,
  60. Main_Ctrl_PositionNED_Reply* reply) override {
  61. reply->set_message("CMD : PositionNED : Accept");
  62. ServerUnaryReactor* reactor = context->DefaultReactor();
  63. reactor->Finish(Status::OK);
  64. return reactor;
  65. }
  66. };
  67. void RunServer(uint16_t port) {
  68. std::string server_address = "127.0.0.1:" + std::to_string(port);
  69. GreeterServiceImpl service;
  70. grpc::EnableDefaultHealthCheckService(true);
  71. grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  72. ServerBuilder builder;
  73. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  74. builder.RegisterService(&service);
  75. std::unique_ptr<Server> server(builder.BuildAndStart());
  76. std::cout << "Server listening on " << server_address << std::endl;
  77. server->Wait();
  78. }
  79. int main(int argc, char** argv) {
  80. RunServer(8081);
  81. return 0;
  82. }