| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #include "modulecomm_impl.h"
- #include <thread>
- #include <iostream>
- #include <QDateTime>
- #include <QMutex>
- #include <QFile>
- namespace iv {
- namespace modulecomm {
- QMutex gmodulecomm_dds_Mutex;
- int createcount = 0;
- }
- }
- void modulecomm_impl::callbackTopic(const TopicSample::Message& message) {
- // static int counter = 0;
- // std::cout << "Message: counter = " << message.counter << std::endl
- // << " message = " << message.msgname.in() << std::endl;
- // std::cout<<"xlen is "<<message.xdata.length()<<std::endl;
- QDateTime dt = QDateTime::fromMSecsSinceEpoch(message.sendtime);
- // std::cout<<" recv time is "<<QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss-zzz").toLatin1().data()<<std::endl;
- if(mbFunPlus)
- {
- mFun((char *)message.xdata.get_buffer(),message.xdata.length(),message.counter,&dt,message.msgname.in());
- }
- else
- {
- (*mpCall)((char *)message.xdata.get_buffer(),message.xdata.length(),message.counter,&dt,message.msgname.in());
- }
- }
- int modulecomm_impl::GetTempConfPath(char *strpath)
- {
- char strtmppath[256];
- QDateTime dt = QDateTime::currentDateTime();
- snprintf(strtmppath,256,"/tmp/adc_modulecomm_conf_%04d%02d%02d%02d%02d.ini",dt.date().year(),
- dt.date().month(),dt.date().day(),dt.time().hour(),dt.time().minute());
- QFile xFile;
- xFile.setFileName(strtmppath);
- char strtem[256];
- char strdata[10000];
- snprintf(strdata,10000,"");
- if(!xFile.exists())
- {
- if(xFile.open(QIODevice::ReadWrite))
- {
- snprintf(strtem,256,"[common]\n");strncat(strdata,strtem,10000);
- snprintf(strtem,256,"DCPSDefaultDiscovery=TheRTPSConfig\n");strncat(strdata,strtem,10000);
- #ifdef dds_use_shm
- snprintf(strtem,256,"DCPSGlobalTransportConfig=myconfig\n");strncat(strdata,strtem,10000);
- snprintf(strtem,256,"[config/myconfig]\n");strncat(strdata,strtem,10000);
- snprintf(strtem,256,"transports=share\n");strncat(strdata,strtem,10000);
- snprintf(strtem,256,"[transport/share]\n");strncat(strdata,strtem,10000);
- snprintf(strtem,256,"transport_type=shmem\n");strncat(strdata,strtem,10000);
- snprintf(strtem,256,"pool_size=100000000\n");strncat(strdata,strtem,10000);
- #endif
- snprintf(strtem,256,"[rtps_discovery/TheRTPSConfig]\n");strncat(strdata,strtem,10000);
- snprintf(strtem,256,"ResendPeriod=5\n");strncat(strdata,strtem,10000);
- xFile.write(strdata,strnlen(strdata,10000));
- xFile.close();
- }
- }
- strncpy(strpath,strtmppath,255);
- return 0;
- }
- modulecomm_impl::modulecomm_impl(const char * strcommname,int ntype )
- {
- int xargc = 3;
- char * xargv[3];
- xargv[0] = "pub";
- xargv[1]= "-DCPSConfigFile";
- xargv[2] = "configuration.ini";
- char strtmppath[256];
- QFile xFile;
- xFile.setFileName(xargv[2]);
- if(!xFile.exists())
- {
- GetTempConfPath(strtmppath);
- xargv[2] = strtmppath;
- }
- strncpy(mstrtopic,strcommname,255);
- iv::modulecomm::gmodulecomm_dds_Mutex.lock();
- if(ntype == type_recv)
- {
- mpSub = new Subscriber(xargc,xargv,strcommname);
- // std::this_thread::sleep_for(std::chrono::milliseconds(10));
- mnType = type_recv;
- }
- else
- {
- mpPub = new Publisher(xargc,xargv,strcommname);
- // std::this_thread::sleep_for(std::chrono::milliseconds(10));
- mnType = type_send;
- }
- iv::modulecomm::createcount++;
- std::cout<<"count is "<<iv::modulecomm::createcount<<std::endl;
- iv::modulecomm::gmodulecomm_dds_Mutex.unlock();
- }
- int modulecomm_impl::listenmsg(ModuleFun xFun)
- {
- if(mnType == type_send)
- {
- std::cout<<"send not listen."<<std::endl;;
- return -1;
- }
- mbFunPlus = true;
- mFun = xFun;
- std::function<void (const TopicSample::Message&)> topicFunction = std::bind(&modulecomm_impl::callbackTopic,this,std::placeholders::_1);
- mpSub->setReceivedTopicFunction(topicFunction);
- return 0;
- }
- int modulecomm_impl::listenmsg(SMCallBack pCall)
- {
- if(mnType == type_send)
- {
- std::cout<<"send not listen."<<std::endl;
- return -1;
- }
- mbFunPlus = false;
- mpCall = pCall;
- std::function<void (const TopicSample::Message&)> topicFunction = std::bind(&modulecomm_impl::callbackTopic,this,std::placeholders::_1);
- mpSub->setReceivedTopicFunction(topicFunction);
- return 0;
- }
- void modulecomm_impl::writemsg(const char *str, int nlen)
- {
- if(mnType == type_recv)
- {
- std::cout<<"recv not send."<<std::endl;
- return ;
- }
- mpPub->sendMessage(mstrtopic,str,nlen);
- }
|