|
@@ -11,6 +11,7 @@ procsm_if_readthread::procsm_if_readthread(procsm *pPSM,SMCallBack pCall,const c
|
|
|
mpCall = pCall;
|
|
|
strncpy(mstrsmname,strsmname,255);
|
|
|
|
|
|
+#ifndef USE_GROUPUDP
|
|
|
#ifdef USEDBUS
|
|
|
bool bconnect = QDBusConnection::sessionBus().connect(QString(),"/catarc/adc", "adc.adciv.modulecomm", strsmname,this,SLOT(onNewMsg(int)));
|
|
|
if(bconnect == false)
|
|
@@ -18,6 +19,7 @@ procsm_if_readthread::procsm_if_readthread(procsm *pPSM,SMCallBack pCall,const c
|
|
|
std::cout<<"procsm_if_readthread::procsm_if_readthread bconect is false"<<std::endl;
|
|
|
}
|
|
|
#endif
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
procsm_if_readthread::procsm_if_readthread(procsm *pPSM,ModuleFun xFun,const char * strsmname)
|
|
@@ -27,6 +29,7 @@ procsm_if_readthread::procsm_if_readthread(procsm *pPSM,ModuleFun xFun,const cha
|
|
|
strncpy(mstrsmname,strsmname,255);
|
|
|
mbFunPlus = true;
|
|
|
|
|
|
+#ifndef USE_GROUPUDP
|
|
|
#ifdef USEDBUS
|
|
|
bool bconnect = QDBusConnection::sessionBus().connect(QString(),"/catarc/adc", "adc.adciv.modulecomm", strsmname,this,SLOT(onNewMsg(int)));
|
|
|
if(bconnect == false)
|
|
@@ -38,6 +41,7 @@ procsm_if_readthread::procsm_if_readthread(procsm *pPSM,ModuleFun xFun,const cha
|
|
|
delete timer;
|
|
|
}
|
|
|
#endif
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
|
|
@@ -114,6 +118,7 @@ void procsm_if_readthread::run()
|
|
|
int nRtn = mpPSM->readmsg(index,str,nBufLen,&nRead,pdt);
|
|
|
if(nRtn == 0)
|
|
|
{
|
|
|
+#ifndef USE_GROUPUDP
|
|
|
#ifdef USEDBUS
|
|
|
if(mbDBUSOK == true)
|
|
|
{
|
|
@@ -127,6 +132,11 @@ void procsm_if_readthread::run()
|
|
|
}
|
|
|
#else
|
|
|
msleep(1);
|
|
|
+#endif
|
|
|
+#else
|
|
|
+ mWaitMutex.lock();
|
|
|
+ mwc.wait(&mWaitMutex,100);
|
|
|
+ mWaitMutex.unlock();
|
|
|
#endif
|
|
|
}
|
|
|
else
|
|
@@ -177,6 +187,16 @@ void procsm_if_readthread::run()
|
|
|
// qDebug("Thread finish.");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+#ifdef USE_GROUPUDP
|
|
|
+ void procsm_if_readthread::WakeRead()
|
|
|
+ {
|
|
|
+ mwc.wakeAll();
|
|
|
+ }
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifndef USE_GROUPUDP
|
|
|
#ifdef USEDBUS
|
|
|
|
|
|
void procsm_if_readthread::onNewMsg(int x)
|
|
@@ -186,6 +206,7 @@ void procsm_if_readthread::onNewMsg(int x)
|
|
|
// qDebug("wake");
|
|
|
}
|
|
|
|
|
|
+#endif
|
|
|
#endif
|
|
|
|
|
|
procsm_if::procsm_if(const char * strsmname,const unsigned int nBufSize,const unsigned int nMaxPacCount,const int nMode)
|
|
@@ -207,12 +228,21 @@ procsm_if::procsm_if(const char * strsmname,const unsigned int nBufSize,const un
|
|
|
|
|
|
mTimer.setTimerType(Qt::PreciseTimer);
|
|
|
|
|
|
+#ifdef USE_GROUPUDP
|
|
|
+ InitSock();
|
|
|
+ mKey = CalcKey(mstrsmname);
|
|
|
+#endif
|
|
|
+
|
|
|
}
|
|
|
|
|
|
procsm_if::~procsm_if()
|
|
|
{
|
|
|
if(mnType == procsm::ModeRead)
|
|
|
{
|
|
|
+#ifdef USE_GROUPUDP
|
|
|
+ mbGroupRecvRun = false;
|
|
|
+ mpthread->join();
|
|
|
+#endif
|
|
|
|
|
|
QTime xTime;
|
|
|
xTime.start();
|
|
@@ -228,6 +258,12 @@ procsm_if::~procsm_if()
|
|
|
}
|
|
|
}
|
|
|
if(bDel)delete mpReadThread;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
}
|
|
|
delete mpPSM;
|
|
|
}
|
|
@@ -243,7 +279,13 @@ int procsm_if::writemsg(const char *str, const unsigned int nSize)
|
|
|
return 0;
|
|
|
#endif
|
|
|
if(mnType == procsm::ModeRead)return -1; //this is listen.
|
|
|
- return mpPSM->writemsg(str,nSize);
|
|
|
+ int nRes = mpPSM->writemsg(str,nSize);
|
|
|
+
|
|
|
+#ifdef USE_GROUPUDP
|
|
|
+ SendGroupMsg();
|
|
|
+#endif
|
|
|
+
|
|
|
+ return nRes;
|
|
|
}
|
|
|
|
|
|
#ifdef USELCM
|
|
@@ -252,6 +294,9 @@ int procsm_if::writemsg(const char *str, const unsigned int nSize)
|
|
|
qDebug("receiv data. ");
|
|
|
}
|
|
|
#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
int procsm_if::listenmsg(SMCallBack pCall)
|
|
|
{
|
|
|
//#ifdef USELCM
|
|
@@ -268,6 +313,11 @@ int procsm_if::listenmsg(SMCallBack pCall)
|
|
|
// mpReadThread->setPriority(QThread::TimeCriticalPriority);
|
|
|
// mpReadThread->start();
|
|
|
mpReadThread->start(QThread::HighestPriority);
|
|
|
+
|
|
|
+#ifdef USE_GROUPUDP
|
|
|
+ mpthread = new std::thread(&procsm_if::ThreadGroupRecv,this);
|
|
|
+
|
|
|
+#endif
|
|
|
// mnType = 1;
|
|
|
return 0;
|
|
|
}
|
|
@@ -287,6 +337,11 @@ int procsm_if::listenmsg(ModuleFun xFun)
|
|
|
// mpReadThread->setPriority(QThread::TimeCriticalPriority);
|
|
|
// mpReadThread->start();
|
|
|
mpReadThread->start(QThread::HighestPriority);
|
|
|
+
|
|
|
+#ifdef USE_GROUPUDP
|
|
|
+ mpthread = new std::thread(&procsm_if::ThreadGroupRecv,this);
|
|
|
+
|
|
|
+#endif
|
|
|
// mnType = 1;
|
|
|
return 0;
|
|
|
}
|
|
@@ -319,5 +374,161 @@ void procsm_if::continuecomm()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+#ifdef USE_GROUPUDP
|
|
|
+
|
|
|
+void procsm_if::InitSock()
|
|
|
+{
|
|
|
+ std::string mcast_ip("236.236.100.100");
|
|
|
+ uint16_t mcast_port = 8888;
|
|
|
+
|
|
|
+
|
|
|
+ notify_fd_ = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
+ if (notify_fd_ == -1) {
|
|
|
+ std::cout << "fail to create notify fd, " << strerror(errno);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ memset(¬ify_addr_, 0, sizeof(notify_addr_));
|
|
|
+ notify_addr_.sin_family = AF_INET;
|
|
|
+ notify_addr_.sin_addr.s_addr = inet_addr(mcast_ip.c_str());
|
|
|
+ notify_addr_.sin_port = htons(mcast_port);
|
|
|
+
|
|
|
+ listen_fd_ = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
+ if (listen_fd_ == -1) {
|
|
|
+ std::cout << "fail to create listen fd, " << strerror(errno);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fcntl(listen_fd_, F_SETFL, O_NONBLOCK) == -1) {
|
|
|
+ std::cout << "fail to set listen fd nonblock, " << strerror(errno);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ memset(&listen_addr_, 0, sizeof(listen_addr_));
|
|
|
+ listen_addr_.sin_family = AF_INET;
|
|
|
+ listen_addr_.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
+ listen_addr_.sin_port = htons(mcast_port);
|
|
|
+
|
|
|
+ int yes = 1;
|
|
|
+ if (setsockopt(listen_fd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
|
|
|
+ std::cout << "fail to setsockopt SO_REUSEADDR, " << strerror(errno);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bind(listen_fd_, (struct sockaddr*)&listen_addr_, sizeof(listen_addr_)) <
|
|
|
+ 0) {
|
|
|
+ std::cout << "fail to bind addr, " << strerror(errno);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ int loop = 1;
|
|
|
+ if (setsockopt(listen_fd_, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
|
|
|
+ sizeof(loop)) < 0) {
|
|
|
+ std::cout << "fail to setsockopt IP_MULTICAST_LOOP, " << strerror(errno);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ struct ip_mreq mreq;
|
|
|
+ mreq.imr_multiaddr.s_addr = inet_addr(mcast_ip.c_str());
|
|
|
+ mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
+ if (setsockopt(listen_fd_, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
|
|
|
+ sizeof(mreq)) < 0) {
|
|
|
+ std::cout << "fail to setsockopt IP_ADD_MEMBERSHIP, " << strerror(errno);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void procsm_if::ThreadGroupRecv()
|
|
|
+{
|
|
|
+ int timeout_ms = 100;
|
|
|
+ while(mbGroupRecvRun)
|
|
|
+ {
|
|
|
+ // std::cout<<"poll"<<std::endl;
|
|
|
+ struct pollfd fds;
|
|
|
+ fds.fd = listen_fd_;
|
|
|
+ fds.events = POLLIN;
|
|
|
+ int ready_num = poll(&fds, 1, timeout_ms);
|
|
|
+ if (ready_num > 0) {
|
|
|
+ char buf[32] = {0}; // larger than ReadableInfo::kSize
|
|
|
+ ssize_t nbytes = recvfrom(listen_fd_, buf, 32, 0, nullptr, nullptr);
|
|
|
+ if (nbytes == -1) {
|
|
|
+ std::cout << "fail to recvfrom, " << strerror(errno);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(nbytes == 8)
|
|
|
+ {
|
|
|
+// std::cout<<"recv key."<<std::endl;
|
|
|
+ qint64 * pkey = (qint64 *)buf;
|
|
|
+ if(*pkey == mKey)
|
|
|
+ {
|
|
|
+ mpReadThread->WakeRead();
|
|
|
+ //Waaa;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ std::cout<<"key error"<<std::endl;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // return info->DeserializeFrom(buf, nbytes);
|
|
|
+ } else if (ready_num == 0) {
|
|
|
+// std::cout << "timeout, no readableinfo.";
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ if (errno == EINTR) {
|
|
|
+ std::cout << "poll was interrupted."<<std::endl;
|
|
|
+ } else {
|
|
|
+ std::cout << "fail to poll, " << strerror(errno);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+qint64 procsm_if::CalcKey(const char *strsmname)
|
|
|
+{
|
|
|
+ qint64 xkey = 0;
|
|
|
+ unsigned int i;
|
|
|
+ int nlen = strnlen(strsmname,256);
|
|
|
+ for(i=0;i<nlen;i++)
|
|
|
+ {
|
|
|
+ qint64 temp = *(strsmname+i);
|
|
|
+ temp = temp<<(4+i);
|
|
|
+ xkey = xkey + temp;
|
|
|
+ if(i>=3)break;
|
|
|
+ }
|
|
|
+ for(i=0;i<nlen;i++)
|
|
|
+ {
|
|
|
+ qint64 temp = *(strsmname+nlen-i-1);
|
|
|
+ temp = temp<<i;
|
|
|
+ xkey = xkey + temp;
|
|
|
+ if(i>=3)break;
|
|
|
+ }
|
|
|
+ for(i=0;i<strnlen(strsmname,256);i++)
|
|
|
+ {
|
|
|
+ qint64 temp = *(strsmname+i);
|
|
|
+ xkey = xkey + temp;
|
|
|
+ }
|
|
|
+ return xkey;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void procsm_if::SendGroupMsg()
|
|
|
+{
|
|
|
+// qint64 timenow = std::chrono::system_clock::now().time_since_epoch().count();
|
|
|
+ ssize_t nbytes =
|
|
|
+ sendto(notify_fd_, &mKey, sizeof(qint64), 0,
|
|
|
+ (struct sockaddr*)¬ify_addr_, sizeof(notify_addr_));
|
|
|
+
|
|
|
+ if(nbytes <= 0)
|
|
|
+ {
|
|
|
+ std::cout<<"Send Fail."<<std::endl;
|
|
|
+ }
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
|
|
|
|