Browse Source

change modulecomm. add code for find attach fail.

yuchuli 3 years ago
parent
commit
1d918ac11d

+ 78 - 0
src/common/modulecomm/shm/procsm.cpp

@@ -9,6 +9,25 @@
 #include <unistd.h>
 #endif
 
+
+//#define RESET   "\033[0m"
+//#define BLACK   "\033[30m"      /* Black */
+//#define RED     "\033[31m"      /* Red */
+//#define GREEN   "\033[32m"      /* Green */
+//#define YELLOW  "\033[33m"      /* Yellow */
+//#define BLUE    "\033[34m"      /* Blue */
+//#define MAGENTA "\033[35m"      /* Magenta */
+//#define CYAN    "\033[36m"      /* Cyan */
+//#define WHITE   "\033[37m"      /* White */
+//#define BOLDBLACK   "\033[1m\033[30m"      /* Bold Black */
+//#define BOLDRED     "\033[1m\033[31m"      /* Bold Red */
+//#define BOLDGREEN   "\033[1m\033[32m"      /* Bold Green */
+//#define BOLDYELLOW  "\033[1m\033[33m"      /* Bold Yellow */
+//#define BOLDBLUE    "\033[1m\033[34m"      /* Bold Blue */
+//#define BOLDMAGENTA "\033[1m\033[35m"      /* Bold Magenta */
+//#define BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
+//#define BOLDWHITE   "\033[1m\033[37m"      /* Bold White */
+
 class AttachThread : public QThread
 {
   public:
@@ -28,16 +47,44 @@ class AttachThread : public QThread
     }
 };
 
+void procsm::threadAttachMon()
+{
+    std::cout<<"\033[1m\033[32m"<<mstrsmname<<" threadAttachMon run."<<"\033[0m"<<std::endl;
+
+    int nCount = 0;
+    while(mbAttachRun)
+    {
+        if(mbInitComplete)
+        {
+            std::cout<<"\033[1m\033[32m"<<mstrsmname<<" Init Succesffuly."<<"\033[0m"<<std::endl;
+            break;
+        }
+        std::this_thread::sleep_for(std::chrono::milliseconds(10));
+        nCount++;
+        if(nCount>300)
+        {
+            std::cout<<"\033[1m\033[31m"<<" modulecomm msg "<<mstrsmname<<" Attach Fail."<<" Now Exit."<<"\033[0m"<<std::endl;
+            std::this_thread::sleep_for(std::chrono::milliseconds(10));
+            exit(-1);
+        }
+    }
+    mbAttachThreadComplete = true;
+}
+
 procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned int nMaxPacCount,const int nMode)
 {
 //    mnBufSize = nBufSize;
 
 //    qDebug("create dbus");
 
+
+
     strncpy(mstrsmname,strsmname,256);
 
     mpASMPtr = new QSharedMemory(strsmname);
 
+    mpthreadattmon = new std::thread(&procsm::threadAttachMon,this);
+
 
     char strasmname[256];
 
@@ -63,6 +110,7 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
     }
     else
     {
+        mbInitComplete = true;
         return;
     }
 
@@ -159,6 +207,36 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
 
 
     }
+
+    mbInitComplete = true;
+}
+
+procsm::~procsm()
+{
+    if(mbAttachThreadComplete == false)
+    {
+        std::cout<<" Try to end Attach Monitor Thread."<<std::endl;
+        mbAttachRun = false;
+        mpthreadattmon->join();
+    }
+
+    if(mpASMPtr->isAttached())
+    {
+        std::cout<<"\033[32m"<<mstrsmname<<" detach ASMPtr "<<"\033[0m"<<std::endl;
+        mpASMPtr->lock();
+        if(mpASM->isAttached())
+        {
+            std::cout<<"\033[32m"<<mstrsmname<<" detach ASM "<<"\033[0m"<<std::endl;
+            mpASM->detach();
+        }
+        mpASMPtr->unlock();
+        delete mpASM;
+        mpASMPtr->detach();
+    }
+    delete mpASMPtr;
+    std::cout<<"\033[32m"<<mstrsmname<<" ~procsm() "<<"\033[0m"<<std::endl;
+
+
 }
 
 void procsm::recreateasm(int nbufsize)

+ 11 - 0
src/common/modulecomm/shm/procsm.h

@@ -6,6 +6,7 @@
 #include <QDateTime>
 #include <QList>
 #include <QVariant>
+#include <thread>
 
 #ifdef USEDBUS
 #include <QtDBus/QDBusMessage>
@@ -79,6 +80,7 @@ class procsm : public QObject
 #endif
 public:
     procsm(const char * strsmname,const unsigned int nBufSize,const unsigned int nMaxPacCount,const int nMode);
+    ~procsm();
     int writemsg(const char * str,const unsigned int nSize);
     unsigned int getcurrentnext();
     int readmsg(unsigned int index,char * str,unsigned int nMaxSize,unsigned int * nRead,QDateTime * pdt);
@@ -124,6 +126,15 @@ private:
 private:
     char mstrsmname[256];
 
