TopicsSubscriber.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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)
  44. {
  45. strncpy(mstrtopic,strtopic,255);
  46. mnPort = nPort;
  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. auto tcp2_transport = std::make_shared<TCPv4TransportDescriptor>();
  58. //Set initial peers.
  59. Locator_t initial_peer_locator;
  60. initial_peer_locator.kind = LOCATOR_KIND_TCPv4;
  61. IPLocator::setIPv4(initial_peer_locator, strpubip);
  62. initial_peer_locator.port = mnPort;
  63. PParam.rtps.builtin.initialPeersList.push_back(initial_peer_locator);
  64. // PParam.rtps.builtin.discovery_config.leaseDuration = c_TimeInfinite;
  65. // PParam.rtps.builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(5, 0);
  66. // PParam.rtps.setName("Participant_sub");
  67. tcp2_transport->wait_for_tcp_negotiation = false;
  68. //Link the Transport Layer to the Participant.
  69. PParam.rtps.userTransports.push_back(tcp2_transport);
  70. mp_participant = Domain::createParticipant(PParam);
  71. if(mp_participant == nullptr)
  72. {
  73. return false;
  74. }
  75. //Register the type
  76. Domain::registerType(mp_participant, static_cast<TopicDataType*>(&myType));
  77. // Create Subscriber
  78. SubscriberAttributes Rparam;
  79. Rparam.topic.topicKind = NO_KEY;
  80. Rparam.topic.topicDataType = myType.getName(); //Must be registered before the creation of the subscriber
  81. Rparam.topic.topicName = strtopic;//"TopicsPubSubTopic";
  82. Rparam.topic.historyQos.depth = 30;
  83. Rparam.topic.resourceLimitsQos.max_samples = 50;
  84. Rparam.topic.resourceLimitsQos.allocated_samples = 20;
  85. Rparam.qos.m_reliability.kind = RELIABLE_RELIABILITY_QOS;
  86. mp_subscriber = Domain::createSubscriber(mp_participant,Rparam, static_cast<SubscriberListener*>(&m_listener));
  87. if(mp_subscriber == nullptr)
  88. {
  89. return false;
  90. }
  91. return true;
  92. }
  93. void TopicsSubscriber::SubListener::onSubscriptionMatched(Subscriber* sub,MatchingInfo& info)
  94. {
  95. (void)sub;
  96. if (info.status == MATCHED_MATCHING)
  97. {
  98. n_matched++;
  99. std::cout << "Subscriber matched" << std::endl;
  100. }
  101. else
  102. {
  103. n_matched--;
  104. std::cout << "Subscriber unmatched" << std::endl;
  105. }
  106. }
  107. void TopicsSubscriber::SubListener::setReceivedTopicFunction(ModuleFun xFun)
  108. {
  109. mFun = xFun;
  110. mbSetFun = true;
  111. }
  112. void TopicsSubscriber::SubListener::onNewDataMessage(Subscriber* sub)
  113. {
  114. // Take data
  115. TopicSample::Message st;
  116. static int ncount = 0;
  117. static int nmaxlatancy = 0;
  118. std::cout<<"new msg"<<std::endl;
  119. // char * strbuf = new char[1000000];
  120. // eprosima::fastcdr::FastBuffer pbuf(strbuf,1000000);
  121. // eprosima::fastcdr::Cdr * pxcdr;//
  122. // pxcdr = new eprosima::fastcdr::Cdr(pbuf);
  123. // if(sub->takeNextData(pxcdr, &m_info))
  124. // {
  125. // if(m_info.sampleKind == ALIVE)
  126. // {
  127. // // Print your structure data here.
  128. // ++n_msg;
  129. // std::cout << "Sample received, count=" << n_msg<<std::endl;
  130. // st.deserialize(*pxcdr);
  131. // std::cout<<" size is "<<TopicSample::Message::getCdrSerializedSize(st)<<std::endl;
  132. // }
  133. // }
  134. // return;
  135. // sub->get_first_untaken_info(&m_info);
  136. std::cout<<"count is "<<sub->getUnreadCount()<<std::endl;
  137. if(sub->takeNextData(&st, &m_info))
  138. {
  139. if(m_info.sampleKind == ALIVE)
  140. {
  141. // Print your structure data here.
  142. ++n_msg;
  143. ncount++;
  144. std::cout << "Sample received, count=" << st.counter() <<" total: "<<ncount<<std::endl;
  145. qint64 timex = QDateTime::currentMSecsSinceEpoch();
  146. int nlatancy = (timex - st.sendtime());
  147. if(nlatancy>nmaxlatancy)nmaxlatancy = nlatancy;
  148. std::cout<<" latency is "<<nlatancy<<" max: "<<nmaxlatancy<<std::endl;
  149. std::cout<<" size is "<<st.xdata().size()<<std::endl;
  150. QDateTime dt = QDateTime::fromMSecsSinceEpoch(st.sendtime());
  151. if(mbSetFun) mFun((char *)(st.xdata().data()),st.xdata().size(),st.counter(),&dt,st.msgname().data());
  152. }
  153. }
  154. }
  155. void TopicsSubscriber::setReceivedTopicFunction(ModuleFun xFun)
  156. {
  157. m_listener.setReceivedTopicFunction(xFun);
  158. }
  159. void TopicsSubscriber::run()
  160. {
  161. std::cout << "Waiting for Data, press Enter to stop the Subscriber. "<<std::endl;
  162. std::cin.ignore();
  163. std::cout << "Shutting down the Subscriber." << std::endl;
  164. }