TopicsSubscriber.cxx 6.3 KB

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