main.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <QCoreApplication>
  2. #include <yaml-cpp/yaml.h>
  3. #include <brpc/server.h>
  4. #include "echo.pb.h"
  5. #include "modulecomm.h"
  6. #include <thread>
  7. #include "ivfault.h"
  8. #include "ivlog.h"
  9. #include "ivversion.h"
  10. #include "ivbacktrace.h"
  11. iv::Ivfault *gfault = nullptr;
  12. iv::Ivlog *givlog = nullptr;
  13. namespace iv {
  14. struct msgunit
  15. {
  16. char mstrmsgname[256];
  17. int mnBufferSize = 10000;
  18. int mnBufferCount = 1;
  19. void * mpa;
  20. };
  21. }
  22. std::vector<iv::msgunit> mvectormsgunit;
  23. void * gpa;
  24. unsigned int gport = 60031;
  25. namespace iv {
  26. class EchoServiceImpl : public EchoService {
  27. public:
  28. EchoServiceImpl() {};
  29. virtual ~EchoServiceImpl() {};
  30. virtual void Echo(google::protobuf::RpcController* cntl_base,
  31. const EchoRequest* request,
  32. EchoResponse* response,
  33. google::protobuf::Closure* done) {
  34. // This object helps you to call done->Run() in RAII style. If you need
  35. // to process the request asynchronously, pass done_guard.release().
  36. brpc::ClosureGuard done_guard(done);
  37. brpc::Controller* cntl =
  38. static_cast<brpc::Controller*>(cntl_base);
  39. // The purpose of following logs is to help you to understand
  40. // how clients interact with servers more intuitively. You should
  41. // remove these logs in performance-sensitive servers.
  42. // LOG(INFO) << "Received request[log_id=" << cntl->log_id()
  43. // << "] from " << cntl->remote_side()
  44. // << " to " << cntl->local_side()
  45. // << ": " << request->msgname()
  46. // << " (attached=" << cntl->request_attachment() << ")";
  47. // givlog->debug("ECHOSTATE","HELLO");
  48. givlog->debug("ECHOSTATE","Received request[log_id=%d] from%d to %d: %s",cntl->log_id(),cntl->remote_side(),cntl->local_side(),request->msgname().data());
  49. // Fill response.
  50. response->set_msgname(request->msgname());
  51. response->set_index(request->index());
  52. int i;
  53. for(i=0;i<mvectormsgunit.size();i++)
  54. {
  55. if(strncmp(request->msgname().data(),mvectormsgunit[i].mstrmsgname,255) == 0)
  56. {
  57. iv::modulecomm::ModuleSendMsg(mvectormsgunit[i].mpa,request->xdata().data(),request->xdata().length());
  58. break;
  59. }
  60. }
  61. // You can compress the response by setting Controller, but be aware
  62. // that compression may be costly, evaluate before turning on.
  63. // cntl->set_response_compress_type(brpc::COMPRESS_TYPE_GZIP);
  64. if (true) {
  65. // Set attachment which is wired to network directly instead of
  66. // being serialized into protobuf messages.
  67. cntl->response_attachment().append(cntl->request_attachment());
  68. }
  69. }
  70. };
  71. }
  72. void dec_yaml(const char * stryamlpath)
  73. {
  74. YAML::Node config;
  75. try
  76. {
  77. config = YAML::LoadFile(stryamlpath);
  78. }
  79. catch(YAML::BadFile e)
  80. {
  81. qDebug("load error.");
  82. return;
  83. }
  84. int i;
  85. int nmodulesize;
  86. std::vector<std::string> vecmodulename;
  87. if(config["port"])
  88. {
  89. int nport = config["port"].as<int>();
  90. gport = nport;
  91. }
  92. if(config["message"])
  93. {
  94. nmodulesize = config["message"].size();
  95. for(i=0;i<nmodulesize;i++)
  96. {
  97. std::string strname = config["message"][i].as<std::string>();
  98. vecmodulename.push_back(strname);
  99. }
  100. }
  101. else
  102. {
  103. return;
  104. }
  105. if(nmodulesize <1)return;
  106. std::string strmsgname;
  107. for(i=0;i<nmodulesize;i++)
  108. {
  109. if(config[vecmodulename[i].data()])
  110. {
  111. if(config[vecmodulename[i].data()]["msgname"])
  112. {
  113. strmsgname = config[vecmodulename[i].data()]["msgname"].as<std::string>();
  114. iv::msgunit xmu;
  115. strncpy(xmu.mstrmsgname,strmsgname.data(),255);
  116. if(config[vecmodulename[i].data()]["buffersize"])
  117. {
  118. xmu.mnBufferSize = config[vecmodulename[i].data()]["buffersize"].as<int>();
  119. }
  120. if(config[vecmodulename[i].data()]["buffercount"])
  121. {
  122. xmu.mnBufferCount = config[vecmodulename[i].data()]["buffercount"].as<int>();
  123. }
  124. mvectormsgunit.push_back(xmu);
  125. }
  126. }
  127. }
  128. return;
  129. }
  130. void rpcthread()
  131. {
  132. // Generally you only need one Server.
  133. brpc::Server server;
  134. // Instance of your service.
  135. iv::EchoServiceImpl echo_service_impl;
  136. // Add the service into server. Notice the second parameter, because the
  137. // service is put on stack, we don't want server to delete it, otherwise
  138. // use brpc::SERVER_OWNS_SERVICE.
  139. if (server.AddService(&echo_service_impl,
  140. brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
  141. std::cout << "Fail to add service" << std::endl;
  142. return ;
  143. }
  144. // Start the server.
  145. brpc::ServerOptions options;
  146. options.idle_timeout_sec = -1;
  147. if (server.Start(gport, &options) != 0) {
  148. std::cout << "Fail to start EchoServer"<<std::endl;
  149. return ;
  150. }
  151. // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
  152. server.RunUntilAskedToQuit();
  153. }
  154. int main(int argc, char *argv[])
  155. {
  156. RegisterIVBackTrace();
  157. showversion("driver_rpc_server");
  158. QCoreApplication a(argc, argv);
  159. gfault = new iv::Ivfault("driver_rpc_server");
  160. givlog = new iv::Ivlog("driver_rpc_server");
  161. char stryamlpath[256];
  162. if(argc<2)
  163. {
  164. snprintf(stryamlpath,255,"driver_rpc_server.yaml");
  165. // strncpy(stryamlpath,abs_ymlpath,255);
  166. }
  167. else
  168. {
  169. strncpy(stryamlpath,argv[1],255);
  170. }
  171. dec_yaml(stryamlpath);
  172. int i;
  173. for(i=0;i<mvectormsgunit.size();i++)
  174. {
  175. mvectormsgunit[i].mpa = iv::modulecomm::RegisterSend(mvectormsgunit[i].mstrmsgname,mvectormsgunit[i].mnBufferSize,
  176. mvectormsgunit[i].mnBufferCount);
  177. }
  178. std::thread * pthread = new std::thread(rpcthread);
  179. return a.exec();
  180. }