Browse Source

add common/makeprotointerface for auto create ivprotoif.pro for view_message. but view_viewmessage code not write.

yuchuli 3 years ago
parent
commit
d93aa4c997

+ 33 - 0
autogen_lib.sh

@@ -116,3 +116,36 @@ rm .qmake.stash
 cd ../../../
 
 
+cd src/common/makeprotointerface/
+$qtmake makeprotointerface.pro
+make $MAKEOPT
+make clean
+cp makeprotointerface ./../../../bin/
+rm Makefile
+rm .qmake.stash
+cd ../../../
+
+PWD=`pwd`
+
+PROTOPATH=$PWD/src/include/proto
+MSGTYPEPATH=$PWD/src/include/msgtype
+PROFOLDERPATH=$PWD/src/common
+
+echo $PROTOPATH
+echo $MSGTYPEPATH
+echo $PROFOLDERPATH
+
+$PWD/bin/makeprotointerface $PROTOPATH $MSGTYPEPATH $PROFOLDERPATH
+
+cp $PWD/src/common/ivprotoif/ivprotoif.h $PWD/include
+
+cd src/common/ivprotoif/
+$qtmake ivprotoif.pro
+make $MAKEOPT
+make clean
+cp libivprotoif.so ./../../../bin/
+rm Makefile
+rm .qmake.stash
+cd ../../../
+
+

+ 73 - 0
src/common/makeprotointerface/.gitignore

@@ -0,0 +1,73 @@
+# This file is used to ignore files which are generated
+# ----------------------------------------------------------------------------
+
+*~
+*.autosave
+*.a
+*.core
+*.moc
+*.o
+*.obj
+*.orig
+*.rej
+*.so
+*.so.*
+*_pch.h.cpp
+*_resource.rc
+*.qm
+.#*
+*.*#
+core
+!core/
+tags
+.DS_Store
+.directory
+*.debug
+Makefile*
+*.prl
+*.app
+moc_*.cpp
+ui_*.h
+qrc_*.cpp
+Thumbs.db
+*.res
+*.rc
+/.qmake.cache
+/.qmake.stash
+
+# qtcreator generated files
+*.pro.user*
+
+# xemacs temporary files
+*.flc
+
+# Vim temporary files
+.*.swp
+
+# Visual Studio generated files
+*.ib_pdb_index
+*.idb
+*.ilk
+*.pdb
+*.sln
+*.suo
+*.vcproj
+*vcproj.*.*.user
+*.ncb
+*.sdf
+*.opensdf
+*.vcxproj
+*vcxproj.*
+
+# MinGW generated files
+*.Debug
+*.Release
+
+# Python byte code
+*.pyc
+
+# Binaries
+# --------
+*.dll
+*.exe
+

+ 34 - 0
src/common/makeprotointerface/main.cpp

@@ -0,0 +1,34 @@
+#include <QCoreApplication>
+
+#include <iostream>
+
+#include "protointerface.h"
+
+protointerface * ppif;
+
+int main(int argc, char *argv[])
+{
+    QCoreApplication a(argc, argv);
+
+    if(argc < 4)
+    {
+        std::cout<<"makeprotointerface need 3 args."<<std::endl;
+        return -1;
+    }
+
+    std::string strprotopath = argv[1];
+    std::string strmsgtypepath = argv[2];
+    std::string strprofolder = argv[3];
+
+//    std::string strprotopath = "/home/yuchuli/qt/modularization/src/include/proto";
+//    std::string strmsgtypepath = "/home/yuchuli/qt/modularization/src/include/msgtype";
+//    std::string strprofolder = "/home/yuchuli/qt/modularization/src/common";
+
+    ppif = new protointerface(strprotopath,strmsgtypepath,strprofolder);
+
+    ppif->Proc();
+
+    return 0;
+
+//    return a.exec();
+}

+ 27 - 0
src/common/makeprotointerface/makeprotointerface.pro

@@ -0,0 +1,27 @@
+QT -= gui
+
+CONFIG += c++11 console
+CONFIG -= app_bundle
+
+# The following define makes your compiler emit warnings if you use
+# any Qt feature that has been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if it uses deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += \
+        main.cpp \
+        protointerface.cpp
+
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target
+
+HEADERS += \
+    protointerface.h

+ 363 - 0
src/common/makeprotointerface/protointerface.cpp

