Skip to content

Commit b43aa6d

Browse files
Create Participant with default profile (use environment XML configuration) (#4534)
* Refs #20543: Default Domain Id value Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Rename method Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Apply rev suggestions Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Address missed suggestions Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Apply last(?) revision suggestion Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Apply Edu' suggestion Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Apply Edu' suggestion (2) Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Add missing test Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Fix test Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Apply rev suggestions Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Instanciate test listener in stack Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> * Refs #20543: Include new feature in versions.md Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com> --------- Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com>
1 parent 59130b7 commit b43aa6d

4 files changed

Lines changed: 125 additions & 7 deletions

File tree

include/fastdds/dds/domain/DomainParticipantFactory.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ class DomainParticipantFactory
9696
DomainParticipantListener* listener = nullptr,
9797
const StatusMask& mask = StatusMask::all());
9898

99+
/**
100+
* Create a Participant with default domain id and qos.
101+
*
102+
* @return DomainParticipant pointer. (nullptr if not created.)
103+
*/
104+
FASTDDS_EXPORTED_API DomainParticipant* create_participant_with_default_profile();
105+
106+
107+
/**
108+
* Create a Participant with default domain id and qos.
109+
*
110+
* @return DomainParticipant pointer. (nullptr if not created.)
111+
* @param listener DomainParticipantListener Pointer
112+
* @param mask StatusMask Reference
113+
*/
114+
FASTDDS_EXPORTED_API DomainParticipant* create_participant_with_default_profile(
115+
DomainParticipantListener* listener,
116+
const StatusMask& mask);
117+
99118
/**
100119
* Create a Participant.
101120
*
@@ -342,6 +361,8 @@ class DomainParticipantFactory
342361

343362
mutable bool default_xml_profiles_loaded;
344363

364+
DomainId_t default_domain_id_;
365+
345366
DomainParticipantFactoryQos factory_qos_;
346367

347368
DomainParticipantQos default_participant_qos_;

src/cpp/fastdds/domain/DomainParticipantFactory.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace dds {
5454

5555
DomainParticipantFactory::DomainParticipantFactory()
5656
: default_xml_profiles_loaded(false)
57+
, default_domain_id_(0)
5758
, default_participant_qos_(PARTICIPANT_QOS_DEFAULT)
5859
, topic_pool_(fastrtps::rtps::TopicPayloadPoolRegistry::instance())
5960
, rtps_domain_(fastrtps::rtps::RTPSDomainImpl::get_instance())
@@ -155,7 +156,7 @@ ReturnCode_t DomainParticipantFactory::delete_participant(
155156
DomainParticipant* DomainParticipantFactory::create_participant(
156157
DomainId_t did,
157158
const DomainParticipantQos& qos,
158-
DomainParticipantListener* listen,
159+
DomainParticipantListener* listener,
159160
const StatusMask& mask)
160161
{
161162
load_profiles();
@@ -164,10 +165,10 @@ DomainParticipant* DomainParticipantFactory::create_participant(
164165

165166
DomainParticipant* dom_part = new DomainParticipant(mask);
166167
#ifndef FASTDDS_STATISTICS
167-
DomainParticipantImpl* dom_part_impl = new DomainParticipantImpl(dom_part, did, pqos, listen);
168+
DomainParticipantImpl* dom_part_impl = new DomainParticipantImpl(dom_part, did, pqos, listener);
168169
#else
169170
eprosima::fastdds::statistics::dds::DomainParticipantImpl* dom_part_impl =
170-
new eprosima::fastdds::statistics::dds::DomainParticipantImpl(dom_part, did, pqos, listen);
171+
new eprosima::fastdds::statistics::dds::DomainParticipantImpl(dom_part, did, pqos, listener);
171172
#endif // FASTDDS_STATISTICS
172173

173174
if (fastrtps::rtps::GUID_t::unknown() != dom_part_impl->guid())
@@ -206,10 +207,23 @@ DomainParticipant* DomainParticipantFactory::create_participant(
206207
return dom_part;
207208
}
208209

210+
DomainParticipant* DomainParticipantFactory::create_participant_with_default_profile()
211+
{
212+
return create_participant_with_default_profile(nullptr, StatusMask::none());
213+
}
214+
215+
DomainParticipant* DomainParticipantFactory::create_participant_with_default_profile(
216+
DomainParticipantListener* listener,
217+
const StatusMask& mask)
218+
{
219+
load_profiles();
220+
return create_participant(default_domain_id_, default_participant_qos_, listener, mask);
221+
}
222+
209223
DomainParticipant* DomainParticipantFactory::create_participant_with_profile(
210224
DomainId_t did,
211225
const std::string& profile_name,
212-
DomainParticipantListener* listen,
226+
DomainParticipantListener* listener,
213227
const StatusMask& mask)
214228
{
215229
load_profiles();
@@ -220,15 +234,15 @@ DomainParticipant* DomainParticipantFactory::create_participant_with_profile(
220234
{
221235
DomainParticipantQos qos = default_participant_qos_;
222236
utils::set_qos_from_attributes(qos, attr.rtps);
223-
return create_participant(did, qos, listen, mask);
237+
return create_participant(did, qos, listener, mask);
224238
}
225239

226240
return nullptr;
227241
}
228242

229243
DomainParticipant* DomainParticipantFactory::create_participant_with_profile(
230244
const std::string& profile_name,
231-
DomainParticipantListener* listen,
245+
DomainParticipantListener* listener,
232246
const StatusMask& mask)
233247
{
234248
load_profiles();
@@ -239,7 +253,7 @@ DomainParticipant* DomainParticipantFactory::create_participant_with_profile(
239253
{
240254
DomainParticipantQos qos = default_participant_qos_;
241255
utils::set_qos_from_attributes(qos, attr.rtps);
242-
return create_participant(attr.domainId, qos, listen, mask);
256+
return create_participant(attr.domainId, qos, listener, mask);
243257
}
244258

245259
return nullptr;
@@ -349,6 +363,10 @@ ReturnCode_t DomainParticipantFactory::load_profiles()
349363
{
350364
reset_default_participant_qos();
351365
}
366+
// Take the default domain id from the default participant profile
367+
eprosima::fastrtps::ParticipantAttributes attr;
368+
XMLProfileManager::getDefaultParticipantAttributes(attr);
369+
default_domain_id_ = attr.domainId;
352370

353371
RTPSDomain::set_filewatch_thread_config(factory_qos_.file_watch_threads(), factory_qos_.file_watch_threads());
354372
}

test/unittest/dds/participant/ParticipantTests.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <chrono>
16+
#include <cstdlib>
1617
#include <fstream>
1718
#include <future>
1819
#include <memory>
@@ -64,6 +65,7 @@
6465
#include <xmlparser/attributes/PublisherAttributes.hpp>
6566
#include <xmlparser/attributes/SubscriberAttributes.hpp>
6667

68+
#include "../../common/env_var_utils.hpp"
6769
#include "../../logging/mock/MockConsumer.h"
6870

6971
#if defined(__cplusplus_winrt)
@@ -568,6 +570,82 @@ TEST(ParticipantTests, CreateDomainParticipantWithProfile)
568570
ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant(participant) == ReturnCode_t::RETCODE_OK);
569571
}
570572

573+
TEST(ParticipantTests, CreateDomainParticipantWithDefaultProfile)
574+
{
575+
uint32_t domain_id = 123u; // This is the domain ID set in the default profile above
576+
577+
// set XML profile as environment variable: "export FASTDDS_DEFAULT_PROFILES_FILE=test_xml_profile.xml"
578+
eprosima::testing::set_environment_variable("FASTDDS_DEFAULT_PROFILES_FILE", "test_xml_profile.xml");
579+
580+
//participant using the given profile
581+
DomainParticipant* default_env_participant =
582+
DomainParticipantFactory::get_instance()->create_participant_with_default_profile();
583+
584+
// unset XML profile environment variable
585+
eprosima::testing::clear_environment_variable("FASTDDS_DEFAULT_PROFILES_FILE");
586+
587+
ASSERT_NE(default_env_participant, nullptr);
588+
ASSERT_EQ(default_env_participant->get_domain_id(), domain_id);
589+
ASSERT_EQ(default_env_participant->get_listener(), nullptr);
590+
ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant(
591+
default_env_participant) == ReturnCode_t::RETCODE_OK);
592+
}
593+
594+
TEST(ParticipantTests, CreateDomainParticipantWithDefaultProfileListener)
595+
{
596+
uint32_t domain_id = 123u; // This is the domain ID set in the default profile above
597+
598+
// set XML profile as environment variable: "export FASTDDS_DEFAULT_PROFILES_FILE=test_xml_profile.xml"
599+
eprosima::testing::set_environment_variable("FASTDDS_DEFAULT_PROFILES_FILE", "test_xml_profile.xml");
600+
601+
DomainParticipantListener listener;
602+
603+
//participant using the given profile
604+
DomainParticipant* default_env_participant =
605+
DomainParticipantFactory::get_instance()->create_participant_with_default_profile(&listener,
606+
StatusMask::none());
607+
608+
// unset XML profile environment variable
609+
eprosima::testing::clear_environment_variable("FASTDDS_DEFAULT_PROFILES_FILE");
610+
611+
ASSERT_NE(default_env_participant, nullptr);
612+
ASSERT_EQ(default_env_participant->get_domain_id(), domain_id);
613+
ASSERT_EQ(default_env_participant->get_listener(), &listener);
614+
ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant(
615+
default_env_participant) == ReturnCode_t::RETCODE_OK);
616+
}
617+
618+
TEST(ParticipantTests, CreateDomainParticipantWithoutDefaultProfile)
619+
{
620+
uint32_t default_domain_id = 0u; // This is the default domain ID
621+
622+
//participant using default values
623+
DomainParticipant* default_participant =
624+
DomainParticipantFactory::get_instance()->create_participant_with_default_profile();
625+
ASSERT_NE(default_participant, nullptr);
626+
ASSERT_EQ(default_participant->get_domain_id(), default_domain_id);
627+
ASSERT_EQ(default_participant->get_listener(), nullptr);
628+
ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant(
629+
default_participant) == ReturnCode_t::RETCODE_OK);
630+
}
631+
632+
TEST(ParticipantTests, CreateDomainParticipantWithoutDefaultProfileListener)
633+
{
634+
uint32_t default_domain_id = 0u; // This is the default domain ID
635+
636+
DomainParticipantListener listener;
637+
638+
//participant using default values
639+
DomainParticipant* default_participant =
640+
DomainParticipantFactory::get_instance()->create_participant_with_default_profile(&listener,
641+
StatusMask::none());
642+
ASSERT_NE(default_participant, nullptr);
643+
ASSERT_EQ(default_participant->get_domain_id(), default_domain_id);
644+
ASSERT_EQ(default_participant->get_listener(), &listener);
645+
ASSERT_TRUE(DomainParticipantFactory::get_instance()->delete_participant(
646+
default_participant) == ReturnCode_t::RETCODE_OK);
647+
}
648+
571649
TEST(ParticipantTests, GetParticipantProfileQos)
572650
{
573651
DomainParticipantFactory::get_instance()->load_XML_profiles_file("test_xml_profile.xml");

versions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Forthcoming
2323
* StringMatching
2424
* TimeConversion
2525
* DBQueue
26+
* Added create participant methods that use environment XML profile for participant configuration.
2627

2728
Version 2.14.0
2829
--------------

0 commit comments

Comments
 (0)