Skip to content

Commit

Permalink
Refs #21280: Apply suggestion
Browse files Browse the repository at this point in the history
Signed-off-by: elianalf <62831776+elianalf@users.noreply.github.com>
  • Loading branch information
elianalf committed Jul 22, 2024
1 parent bc4c229 commit 78357ea
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/cpp/rtps_entities/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion examples/cpp/rtps_entities/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
26 changes: 11 additions & 15 deletions examples/cpp/rtps_entities/ReaderApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down
5 changes: 1 addition & 4 deletions examples/cpp/rtps_entities/ReaderApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
51 changes: 31 additions & 20 deletions examples/cpp/rtps_entities/WriterApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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<std::mutex> period_lock(terminate_cv_mtx_);
Expand Down Expand Up @@ -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<std::mutex> matched_lock(terminate_cv_mtx_);
Expand All @@ -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
Expand All @@ -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()
Expand Down
9 changes: 3 additions & 6 deletions examples/cpp/rtps_entities/WriterApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*,
Expand All @@ -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();
Expand Down Expand Up @@ -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

0 comments on commit 78357ea

Please sign in to comment.