procsm_if.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "procsm_if.h"
  2. #include <QTimer>
  3. #include <iostream>
  4. procsm_if_readthread::procsm_if_readthread(procsm *pPSM,SMCallBack pCall,const char * strsmname)
  5. {
  6. mpPSM = pPSM;
  7. mpCall = pCall;
  8. strncpy(mstrsmname,strsmname,255);
  9. #ifdef USEDBUS
  10. bool bconnect = QDBusConnection::sessionBus().connect(QString(),"/catarc/adc", "adc.adciv.modulecomm", strsmname,this,SLOT(onNewMsg(int)));
  11. if(bconnect == false)
  12. {
  13. std::cout<<"procsm_if_readthread::procsm_if_readthread bconect is false"<<std::endl;
  14. }
  15. #endif
  16. }
  17. procsm_if_readthread::procsm_if_readthread(procsm *pPSM,ModuleFun xFun,const char * strsmname)
  18. {
  19. mpPSM = pPSM;
  20. mFun = xFun;
  21. strncpy(mstrsmname,strsmname,255);
  22. mbFunPlus = true;
  23. #ifdef USEDBUS
  24. bool bconnect = QDBusConnection::sessionBus().connect(QString(),"/catarc/adc", "adc.adciv.modulecomm", strsmname,this,SLOT(onNewMsg(int)));
  25. if(bconnect == false)
  26. {
  27. std::cout<<"procsm_if_readthread::procsm_if_readthread bconect is false"<<std::endl;
  28. mbDBUSOK = false;
  29. QTimer * timer = new QTimer();
  30. timer->setTimerType(Qt::PreciseTimer);
  31. delete timer;
  32. }
  33. #endif
  34. }
  35. #ifdef USELCM
  36. void procsm_if_readthread::handlerMethod(const lcm::ReceiveBuffer *rbuf, const std::string &channel)
  37. {
  38. qDebug("lcm receiv data. ");
  39. mxindex++;
  40. QDateTime dt = QDateTime::currentDateTime();
  41. if(mbFunPlus)
  42. {
  43. mFun((char *)rbuf->data,rbuf->data_size,mxindex,&dt,mstrsmname);
  44. }
  45. else
  46. {
  47. (*mpCall)((char *)rbuf->data,rbuf->data_size,mxindex,&dt,mstrsmname);
  48. }
  49. }
  50. #endif
  51. void procsm_if_readthread::puaseread()
  52. {
  53. mbRun = false;
  54. }
  55. void procsm_if_readthread::continueread()
  56. {
  57. mbRun = true;
  58. }
  59. void procsm_if_readthread::run()
  60. {
  61. #ifdef USELCM
  62. mlcm.subscribe(mstrsmname,&procsm_if_readthread::handlerMethod,this);
  63. while(!QThread::isInterruptionRequested())
  64. {
  65. mlcm.handle();
  66. }
  67. return;
  68. #endif
  69. QTime xTime;
  70. xTime.start();
  71. unsigned int nBufLen = 1;
  72. unsigned int nRead;
  73. char * str = new char[nBufLen];
  74. unsigned int index =0;
  75. QDateTime *pdt = new QDateTime();
  76. bool bAttach = false;
  77. while(!QThread::isInterruptionRequested())
  78. {
  79. if(mbRun == false)
  80. {
  81. msleep(10);
  82. continue;
  83. }
  84. if(bAttach == false)
  85. {
  86. bAttach = mpPSM->AttachMem();
  87. if(bAttach == false)
  88. {
  89. msleep(100);
  90. continue;
  91. }
  92. else
  93. {
  94. index = mpPSM->getcurrentnext();
  95. }
  96. }
  97. int nRtn = mpPSM->readmsg(index,str,nBufLen,&nRead,pdt);
  98. if(nRtn == 0)
  99. {
  100. #ifdef USEDBUS
  101. if(mbDBUSOK == true)
  102. {
  103. mWaitMutex.lock();
  104. mwc.wait(&mWaitMutex,10);
  105. mWaitMutex.unlock();
  106. }
  107. else
  108. {
  109. msleep(1);
  110. }
  111. #else
  112. msleep(1);
  113. #endif
  114. }
  115. else
  116. {
  117. if(nRtn == -1)
  118. {
  119. nBufLen = nRead;
  120. delete str;
  121. if(nBufLen < 1)nBufLen = 1;
  122. str = new char[nBufLen];
  123. // std::cout<<"modulecomm resize str to "<<nBufLen<<std::endl;
  124. }
  125. else
  126. {
  127. if(nRtn == -2)
  128. {
  129. index = mpPSM->getcurrentnext();
  130. }
  131. else
  132. {
  133. if(nRtn >0)
  134. {
  135. if(mbFunPlus)
  136. {
  137. mFun(str,nRtn,index,pdt,mstrsmname);
  138. }
  139. else
  140. {
  141. (*mpCall)(str,nRtn,index,pdt,mstrsmname);
  142. }
  143. index++;
  144. }
  145. else
  146. {
  147. usleep(100);
  148. }
  149. }
  150. }
  151. }
  152. }
  153. delete str;
  154. delete pdt;
  155. // qDebug("Thread finish.");
  156. }
  157. #ifdef USEDBUS
  158. void procsm_if_readthread::onNewMsg(int x)
  159. {
  160. if(x == 100)std::cout<<x<<std::endl;
  161. mwc.wakeAll();
  162. // qDebug("wake");
  163. }
  164. #endif
  165. procsm_if::procsm_if(const char * strsmname,const unsigned int nBufSize,const unsigned int nMaxPacCount,const int nMode)
  166. {
  167. strncpy(mstrsmname,strsmname,255);
  168. #ifdef USELCM
  169. if(nMode == procsm::ModeWrite)
  170. {
  171. }
  172. else
  173. {
  174. }
  175. return;
  176. #endif
  177. mpPSM = new procsm(strsmname,nBufSize,nMaxPacCount,nMode);
  178. mnType = nMode;
  179. mTimer.setTimerType(Qt::PreciseTimer);
  180. }
  181. procsm_if::~procsm_if()
  182. {
  183. if(mnType == procsm::ModeRead)
  184. {
  185. QTime xTime;
  186. xTime.start();
  187. bool bDel = true;
  188. mpReadThread->requestInterruption();
  189. while(!mpReadThread->isFinished())
  190. {
  191. if(xTime.elapsed() > 1000)
  192. {
  193. bDel = false;
  194. std::cout<<"procsm_if Thread Read not finish."<<std::endl;
  195. break;
  196. }
  197. }
  198. if(bDel)delete mpReadThread;
  199. }
  200. delete mpPSM;
  201. }
  202. int procsm_if::writemsg(const char *str, const unsigned int nSize)
  203. {
  204. if(mbRun == false)return -2;
  205. #ifdef USELCM
  206. int nres = mlcm.publish(mstrsmname,str,nSize);
  207. qDebug("publish message. res = %d",nres);
  208. return 0;
  209. #endif
  210. if(mnType == procsm::ModeRead)return -1; //this is listen.
  211. return mpPSM->writemsg(str,nSize);
  212. }
  213. #ifdef USELCM
  214. void procsm_if::handlerMethod(const lcm::ReceiveBuffer *rbuf, const std::string &channel)
  215. {
  216. qDebug("receiv data. ");
  217. }
  218. #endif
  219. int procsm_if::listenmsg(SMCallBack pCall)
  220. {
  221. //#ifdef USELCM
  222. //// mlcm.subscribe(mstrsmname,&handlerMethod2);
  223. // mlcm.subscribe(mstrsmname,&procsm_if::handlerMethod,this);
  224. // while(true)
  225. // {
  226. // mlcm.handle();
  227. // }
  228. // return 0;
  229. //#endif
  230. if(mnType == procsm::ModeWrite)return -1; //listening.
  231. mpReadThread = new procsm_if_readthread(mpPSM,pCall,mstrsmname);
  232. // mpReadThread->setPriority(QThread::TimeCriticalPriority);
  233. // mpReadThread->start();
  234. mpReadThread->start(QThread::HighestPriority);
  235. // mnType = 1;
  236. return 0;
  237. }
  238. int procsm_if::listenmsg(ModuleFun xFun)
  239. {
  240. //#ifdef USELCM
  241. // mlcm.subscribe(mstrsmname,&procsm_if::handlerMethod,this);
  242. // while(true)
  243. // {
  244. // mlcm.handle();
  245. // }
  246. // return 0;
  247. //#endif
  248. if(mnType == procsm::ModeWrite)return -1; //listening.
  249. mpReadThread = new procsm_if_readthread(mpPSM,xFun,mstrsmname);
  250. // mpReadThread->setPriority(QThread::TimeCriticalPriority);
  251. // mpReadThread->start();
  252. mpReadThread->start(QThread::HighestPriority);
  253. // mnType = 1;
  254. return 0;
  255. }
  256. void procsm_if::stoplisten()
  257. {
  258. if(mnType != 1)return;
  259. mpReadThread->requestInterruption();
  260. while(!mpReadThread->isFinished());
  261. mnType = 0;
  262. // mpReadThread->deleteLater();
  263. qDebug("stop listen ok");
  264. }
  265. void procsm_if::pausecomm()
  266. {
  267. mbRun = false;
  268. if(mnType == procsm::ModeRead)
  269. {
  270. mpReadThread->puaseread();
  271. }
  272. }
  273. void procsm_if::continuecomm()
  274. {
  275. mbRun = true;
  276. if(mnType == procsm::ModeRead)
  277. {
  278. mpReadThread->continueread();
  279. }
  280. }