modulecomm.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "modulecomm.h"
  2. #include <iostream>
  3. #include "modulecomm_shm.h"
  4. #include "modulecomm_inter.h"
  5. #ifdef USE_FASTRTPS
  6. #include "modulecomm_fastrtps_shm.h"
  7. #include "modulecomm_fastrtps_tcp.h"
  8. #endif
  9. #include <QtXml>
  10. #include <QFile>
  11. namespace iv {
  12. namespace modulecomm {
  13. struct ModeduleInfo
  14. {
  15. modulecomm_base * mphandle;
  16. ModuleComm_TYPE mmctype;
  17. };
  18. static ModuleComm_TYPE getdefmodulecommtype()
  19. {
  20. #ifdef ANDROID
  21. return ModuleComm_INTERIOR;
  22. #endif
  23. ModuleComm_TYPE xrtn = ModuleComm_SHAREMEM;
  24. QDomDocument xdoc;
  25. QFile file("./modulecomm.xml");
  26. if (!file.open(QIODevice::ReadOnly))
  27. {
  28. return xrtn;
  29. }
  30. if (!xdoc.setContent(&file)) {
  31. file.close();
  32. return xrtn;
  33. }
  34. file.close();
  35. QDomElement docElem = xdoc.documentElement();
  36. QDomNode n = docElem.firstChild();
  37. while(!n.isNull())
  38. {
  39. QDomElement e = n.toElement();
  40. if(e.nodeName() == "commtype")
  41. {
  42. QString strvalue = e.attribute("value","0");
  43. if(strvalue == "0")
  44. {
  45. xrtn = ModuleComm_SHAREMEM;
  46. }
  47. if(strvalue == "1")
  48. {
  49. xrtn = ModuleComm_INTERIOR;
  50. }
  51. #ifdef USE_FASTRTPS
  52. if(strvalue == "2")
  53. {
  54. xrtn = ModuleComm_FASTRTPS;
  55. }
  56. #endif
  57. break;
  58. }
  59. n = n.nextSibling();
  60. }
  61. return xrtn;
  62. }
  63. void * RegisterSend(const char * strcommname)
  64. {
  65. return RegisterSend(strcommname,1000,1);
  66. }
  67. void * RegisterSend(const char * strcommname,const unsigned int nBufSize,const unsigned int nMsgBufCount,
  68. ModuleComm_TYPE xmctype,const unsigned short nport)
  69. {
  70. iv::modulecomm::ModeduleInfo * pmi = new iv::modulecomm::ModeduleInfo;
  71. pmi->mmctype = xmctype;
  72. pmi->mphandle = 0;
  73. ModuleComm_TYPE mctype = xmctype;
  74. if(mctype == ModuleComm_UNDEFINE)
  75. {
  76. mctype = getdefmodulecommtype();
  77. }
  78. switch (mctype) {
  79. case ModuleComm_SHAREMEM:
  80. {
  81. iv::modulecomm_shm * p = new iv::modulecomm_shm();
  82. p->RegisterSend(strcommname,nBufSize,nMsgBufCount);
  83. pmi->mphandle = (iv::modulecomm_base *)p;
  84. }
  85. break;
  86. case ModuleComm_INTERIOR:
  87. {
  88. iv::modulecomm_inter * p = new iv::modulecomm_inter();
  89. p->RegisterSend(strcommname,nBufSize,nMsgBufCount);
  90. pmi->mphandle = (iv::modulecomm_base *)p;
  91. }
  92. break;
  93. #ifdef USE_FASTRTPS
  94. case ModuleComm_FASTRTPS:
  95. {
  96. iv::modulecomm_fastrtps_shm * p = new iv::modulecomm_fastrtps_shm();
  97. p->RegisterSend(strcommname,nBufSize,nMsgBufCount);
  98. pmi->mphandle = (iv::modulecomm_base *)p;
  99. }
  100. break;
  101. case ModuleComm_FASTRTPS_TCP:
  102. {
  103. iv::modulecomm_fastrtps_tcp * p = new iv::modulecomm_fastrtps_tcp();
  104. p->RegisterSend(strcommname,nBufSize,nMsgBufCount,nport);
  105. pmi->mphandle = (iv::modulecomm_base *)p;
  106. }
  107. break;
  108. #endif
  109. default:
  110. break;
  111. }
  112. return pmi;
  113. }
  114. void * RegisterRecv(const char * strcommname,SMCallBack pCall,ModuleComm_TYPE xmctype,const char * strip,
  115. const unsigned short nPort)
  116. {
  117. iv::modulecomm::ModeduleInfo * pmi = new iv::modulecomm::ModeduleInfo;
  118. pmi->mmctype = xmctype;
  119. pmi->mphandle = 0;
  120. ModuleComm_TYPE mctype = xmctype;
  121. if(mctype == ModuleComm_UNDEFINE)
  122. {
  123. mctype = getdefmodulecommtype();
  124. }
  125. switch (mctype) {
  126. case ModuleComm_SHAREMEM:
  127. {
  128. iv::modulecomm_shm * p = new iv::modulecomm_shm();
  129. p->RegisterRecv(strcommname,pCall);
  130. pmi->mphandle = (iv::modulecomm_base *)p;
  131. }
  132. break;
  133. case ModuleComm_INTERIOR:
  134. {
  135. iv::modulecomm_inter * p = new iv::modulecomm_inter();
  136. p->RegisterRecv(strcommname,pCall);
  137. pmi->mphandle = (iv::modulecomm_base *)p;
  138. }
  139. break;
  140. #ifdef USE_FASTRTPS
  141. case ModuleComm_FASTRTPS:
  142. {
  143. iv::modulecomm_fastrtps_shm * p = new iv::modulecomm_fastrtps_shm();
  144. p->RegisterRecv(strcommname,pCall);
  145. pmi->mphandle = (iv::modulecomm_base *)p;
  146. }
  147. break;
  148. case ModuleComm_FASTRTPS_TCP:
  149. {
  150. iv::modulecomm_fastrtps_tcp * p = new iv::modulecomm_fastrtps_tcp();
  151. p->RegisterRecv(strcommname,pCall,strip,nPort);
  152. pmi->mphandle = (iv::modulecomm_base *)p;
  153. }
  154. break;
  155. #endif
  156. default:
  157. break;
  158. }
  159. return pmi;
  160. }
  161. void * RegisterRecvPlus(const char * strcommname,ModuleFun xFun,
  162. ModuleComm_TYPE xmctype,const char * strip,
  163. const unsigned short nPort)
  164. {
  165. iv::modulecomm::ModeduleInfo * pmi = new iv::modulecomm::ModeduleInfo;
  166. pmi->mmctype = xmctype;
  167. pmi->mphandle = 0;
  168. ModuleComm_TYPE mctype = xmctype;
  169. if(mctype == ModuleComm_UNDEFINE)
  170. {
  171. mctype = getdefmodulecommtype();
  172. }
  173. switch (mctype) {
  174. case ModuleComm_SHAREMEM:
  175. {
  176. iv::modulecomm_shm * p = new iv::modulecomm_shm();
  177. p->RegisterRecvPlus(strcommname,xFun);
  178. pmi->mphandle = (iv::modulecomm_base *)p;
  179. }
  180. break;
  181. case ModuleComm_INTERIOR:
  182. {
  183. iv::modulecomm_inter * p = new iv::modulecomm_inter();
  184. p->RegisterRecvPlus(strcommname,xFun);
  185. pmi->mphandle = (iv::modulecomm_base *)p;
  186. }
  187. break;
  188. #ifdef USE_FASTRTPS
  189. case ModuleComm_FASTRTPS:
  190. {
  191. iv::modulecomm_fastrtps_shm * p = new iv::modulecomm_fastrtps_shm();
  192. p->RegisterRecvPlus(strcommname,xFun);
  193. pmi->mphandle = (iv::modulecomm_base *)p;
  194. }
  195. break;
  196. case ModuleComm_FASTRTPS_TCP:
  197. {
  198. iv::modulecomm_fastrtps_tcp * p = new iv::modulecomm_fastrtps_tcp();
  199. p->RegisterRecvPlus(strcommname,xFun,strip,nPort);
  200. pmi->mphandle = (iv::modulecomm_base *)p;
  201. }
  202. break;
  203. #endif
  204. default:
  205. break;
  206. }
  207. return pmi;
  208. }
  209. void ModuleSendMsg(void * pHandle,const char * strdata,const unsigned int nDataLen)
  210. {
  211. iv::modulecomm::ModeduleInfo * pmi = (iv::modulecomm::ModeduleInfo * )pHandle;
  212. if(pmi->mphandle == 0)
  213. {
  214. qDebug("handler error. use fastrtps but not define.");
  215. return;
  216. }
  217. pmi->mphandle->ModuleSendMsg(strdata,nDataLen);
  218. }
  219. void Unregister(void * pHandle)
  220. {
  221. iv::modulecomm::ModeduleInfo * pmi = (iv::modulecomm::ModeduleInfo * )pHandle;
  222. if(pmi->mphandle == 0)
  223. {
  224. qDebug("handler error. use fastrtps but not define.");
  225. return;
  226. }
  227. pmi->mphandle->Unregister();
  228. }
  229. void PauseComm(void *pHandle)
  230. {
  231. iv::modulecomm::ModeduleInfo * pmi = (iv::modulecomm::ModeduleInfo * )pHandle;
  232. if(pmi->mphandle == 0)
  233. {
  234. qDebug("handler error. use fastrtps but not define.");
  235. return;
  236. }
  237. pmi->mphandle->PauseComm();
  238. }
  239. void ContintuComm(void *pHandle)
  240. {
  241. iv::modulecomm::ModeduleInfo * pmi = (iv::modulecomm::ModeduleInfo * )pHandle;
  242. if(pmi->mphandle == 0)
  243. {
  244. qDebug("handler error. use fastrtps but not define.");
  245. return;
  246. }
  247. pmi->mphandle->ContintuComm();
  248. }
  249. }
  250. }