main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3. #include <iostream>
  4. #include <vector>
  5. #include <iostream>
  6. #include <memory>
  7. #include <string>
  8. #include <thread>
  9. #include <grpcpp/grpcpp.h>
  10. #include <grpcpp/health_check_service_interface.h>
  11. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  12. #include "grpcgroup.pb.h"
  13. #include "groupmsgbuf.h"
  14. #include "ivbacktrace.h"
  15. #include "xmlparam.h"
  16. #include <signal.h>
  17. using grpc::Server;
  18. using grpc::ServerBuilder;
  19. using grpc::ServerContext;
  20. using grpc::Status;
  21. #include "./../driver_group_grpc_client/grpcgroup.grpc.pb.h"
  22. #include "./../driver_db_grpc_server/grpcdb.grpc.pb.h"
  23. static groupmsgbuf * gpmsgbuf;
  24. std::string gstrserver;
  25. bool gbSaveToDB;
  26. std::string gstrdbpath;
  27. static std::thread * gptheadgroup;
  28. static std::thread * gptheaddb;
  29. static std::unique_ptr<Server> gserver_grpc;
  30. static std::unique_ptr<Server> gserver_db;
  31. // Logic and data behind the server's behavior.
  32. class GroupServiceImpl final : public iv::group::groupservice::Service{
  33. Status grpcgroup(ServerContext* context, const iv::group::groupRequest* request,
  34. iv::group::groupReply* reply) override {
  35. gpmsgbuf->ProcGroupMsg(request,reply);
  36. return Status::OK;
  37. }
  38. Status queryallgroup(ServerContext* context, const iv::group::groupRequest* request,
  39. iv::group::groupReply* reply) override {
  40. gpmsgbuf->ProcQueryMsg(request,reply);
  41. return Status::OK;
  42. }
  43. };
  44. class DBServiceImpl final : public iv::db::dbservice::Service{
  45. Status querylist(ServerContext* context, const iv::db::listRequest * request,
  46. iv::db::listReply * reply) override {
  47. std::cout<<"vehid is "<<request->strvehid().data()<<std::endl;
  48. // gpmsgbuf->ProcGroupMsg(request,reply);
  49. gpmsgbuf->ProcDBListMsg(request,reply);
  50. return Status::OK;
  51. }
  52. Status querydata(ServerContext* context, const iv::db::dataRequest * request,
  53. iv::db::dataReply * reply) override {
  54. // gpmsgbuf->ProcGroupMsg(request,reply);
  55. gpmsgbuf->ProcDBFileReqMsg(request,reply);
  56. return Status::OK;
  57. }
  58. Status downdbfile(grpc::ServerContext *context, const iv::db::Filedbreq *request,
  59. iv::db::FiledbReply *reply) override{
  60. gpmsgbuf->ProcDBFileDownMsg(request,reply);
  61. return Status::OK;
  62. }
  63. };
  64. void RunServer() {
  65. std::string server_address = gstrserver ;//("0.0.0.0:31001");
  66. GroupServiceImpl service;
  67. grpc::EnableDefaultHealthCheckService(true);
  68. // grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  69. ServerBuilder builder;
  70. // Listen on the given address without any authentication mechanism.
  71. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  72. builder.SetMaxReceiveMessageSize(300000000);
  73. // builder.SetMaxMessageSize(100000000);
  74. // builder.SetMaxSendMessageSize(100000000);
  75. // Register "service" as the instance through which we'll communicate with
  76. // clients. In this case it corresponds to an *synchronous* service.
  77. builder.RegisterService(&service);
  78. // Finally assemble the server.
  79. // std::unique_ptr<Server> server(builder.BuildAndStart());
  80. gserver_grpc = builder.BuildAndStart();
  81. std::cout << "Server listening on " << server_address << std::endl;
  82. // Wait for the server to shutdown. Note that some other thread must be
  83. // responsible for shutting down the server for this call to ever return.
  84. // server->Wait();
  85. gserver_grpc->Wait();
  86. }
  87. static std::string getdefdbpath()
  88. {
  89. std::string strhome = getenv("HOME");
  90. std::string strpath = strhome + "/groupdb.db";
  91. return strpath;
  92. }
  93. void RunServerDB() {
  94. std::string server_address("0.0.0.0:31011");
  95. DBServiceImpl service;
  96. grpc::EnableDefaultHealthCheckService(true);
  97. // grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  98. ServerBuilder builder;
  99. // Listen on the given address without any authentication mechanism.
  100. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  101. builder.SetMaxReceiveMessageSize(300000000);
  102. // builder.SetMaxMessageSize(100000000);
  103. // builder.SetMaxSendMessageSize(100000000);
  104. // Register "service" as the instance through which we'll communicate with
  105. // clients. In this case it corresponds to an *synchronous* service.
  106. builder.RegisterService(&service);
  107. // Finally assemble the server.
  108. gserver_db = builder.BuildAndStart();
  109. // std::unique_ptr<Server> server(builder.BuildAndStart());
  110. std::cout << "Server listening on " << server_address << std::endl;
  111. // Wait for the server to shutdown. Note that some other thread must be
  112. // responsible for shutting down the server for this call to ever return.
  113. // server->Wait();
  114. gserver_db->Wait();
  115. // server->Shutdown();
  116. }
  117. void threadgroup()
  118. {
  119. RunServer();
  120. }
  121. void threaddb()
  122. {
  123. RunServerDB();
  124. }
  125. void signal_handler(int sig)
  126. {
  127. if(sig == SIGINT)
  128. {
  129. gserver_grpc->Shutdown();
  130. gserver_db->Shutdown();
  131. qDebug("shut down grpc.");
  132. delete gpmsgbuf;
  133. exit(0);
  134. }
  135. }
  136. int main(int argc, char *argv[])
  137. {
  138. QCoreApplication a(argc, argv);
  139. // std::shared_ptr<::google::protobuf::Message> msg_ptr = NULL;
  140. // msg_ptr =std::shared_ptr<::google::protobuf::Message>(new iv::group::vehicleinfo);
  141. // iv::group::vehicleinfo xinfo;
  142. // xinfo.set_groupid(1);
  143. // ::google::protobuf::Message * pmsg = (::google::protobuf::Message *)&xinfo;
  144. // ::google::protobuf::Message * pmsg2 = pmsg->New(NULL);
  145. // iv::group::vehicleinfo *pinfo2 = (iv::group::vehicleinfo *)pmsg2;
  146. // pinfo2->set_groupid(2);
  147. RegisterIVBackTrace();
  148. iv::xmlparam::Xmlparam xp("./driver_group_grpc_server.xml");
  149. gstrserver = xp.GetParam("server","0.0.0.0:31001");
  150. gbSaveToDB = xp.GetParam("savetodb",true);
  151. gstrdbpath = xp.GetParam("dbpath",getdefdbpath());
  152. std::cout<<"server: "<<gstrserver<<" savetodb:"<<gbSaveToDB<<" db path:"<<gstrdbpath<<std::endl;
  153. gpmsgbuf = new groupmsgbuf();
  154. gptheadgroup = new std::thread(threadgroup);
  155. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  156. gptheaddb = new std::thread(threaddb);
  157. signal(SIGINT,signal_handler);
  158. return a.exec();
  159. }