TopicsSubscriber.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <fastrtps/participant/Participant.h>
  21. #include <fastrtps/attributes/ParticipantAttributes.h>
  22. #include <fastrtps/subscriber/Subscriber.h>
  23. #include <fastrtps/attributes/SubscriberAttributes.h>
  24. #include <fastrtps/Domain.h>
  25. #include <fastcdr/FastBuffer.h>
  26. #include <fastcdr/FastCdr.h>
  27. #include <fastcdr/Cdr.h>
  28. #include <fastrtps/participant/Participant.h>
  29. #include <fastrtps/attributes/ParticipantAttributes.h>
  30. #include <fastrtps/attributes/PublisherAttributes.h>
  31. #include <fastrtps/publisher/Publisher.h>
  32. #include <fastrtps/Domain.h>
  33. #include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
  34. #include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
  35. #include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
  36. #include "TopicsSubscriber.h"
  37. using namespace eprosima::fastrtps;
  38. using namespace eprosima::fastrtps::rtps;
  39. using namespace eprosima::fastdds::rtps;
  40. #include <QDateTime>
  41. TopicsSubscriber::TopicsSubscriber() : mp_participant(nullptr), mp_subscriber(nullptr) {}
  42. TopicsSubscriber::~TopicsSubscriber() { Domain::removeParticipant(mp_participant);}
  43. bool TopicsSubscriber::init(const char * strtopic,const char * strpubip,const unsigned short nPort,int ntype)
  44. {
  45. strncpy(mstrtopic,strtopic,255);
  46. // Create RTPSParticipant
  47. ParticipantAttributes PParam;
  48. PParam.rtps.setName("Participant_subscriber"); //You can put the name you want
  49. PParam.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SIMPLE;
  50. PParam.rtps.builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = true;
  51. PParam.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter = true;
  52. PParam.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationWriterANDSubscriptionReader = true;
  53. PParam.rtps.builtin.discovery_config.leaseDuration = c_TimeInfinite;
  54. // SharedMem transport configuration
  55. PParam.rtps.useBuiltinTransports = false;
  56. if(ntype == 0)
  57. {
  58. auto sm_transport = std::make_shared<SharedMemTransportDescriptor>();
  59. sm_transport->segment_size(100*1024*1024);
  60. PParam.rtps.userTransports.push_back(sm_transport);
  61. // UDP
  62. auto udp_transport = std::make_shared<UDPv4TransportDescriptor>();
  63. //udp_transport->interfaceWhiteList.push_back("127.0.0.1");
  64. PParam.rtps.userTransports.push_back(udp_transport);
  65. }
  66. else
  67. {
  68. auto tcp2_transport = std::make_shared<TCPv4TransportDescriptor>();
  69. //Set initial peers.
  70. Locator_t initial_peer_locator;
  71. initial_peer_locator.kind = LOCATOR_KIND_TCPv4;
  72. IPLocator::setIPv4(initial_peer_locator, strpubip);
  73. initial_peer_locator.port = nPort;
  74. PParam.rtps.builtin.initialPeersList.push_back(initial_peer_locator);
  75. // PParam.rtps.builtin.discovery_config.leaseDuration = c_TimeInfinite;
  76. // PParam.rtps.builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(5, 0);
  77. // PParam.rtps.setName("Participant_sub");
  78. tcp2_transport->wait_for_tcp_negotiation = false;
  79. //Link the Transport Layer to the Participant.
  80. PParam.rtps.userTransports.push_back(tcp2_transport);
  81. }
  82. mp_participant = Domain::createParticipant(PParam);
  83. if(mp_participant == nullptr)
  84. {
  85. return false;
  86. }
  87. //Register the type
  88. Domain::registerType(mp_participant, static_cast<TopicDataType*>(&myType));
  89. // Create Subscriber
  90. SubscriberAttributes Rparam;
  91. Rparam.topic.topicKind = NO_KEY;
  92. Rparam.topic.topicDataType = myType.getName(); //Must be registered before the creation of the subscriber
  93. Rparam.topic.topicName = strtopic;//"TopicsPubSubTopic";
  94. Rparam.topic.historyQos.depth = 30;
  95. Rparam.topic.resourceLimitsQos.max_samples = 50;
  96. Rparam.topic.resourceLimitsQos.allocated_samples = 20;
  97. Rparam.qos.m_reliability.kind = RELIABLE_RELIABILITY_QOS;
  98. mp_subscriber = Domain::createSubscriber(mp_participant,Rparam, static_cast<SubscriberListener*>(&m_listener));
  99. if(mp_subscriber == nullptr)
  100. {
  101. return false;
  102. }
  103. return true;
  104. }
  105. void TopicsSubscriber::SubListener::onSubscriptionMatched(Subscriber* sub,MatchingInfo& info)
  106. {
  107. (void)sub;
  108. if (info.status == MATCHED_MATCHING)
  109. {
  110. n_matched++;
  111. std::cout << "Subscriber matched" << std::endl;
  112. }
  113. else
  114. {
  115. n_matched--;
  116. std::cout << "Subscriber unmatched" << std::endl;
  117. }
  118. }
  119. void TopicsSubscriber::SubListener::setReceivedTopicFunction(ModuleFun xFun)
  120. {
  121. mFun = xFun;
  122. mbSetFun = true;
  123. }
  124. void TopicsSubscriber::SubListener::onNewDataMessage(Subscriber* sub)
  125. {
  126. // Take data
  127. TopicSample::Message st;
  128. static int ncount = 0;
  129. static int nmaxlatancy = 0;
  130. std::cout<<"new msg"<<std::endl;
  131. // char * strbuf = new char[1000000];
  132. // eprosima::fastcdr::FastBuffer pbuf(strbuf,1000000);
  133. // eprosima::fastcdr::Cdr * pxcdr;//
  134. // pxcdr = new eprosima::fastcdr::Cdr(pbuf);
  135. // if(sub->takeNextData(pxcdr, &m_info))
  136. // {
  137. // if(m_info.sampleKind == ALIVE)
  138. // {
  139. // // Print your structure data here.
  140. // ++n_msg;
  141. // std::cout << "Sample received, count=" << n_msg<<std::endl;
  142. // st.deserialize(*pxcdr);
  143. // std::cout<<" size is "<<TopicSample::Message::getCdrSerializedSize(st)<<std::endl;
  144. // }
  145. // }
  146. // return;
  147. // sub->get_first_untaken_info(&m_info);
  148. std::cout<<"count is "<<sub->getUnreadCount()<<std::endl;
  149. if(sub->takeNextData(&st, &m_info))
  150. {
  151. if(m_info.sampleKind == ALIVE)
  152. {
  153. // Print your structure data here.
  154. ++n_msg;
  155. ncount++;
  156. std::cout << "Sample received, count=" << st.counter() <<" total: "<<ncount<<std::endl;
  157. qint64 timex = QDateTime::currentMSecsSinceEpoch();
  158. int nlatancy = (timex - st.sendtime());
  159. if(nlatancy>nmaxlatancy)nmaxlatancy = nlatancy;
  160. std::cout<<" latency is "<<nlatancy<<" max: "<<nmaxlatancy<<std::endl;
  161. std::cout<<" size is "<<st.xdata().size()<<std::endl;
  162. QDateTime dt = QDateTime::fromMSecsSinceEpoch(st.sendtime());
  163. if(mbSetFun) mFun((char *)(st.xdata().data()),st.xdata().size(),st.counter(),&dt,st.msgname().data());
  164. }
  165. }
  166. }
  167. void TopicsSubscriber::setReceivedTopicFunction(ModuleFun xFun)
  168. {
  169. m_listener.setReceivedTopicFunction(xFun);
  170. }
  171. void TopicsSubscriber::run()
  172. {
  173. std::cout << "Waiting for Data, press Enter to stop the Subscriber. "<<std::endl;
  174. std::cin.ignore();
  175. std::cout << "Shutting down the Subscriber." << std::endl;
  176. }