123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #ifdef USE_FASTRTPS
- #include "modulecomm_impl_tcp.h"
- #include <thread>
- #include <iostream>
- #include <QDateTime>
- #include <QMutex>
- #include <QFile>
- namespace iv {
- namespace modulecomm {
- static QMutex gmodulecomm_dds_Mutex;
- static int createcount = 0;
- }
- }
- void modulecomm_impl_tcp::callbackTopic(const char * strdata,const unsigned int nSize,const unsigned int index, QDateTime * dt,const char * strmemname) {
- if(mbFunPlus)
- {
- mFun(strdata,nSize,index,dt,strmemname);
- }
- else
- {
- (*mpCall)(strdata,nSize,index,dt,strmemname);
- }
- }
- int modulecomm_impl_tcp::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_tcp::modulecomm_impl_tcp(const char * strcommname,int ntype,const unsigned short nport ,const char * strip )
- {
- strncpy(mstrtopic,strcommname,255);
- iv::modulecomm::gmodulecomm_dds_Mutex.lock();
- if(ntype == type_recv)
- {
- mpSub = new TopicsSubscriber();
- mpSub->init(strcommname,strip,nport,1);
- // std::this_thread::sleep_for(std::chrono::milliseconds(10));
- mnType = type_recv;
- }
- else
- {
- mpPub = new TopicsPublisher();
- mpPub->init(strcommname,nport,1);
- // 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_tcp::listenmsg(ModuleFun xFun)
- {
- if(mnType == type_send)
- {
- std::cout<<"send not listen."<<std::endl;;
- return -1;
- }
- mbFunPlus = true;
- mFun = xFun;
- ModuleFun topicFunction = std::bind(&modulecomm_impl_tcp::callbackTopic,this,std::placeholders::_1,
- std::placeholders::_2,
- std::placeholders::_3,
- std::placeholders::_4,
- std::placeholders::_5);
- mpSub->setReceivedTopicFunction(topicFunction);
- return 0;
- }
- int modulecomm_impl_tcp::listenmsg(SMCallBack pCall)
- {
- if(mnType == type_send)
- {
- std::cout<<"send not listen."<<std::endl;
- return -1;
- }
- mbFunPlus = false;
- mpCall = pCall;
- ModuleFun topicFunction = std::bind(&modulecomm_impl_tcp::callbackTopic,this,std::placeholders::_1,
- std::placeholders::_2,
- std::placeholders::_3,
- std::placeholders::_4,
- std::placeholders::_5);
- mpSub->setReceivedTopicFunction(topicFunction);
- return 0;
- }
- void modulecomm_impl_tcp::writemsg(const char *str, int nlen)
- {
- if(mnType == type_recv)
- {
- std::cout<<"recv not send."<<std::endl;
- return ;
- }
- mpPub->senddata(str,nlen);
- }
- #endif
|