modulecomm_impl_shm.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifdef USE_FASTRTPS
  2. #include "modulecomm_impl_shm.h"
  3. #include <thread>
  4. #include <iostream>
  5. #include <QDateTime>
  6. #include <QMutex>
  7. #include <QFile>
  8. namespace iv {
  9. namespace modulecomm {
  10. static QMutex gmodulecomm_dds_Mutex;
  11. static int createcount = 0;
  12. }
  13. }
  14. void modulecomm_impl_shm::callbackTopic(const char * strdata,const unsigned int nSize,const unsigned int index, QDateTime * dt,const char * strmemname) {
  15. if(mbFunPlus)
  16. {
  17. mFun(strdata,nSize,index,dt,strmemname);
  18. }
  19. else
  20. {
  21. (*mpCall)(strdata,nSize,index,dt,strmemname);
  22. }
  23. }
  24. int modulecomm_impl_shm::GetTempConfPath(char *strpath)
  25. {
  26. char strtmppath[256];
  27. QDateTime dt = QDateTime::currentDateTime();
  28. snprintf(strtmppath,256,"/tmp/adc_modulecomm_conf_%04d%02d%02d%02d%02d.ini",dt.date().year(),
  29. dt.date().month(),dt.date().day(),dt.time().hour(),dt.time().minute());
  30. QFile xFile;
  31. xFile.setFileName(strtmppath);
  32. char strtem[256];
  33. char strdata[10000];
  34. snprintf(strdata,10000,"");
  35. if(!xFile.exists())
  36. {
  37. if(xFile.open(QIODevice::ReadWrite))
  38. {
  39. snprintf(strtem,256,"[common]\n");strncat(strdata,strtem,10000);
  40. snprintf(strtem,256,"DCPSDefaultDiscovery=TheRTPSConfig\n");strncat(strdata,strtem,10000);
  41. #ifdef dds_use_shm
  42. snprintf(strtem,256,"DCPSGlobalTransportConfig=myconfig\n");strncat(strdata,strtem,10000);
  43. snprintf(strtem,256,"[config/myconfig]\n");strncat(strdata,strtem,10000);
  44. snprintf(strtem,256,"transports=share\n");strncat(strdata,strtem,10000);
  45. snprintf(strtem,256,"[transport/share]\n");strncat(strdata,strtem,10000);
  46. snprintf(strtem,256,"transport_type=shmem\n");strncat(strdata,strtem,10000);
  47. snprintf(strtem,256,"pool_size=100000000\n");strncat(strdata,strtem,10000);
  48. #endif
  49. snprintf(strtem,256,"[rtps_discovery/TheRTPSConfig]\n");strncat(strdata,strtem,10000);
  50. snprintf(strtem,256,"ResendPeriod=5\n");strncat(strdata,strtem,10000);
  51. xFile.write(strdata,strnlen(strdata,10000));
  52. xFile.close();
  53. }
  54. }
  55. strncpy(strpath,strtmppath,255);
  56. return 0;
  57. }
  58. modulecomm_impl_shm::modulecomm_impl_shm(const char * strcommname,int ntype )
  59. {
  60. strncpy(mstrtopic,strcommname,255);
  61. iv::modulecomm::gmodulecomm_dds_Mutex.lock();
  62. if(ntype == type_recv)
  63. {
  64. mpSub = new TopicsSubscriber();
  65. mpSub->init(strcommname);
  66. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  67. mnType = type_recv;
  68. }
  69. else
  70. {
  71. mpPub = new TopicsPublisher();
  72. mpPub->init(strcommname);
  73. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  74. mnType = type_send;
  75. }
  76. iv::modulecomm::createcount++;
  77. std::cout<<"count is "<<iv::modulecomm::createcount<<std::endl;
  78. iv::modulecomm::gmodulecomm_dds_Mutex.unlock();
  79. }
  80. int modulecomm_impl_shm::listenmsg(ModuleFun xFun)
  81. {
  82. if(mnType == type_send)
  83. {
  84. std::cout<<"send not listen."<<std::endl;;
  85. return -1;
  86. }
  87. mbFunPlus = true;
  88. mFun = xFun;
  89. ModuleFun topicFunction = std::bind(&modulecomm_impl_shm::callbackTopic,this,std::placeholders::_1,
  90. std::placeholders::_2,
  91. std::placeholders::_3,
  92. std::placeholders::_4,
  93. std::placeholders::_5);
  94. mpSub->setReceivedTopicFunction(topicFunction);
  95. return 0;
  96. }
  97. int modulecomm_impl_shm::listenmsg(SMCallBack pCall)
  98. {
  99. if(mnType == type_send)
  100. {
  101. std::cout<<"send not listen."<<std::endl;
  102. return -1;
  103. }
  104. mbFunPlus = false;
  105. mpCall = pCall;
  106. ModuleFun topicFunction = std::bind(&modulecomm_impl_shm::callbackTopic,this,std::placeholders::_1,
  107. std::placeholders::_2,
  108. std::placeholders::_3,
  109. std::placeholders::_4,
  110. std::placeholders::_5);
  111. mpSub->setReceivedTopicFunction(topicFunction);
  112. return 0;
  113. }
  114. void modulecomm_impl_shm::writemsg(const char *str, int nlen)
  115. {
  116. if(mnType == type_recv)
  117. {
  118. std::cout<<"recv not send."<<std::endl;
  119. return ;
  120. }
  121. mpPub->senddata(str,nlen);
  122. }
  123. #endif