main.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3. #include <iostream>
  4. #include <vector>
  5. #include "rpc_server.h"
  6. #include "cumsgbuffer.h"
  7. #include "pcmsgbuffer.h"
  8. struct uploadresponsemsg {
  9. int nres; //0 no message 1 have message
  10. int id;
  11. qint64 ntime;
  12. std::string strVIN;
  13. std::string strctrlMD5;
  14. std::vector<char> xdata;
  15. MSGPACK_DEFINE(nres,id, ntime,strVIN, strctrlMD5,xdata);
  16. };
  17. struct queryrespmsg {
  18. int nres;
  19. int id;
  20. qint64 ntime;
  21. std::vector<char> xdata;
  22. MSGPACK_DEFINE(nres,id, ntime,xdata);
  23. };
  24. struct ctrlrespmsg {
  25. int nsendid; // 0 write to buffer
  26. MSGPACK_DEFINE(nsendid);
  27. };
  28. struct queryctrlstate {
  29. int nstate; // 0 write to buffer
  30. MSGPACK_DEFINE(nstate);
  31. };
  32. static cumsgbuffer gcumsgbuf;
  33. static pcmsgbuffer gpcmsgbuf;
  34. using namespace rest_rpc;
  35. using namespace rpc_service;
  36. uploadresponsemsg get_clientresp(rpc_conn conn,int id,qint64 ntime,std::string strVIN,std::string strqueryMD5,std::string strctrlMD5,std::vector<char> xdata) {
  37. std::cout<<id<<" "<<strVIN<<" data size is "<<xdata.size()<<std::endl;
  38. uploadresponsemsg x;
  39. gcumsgbuf.addmsg(id,ntime,strVIN,strqueryMD5,strctrlMD5,&xdata);
  40. x.xdata.clear();
  41. int nres = gpcmsgbuf.getmsg(strVIN,strctrlMD5,x.id,x.ntime,&x.xdata);
  42. if(nres == 1)
  43. {
  44. x.nres = 1;
  45. }
  46. else x.nres = 0;
  47. // qDebug("res is %d",x.nres);
  48. // x.id = 1;
  49. // x.name = "hello";
  50. // char * strdata = new char[1000000];
  51. // x.strdata.append(strdata,1000);
  52. // delete strdata;
  53. // x.nlen = 1000;
  54. // x.dt = QDateTime::currentMSecsSinceEpoch();
  55. // x.xdata.push_back(1);
  56. // x.xdata.push_back(2);
  57. // x.xdata.push_back(3);
  58. return x;
  59. // return { 1, "tom", 20 };
  60. }
  61. queryrespmsg query_clientmsg(rpc_conn conn,std::string strVIN,std::string strqueryMD5,qint64 nlasttime)
  62. {
  63. queryrespmsg x;
  64. x.nres = gcumsgbuf.getmsg(strVIN,strqueryMD5,nlasttime,x.id,x.ntime,&x.xdata);
  65. return x;
  66. }
  67. ctrlrespmsg ctrl_client(rpc_conn conn,int id,qint64 ntime,std::string strVIN,std::string strctrlMD5,std::vector<char> xdata)
  68. {
  69. ctrlrespmsg x;
  70. x.nsendid = gpcmsgbuf.addmsg(id,ntime,strVIN,strctrlMD5,&xdata);
  71. // x.nres = ;
  72. return x;
  73. }
  74. queryctrlstate query_ctrlstate(rpc_conn conn,int nsendid)
  75. {
  76. queryctrlstate x;
  77. x.nstate = gpcmsgbuf.querysendstate(nsendid);
  78. return x;
  79. }
  80. int main(int argc, char *argv[])
  81. {
  82. QCoreApplication a(argc, argv);
  83. gpcmsgbuf.start();
  84. rpc_server server(9000, std::thread::hardware_concurrency());
  85. server.register_handler("get_clientresp", get_clientresp);
  86. server.register_handler("query_clientmsg", query_clientmsg);
  87. server.register_handler("ctrl_client", ctrl_client);
  88. server.register_handler("query_ctrlstate", query_ctrlstate);
  89. server.run();
  90. return a.exec();
  91. }