TopicsPublisher.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 TopicsPublisher.cpp
  16. * This file contains the implementation of the publisher 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/publisher/Publisher.h>
  24. #include <fastrtps/attributes/PublisherAttributes.h>
  25. #include <fastrtps/Domain.h>
  26. #include <fastrtps/participant/Participant.h>
  27. #include <fastrtps/attributes/ParticipantAttributes.h>
  28. #include <fastrtps/attributes/PublisherAttributes.h>
  29. #include <fastrtps/publisher/Publisher.h>
  30. #include <fastrtps/Domain.h>
  31. #include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
  32. #include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
  33. #include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
  34. #include <thread>
  35. #include <chrono>
  36. #include "TopicsPublisher.h"
  37. using namespace eprosima::fastrtps;
  38. using namespace eprosima::fastrtps::rtps;
  39. using namespace eprosima::fastdds::rtps;
  40. TopicsPublisher::TopicsPublisher() : mp_participant(nullptr), mp_publisher(nullptr) {}
  41. TopicsPublisher::~TopicsPublisher() { Domain::removeParticipant(mp_participant);}
  42. bool TopicsPublisher::init(const char * strtopic,const unsigned short nListenPort,const int ntype )
  43. {
  44. strncpy(mstrtopic,strtopic,255);
  45. // Create RTPSParticipant
  46. ParticipantAttributes PParam;
  47. PParam.rtps.sendSocketBufferSize = 100000000;
  48. PParam.rtps.setName("Participant_publisher"); //You can put here 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 shm_transport = std::make_shared<SharedMemTransportDescriptor>();
  59. shm_transport->segment_size(100*1024*1024);
  60. PParam.rtps.userTransports.push_back(shm_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. //Create a descriptor for the new transport.
  69. auto tcp_transport = std::make_shared<TCPv4TransportDescriptor>();
  70. tcp_transport->add_listener_port(nListenPort);
  71. tcp_transport->set_WAN_address("0.0.0.0");
  72. tcp_transport->wait_for_tcp_negotiation = false;
  73. tcp_transport->sendBufferSize = 0;
  74. tcp_transport->receiveBufferSize = 0;
  75. PParam.rtps.userTransports.push_back(tcp_transport);
  76. }
  77. mp_participant = Domain::createParticipant(PParam);
  78. if(mp_participant == nullptr)
  79. {
  80. return false;
  81. }
  82. //Register the type
  83. Domain::registerType(mp_participant, static_cast<TopicDataType*>(&myType));
  84. // Create Publisher
  85. PublisherAttributes Wparam;
  86. Wparam.topic.topicKind = NO_KEY;
  87. Wparam.topic.topicDataType = myType.getName(); //This type MUST be registered
  88. Wparam.topic.topicName = strtopic;//"TopicsPubSubTopic";
  89. Wparam.topic.historyQos.depth = 30;
  90. Wparam.topic.resourceLimitsQos.max_samples = 50;
  91. Wparam.topic.resourceLimitsQos.allocated_samples = 20;
  92. Wparam.times.heartbeatPeriod.seconds = 2;
  93. Wparam.times.heartbeatPeriod.nanosec = 200 * 1000 * 1000;
  94. Wparam.qos.m_reliability.kind = RELIABLE_RELIABILITY_QOS;
  95. Wparam.qos.m_publishMode.kind = ASYNCHRONOUS_PUBLISH_MODE;
  96. mp_publisher = Domain::createPublisher(mp_participant,Wparam,static_cast<PublisherListener*>(&m_listener));
  97. if(mp_publisher == nullptr)
  98. {
  99. return false;
  100. }
  101. std::cout << "Publisher created, waiting for Subscribers." << std::endl;
  102. return true;
  103. }
  104. void TopicsPublisher::PubListener::onPublicationMatched(Publisher* pub,MatchingInfo& info)
  105. {
  106. (void)pub;
  107. if (info.status == MATCHED_MATCHING)
  108. {
  109. n_matched++;
  110. std::cout << "Publisher matched" << std::endl;
  111. }
  112. else
  113. {
  114. n_matched--;
  115. std::cout << "Publisher unmatched" << std::endl;
  116. }
  117. }
  118. void TopicsPublisher::run()
  119. {
  120. while(m_listener.n_matched == 0)
  121. {
  122. std::this_thread::sleep_for(std::chrono::milliseconds(250)); // Sleep 250 ms
  123. }
  124. // Publication code
  125. TopicSample::Message st;
  126. /* Initialize your structure here */
  127. int msgsent = 0;
  128. char ch = 'y';
  129. do
  130. {
  131. if(ch == 'y')
  132. {
  133. mp_publisher->write(&st); ++msgsent;
  134. std::cout << "Sending sample, count=" << msgsent << ", send another sample?(y-yes,n-stop): ";
  135. }
  136. else if(ch == 'n')
  137. {
  138. std::cout << "Stopping execution " << std::endl;
  139. break;
  140. }
  141. else
  142. {
  143. std::cout << "Command " << ch << " not recognized, please enter \"y/n\":";
  144. }
  145. } while(std::cin >> ch);
  146. }
  147. #include <QDateTime>
  148. void TopicsPublisher::senddata(const char *str, int nsize)
  149. {
  150. static int ncount = 1;
  151. std::cout<<"send data."<<std::endl;
  152. // while(m_listener.n_matched == 0)
  153. TopicSample::SomeBytes x;
  154. x.resize(nsize);
  155. memcpy(x.data(),str,nsize);
  156. TopicSample::Message st;
  157. st.msgname(mstrtopic);
  158. // st.msgname("topictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopictopic");
  159. st.counter(ncount);ncount++;
  160. st.sendtime(QDateTime::currentMSecsSinceEpoch());
  161. TopicSample::SomeBytes & px = x;
  162. st.xdata(x);
  163. int ndatasize = TopicSample::Message::getCdrSerializedSize(st);
  164. std::cout<<"size is "<<ndatasize<<std::endl;
  165. mp_publisher->write(&st);
  166. }
  167. #endif