@@ -0,0 +1,363 @@
+#include "protointerface.h"
+
+#include <QDir>
+#include <QFile>
+
+protointerface::protointerface(std::string strprotopath,std::string strmsgtypepath,std::string strprofolder)
+{
+    mstrprotopath = strprotopath;
+    mstrmsgtypepath = strmsgtypepath;
+    mstrprofolder = strprofolder;
+}
+
+
+std::vector<std::string> protointerface::GetMessageName(std::string strfilepath,std::vector<std::string> & xvectormsgname)
+{
+    std::vector<std::string> xvector;
+
+    QFile xFile;
+
+    xFile.setFileName(strfilepath.data());
+    if(!xFile.open(QIODevice::ReadOnly))
+    {
+        return xvector;
+    }
+
+
+    QByteArray ba = xFile.readAll();
+
+    xFile.close();
+
+    QList<QByteArray> baline =ba.split('\n');//x.split(QRegExp("\n ")) ;//ba.split('\n');
+    int nline = baline.size();
+    int i;
+    bool bFindPackName = false;
+    std::string strclasspre = "";
+    std::string strnamepre = "";
+    for(i=0;i<nline;i++)
+    {
+        QString x(baline[i]);
+        x.trimmed();
+//         QList<QByteArray> badata = baline[i].split('\t');
+
+        QStringList badata = x.split(QRegExp("[\t ,;{]+"));
+        if(bFindPackName == false)
+        {
+        if(badata[0] == "package")
+        {
+//            qDebug("package name is %s",QString(badata[1]).toLatin1().data());
+            bFindPackName = true;
+            QString xpac(badata[1]);
+            xpac.trimmed();
+            QStringList bapack = xpac.split(QRegExp("[.]"));
+            unsigned int j;
+            for(j=0;j<bapack.size();j++)
+            {
+                strclasspre = strclasspre + QString(bapack[j]).toStdString() + "::";
+                strnamepre = strnamepre + QString(bapack[j]).toStdString() + "_";
+            }
+        }
+        }
+        else
+        {
+            if(badata[0] == "message")
+            {
+ //               qDebug("message name is %s",QString(badata[1]).toLatin1().data());
+                std::string strclassname = strclasspre + QString(badata[1]).toStdString();
+                std::string strxname = strnamepre +  QString(badata[1]).toStdString();
+//                qDebug("class name is %s",strclassname.data());
+                xvector.push_back(strclassname);
+                xvectormsgname.push_back(strxname);
+            }
+        }
+
+    }
+
+
+
+    return xvector;
+}
+
+std::vector<std::string> protointerface::GetFileNameVector(std::string strpath,std::string strsuff)
+{
+    std::vector<std::string> xvector;
+    QDir dir(strpath.data());
+
+    QStringList nameFilters;
+    nameFilters<<strsuff.data();
+
+    QStringList files = dir.entryList(nameFilters,QDir::Files,QDir::Name);
+
+    unsigned int i;
+    for(i=0;i<files.size();i++)
+    {
+        xvector.push_back(QString(files.at(i)).toStdString());
+    }
+
+    return xvector;
+
+}
+
+bool protointerface::ccodeisexist(std::string strhname)
+{
+    std::string strpath = mstrmsgtypepath;
+    strpath = strpath + "/" + strhname;
+    QFile xFile;
+    xFile.setFileName(strpath.data());
+
+    if(xFile.exists())
+    {
+        return true;
+    }
+    return false;
+}
+
+void protointerface::WriteCodeLine(std::string strline,QFile * pFile)
+{
+    char str[1000];
+    snprintf(str,1000,"%s\n",strline.data());
+    pFile->write(str);
+}
+
+void protointerface::Proc()
+{
+
+    QFile xFileout;
+    QString strprofolder = mstrprofolder.data();
+    strprofolder = strprofolder + "/ivprotoif";
+    QDir xDir;
+    xDir.mkpath(strprofolder);
+    xFileout.setFileName(strprofolder + "/ivprotoif.cpp");
+    if(!xFileout.open(QIODevice::ReadWrite|QIODevice::Truncate))
+    {
+        return;
+    }
+    QFile xFileHeader;
+    xFileHeader.setFileName(strprofolder + "/ivprotoif.h");
+    if(!xFileHeader.open(QIODevice::ReadWrite|QIODevice::Truncate))
+    {
+        return;
+    }
+
+    QFile xFilePro;
+    xFilePro.setFileName(strprofolder + "/ivprotoif.pro");
+    if(!xFilePro.open(QIODevice::ReadWrite|QIODevice::Truncate))
+    {
+        return;
+    }
+
+
+
+
+    std::vector<std::string> xvectorproto = GetFileNameVector(mstrprotopath,"*.proto");
+
+    std::vector<std::string> xvectorclassname;
+    std::vector<std::string > xvectorname;
+    std::vector<std::string> xvectorprotoname;
+
+    unsigned int i;
+    for(i=0;i<xvectorproto.size();i++)
+    {
+        std::vector<std::string> xven;
+        QString strtemp(xvectorproto[i].data());
+        strtemp = strtemp.left(strtemp.size() - 6);
+
+        std::string strheadarfilename = strtemp.toStdString() + ".pb.h";
+        if(!ccodeisexist(strheadarfilename))
+        {
+            qDebug("proto %s is not generate c++ code.",xvectorproto[i].data());
+            continue;
+        }
+        xvectorprotoname.push_back(strtemp.toStdString());
+//        qDebug("header file is %s",strheadarfilename.data());
+        char strline[1000];
+        snprintf(strline,1000,"#include \"%s\"\n",strheadarfilename.data());
+        xFileout.write(strline);
+//        qDebug("%d:%s",i,xvectorproto[i].data());
+        std::vector<std::string> xvectorcla = GetMessageName(mstrprotopath+"/" + xvectorproto[i],xven);
+        unsigned int j;
+        for(j=0;j<xvectorcla.size();j++)
+        {
+            xvectorclassname.push_back(xvectorcla[j]);
+            xvectorname.push_back(xven[j]);
+        }
+    }
+
+
+    char strline[1000];
+    WriteCodeLine("",&xFileout);
+    WriteCodeLine("namespace iv",&xFileout);
+    WriteCodeLine("{",&xFileout);
+    WriteCodeLine("\tstruct protomsgtype",&xFileout);
+    WriteCodeLine("\t{",&xFileout);
+    WriteCodeLine("\t\tstd::string mstrtypename;",&xFileout);
+    WriteCodeLine("\t\t::google::protobuf::Message * mpmsg;",&xFileout);
+    WriteCodeLine("\t};",&xFileout);
+    WriteCodeLine("}",&xFileout);
+    WriteCodeLine("",&xFileout);
+
+
+    snprintf(strline,1000,"#define MSGCOUNT %d\n",xvectorclassname.size());xFileout.write(strline);
+    snprintf(strline,1000,"const int typecount = %d;\n",xvectorclassname.size());xFileout.write(strline);
+    snprintf(strline,1000,"iv::protomsgtype gmsgtype[MSGCOUNT];\n");xFileout.write(strline);
+
+//    snprintf(strline,1000,"{\n");xFileout.write(strline);
+    snprintf(strline,1000,"void InitMsgType()\n");xFileout.write(strline);
+    snprintf(strline,1000,"{\n");xFileout.write(strline);
+    for(i=0;i<xvectorclassname.size();i++)
+    {
+
+        snprintf(strline,1000,"\tgmsgtype[%d].mstrtypename = \"%s\";\n",
+                 i,xvectorclassname[i].data());
+        xFileout.write(strline);
+        snprintf(strline,1000,"\tgmsgtype[%d].mpmsg  = (::google::protobuf::Message *)(new %s);\n",
+                 i,xvectorclassname[i].data());
+        xFileout.write(strline);
+    }
+    snprintf(strline,1000,"}\n");xFileout.write(strline);
+
+    snprintf(strline,1000,"\n");xFileout.write(strline);
+    snprintf(strline,1000,"std::shared_ptr<::google::protobuf::Message > GetProtoMessage(char * strdata,int nsize,unsigned int protoindex)\n");
+    xFileout.write(strline);
+    snprintf(strline,1000,"{\n");xFileout.write(strline);
+    snprintf(strline,1000,"\tstd::shared_ptr<::google::protobuf::Message > msg_ptr = NULL;\n");xFileout.write(strline);
+    snprintf(strline,1000,"\tif(protoindex >= MSGCOUNT)\n");xFileout.write(strline);
+    snprintf(strline,1000,"\t{\n");xFileout.write(strline);
+    snprintf(strline,1000,"\t\treturn msg_ptr;\n");xFileout.write(strline);
+    snprintf(strline,1000,"\t}\n");xFileout.write(strline);
+    WriteCodeLine("",&xFileout);
+
+    WriteCodeLine("\tmsg_ptr =std::shared_ptr<::google::protobuf::Message>(gmsgtype[protoindex].mpmsg->New(NULL));",&xFileout);
+    WriteCodeLine("",&xFileout);
+    WriteCodeLine("\tif(msg_ptr->ParseFromArray(strdata,nsize))",&xFileout);
+    WriteCodeLine("\t{",&xFileout);
+    WriteCodeLine("\treturn msg_ptr;",&xFileout);
+    WriteCodeLine("\t}",&xFileout);
+    WriteCodeLine("",&xFileout);
+    WriteCodeLine("\treturn NULL;",&xFileout);
+    WriteCodeLine("}",&xFileout);
+    WriteCodeLine("",&xFileout);
+
+
+    WriteCodeLine("std::vector<unsigned int> GetProbProtoIndex(char * strdata,int nsize,std::vector<std::string > & xvectorprobname)",&xFileout);
+    WriteCodeLine("{",&xFileout);
+    WriteCodeLine("\tstd::vector<unsigned int> xvectorrtn;",&xFileout);
+    WriteCodeLine("\txvectorrtn.clear();",&xFileout);
+    WriteCodeLine("\txvectorprobname.clear();",&xFileout);
+    WriteCodeLine("\tunsigned int i;",&xFileout);
+    WriteCodeLine("\tfor(i=0;i<MSGCOUNT;i++)",&xFileout);
+    WriteCodeLine("\t{",&xFileout);
+    WriteCodeLine("\t\tif(gmsgtype[i].mpmsg->ParseFromArray(strdata,nsize))",&xFileout);
+    WriteCodeLine("\t\t{",&xFileout);
+    WriteCodeLine("\t\t\txvectorrtn.push_back(i);",&xFileout);
+    WriteCodeLine("\t\t\txvectorprobname.push_back(gmsgtype[i].mstrtypename);",&xFileout);
+    WriteCodeLine("\t\t}",&xFileout);
+    WriteCodeLine("\t}",&xFileout);
+    WriteCodeLine("\treturn xvectorrtn;",&xFileout);
+    WriteCodeLine("}",&xFileout);
+    WriteCodeLine("",&xFileout);
+
+
+
+
+//    for(i=0;i<xvectorclassname.size();i++)
+//    {
+
+//        snprintf(strline,1000,"\t%s\t%s;\n",xvectorclassname[i].data(),xvectorname[i].data());
+// //       qDebug("%s %s",xvectorclassname[i].data(),xvectorname[i].data());
+//        xFileout.write(strline);
+//    }
+
+    xFileout.close();
+
+
+
+
+    WriteCodeLine("#ifndef IVPROTOIF_H",&xFileHeader);
+    WriteCodeLine("#define IVPROTOIF_H",&xFileHeader);
+    WriteCodeLine("",&xFileHeader);
+    WriteCodeLine("#include <QtCore/qglobal.h>",&xFileHeader);
+    WriteCodeLine("#include <google/protobuf/stubs/common.h>",&xFileHeader);
+    WriteCodeLine("#include <google/protobuf/stubs/port.h>",&xFileHeader);
+    WriteCodeLine("#include <google/protobuf/stubs/once.h>",&xFileHeader);
+    WriteCodeLine("#include <google/protobuf/io/coded_stream.h>",&xFileHeader);
+    WriteCodeLine("#include <google/protobuf/wire_format_lite_inl.h>",&xFileHeader);
+    WriteCodeLine("#include <google/protobuf/descriptor.h>",&xFileHeader);
+    WriteCodeLine("#include <google/protobuf/generated_message_reflection.h>",&xFileHeader);
+    WriteCodeLine("#include <google/protobuf/reflection_ops.h>",&xFileHeader);
+    WriteCodeLine("#include <google/protobuf/wire_format.h>",&xFileHeader);
+    WriteCodeLine("",&xFileHeader);
+    WriteCodeLine("#if defined(PROTOINTERFACE_LIBRARY)",&xFileHeader);
+    WriteCodeLine("#  define PROTOINTERFACESHARED_EXPORT Q_DECL_EXPORT",&xFileHeader);
+    WriteCodeLine("#else",&xFileHeader);
+    WriteCodeLine("#  define PROTOINTERFACESHARED_EXPORT Q_DECL_IMPORT",&xFileHeader);
+    WriteCodeLine("#endif",&xFileHeader);
+    WriteCodeLine("",&xFileHeader);
+    WriteCodeLine("",&xFileHeader);
+    WriteCodeLine("void PROTOINTERFACESHARED_EXPORT InitMsgType();",&xFileHeader);
+    WriteCodeLine("std::shared_ptr<::google::protobuf::Message > PROTOINTERFACESHARED_EXPORT GetProtoMessage(char * strdata,int nsize,unsigned int protoindex);",&xFileHeader);
+    WriteCodeLine("std::vector<unsigned int> PROTOINTERFACESHARED_EXPORT GetProbProtoIndex(char * strdata,int nsize,std::vector<std::string > & xvectorprobname);",&xFileHeader);
+
+    WriteCodeLine("#endif",&xFileHeader);
+    WriteCodeLine("",&xFileHeader);
+    xFileHeader.close();
+
+    WriteCodeLine("QT -= gui", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("TEMPLATE = lib", &xFilePro);
+    WriteCodeLine("DEFINES += PROTOINTERFACE_LIBRARY", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("CONFIG += c++11", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("SOURCES += ivprotoif.cpp \\", &xFilePro);
+    for(i=0;i<xvectorprotoname.size();i++)
+    {
+        char strtemp[1000];
+        if(i<(xvectorprotoname.size()-1))
+        {
+            snprintf(strtemp,1000,"\t%s/%s.pb.cc \\",mstrmsgtypepath.data(),
+                 xvectorprotoname[i].data());
+        }
+        else
+        {
+            snprintf(strtemp,1000,"\t%s/%s.pb.cc",mstrmsgtypepath.data(),
+                 xvectorprotoname[i].data());
+        }
+        WriteCodeLine(strtemp,&xFilePro);
+    }
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("HEADERS += ivprotoif.h \\", &xFilePro);
+    for(i=0;i<xvectorprotoname.size();i++)
+    {
+        char strtemp[1000];
+        if(i<(xvectorprotoname.size()-1))
+        {
+        snprintf(strtemp,1000,"\t%s/%s.pb.h \\",mstrmsgtypepath.data(),
+                 xvectorprotoname[i].data());
+        }
+        else
+        {
+            snprintf(strtemp,1000,"\t%s/%s.pb.h ",mstrmsgtypepath.data(),
+                     xvectorprotoname[i].data());
+        }
+        WriteCodeLine(strtemp,&xFilePro);
+    }
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("DEFINES += QT_DEPRECATED_WARNINGS", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("CONFIG += plugin", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+    snprintf(strline,1000,"INCLUDEPATH += %s",mstrmsgtypepath.data());
+    WriteCodeLine(strline,&xFilePro);
+    WriteCodeLine("LIBS+= -lprotobuf", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+    WriteCodeLine("", &xFilePro);
+
+    xFilePro.close();
+
+
+
+}

+ 33 - 0
src/common/makeprotointerface/protointerface.h

@@ -0,0 +1,33 @@
+#ifndef PROTOINTERFACE_H
+#define PROTOINTERFACE_H
+
+#include <string>
+#include <vector>
+#include <QFile>
+
+class protointerface
+{
+public:
+    protointerface(std::string strprotopath,std::string strmsgtypepath,std::string strprofolder);
+
+private:
+    std::string mstrprotopath;
+    std::string mstrmsgtypepath;
+    std::string mstrprofolder;
+
+    std::vector<std::string> mvectorproto;
+
+private:
+    std::vector<std::string> GetFileNameVector(std::string strpath,std::string strsuff);
+
+    std::vector<std::string> GetMessageName(std::string strfilepath,std::vector<std::string> & xvectormsgname);
+
+    bool ccodeisexist(std::string strhname);
+
+    inline void WriteCodeLine(std::string strline,QFile * pFile);
+
+public:
+    void Proc();
+};
+
+#endif // PROTOINTERFACE_H

+ 2 - 1
src/driver/driver_group_grpc_client/groupclient.cpp

@@ -96,9 +96,10 @@ void groupclient::run()
 
     while(!QThread::isInterruptionRequested())
     {
-        std::this_thread::sleep_for(std::chrono::milliseconds(1));
+
         if(abs(xTime.elapsed()-nlastsend)<ninterval)
         {
+            std::this_thread::sleep_for(std::chrono::milliseconds(1));
             continue;
         }
 

+ 12 - 0
src/driver/driver_group_grpc_server/main.cpp

@@ -96,6 +96,18 @@ int main(int argc, char *argv[])
 {
     QCoreApplication a(argc, argv);
 
+//    std::shared_ptr<::google::protobuf::Message> msg_ptr = NULL;
+//    msg_ptr =std::shared_ptr<::google::protobuf::Message>(new iv::group::vehicleinfo);
+
+//    iv::group::vehicleinfo xinfo;
+//    xinfo.set_groupid(1);
+//    ::google::protobuf::Message * pmsg = (::google::protobuf::Message *)&xinfo;
+
+//    ::google::protobuf::Message * pmsg2 = pmsg->New(NULL);
+
+//    iv::group::vehicleinfo *pinfo2 = (iv::group::vehicleinfo *)pmsg2;
+//    pinfo2->set_groupid(2);
+
     RegisterIVBackTrace();
 
     iv::xmlparam::Xmlparam xp("./driver_group_grpc_server.xml");