TopicsSubscriber.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*!
  15. * @file TopicsSubscriber.cpp
  16. * This file contains the implementation of the subscriber functions.
  17. *
  18. * This file was generated by the tool fastcdrgen.
  19. */
  20. #ifdef USE_FASTRTPS
  21. #include <fastrtps/participant/Participant.h>
  22. #include <fastrtps/attributes/ParticipantAttributes.h>
  23. #include <fastrtps/subscriber/Subscriber.h>
  24. #include <fastrtps/attributes/SubscriberAttributes.h>
  25. #include <fastrtps/Domain.h>
  26. #include <fastcdr/FastBuffer.h>
  27. #include <fastcdr/FastCdr.h>
  28. #include <fastcdr/Cdr.h>
  29. #include <fastrtps/participant/Participant.h>
  30. #include <fastrtps/attributes/ParticipantAttributes.h>
  31. #include <fastrtps/attributes/PublisherAttributes.h>
  32. #include <fastrtps/publisher/Publisher.h>
  33. #include <fastrtps/Domain.h>
  34. #include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
  35. #include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
  36. #include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
  37. #include "TopicsSubscriber.h"
  38. using namespace eprosima::fastrtps;
  39. using namespace eprosima::fastrtps::rtps;
  40. using namespace eprosima::fastdds::rtps;
  41. #include <QDateTime>
  42. TopicsSubscriber::TopicsSubscriber() : mp_participant(nullptr), mp_subscriber(nullptr) {}
  43. TopicsSubscriber::~TopicsSubscriber() { Domain::removeParticipant(mp_participant);}
  44. bool TopicsSubscriber::init(const char * strtopic,const char * strpubip,const unsigned short nPort,int ntype)
  45. {
  46. strncpy(mstrtopic,strtopic,255);
  47. // Create RTPSParticipant
  48. ParticipantAttributes PParam;
  49. PParam.rtps.setName("Participant_subscriber"); //You can put the name you want
  50. PParam.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SIMPLE;
  51. PParam.rtps.builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = true;
  52. PParam.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter = true;
  53. PParam.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationWriterANDSubscriptionReader = true;
  54. PParam.rtps.builtin.discovery_config.leaseDuration = c_TimeInfinite;
  55. // SharedMem transport configuration
  56. PParam.rtps.useBuiltinTransports = false;
  57. if(ntype == 0)
  58. {
  59. auto sm_transport = std::make_shared<SharedMemTransportDescriptor>();
  60. sm_transport->segment_size(100*1024*1024);
  61. PParam.rtps.userTransports.push_back(sm_transport);
  62. // UDP
  63. auto udp_transport = std::make_shared<UDPv4TransportDescriptor>();
  64. //udp_transport->interfaceWhiteList.push_back("127.0.0.1");
  65. PParam.rtps.userTransports.push_back(udp_transport);
  66. }
  67. else
  68. {
  69. auto tcp2_transport = std::make_shared<TCPv4TransportDescriptor>();
  70. //Set initial peers.
  71. Locator_t initial_peer_locator;
  72. initial_peer_locator.kind = LOCATOR_KIND_TCPv4;
  73. IPLocator::setIPv4(initial_peer_locator, strpubip);
  74. initial_peer_locator.port = nPort;
  75. PParam.rtps.builtin.initialPeersList.push_back(initial_peer_locator);
  76. // PParam.rtps.builtin.discovery_config.leaseDuration = c_TimeInfinite;
  77. // PParam.rtps.builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(5, 0);
  78. // PParam.rtps.setName("Participant_sub");
  79. tcp2_transport->wait_for_tcp_negotiation = false;
  80. //Link the Transport Layer to the Participant.
  81. PParam.rtps.userTransports.push_back(tcp2_transport);
  82. }
  83. mp_participant = Domain::createParticipant(PParam);
  84. if(mp_participant == nullptr)
  85. {
  86. return false;
  87. }
  88. //Register the type
  89. Domain::registerType(mp_participant, static_cast<TopicDataType*>(&myType));
  90. // Create Subscriber
  91. SubscriberAttributes Rparam;
  92. Rparam.topic.topicKind = NO_KEY;
  93. Rparam.topic.topicDataType = myType.getName(); //Must be registered before the creation of the subscriber
  94. Rparam.topic.topicName = strtopic;//"TopicsPubSubTopic";
  95. Rparam.topic.historyQos.depth = 30;
  96. Rparam.topic.resourceLimitsQos.max_samples = 50;
  97. Rparam.topic.resourceLimitsQos.allocated_samples = 20;
  98. Rparam.qos.m_reliability.kind = RELIABLE_RELIABILITY_QOS;
  99. mp_subscriber = Domain::createSubscriber(mp_participant,Rparam, static_cast<SubscriberListener*>(&m_listener));
  100. if(mp_subscriber == nullptr)
  101. {
  102. return false;
  103. }
  104. return true;
  105. }
  106. void TopicsSubscriber::SubListener::onSubscriptionMatched(Subscriber* sub,MatchingInfo& info)
  107. {
  108. (void)sub;
  109. if (info.status == MATCHED_MATCHING)
  110. {
  111. n_matched++;
  112. std::cout << "Subscriber matched" << std::endl;
  113. }
  114. else
  115. {
  116. n_matched--;
  117. std::cout << "Subscriber unmatched" << std::endl;
  118. }
  119. }
  120. void TopicsSubscriber::SubListener::setReceivedTopicFunction(ModuleFun xFun)
  121. {
  122. mFun = xFun;
  123. mbSetFun = true;
  124. }
  125. void TopicsSubscriber::SubListener::onNewDataMessage(Subscriber* sub)
  126. {
  127. // Take data
  128. TopicSample::Message st;
  129. static int ncount = 0;
  130. static int nmaxlatancy = 0;
  131. std::cout<<"new msg"<<std::endl;
  132. // char * strbuf = new char[1000000];
  133. // eprosima::fastcdr::FastBuffer pbuf(strbuf,1000000);
  134. // eprosima::fastcdr::Cdr * pxcdr;//
  135. // pxcdr = new eprosima::fastcdr::Cdr(pbuf);
  136. // if(sub->takeNextData(pxcdr, &m_info))
  137. // {
  138. // if(m_info.sampleKind == ALIVE)
  139. // {
  140. // // Print your structure data here.
  141. // ++n_msg;
  142. // std::cout << "Sample received, count=" << n_msg<<std::endl;
  143. // st.deserialize(*pxcdr);
  144. // std::cout<<" size is "<<TopicSample::Message::getCdrSerializedSize(st)<<std::endl;
  145. // }
  146. // }
  147. // return;
  148. // sub->get_first_untaken_info(&m_info);
  149. std::cout<<"count is "<<sub->getUnreadCount()<<std::endl;
  150. if(sub->takeNextData(&st, &m_info))
  151. {
  152. if(m_info.sampleKind == ALIVE)
  153. {
  154. // Print your structure data here.
  155. ++n_msg;
  156. ncount++;
  157. std::cout << "Sample received, count=" << st.counter() <<" total: "<<ncount<<std::endl;
  158. qint64 timex = QDateTime::currentMSecsSinceEpoch();
  159. int nlatancy = (timex - st.sendtime());
  160. if(nlatancy>nmaxlatancy)nmaxlatancy = nlatancy;
  161. std::cout<<" latency is "<<nlatancy<<" max: "<<nmaxlatancy<<std::endl;
  162. std::cout<<" size is "<<st.xdata().size()<<std::endl;
  163. QDateTime dt = QDateTime::fromMSecsSinceEpoch(st.sendtime());
  164. if(mbSetFun) mFun((char *)(st.xdata().data()),st.xdata().size(),st.counter(),&dt,st.msgname().data());
  165. }
  166. }
  167. }
  168. void TopicsSubscriber::setReceivedTopicFunction(ModuleFun xFun)
  169. {
  170. m_listener.setReceivedTopicFunction(xFun);
  171. }
  172. void TopicsSubscriber::run()
  173. {
  174. std::cout << "Waiting for Data, press Enter to stop the Subscriber. "<<std::endl;
  175. std::cin.ignore();
  176. std::cout << "Shutting down the Subscriber." << std::endl;
  177. }
  178. #endif