diff --git a/src/cpp/fastdds/domain/DomainParticipantImpl.cpp b/src/cpp/fastdds/domain/DomainParticipantImpl.cpp index a457b20e595..d0234bf41c6 100644 --- a/src/cpp/fastdds/domain/DomainParticipantImpl.cpp +++ b/src/cpp/fastdds/domain/DomainParticipantImpl.cpp @@ -127,7 +127,10 @@ DomainParticipantImpl::DomainParticipantImpl( // Pre calculate participant id and generated guid participant_id_ = qos_.wire_protocol().participant_id; - eprosima::fastrtps::rtps::RTPSDomainImpl::create_participant_guid(participant_id_, guid_); + if (!eprosima::fastrtps::rtps::RTPSDomainImpl::create_participant_guid(participant_id_, guid_)) + { + EPROSIMA_LOG_ERROR(DOMAIN_PARTICIPANT, "Error generating GUID for participant"); + } /* Fill physical data properties if they are found and empty */ std::string* property_value = fastrtps::rtps::PropertyPolicyHelper::find_property( @@ -255,6 +258,12 @@ ReturnCode_t DomainParticipantImpl::enable() // Should not have been previously enabled assert(get_rtps_participant() == nullptr); + // Preconditions + if (guid_ == GUID_t::unknown()) + { + return ReturnCode_t::RETCODE_PRECONDITION_NOT_MET; + } + fastrtps::rtps::RTPSParticipantAttributes rtps_attr; utils::set_attributes_from_qos(rtps_attr, qos_); rtps_attr.participantID = participant_id_; diff --git a/src/cpp/rtps/RTPSDomain.cpp b/src/cpp/rtps/RTPSDomain.cpp index 17e80faa76e..4b5565b2783 100644 --- a/src/cpp/rtps/RTPSDomain.cpp +++ b/src/cpp/rtps/RTPSDomain.cpp @@ -636,19 +636,40 @@ uint32_t RTPSDomainImpl::get_id_for_prefix( return ret; } -void RTPSDomainImpl::create_participant_guid( - int32_t& participant_id, - GUID_t& guid) +bool RTPSDomainImpl::reserve_participant_id( + int32_t& participant_id) { + auto instance = get_instance(); + std::lock_guard guard(instance->m_mutex); if (participant_id < 0) { - auto instance = get_instance(); - std::lock_guard guard(instance->m_mutex); participant_id = instance->getNewId(); } + else + { + if (instance->m_RTPSParticipantIDs[participant_id].reserved == true) + { + return false; + } + instance->m_RTPSParticipantIDs[participant_id].reserved = true; + } + + return true; +} + +bool RTPSDomainImpl::create_participant_guid( + int32_t& participant_id, + GUID_t& guid) +{ + bool ret_value = reserve_participant_id(participant_id); + + if (ret_value) + { + guid_prefix_create(participant_id, guid.guidPrefix); + guid.entityId = c_EntityId_RTPSParticipant; + } - guid_prefix_create(participant_id, guid.guidPrefix); - guid.entityId = c_EntityId_RTPSParticipant; + return ret_value; } RTPSParticipantImpl* RTPSDomainImpl::find_local_participant( diff --git a/src/cpp/rtps/RTPSDomainImpl.hpp b/src/cpp/rtps/RTPSDomainImpl.hpp index fff743ec2e3..0fc9ff0d763 100644 --- a/src/cpp/rtps/RTPSDomainImpl.hpp +++ b/src/cpp/rtps/RTPSDomainImpl.hpp @@ -149,8 +149,10 @@ class RTPSDomainImpl * @param [in, out] participant_id Participant identifier for which to generate the GUID. * When negative, it will be modified to the first non-existent participant id. * @param [out] guid GUID corresponding to participant_id + * + * @return True value if guid was created. False in other case. */ - static void create_participant_guid( + static bool create_participant_guid( int32_t& participant_id, GUID_t& guid); @@ -216,6 +218,16 @@ class RTPSDomainImpl int32_t input_id, uint32_t& participant_id); + /** + * Reserves a participant id. + * @param [in, out] participant_id Participant identifier for reservation. + * When negative, it will be modified to the first non-existent participant id. + * + * @return True value if reservation was possible. False in other case. + */ + static bool reserve_participant_id( + int32_t& participant_id); + uint32_t get_id_for_prefix( uint32_t participant_id); diff --git a/test/mock/rtps/RTPSDomainImpl/rtps/RTPSDomainImpl.hpp b/test/mock/rtps/RTPSDomainImpl/rtps/RTPSDomainImpl.hpp index 55ee66304a8..75a47432abc 100644 --- a/test/mock/rtps/RTPSDomainImpl/rtps/RTPSDomainImpl.hpp +++ b/test/mock/rtps/RTPSDomainImpl/rtps/RTPSDomainImpl.hpp @@ -61,10 +61,11 @@ class RTPSDomainImpl return nullptr; } - static void create_participant_guid( + static bool create_participant_guid( int32_t& /*participant_id*/, GUID_t& /*guid*/) { + return true; } /**