From 78357ea70732b8ccf6995c46d71fb5be8afe3ec9 Mon Sep 17 00:00:00 2001 From: elianalf <62831776+elianalf@users.noreply.github.com> Date: Mon, 22 Jul 2024 09:18:17 +0200 Subject: [PATCH] Refs #21280: Apply suggestion Signed-off-by: elianalf <62831776+elianalf@users.noreply.github.com> --- examples/cpp/rtps_entities/Application.hpp | 2 +- examples/cpp/rtps_entities/CMakeLists.txt | 2 +- examples/cpp/rtps_entities/ReaderApp.cpp | 26 +++++------ examples/cpp/rtps_entities/ReaderApp.hpp | 5 +-- examples/cpp/rtps_entities/WriterApp.cpp | 51 +++++++++++++--------- examples/cpp/rtps_entities/WriterApp.hpp | 9 ++-- 6 files changed, 48 insertions(+), 47 deletions(-) diff --git a/examples/cpp/rtps_entities/Application.hpp b/examples/cpp/rtps_entities/Application.hpp index 9b44b807723..6de2d2f520c 100644 --- a/examples/cpp/rtps_entities/Application.hpp +++ b/examples/cpp/rtps_entities/Application.hpp @@ -51,4 +51,4 @@ class Application } // namespace fastdds } // namespace eprosima -#endif /* FASTDDS_EXAMPLES_CPP_RTPS_ENTITIES__APPLICATION_HPP */ +#endif // FASTDDS_EXAMPLES_CPP_RTPS_ENTITIES__APPLICATION_HPP diff --git a/examples/cpp/rtps_entities/CMakeLists.txt b/examples/cpp/rtps_entities/CMakeLists.txt index cf5c660465e..c0fa1cdc551 100644 --- a/examples/cpp/rtps_entities/CMakeLists.txt +++ b/examples/cpp/rtps_entities/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima). +# Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima). # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/examples/cpp/rtps_entities/ReaderApp.cpp b/examples/cpp/rtps_entities/ReaderApp.cpp index d2a364fd8e1..e00e9a77a50 100644 --- a/examples/cpp/rtps_entities/ReaderApp.cpp +++ b/examples/cpp/rtps_entities/ReaderApp.cpp @@ -112,20 +112,6 @@ ReaderApp::ReaderApp( throw std::runtime_error("RTPS Reader creation failed"); } - if (!register_entity(topic_name)) - { - throw std::runtime_error("Entity registration failed"); - } -} - -ReaderApp::~ReaderApp() -{ - RTPSDomain::removeRTPSParticipant(rtps_participant_); - delete(reader_history_); -} - -bool ReaderApp::register_entity(std::string topic_name) -{ std::cout << "Registering RTPS Reader" << std::endl; TopicAttributes topic_att; @@ -137,7 +123,17 @@ bool ReaderApp::register_entity(std::string topic_name) reader_qos.m_durability.kind = eprosima::fastdds::dds::TRANSIENT_LOCAL_DURABILITY_QOS; reader_qos.m_reliability.kind = eprosima::fastdds::dds::RELIABLE_RELIABILITY_QOS; - return rtps_participant_->registerReader(rtps_reader_, topic_att, reader_qos); + // Register entity + if (!rtps_participant_->registerReader(rtps_reader_, topic_att, reader_qos)) + { + throw std::runtime_error("Entity registration failed"); + } +} + +ReaderApp::~ReaderApp() +{ + RTPSDomain::removeRTPSParticipant(rtps_participant_); + delete(reader_history_); } void ReaderApp::on_reader_matched( diff --git a/examples/cpp/rtps_entities/ReaderApp.hpp b/examples/cpp/rtps_entities/ReaderApp.hpp index 763123aae0a..1d4f09782c3 100644 --- a/examples/cpp/rtps_entities/ReaderApp.hpp +++ b/examples/cpp/rtps_entities/ReaderApp.hpp @@ -67,9 +67,6 @@ class ReaderApp : public Application, public ReaderListener private: - //! Register entity - bool register_entity(std::string topic_name); - //! Method to deserialize the payload bool deserialize_payload( const SerializedPayload_t& payload, @@ -104,4 +101,4 @@ class ReaderApp : public Application, public ReaderListener } // namespace fastdds } // namespace eprosima -#endif /* FASTDDS_EXAMPLES_CPP_RTPS_ENTITIES__READERAPP_HPP */ +#endif // FASTDDS_EXAMPLES_CPP_RTPS_ENTITIES__READERAPP_HPP diff --git a/examples/cpp/rtps_entities/WriterApp.cpp b/examples/cpp/rtps_entities/WriterApp.cpp index 0968e51a7e9..045f936480a 100644 --- a/examples/cpp/rtps_entities/WriterApp.cpp +++ b/examples/cpp/rtps_entities/WriterApp.cpp @@ -104,20 +104,6 @@ WriterApp::WriterApp( throw std::runtime_error("RTPS Writer creation failed"); } - if (!register_entity(topic_name)) - { - throw std::runtime_error("Entity registration failed"); - } -} - -WriterApp::~WriterApp() -{ - RTPSDomain::removeRTPSParticipant(rtps_participant_); - delete(writer_history_); -} - -bool WriterApp::register_entity(std::string topic_name) -{ std::cout << "Registering RTPS Writer" << std::endl; TopicAttributes topic_att; @@ -129,7 +115,17 @@ bool WriterApp::register_entity(std::string topic_name) writer_qos.m_durability.kind = eprosima::fastdds::dds::TRANSIENT_LOCAL_DURABILITY_QOS; writer_qos.m_reliability.kind = eprosima::fastdds::dds::RELIABLE_RELIABILITY_QOS; - return rtps_participant_->registerWriter(rtps_writer_, topic_att, writer_qos); + // Register entity + if (!rtps_participant_->registerWriter(rtps_writer_, topic_att, writer_qos)) + { + throw std::runtime_error("Entity registration failed"); + } +} + +WriterApp::~WriterApp() +{ + RTPSDomain::removeRTPSParticipant(rtps_participant_); + delete(writer_history_); } void WriterApp::on_writer_matched( @@ -154,7 +150,10 @@ void WriterApp::run() { while (!is_stopped() && ((samples_ == 0) || (samples_sent_ < samples_))) { - add_change_to_history(); + if(add_change_to_history()) + { + std::cout << "Message " << data_->message() << " with index " << data_->index() << " SENT" << std::endl; + } // Wait for period or stop event std::unique_lock period_lock(terminate_cv_mtx_); @@ -205,7 +204,7 @@ bool WriterApp::serialize_payload( return true; } -void WriterApp::add_change_to_history() +bool WriterApp::add_change_to_history() { // Wait for the data endpoints discovery std::unique_lock matched_lock(terminate_cv_mtx_); @@ -215,6 +214,8 @@ void WriterApp::add_change_to_history() return ((matched_ > 0) || is_stopped()); }); + bool ret = false; + CacheChange_t* ch = writer_history_->create_change(255, ALIVE); // In the case history is full, remove some old changes @@ -230,12 +231,22 @@ void WriterApp::add_change_to_history() if (serialize_payload(data_, ch->serializedPayload)) { ++samples_sent_; - } - if(writer_history_->add_change(ch)) + if(writer_history_->add_change(ch)) + { + ret = true; + } + else + { + std::cout << "Fail to add the change to the history!" << std::endl; + } + } + else { - std::cout << "Message " << data_->message() << " with index " << data_->index() << " SENT" << std::endl; + std::cout << "Fail to serialize the payload!" << std::endl; } + + return ret; } bool WriterApp::is_stopped() diff --git a/examples/cpp/rtps_entities/WriterApp.hpp b/examples/cpp/rtps_entities/WriterApp.hpp index 1fd55306d26..41bb98d0f93 100644 --- a/examples/cpp/rtps_entities/WriterApp.hpp +++ b/examples/cpp/rtps_entities/WriterApp.hpp @@ -51,9 +51,6 @@ class WriterApp : public Application, public WriterListener //! Run RTPS Writer void run(); - //! Add a new change to Writer History - void add_change_to_history(); - //! Writer matched method void on_writer_matched( RTPSWriter*, @@ -64,8 +61,8 @@ class WriterApp : public Application, public WriterListener private: - //! Register entity - bool register_entity(std::string topic_name); + //! Add a new change to Writer History + bool add_change_to_history(); //! Return the current state of execution bool is_stopped(); @@ -105,4 +102,4 @@ class WriterApp : public Application, public WriterListener } // namespace fastdds } // namespace eprosima -#endif /* FASTDDS_EXAMPLES_CPP_RTPS_ENTITIES__WRITERAPP_HPP */ +#endif // FASTDDS_EXAMPLES_CPP_RTPS_ENTITIES__WRITERAPP_HPP