diff --git a/src/cpp/rtps/builtin/liveliness/WLP.cpp b/src/cpp/rtps/builtin/liveliness/WLP.cpp index 969bf950bf6..f9b27a13627 100644 --- a/src/cpp/rtps/builtin/liveliness/WLP.cpp +++ b/src/cpp/rtps/builtin/liveliness/WLP.cpp @@ -711,6 +711,8 @@ bool WLP::remove_local_writer( EPROSIMA_LOG_INFO(RTPS_LIVELINESS, W->getGuid().entityId << " from Liveliness Protocol"); + LivelinessData::WriterStatus writer_status; + if (W->get_liveliness_kind() == AUTOMATIC_LIVELINESS_QOS) { auto it = std::find( @@ -764,7 +766,8 @@ bool WLP::remove_local_writer( if (!pub_liveliness_manager_->remove_writer( W->getGuid(), W->get_liveliness_kind(), - W->get_liveliness_lease_duration())) + W->get_liveliness_lease_duration(), + writer_status)) { EPROSIMA_LOG_ERROR(RTPS_LIVELINESS, "Could not remove writer " << W->getGuid() << " from liveliness manager"); @@ -808,7 +811,8 @@ bool WLP::remove_local_writer( if (!pub_liveliness_manager_->remove_writer( W->getGuid(), W->get_liveliness_kind(), - W->get_liveliness_lease_duration())) + W->get_liveliness_lease_duration(), + writer_status)) { EPROSIMA_LOG_ERROR(RTPS_LIVELINESS, "Could not remove writer " << W->getGuid() << " from liveliness manager"); diff --git a/src/cpp/rtps/reader/StatefulReader.cpp b/src/cpp/rtps/reader/StatefulReader.cpp index a4f7c8301d5..538cfde98ac 100644 --- a/src/cpp/rtps/reader/StatefulReader.cpp +++ b/src/cpp/rtps/reader/StatefulReader.cpp @@ -382,10 +382,22 @@ bool StatefulReader::matched_writer_remove( auto wlp = this->mp_RTPSParticipant->wlp(); if ( wlp != nullptr) { + LivelinessData::WriterStatus writer_liveliness_status; wlp->sub_liveliness_manager_->remove_writer( writer_guid, liveliness_kind_, - liveliness_lease_duration_); + liveliness_lease_duration_, + writer_liveliness_status); + + if (writer_liveliness_status == LivelinessData::WriterStatus::ALIVE) + { + wlp->update_liveliness_changed_status(writer_guid, this, -1, 0); + } + else if (writer_liveliness_status == LivelinessData::WriterStatus::NOT_ALIVE) + { + wlp->update_liveliness_changed_status(writer_guid, this, 0, -1); + } + } else { diff --git a/src/cpp/rtps/reader/StatelessReader.cpp b/src/cpp/rtps/reader/StatelessReader.cpp index 2b1c6b91d17..66afd9bfabc 100644 --- a/src/cpp/rtps/reader/StatelessReader.cpp +++ b/src/cpp/rtps/reader/StatelessReader.cpp @@ -232,10 +232,21 @@ bool StatelessReader::matched_writer_remove( auto wlp = mp_RTPSParticipant->wlp(); if ( wlp != nullptr) { + LivelinessData::WriterStatus writer_liveliness_status; wlp->sub_liveliness_manager_->remove_writer( writer_guid, liveliness_kind_, - liveliness_lease_duration_); + liveliness_lease_duration_, + writer_liveliness_status); + + if (writer_liveliness_status == LivelinessData::WriterStatus::ALIVE) + { + wlp->update_liveliness_changed_status(writer_guid, this, -1, 0); + } + else if (writer_liveliness_status == LivelinessData::WriterStatus::NOT_ALIVE) + { + wlp->update_liveliness_changed_status(writer_guid, this, 0, -1); + } } else { diff --git a/src/cpp/rtps/writer/LivelinessManager.cpp b/src/cpp/rtps/writer/LivelinessManager.cpp index 357826bafb9..8c21676dbeb 100644 --- a/src/cpp/rtps/writer/LivelinessManager.cpp +++ b/src/cpp/rtps/writer/LivelinessManager.cpp @@ -107,10 +107,10 @@ bool LivelinessManager::add_writer( bool LivelinessManager::remove_writer( GUID_t guid, fastdds::dds::LivelinessQosPolicyKind kind, - Duration_t lease_duration) + Duration_t lease_duration, + LivelinessData::WriterStatus& writer_status) { bool removed = false; - LivelinessData::WriterStatus status; { // collection guard @@ -118,9 +118,9 @@ bool LivelinessManager::remove_writer( // writers_ elements guard std::lock_guard __(mutex_); - removed = writers_.remove_if([guid, kind, lease_duration, &status](LivelinessData& writer) + removed = writers_.remove_if([guid, kind, lease_duration, &writer_status](LivelinessData& writer) { - status = writer.status; + writer_status = writer.status; return writer.guid == guid && writer.kind == kind && writer.lease_duration == lease_duration && @@ -133,18 +133,6 @@ bool LivelinessManager::remove_writer( return false; } - if (callback_ != nullptr) - { - if (status == LivelinessData::WriterStatus::ALIVE) - { - callback_(guid, kind, lease_duration, -1, 0); - } - else if (status == LivelinessData::WriterStatus::NOT_ALIVE) - { - callback_(guid, kind, lease_duration, 0, -1); - } - } - std::unique_lock lock(mutex_); if (timer_owner_ != nullptr) diff --git a/src/cpp/rtps/writer/LivelinessManager.hpp b/src/cpp/rtps/writer/LivelinessManager.hpp index 4a37c5599b4..fb8561c9891 100644 --- a/src/cpp/rtps/writer/LivelinessManager.hpp +++ b/src/cpp/rtps/writer/LivelinessManager.hpp @@ -82,15 +82,17 @@ class LivelinessManager /** * @brief Removes a writer - * @param guid GUID of the writer - * @param kind Liveliness kind - * @param lease_duration Liveliness lease duration + * @param [in] guid GUID of the writer + * @param [in] kind Liveliness kind + * @param [in] lease_duration Liveliness lease duration + * @param [in,out] writer_liveliness_status The liveliness status of the writer * @return True if the writer was successfully removed */ bool remove_writer( GUID_t guid, fastdds::dds::LivelinessQosPolicyKind kind, - Duration_t lease_duration); + Duration_t lease_duration, + LivelinessData::WriterStatus& writer_liveliness_status); /** * @brief Asserts liveliness of a writer in the set diff --git a/test/unittest/rtps/writer/LivelinessManagerTests.cpp b/test/unittest/rtps/writer/LivelinessManagerTests.cpp index 442ed485e5c..c484e694c0c 100644 --- a/test/unittest/rtps/writer/LivelinessManagerTests.cpp +++ b/test/unittest/rtps/writer/LivelinessManagerTests.cpp @@ -154,22 +154,27 @@ TEST_F(LivelinessManagerTests, WriterCannotBeRemovedTwice) GuidPrefix_t guidP; guidP.value[0] = 1; GUID_t guid(guidP, 0); + LivelinessData::WriterStatus writer_status; EXPECT_EQ(liveliness_manager.add_writer(guid, fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t(1)), true); - EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t(1)), true); - EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t(1)), false); + EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t(1), + writer_status), true); + EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t(1), + writer_status), false); EXPECT_EQ(liveliness_manager.add_writer(guid, fastdds::dds::MANUAL_BY_PARTICIPANT_LIVELINESS_QOS, Duration_t( 1)), true); EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::MANUAL_BY_PARTICIPANT_LIVELINESS_QOS, Duration_t( - 1)), true); + 1), writer_status), true); EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::MANUAL_BY_PARTICIPANT_LIVELINESS_QOS, Duration_t( - 1)), false); + 1), writer_status), false); EXPECT_EQ(liveliness_manager.add_writer(guid, fastdds::dds::MANUAL_BY_TOPIC_LIVELINESS_QOS, Duration_t(1)), true); - EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::MANUAL_BY_TOPIC_LIVELINESS_QOS, Duration_t(1)), + EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::MANUAL_BY_TOPIC_LIVELINESS_QOS, Duration_t(1), + writer_status), true); - EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::MANUAL_BY_TOPIC_LIVELINESS_QOS, Duration_t(1)), + EXPECT_EQ(liveliness_manager.remove_writer(guid, fastdds::dds::MANUAL_BY_TOPIC_LIVELINESS_QOS, Duration_t(1), + writer_status), false); } @@ -494,12 +499,14 @@ TEST_F(LivelinessManagerTests, TimerOwnerRemoved) GuidPrefix_t guidP; guidP.value[0] = 1; + LivelinessData::WriterStatus writer_status; liveliness_manager.add_writer(GUID_t(guidP, 1), fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t(0.5)); liveliness_manager.add_writer(GUID_t(guidP, 2), fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t(1)); liveliness_manager.assert_liveliness(GUID_t(guidP, 1), fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t(0.5)); - liveliness_manager.remove_writer(GUID_t(guidP, 1), fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t(0.5)); + liveliness_manager.remove_writer(GUID_t(guidP, 1), fastdds::dds::AUTOMATIC_LIVELINESS_QOS, Duration_t( + 0.5), writer_status); wait_liveliness_lost(1u); EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 2));