Browse Source

change driver_cloud_grpc_thread. and driver_cloud_grpc_server_thread.

yuchuli 3 years ago
parent
commit
9b57e39ceb

+ 1 - 0
src/driver/driver_cloud_grpc_server_thread/main.cpp

@@ -60,6 +60,7 @@ class UploadServiceImpl final : public iv::UploadThread::Service {
     int nres = gpcmsgbuf.getmsg(request->strvin(),request->strctrlmd5(),id,ntime,&xvectorctrldata);
 
     reply->set_nres(nres);
+    reply->set_nreqsendtime(request->nsendtime());
 
     if(nres == 1)
     {

+ 16 - 1
src/driver/driver_cloud_grpc_server_thread/picbuf.cpp

@@ -9,7 +9,17 @@ PicBuf::PicBuf()
 
 void PicBuf::AddData(iv::PicData xPicData)
 {
-    std::cout<<"pic time is "<<xPicData.mnMsgTime<<std::endl;
+ //   std::cout<<"pic time is "<<xPicData.mnMsgTime<<std::endl;
+    qint64 xsecond = QDateTime::currentMSecsSinceEpoch()/1000;
+    mnFrameSecondCount++;
+    if(xsecond != mnSecondTime)
+    {
+        mnSecondTime = xsecond;
+        mnFrameRate = mnFrameSecondCount;
+        mnFrameSecondCount = 0;
+        std::cout<<"pic framefate: "<<mnFrameRate<<std::endl;
+
+    }
     static int nunusual = 0;
     mMutexBuf.lock();
 
@@ -108,3 +118,8 @@ PicBuf::PicBuf(const PicBuf &other)
     mnReceiveBufTime = other.mnReceiveBufTime;
     mLastBufTime = other.mLastBufTime;
 }
+
+int PicBuf::GetFrameRate()
+{
+    return mnFrameRate;
+}

+ 7 - 0
src/driver/driver_cloud_grpc_server_thread/picbuf.h

@@ -4,6 +4,7 @@
 #include <memory>
 #include <vector>
 #include <QMutex>
+#include <QDateTime>
 
 namespace iv {
 struct PicData
@@ -24,11 +25,17 @@ private:
     qint64 mnReceiveBufTime = 50;
     qint64 mLastBufTime = 0;
     QMutex mMutexBuf;
+
+private:
+    int mnFrameRate = 0;
+    qint64 mnSecondTime = 0;   //Time  Second.
+    int mnFrameSecondCount =0; //Last Second Receive message
 public:
     PicBuf();
     PicBuf(const PicBuf & other);
     void AddData(iv::PicData xPicData);
     int GetData(qint64 & nLastTime,iv::PicData & xPicData);  //0 No Data  1 Have Data
+    int GetFrameRate();
 };
 
 #endif // PICBUF_H

+ 30 - 1
src/driver/driver_cloud_grpc_thread/grpcclient.cpp

@@ -129,6 +129,7 @@ void grpcclient::run()
 
  //   ClientContext context;
 
+    std::vector<qint64> xvectorlatency;
 
 
     while(!QThread::isInterruptionRequested())
@@ -178,6 +179,8 @@ void grpcclient::run()
                 context.set_deadline(timespec);
                 qint64 time1 = QDateTime::currentMSecsSinceEpoch();
 
+
+
                 request.set_id(nid);
                 request.set_ntime(time1);
                 request.set_strquerymd5(gstrqueryMD5);
@@ -186,6 +189,8 @@ void grpcclient::run()
                 request.set_xdata(strbuf,nbytesize);
                 request.set_kepptime(nkeeptime);
                 request.set_bimportant(bImportant);
+                request.set_nsendtime(QDateTime::currentMSecsSinceEpoch());
+                request.set_nlatency(CalcLateny(xvectorlatency));
                 nid++;
 
                 nlastsend = xTime.elapsed();
@@ -194,6 +199,8 @@ void grpcclient::run()
                 if (status.ok()) {
                     std::cout<<"  data size is "<<nbytesize<<std::endl;
                     std::cout<<nid<<" upload successfully"<<std::endl;
+                    xvectorlatency.push_back((QDateTime::currentMSecsSinceEpoch() - reply.nreqsendtime()));
+                    while(xvectorlatency.size()>10)xvectorlatency.erase(xvectorlatency.begin());
                     if(reply.nres() == 1)
                     {
                         iv::cloud::cloudmsg xmsg;
@@ -481,6 +488,7 @@ void grpcclient::threadpicupload(int nCamPos)
         bool bUpdate = false;
         qint64 nMsgTime = 0;
         int nSize = 0;
+        qint64 npiclatency;
 
         mpicbuf[nCamPos].mWaitMutex.lock();
         mpicbuf[nCamPos].mwc.wait(&mpicbuf[nCamPos].mWaitMutex,100);
@@ -491,10 +499,12 @@ void grpcclient::threadpicupload(int nCamPos)
         if(bUpdate == true)
         {
             nMsgTime = mpicbuf[nCamPos].mnMsgTime;
-            std::cout<<"upload "<<nMsgTime<<std::endl;
+
             mpicbuf[nCamPos].mbRefresh = false;
             pstr_ptr = mpicbuf[nCamPos].mpstrmsgdata;
             nSize = mpicbuf[nCamPos].mDataSize;
+            npiclatency = CalcLateny(mpicbuf[nCamPos].mvectorlatency);
+            std::cout<<"upload "<<nMsgTime<<"  latency:  "<<npiclatency<<std::endl;
         }
         mpicbuf[nCamPos].mMutex.unlock();
         if(bUpdate == false)
@@ -511,6 +521,7 @@ void grpcclient::threadpicupload(int nCamPos)
         request.set_ncampos(nCamPos);
         request.set_strvin(gstrVIN);
         request.set_xdata(pstr_ptr.get(),nSize);
+        request.set_nlatency(npiclatency);
         nid++;
 
         nlastsend = xTime.elapsed();
@@ -518,6 +529,11 @@ void grpcclient::threadpicupload(int nCamPos)
         Status status = stub_->uploadpic(&context, request, &reply);
         if (status.ok()) {
 
+            qint64 nlaten = QDateTime::currentMSecsSinceEpoch() - time1;
+            mpicbuf[nCamPos].mMutex.lock();
+            mpicbuf[nCamPos].mvectorlatency.push_back(nlaten);
+            while(mpicbuf[nCamPos].mvectorlatency.size()>10)mpicbuf[nCamPos].mvectorlatency.erase(mpicbuf[nCamPos].mvectorlatency.begin());
+            mpicbuf[nCamPos].mMutex.unlock();
             if(reply.nres() == 1)
             {
 //                iv::cloud::cloudmsg xmsg;
@@ -547,3 +563,16 @@ void grpcclient::threadpicupload(int nCamPos)
 
     std::cout<<"threadpicupload cam pos: "<<nCamPos<<" exit."<<std::endl;
 }
+
+qint64 grpcclient::CalcLateny(std::vector<qint64> &xvectorlatency)
+{
+    if(xvectorlatency.size() == 0)return 1000;
+    unsigned int i;
+    qint64 nLatencyTotal = 0;
+    for(i=0;i<xvectorlatency.size();i++)
+    {
+        nLatencyTotal = nLatencyTotal + xvectorlatency[i];
+    }
+    qint64 nLatencyAvg = nLatencyTotal/xvectorlatency.size();
+    return nLatencyAvg;
+}

+ 4 - 1
src/driver/driver_cloud_grpc_thread/grpcclient.h

@@ -57,6 +57,7 @@ struct  threadpicunit
      QMutex mWaitMutex;
      QWaitCondition mwc;
      QMutex mMutex;
+     std::vector<qint64> mvectorlatency;
 };
 }
 
@@ -72,7 +73,7 @@ public:
     ~grpcclient();
 
 private:
-    std::string gstrserverip = "111.33.136.149";//"0.0.0.0";//"192.168.14.98";// "123.57.212.138";
+    std::string gstrserverip = "127.0.0.1";//111.33.136.149";//"0.0.0.0";//"192.168.14.98";// "123.57.212.138";
     std::string gstrserverport = "50051";//"9000";
     std::string gstruploadinterval = "1000";
     void * gpa;
@@ -114,6 +115,8 @@ private:
 
     bool mbPicUpload = true;
 
+    qint64 CalcLateny(std::vector<qint64> & xvectorlatency);
+
 };
 
 #endif // GRPCCLIENT_H

+ 1 - 1
src/driver/driver_cloud_grpc_thread/main.cpp

@@ -46,7 +46,7 @@ int main(int argc, char *argv[])
     char stryamlpath[256];
     if(argc<2)
     {
-        snprintf(stryamlpath,255,"driver_cloud_grpc_client.yaml");
+        snprintf(stryamlpath,255,"driver_cloud_grpc_thread.yaml");
 //        strncpy(stryamlpath,abs_ymlpath,255);
     }
     else

+ 7 - 0
src/include/proto3/uploadthreadmsg.proto

@@ -104,10 +104,12 @@ message PicUpRequestThread{
   int32 nCamPos = 2; //0 front 1 rear 2 left 3 right
   int64 nPicTime = 3;
   bytes xdata = 4;
+  int64 nLatency = 5; //Pic latency
 }
 
 message PicUpReplyThread{
   int32 nres = 1; 
+  int64 nPicSendTime = 2; // Return Pic Time For Calculate Latency
 }
 
 message PicDownReqThread{
@@ -115,6 +117,8 @@ message PicDownReqThread{
   string strqueryMD5 = 2;
   int32 nCamPos = 3; //0 front 1 rear 2 left 3 right
   string strClientID = 4; ///
+  int64 nPicQueryTime = 5 ;
+  int64 nPicQueryLatency = 6;
 }
 
 message PicDownReplyThread{
@@ -122,6 +126,9 @@ message PicDownReplyThread{
   int32 nCamPos = 2; //0 front 1 rear 2 left 3 right
   int64 nPicTime = 3;  // > this PicTime
   bytes xdata = 4;
+  int64 nPicFrameRate = 5;  //Pic FrameRate  to Server.
+  int64 nPicUpLatency = 6; // Pic Latency to Server
+  int64 nPicDownLatency = 7;  //Pic Latency in Down.
 }