modulecomm_impl_tcp.cpp 4.6 KB

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