奥特QT DDS 插件库
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

124 行
2.8KB

  1. #include "hello_world_publisher.hpp"
  2. #include <iostream>
  3. #include <thread>
  4. HelloWorldPublisher::PubListener::PubListener()
  5. : matched_(0)
  6. {
  7. }
  8. HelloWorldPublisher::PubListener::~PubListener()
  9. {
  10. }
  11. void HelloWorldPublisher::PubListener::on_publication_matched(
  12. DataWriter*,
  13. const PublicationMatchedStatus& info)
  14. {
  15. if (info.current_count_change == 1)
  16. {
  17. matched_ = info.total_count;
  18. std::cout << "Publisher matched." << std::endl;
  19. }
  20. else if (info.current_count_change == -1)
  21. {
  22. matched_ = info.total_count;
  23. std::cout << "Publisher unmatched." << std::endl;
  24. }
  25. else
  26. {
  27. std::cout << info.current_count_change
  28. << " is not a valid value for PublicationMatchedStatus current count change." << std::endl;
  29. }
  30. }
  31. HelloWorldPublisher::HelloWorldPublisher()
  32. : participant_(nullptr)
  33. , publisher_(nullptr)
  34. , topic_(nullptr)
  35. , writer_(nullptr)
  36. , type_(new HelloWorldPubSubType())
  37. {
  38. }
  39. HelloWorldPublisher::~HelloWorldPublisher()
  40. {
  41. if (writer_ != nullptr)
  42. {
  43. publisher_->delete_datawriter(writer_);
  44. }
  45. if (publisher_ != nullptr)
  46. {
  47. participant_->delete_publisher(publisher_);
  48. }
  49. if (topic_ != nullptr)
  50. {
  51. participant_->delete_topic(topic_);
  52. }
  53. DomainParticipantFactory::get_instance()->delete_participant(participant_);
  54. }
  55. bool HelloWorldPublisher::init()
  56. {
  57. hello_.index(0);
  58. hello_.message("HelloWorld");
  59. DomainParticipantQos participantQos;
  60. participantQos.name("Participant_publisher");
  61. participant_ = DomainParticipantFactory::get_instance()->create_participant(0, participantQos);
  62. if (participant_ == nullptr)
  63. {
  64. return false;
  65. }
  66. // Register the Type
  67. type_.register_type(participant_);
  68. // Create the publications Topic
  69. topic_ = participant_->create_topic("HelloWorldTopic", "HelloWorld", TOPIC_QOS_DEFAULT);
  70. if (topic_ == nullptr)
  71. {
  72. return false;
  73. }
  74. // Create the Publisher
  75. publisher_ = participant_->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);
  76. if (publisher_ == nullptr)
  77. {
  78. return false;
  79. }
  80. // Create the DataWriter
  81. writer_ = publisher_->create_datawriter(topic_, DATAWRITER_QOS_DEFAULT, &listener_);
  82. if (writer_ == nullptr)
  83. {
  84. return false;
  85. }
  86. return true;
  87. }
  88. bool HelloWorldPublisher::publish()
  89. {
  90. if (listener_.matched_ > 0)
  91. {
  92. hello_.index(hello_.index() + 1);
  93. writer_->write(&hello_);
  94. return true;
  95. }
  96. return false;
  97. }
  98. std::string HelloWorldPublisher::getCurrentMessage() const
  99. {
  100. return hello_.message();
  101. }
  102. int HelloWorldPublisher::getCurrentIndex() const
  103. {
  104. return hello_.index();
  105. }