modulecomm.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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,iv::modulecomm::ModuleComm_UNDEFINE,5100);
  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. (void)nport;
  71. iv::modulecomm::ModeduleInfo * pmi = new iv::modulecomm::ModeduleInfo;
  72. pmi->mmctype = xmctype;
  73. pmi->mphandle = 0;
  74. ModuleComm_TYPE mctype = xmctype;
  75. if(mctype == ModuleComm_UNDEFINE)
  76. {
  77. mctype = getdefmodulecommtype();
  78. }
  79. switch (mctype) {
  80. case ModuleComm_SHAREMEM:
  81. {
  82. iv::modulecomm_shm * p = new iv::modulecomm_shm();
  83. p->RegisterSend(strcommname,nBufSize,nMsgBufCount);
  84. pmi->mphandle = (iv::modulecomm_base *)p;
  85. }
  86. break;
  87. case ModuleComm_INTERIOR:
  88. {
  89. iv::modulecomm_inter * p = new iv::modulecomm_inter();
  90. p->RegisterSend(strcommname,nBufSize,nMsgBufCount);
  91. pmi->mphandle = (iv::modulecomm_base *)p;
  92. }
  93. break;
  94. #ifdef USE_FASTRTPS
  95. case ModuleComm_FASTRTPS:
  96. {
  97. iv::modulecomm_fastrtps_shm * p = new iv::modulecomm_fastrtps_shm();
  98. p->RegisterSend(strcommname,nBufSize,nMsgBufCount);
  99. pmi->mphandle = (iv::modulecomm_base *)p;
  100. }
  101. break;
  102. case ModuleComm_FASTRTPS_TCP:
  103. {
  104. iv::modulecomm_fastrtps_tcp * p = new iv::modulecomm_fastrtps_tcp();
  105. p->RegisterSend(strcommname,nBufSize,nMsgBufCount,nport);
  106. pmi->mphandle = (iv::modulecomm_base *)p;
  107. }
  108. break;
  109. #endif
  110. default:
  111. break;
  112. }
  113. return pmi;
  114. }
  115. void * RegisterRecv(const char * strcommname,SMCallBack pCall,ModuleComm_TYPE xmctype,const char * strip,
  116. const unsigned short nPort)
  117. {
  118. (void)strip;
  119. (void)nPort;
  120. iv::modulecomm::ModeduleInfo * pmi = new iv::modulecomm::ModeduleInfo;
  121. pmi->mmctype = xmctype;
  122. pmi->mphandle = 0;
  123. ModuleComm_TYPE mctype = xmctype;
  124. if(mctype == ModuleComm_UNDEFINE)
  125. {
  126. mctype = getdefmodulecommtype();
  127. }
  128. switch (mctype) {
  129. case ModuleComm_SHAREMEM:
  130. {
  131. iv::modulecomm_shm * p = new iv::modulecomm_shm();
  132. p->RegisterRecv(strcommname,pCall);
  133. pmi->mphandle = (iv::modulecomm_base *)p;
  134. }
  135. break;
  136. case ModuleComm_INTERIOR:
  137. {
  138. iv::modulecomm_inter * p = new iv::modulecomm_inter();
  139. p->RegisterRecv(strcommname,pCall);
  140. pmi->mphandle = (iv::modulecomm_base *)p;
  141. }
  142. break;
  143. #ifdef USE_FASTRTPS
  144. case ModuleComm_FASTRTPS:
  145. {
  146. iv::modulecomm_fastrtps_shm * p = new iv::modulecomm_fastrtps_shm();
  147. p->RegisterRecv(strcommname,pCall);
  148. pmi->mphandle = (iv::modulecomm_base *)p;
  149. }
  150. break;
  151. case ModuleComm_FASTRTPS_TCP:
  152. {
  153. iv::modulecomm_fastrtps_tcp * p = new iv::modulecomm_fastrtps_tcp();
  154. p->RegisterRecv(strcommname,pCall,strip,nPort);
  155. pmi->mphandle = (iv::modulecomm_base *)p;
  156. }
  157. break;
  158. #endif
  159. default:
  160. break;
  161. }
  162. return pmi;
  163. }
  164. void * RegisterRecvPlus(const char * strcommname,ModuleFun xFun,
  165. ModuleComm_TYPE xmctype,const char * strip,
  166. const unsigned short nPort)
  167. {
  168. (void)strip;
  169. (void)nPort;
  170. iv::modulecomm::ModeduleInfo * pmi = new iv::modulecomm::ModeduleInfo;
  171. pmi->mmctype = xmctype;
  172. pmi->mphandle = 0;
  173. ModuleComm_TYPE mctype = xmctype;
  174. if(mctype == ModuleComm_UNDEFINE)
  175. {
  176. mctype = getdefmodulecommtype();
  177. }
  178. switch (mctype) {
  179. case ModuleComm_SHAREMEM:
  180. {
  181. iv::modulecomm_shm * p = new iv::modulecomm_shm();
  182. p->RegisterRecvPlus(strcommname,xFun);
  183. pmi->mphandle = (iv::modulecomm_base *)p;
  184. }
  185. break;
  186. case ModuleComm_INTERIOR:
  187. {
  188. iv::modulecomm_inter * p = new iv::modulecomm_inter();
  189. p->RegisterRecvPlus(strcommname,xFun);
  190. pmi->mphandle = (iv::modulecomm_base *)p;
  191. }
  192. break;
  193. #ifdef USE_FASTRTPS
  194. case ModuleComm_FASTRTPS:
  195. {
  196. iv::modulecomm_fastrtps_shm * p = new iv::modulecomm_fastrtps_shm();
  197. p->RegisterRecvPlus(strcommname,xFun);
  198. pmi->mphandle = (iv::modulecomm_base *)p;
  199. }
  200. break;
  201. case ModuleComm_FASTRTPS_TCP:
  202. {
  203. iv::modulecomm_fastrtps_tcp * p = new iv::modulecomm_fastrtps_tcp();
  204. p->RegisterRecvPlus(strcommname,xFun,strip,nPort);
  205. pmi->mphandle = (iv::modulecomm_base *)p;
  206. }
  207. break;
  208. #endif
  209. default:
  210. break;
  211. }
  212. return pmi;
  213. }
  214. void ModuleSendMsg(void * pHandle,const char * strdata,const unsigned int nDataLen)
  215. {
  216. iv::modulecomm::ModeduleInfo * pmi = (iv::modulecomm::ModeduleInfo * )pHandle;
  217. if(pmi->mphandle == 0)
  218. {
  219. qDebug("handler error. use fastrtps but not define.");
  220. return;
  221. }
  222. pmi->mphandle->ModuleSendMsg(strdata,nDataLen);
  223. }
  224. void Unregister(void * pHandle)
  225. {
  226. iv::modulecomm::ModeduleInfo * pmi = (iv::modulecomm::ModeduleInfo * )pHandle;
  227. if(pmi->mphandle == 0)
  228. {
  229. qDebug("handler error. use fastrtps but not define.");
  230. return;
  231. }
  232. pmi->mphandle->Unregister();
  233. }
  234. void PauseComm(void *pHandle)
  235. {
  236. iv::modulecomm::ModeduleInfo * pmi = (iv::modulecomm::ModeduleInfo * )pHandle;
  237. if(pmi->mphandle == 0)
  238. {
  239. qDebug("handler error. use fastrtps but not define.");
  240. return;
  241. }
  242. pmi->mphandle->PauseComm();
  243. }
  244. void ContintuComm(void *pHandle)
  245. {
  246. iv::modulecomm::ModeduleInfo * pmi = (iv::modulecomm::ModeduleInfo * )pHandle;
  247. if(pmi->mphandle == 0)
  248. {
  249. qDebug("handler error. use fastrtps but not define.");
  250. return;
  251. }
  252. pmi->mphandle->ContintuComm();
  253. }
  254. void * RegisterSend(const char * strcommname,const unsigned int nBufSize,const unsigned int nMsgBufCount)
  255. {
  256. return RegisterSend(strcommname,nBufSize,nMsgBufCount,iv::modulecomm::ModuleComm_UNDEFINE,5100);
  257. }
  258. void * RegisterRecv(const char * strcommname,SMCallBack pCall)
  259. {
  260. return RegisterRecv(strcommname,pCall,iv::modulecomm::ModuleComm_UNDEFINE,0,5100);
  261. }
  262. void * RegisterRecvPlus(const char * strcommname,ModuleFun xFun)
  263. {
  264. return RegisterRecvPlus(strcommname,xFun,iv::modulecomm::ModuleComm_UNDEFINE,0,5100);
  265. }
  266. }
  267. }