From 7de2e93a4fc00b8d53df7926dd6540dbc363394e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Poderoso?= <120394830+JesusPoderoso@users.noreply.github.com> Date: Wed, 27 Mar 2024 08:55:46 +0100 Subject: [PATCH] Create Participant with default profile (use environment XML configuration) (#4534) * Refs #20543: Default Domain Id value Signed-off-by: JesusPoderoso * Refs #20543: Rename method Signed-off-by: JesusPoderoso * Refs #20543: Apply rev suggestions Signed-off-by: JesusPoderoso * Refs #20543: Address missed suggestions Signed-off-by: JesusPoderoso * Refs #20543: Apply last(?) revision suggestion Signed-off-by: JesusPoderoso * Refs #20543: Apply Edu' suggestion Signed-off-by: JesusPoderoso * Refs #20543: Apply Edu' suggestion (2) Signed-off-by: JesusPoderoso * Refs #20543: Add missing test Signed-off-by: JesusPoderoso * Refs #20543: Fix test Signed-off-by: JesusPoderoso * Refs #20543: Apply rev suggestions Signed-off-by: JesusPoderoso * Refs #20543: Instanciate test listener in stack Signed-off-by: JesusPoderoso * Refs #20543: Include new feature in versions.md Signed-off-by: JesusPoderoso --------- Signed-off-by: JesusPoderoso --- .../dds/domain/DomainParticipantFactory.hpp | 21 +++++ .../domain/DomainParticipantFactory.cpp | 32 ++++++-- .../dds/participant/ParticipantTests.cpp | 78 +++++++++++++++++++ versions.md | 1 + 4 files changed, 125 insertions(+), 7 deletions(-) diff --git a/include/fastdds/dds/domain/DomainParticipantFactory.hpp b/include/fastdds/dds/domain/DomainParticipantFactory.hpp index cb13568e001..a222cca44b3 100644 --- a/include/fastdds/dds/domain/DomainParticipantFactory.hpp +++ b/include/fastdds/dds/domain/DomainParticipantFactory.hpp @@ -96,6 +96,25 @@ class DomainParticipantFactory DomainParticipantListener* listener = nullptr, const StatusMask& mask = StatusMask::all()); + /** + * Create a Participant with default domain id and qos. + * + * @return DomainParticipant pointer. (nullptr if not created.) + */ + FASTDDS_EXPORTED_API DomainParticipant* create_participant_with_default_profile(); + + + /** + * Create a Participant with default domain id and qos. + * + * @return DomainParticipant pointer. (nullptr if not created.) + * @param listener DomainParticipantListener Pointer + * @param mask StatusMask Reference + */ + FASTDDS_EXPORTED_API DomainParticipant* create_participant_with_default_profile( + DomainParticipantListener* listener, + const StatusMask& mask); + /** * Create a Participant. * @@ -342,6 +361,8 @@ class DomainParticipantFactory mutable bool default_xml_profiles_loaded; + DomainId_t default_domain_id_; + DomainParticipantFactoryQos factory_qos_; DomainParticipantQos default_participant_qos_; diff --git a/src/cpp/fastdds/domain/DomainParticipantFactory.cpp b/src/cpp/fastdds/domain/DomainParticipantFactory.cpp index de7c65fc57c..67e04a2e51b 100644 --- a/src/cpp/fastdds/domain/DomainParticipantFactory.cpp +++ b/src/cpp/fastdds/domain/DomainParticipantFactory.cpp @@ -54,6 +54,7 @@ namespace dds { DomainParticipantFactory::DomainParticipantFactory() : default_xml_profiles_loaded(false) + , default_domain_id_(0) , default_participant_qos_(PARTICIPANT_QOS_DEFAULT) , topic_pool_(fastrtps::rtps::TopicPayloadPoolRegistry::instance()) , rtps_domain_(fastrtps::rtps::RTPSDomainImpl::get_instance()) @@ -155,7 +156,7 @@ ReturnCode_t DomainParticipantFactory::delete_participant( DomainParticipant* DomainParticipantFactory::create_participant( DomainId_t did, const DomainParticipantQos& qos, - DomainParticipantListener* listen, + DomainParticipantListener* listener, const StatusMask& mask) { load_profiles(); @@ -164,10 +165,10 @@ DomainParticipant* DomainParticipantFactory::create_participant( DomainParticipant* dom_part = new DomainParticipant(mask); #ifndef FASTDDS_STATISTICS - DomainParticipantImpl* dom_part_impl = new DomainParticipantImpl(dom_part, did, pqos, listen); + DomainParticipantImpl* dom_part_impl = new DomainParticipantImpl(dom_part, did, pqos, listener); #else eprosima::fastdds::statistics::dds::DomainParticipantImpl* dom_part_impl = - new eprosima::fastdds::statistics::dds::DomainParticipantImpl(dom_part, did, pqos, listen); + new eprosima::fastdds::statistics::dds::DomainParticipantImpl(dom_part, did, pqos, listener); #endif // FASTDDS_STATISTICS if (fastrtps::rtps::GUID_t::unknown() != dom_part_impl->guid()) @@ -206,10 +207,23 @@ DomainParticipant* DomainParticipantFactory::create_participant( return dom_part; } +DomainParticipant* DomainParticipantFactory::create_participant_with_default_profile() +{ + return create_participant_with_default_profile(nullptr, StatusMask::none()); +} + +DomainParticipant* DomainParticipantFactory::create_participant_with_default_profile( + DomainParticipantListener* listener, + const StatusMask& mask) +{ + load_profiles(); + return create_participant(default_domain_id_, default_participant_qos_, listener, mask); +} + DomainParticipant* DomainParticipantFactory::create_participant_with_profile( DomainId_t did, const std::string& profile_name, - DomainParticipantListener* listen, + DomainParticipantListener* listener, const StatusMask& mask) { load_profiles(); @@ -220,7 +234,7 @@ DomainParticipant* DomainParticipantFactory::create_participant_with_profile( { DomainParticipantQos qos = default_participant_qos_; utils::set_qos_from_attributes(qos, attr.rtps); - return create_participant(did, qos, listen, mask); + return create_participant(did, qos, listener, mask); } return nullptr; @@ -228,7 +242,7 @@ DomainParticipant* DomainParticipantFactory::create_participant_with_profile( DomainParticipant* DomainParticipantFactory::create_participant_with_profile( const std::string& profile_name, - DomainParticipantListener* listen, + DomainParticipantListener* listener, const StatusMask& mask) { load_profiles(); @@ -239,7 +253,7 @@ DomainParticipant* DomainParticipantFactory::create_participant_with_profile( { DomainParticipantQos qos = default_participant_qos_; utils::set_qos_from_attributes(qos, attr.rtps); - return create_participant(attr.domainId, qos, listen, mask); + return create_participant(attr.domainId, qos, listener, mask); } return nullptr; @@ -349,6 +363,10 @@ ReturnCode_t DomainParticipantFactory::load_profiles() { reset_default_participant_qos(); } + // Take the default domain id from the default participant profile + eprosima::fastdds::ParticipantAttributes attr; + XMLProfileManager::getDefaultParticipantAttributes(attr); + default_domain_id_ = attr.domainId; RTPSDomain::set_filewatch_thread_config(factory_qos_.file_watch_threads(), factory_qos_.file_watch_threads()); } diff --git a/test/unittest/dds/participant/ParticipantTests.cpp b/test/unittest/dds/participant/ParticipantTests.cpp index f0b495d650c..445b28c491e 100644 --- a/test/unittest/dds/participant/ParticipantTests.cpp +++ b/test/unittest/dds/participant/ParticipantTests.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include #include @@ -64,6 +65,7 @@ #include #include +#include "../../common/env_var_utils.hpp" #include "../../logging/mock/MockConsumer.h" #if defined(__cplusplus_winrt) @@ -568,6 +570,82 @@ TEST(ParticipantTests, CreateDomainParticipantWithProfile) ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant(participant) == ReturnCode_t::RETCODE_OK); } +TEST(ParticipantTests, CreateDomainParticipantWithDefaultProfile) +{ + uint32_t domain_id = 123u; // This is the domain ID set in the default profile above + + // set XML profile as environment variable: "export FASTDDS_DEFAULT_PROFILES_FILE=test_xml_profile.xml" + eprosima::testing::set_environment_variable("FASTDDS_DEFAULT_PROFILES_FILE", "test_xml_profile.xml"); + + //participant using the given profile + DomainParticipant* default_env_participant = + DomainParticipantFactory::get_instance()->create_participant_with_default_profile(); + + // unset XML profile environment variable + eprosima::testing::clear_environment_variable("FASTDDS_DEFAULT_PROFILES_FILE"); + + ASSERT_NE(default_env_participant, nullptr); + ASSERT_EQ(default_env_participant->get_domain_id(), domain_id); + ASSERT_EQ(default_env_participant->get_listener(), nullptr); + ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant( + default_env_participant) == ReturnCode_t::RETCODE_OK); +} + +TEST(ParticipantTests, CreateDomainParticipantWithDefaultProfileListener) +{ + uint32_t domain_id = 123u; // This is the domain ID set in the default profile above + + // set XML profile as environment variable: "export FASTDDS_DEFAULT_PROFILES_FILE=test_xml_profile.xml" + eprosima::testing::set_environment_variable("FASTDDS_DEFAULT_PROFILES_FILE", "test_xml_profile.xml"); + + DomainParticipantListener listener; + + //participant using the given profile + DomainParticipant* default_env_participant = + DomainParticipantFactory::get_instance()->create_participant_with_default_profile(&listener, + StatusMask::none()); + + // unset XML profile environment variable + eprosima::testing::clear_environment_variable("FASTDDS_DEFAULT_PROFILES_FILE"); + + ASSERT_NE(default_env_participant, nullptr); + ASSERT_EQ(default_env_participant->get_domain_id(), domain_id); + ASSERT_EQ(default_env_participant->get_listener(), &listener); + ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant( + default_env_participant) == ReturnCode_t::RETCODE_OK); +} + +TEST(ParticipantTests, CreateDomainParticipantWithoutDefaultProfile) +{ + uint32_t default_domain_id = 0u; // This is the default domain ID + + //participant using default values + DomainParticipant* default_participant = + DomainParticipantFactory::get_instance()->create_participant_with_default_profile(); + ASSERT_NE(default_participant, nullptr); + ASSERT_EQ(default_participant->get_domain_id(), default_domain_id); + ASSERT_EQ(default_participant->get_listener(), nullptr); + ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant( + default_participant) == ReturnCode_t::RETCODE_OK); +} + +TEST(ParticipantTests, CreateDomainParticipantWithoutDefaultProfileListener) +{ + uint32_t default_domain_id = 0u; // This is the default domain ID + + DomainParticipantListener listener; + + //participant using default values + DomainParticipant* default_participant = + DomainParticipantFactory::get_instance()->create_participant_with_default_profile(&listener, + StatusMask::none()); + ASSERT_NE(default_participant, nullptr); + ASSERT_EQ(default_participant->get_domain_id(), default_domain_id); + ASSERT_EQ(default_participant->get_listener(), &listener); + ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant( + default_participant) == ReturnCode_t::RETCODE_OK); +} + TEST(ParticipantTests, GetParticipantProfileQos) { DomainParticipantFactory::get_instance()->load_XML_profiles_file("test_xml_profile.xml"); diff --git a/versions.md b/versions.md index 141fe89c3b7..9a2ba577c5f 100644 --- a/versions.md +++ b/versions.md @@ -23,6 +23,7 @@ Forthcoming * StringMatching * TimeConversion * DBQueue +* Added create participant methods that use environment XML profile for participant configuration. Version 2.14.0 --------------