cumsgbuffer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "cumsgbuffer.h"
  2. cumsgbuffer::cumsgbuffer()
  3. {
  4. }
  5. void cumsgbuffer::addmsg(int id, qint64 ntime, std::string strVIN, std::string strqueryMD5, std::string strctrlMD5,
  6. std::vector<char> *pxdata,bool bImportant,int nkeeptime,qint64 nculatency)
  7. {
  8. qDebug("ntime is %ld",ntime);
  9. mMutex.lock();
  10. iv::cumsg * pmsg = 0;
  11. int nsize = mvectormsg.size();
  12. int i;
  13. for(i=0;i<nsize;i++)
  14. {
  15. if(strncmp(mvectormsg[i].strVIN.data(),strVIN.data(),255) == 0)
  16. {
  17. pmsg = &mvectormsg[i];
  18. break;
  19. }
  20. }
  21. if(pmsg == 0)
  22. {
  23. iv::cumsg cmsg;
  24. cmsg.id = id;
  25. cmsg.ntime = ntime;
  26. cmsg.strVIN = strVIN;
  27. cmsg.strqueryMD5 = strqueryMD5;
  28. cmsg.strctrlMD5 = strctrlMD5;
  29. cmsg.mnlatency = nculatency;
  30. if(pxdata->size() > 0)
  31. {
  32. cmsg.xdata.resize(pxdata->size());
  33. memcpy(cmsg.xdata.data(),pxdata->data(),pxdata->size());
  34. }
  35. cmsg.mlastuptime = QDateTime::currentMSecsSinceEpoch();
  36. cmsg.mbImportant = bImportant;
  37. cmsg.mkeeptime = nkeeptime;
  38. cmsg.mbhavequery = false;
  39. mvectormsg.push_back(cmsg);
  40. mMutex.unlock();
  41. return;
  42. }
  43. if((pmsg->mbImportant != true)||(pmsg->mbhavequery)||((QDateTime::currentMSecsSinceEpoch() - pmsg->mlastuptime)>=pmsg->mkeeptime))
  44. {
  45. pmsg->id = id;
  46. pmsg->ntime = ntime;
  47. pmsg->mbImportant = bImportant;
  48. pmsg->mbhavequery = false;
  49. pmsg->mlastuptime = QDateTime::currentMSecsSinceEpoch();
  50. pmsg->mkeeptime = nkeeptime;
  51. pmsg->strqueryMD5 = strqueryMD5;
  52. pmsg->strctrlMD5 = strctrlMD5;
  53. pmsg->xdata.clear();
  54. pmsg->mnlatency = nculatency;
  55. if(pxdata->size()>0)
  56. {
  57. pmsg->xdata.resize(pxdata->size());
  58. memcpy(pmsg->xdata.data(),pxdata->data(),pxdata->size());
  59. }
  60. }
  61. mMutex.unlock();
  62. }
  63. int cumsgbuffer::getmsg(std::string strVIN,std::string strqueryMD5, qint64 nlasttime, int &id, qint64 &ntime,
  64. std::vector<char> *pxdata,qint64 * pnculatency)
  65. {
  66. mMutex.lock();
  67. iv::cumsg * pmsg = 0;
  68. int nsize = mvectormsg.size();
  69. int i;
  70. std::vector<iv::cumsg> * pvectormsg = &mvectormsg;
  71. for(i=0;i<nsize;i++)
  72. {
  73. if(strncmp(mvectormsg[i].strVIN.data(),strVIN.data(),255) == 0)
  74. {
  75. pmsg = &mvectormsg[i];
  76. break;
  77. }
  78. }
  79. if(pmsg == 0)
  80. {
  81. std::cout<<" no this vin data vin is "<<strVIN<<std::endl;;
  82. mMutex.unlock();
  83. return -1;
  84. }
  85. if(strqueryMD5 != pmsg->strqueryMD5)
  86. {
  87. std::cout<<" query error."<<std::endl;
  88. mMutex.unlock();
  89. return -2;
  90. }
  91. pmsg->mbhavequery = true;
  92. if(nlasttime == pmsg->ntime)
  93. {
  94. mMutex.unlock();
  95. return 0;
  96. }
  97. qint64 now = QDateTime::currentMSecsSinceEpoch();
  98. if(abs(now - pmsg->mlastuptime)>60000) //Data is old not use.
  99. {
  100. mMutex.unlock();
  101. return 0;
  102. }
  103. id = pmsg->id;
  104. ntime = pmsg->ntime;
  105. pxdata->clear();
  106. *pnculatency = pmsg->mnlatency;
  107. int ndatasize = pmsg->xdata.size();
  108. pxdata->resize(ndatasize);
  109. memcpy(pxdata->data(),pmsg->xdata.data(),ndatasize);
  110. mMutex.unlock();
  111. return 1;
  112. }