+private:
+    bool mbInitComplete = false;
+    void threadAttachMon();
+    bool mbAttachRun = true;
+    bool mbAttachThreadComplete = false;
+
+private:
+    std::thread * mpthreadattmon;
+
 
 
 };

+ 1 - 1
src/tool/IVSysMan/groupunit.cpp

@@ -8,7 +8,7 @@ GroupUnit::GroupUnit()
 void GroupUnit ::CreateView()
 {
 
-    int i= 0;
+    unsigned int i= 0;
     int j=0;
     CreateTitle();
     for(i=0;i<mpPM->mvectorprog.size();i++)

+ 2 - 0
src/tool/IVSysMan/mainwindow.cpp

@@ -149,6 +149,8 @@ MainWindow::~MainWindow()
     iv::ivexit::ExecIVExitCmd(mpivexit,"adcivexit-test");   // test iv exit code. can comment this line.
 #endif
     
+    mPM->setquit();
+
     mPM->requestInterruption();
 
     QThread::msleep(1000);

+ 72 - 1
src/tool/IVSysMan/progmon.cpp

@@ -262,6 +262,12 @@ void ProgMon::onProcessEnd()
             pu->mbRun = false;
             emit SigProcStoped(pu);
 
+            if(mbquit)
+            {
+                std::cout<<"Program "<<pu->strappname<<" quit."<<std::endl;
+                break;
+            }
+
  //           delete pu->mProcess;
 
             if(pu->mbautostart)
@@ -321,6 +327,13 @@ void ProgMon::onChRead()
     qDebug("process INFO: %s ", ba.data());
 #endif
 
+    if(mbAllNoLog == false)
+    {
+        mMutex_stdout.lock();
+        if(mvectorstdout.size()<1000) mvectorstdout.push_back(stdoutunit(proc,ba));
+        mMutex_stdout.unlock();
+    }
+
 //    qDebug("read chanel count is :%d ",proc->readChannelCount());
 }
 
@@ -550,17 +563,75 @@ void ProgMon::run()
         }
         nLastUpdate = nNow;
         int i;
-        mMutex.lock();
+
         for(i=0;i<mvectorprog.size();i++)
         {
+            mMutex.lock();
             mvectorprog[i].UpdateResState();
             if(QThread::isInterruptionRequested())
             {
                 break;
             }
+            mMutex.unlock();
+        }
+
+
+    }
+}
+
+void ProgMon::setquit()
+{
+    mbquit = true;
+}
+
+void ProgMon::threadstdout()
+{
+
+    while(mbstdoutrun)
+    {
+        bool bAllNoLog = true;
+        unsigned int i;
+        mMutex.lock();
+        for(i=0;i<mvectorprog.size();i++)
+        {
+            if(mvectorprog[i].mbSavestdout)
+            {
+                bAllNoLog = false;
+                break;
+            }
         }
         mMutex.unlock();
 
+        if(mbAllNoLog != bAllNoLog)
+        {
+            mbAllNoLog = bAllNoLog;
+            if(bAllNoLog == true)
+            {
+                mMutex_stdout.lock();
+                mvectorstdout.clear();
+                mMutex_stdout.unlock();
+            }
+        }
+
+        if(mbAllNoLog == true)
+        {
+            msleep(100);
+            continue;
+        }
+
+        std::vector<stdoutunit> xvectorstdout;
+        mMutex_stdout.lock();
+        xvectorstdout= mvectorstdout;
+        mvectorstdout.clear();
+        mMutex_stdout.unlock();
+        if(xvectorstdout.size() == 0)
+        {
+            msleep(10);
+        }
+        else
+        {
+
+        }
     }
 }
 

+ 28 - 0
src/tool/IVSysMan/progmon.h

@@ -22,6 +22,20 @@
 #include <windows.h>
 #endif
 
+
+class stdoutunit
+{
+public:
+    QProcess * mpProc;
+    QByteArray mba;
+public:
+    stdoutunit(QProcess * pProc,QByteArray & ba)
+    {
+        mpProc = pProc;
+        mba = ba;
+    }
+};
+
 class ProgMon : public QThread
 {
     Q_OBJECT
@@ -83,6 +97,20 @@ signals:
 
 private:
     void run();
+
+    bool mbquit = false;
+
+public:
+    void setquit();
+
+private:
+    void threadstdout();
+
+    std::vector<stdoutunit> mvectorstdout;
+    QMutex mMutex_stdout;
+    bool mbstdoutrun = true;
+
+    bool mbAllNoLog = true;
 };
 
 #endif // PROGMON_H

+ 5 - 0
src/tool/IVSysMan/progunit.h

@@ -3,6 +3,7 @@
 
 #include <QProcess>
 #include <QThread>
+#include <vector>
 
 #include <QtXml>
 // Used For Program Monitor
@@ -49,6 +50,10 @@ public:
     QTime timeRec;//当前时间
     int count;//次数
 
+    std::vector<QByteArray> mvectorstdout;
+
+    bool mbSavestdout = false;
+