main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3. #include <iostream>
  4. #include <vector>
  5. #include "cumsgbuffer.h"
  6. #include "pcmsgbuffer.h"
  7. #include <iostream>
  8. #include <memory>
  9. #include <string>
  10. #include <grpcpp/grpcpp.h>
  11. #include <grpcpp/health_check_service_interface.h>
  12. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  13. #include "uploadmsg.grpc.pb.h"
  14. using grpc::Server;
  15. using grpc::ServerBuilder;
  16. using grpc::ServerContext;
  17. using grpc::Status;
  18. #include <QDateTime>
  19. static cumsgbuffer gcumsgbuf;
  20. static pcmsgbuffer gpcmsgbuf;
  21. // Logic and data behind the server's behavior.
  22. class UploadServiceImpl final : public iv::Upload::Service {
  23. Status upload(ServerContext* context, const iv::UploadRequest* request,
  24. iv::UploadReply* reply) override {
  25. std::vector<char> xvectordata;
  26. qDebug("size is %d",request->xdata().size());
  27. if(request->xdata().size()>0)
  28. {
  29. xvectordata.resize(request->xdata().size());
  30. memcpy(xvectordata.data(),request->xdata().data(),request->xdata().size());
  31. }
  32. gcumsgbuf.addmsg(request->id(),request->ntime(),request->strvin(),request->strquerymd5(),request->strctrlmd5(),
  33. &xvectordata,request->bimportant(),request->kepptime());
  34. // std::string strVIN,strctrlMD5;
  35. int id;
  36. qint64 ntime;
  37. std::vector<char > xvectorctrldata;
  38. int nres = gpcmsgbuf.getmsg(request->strvin(),request->strctrlmd5(),id,ntime,&xvectorctrldata);
  39. reply->set_nres(nres);
  40. if(nres == 1)
  41. {
  42. reply->set_xdata(xvectorctrldata.data(),xvectorctrldata.size());
  43. }
  44. // char * strdata = new char[10000000];
  45. // qint64 time = QDateTime::currentMSecsSinceEpoch();
  46. // memcpy(strdata,&time,8);
  47. // reply->set_data(strdata,10000000);
  48. // delete strdata;
  49. return Status::OK;
  50. }
  51. Status query(ServerContext* context, const iv::queryreq* request,
  52. iv::queryReply* reply) override {
  53. int id;
  54. qint64 ntime;
  55. std::vector<char > xvectorquerydata;
  56. int nres = gcumsgbuf.getmsg(request->strvin(),request->strquerymd5(),request->nlasttime(),id,ntime,&xvectorquerydata);
  57. reply->set_nres(nres);
  58. if(nres > 0)
  59. {
  60. reply->set_data(xvectorquerydata.data(),xvectorquerydata.size());
  61. reply->set_id(id);
  62. reply->set_ntime(ntime);
  63. }
  64. return Status::OK;
  65. }
  66. Status ctrl(ServerContext* context, const iv::ctrlreq* request,
  67. iv::ctrlReply * reply) override {
  68. std::vector<char> xvectordata;
  69. if(request->data().size()>0)
  70. {
  71. xvectordata.resize(request->data().size());
  72. memcpy(xvectordata.data(),request->data().data(),request->data().size());
  73. }
  74. int nid = gpcmsgbuf.addmsg(request->id(),request->ntime(),request->strvin(),request->strctrlmd5(),&xvectordata,
  75. request->bimportant(),request->kepptime());
  76. reply->set_nsendid(nid);
  77. return Status::OK;
  78. }
  79. };
  80. void RunServer() {
  81. std::string server_address("0.0.0.0:50051");
  82. UploadServiceImpl service;
  83. grpc::EnableDefaultHealthCheckService(true);
  84. // grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  85. ServerBuilder builder;
  86. // Listen on the given address without any authentication mechanism.
  87. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  88. builder.SetMaxReceiveMessageSize(300000000);
  89. // builder.SetMaxMessageSize(100000000);
  90. // builder.SetMaxSendMessageSize(100000000);
  91. // Register "service" as the instance through which we'll communicate with
  92. // clients. In this case it corresponds to an *synchronous* service.
  93. builder.RegisterService(&service);
  94. // Finally assemble the server.
  95. std::unique_ptr<Server> server(builder.BuildAndStart());
  96. std::cout << "Server listening on " << server_address << std::endl;
  97. // Wait for the server to shutdown. Note that some other thread must be
  98. // responsible for shutting down the server for this call to ever return.
  99. server->Wait();
  100. }
  101. int main(int argc, char *argv[])
  102. {
  103. QCoreApplication a(argc, argv);
  104. gpcmsgbuf.start();
  105. RunServer();
  106. return a.exec();
  107. }