TopicsPublisher.cxx 6.7 KB

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