Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[21357] Remove TopicAttributes from public APIs and make it private in xmlparser #100

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions include/DiscoveryItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ namespace discovery_server {

typedef fastdds::rtps::GUID_t GUID_t;

struct TopicDescriptionItem
{
TopicDescriptionItem()
{
name = "UNDEF";
type_name = "UNDEF";
}

TopicDescriptionItem(
const std::string& name,
const std::string& type_name)
: name(name)
, type_name(type_name)
{

}

~TopicDescriptionItem() = default;

std::string name;
std::string type_name;
};

//! common discovery info
struct DiscoveryItem
{
Expand Down
9 changes: 8 additions & 1 deletion include/DiscoveryServerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class DiscoveryServerManager
// Snapshops container
snapshots_list snapshots;

// Topic description profiles
std::map<std::string, TopicDescriptionItem> topic_description_profiles_map;

volatile bool no_callbacks; // ongoing participant destruction
bool auto_shutdown; // close when event processing is finished?
bool enable_prefix_validation; // allow multiple servers share the same prefix? (only for testing purposes)
Expand Down Expand Up @@ -315,7 +318,11 @@ class DiscoveryServerManager
}

// default topic
static TopicAttributes builtin_defaultTopic;
static TopicDescriptionItem default_topic_description;

static bool fill_topic_description_profile(
tinyxml2::XMLElement* elem,
TopicDescriptionItem& topic_description);

// parsing regex
static const std::regex ipv4_regular_expression;
Expand Down
6 changes: 3 additions & 3 deletions include/LateJoiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,11 @@ void DelayedEndpointCreation<ReaderWriter>::operator ()(
if (type_name == "UNDEF")
{
topic = manager.getParticipantTopicByName(part,
DiscoveryServerManager::builtin_defaultTopic.getTopicName().to_string());
DiscoveryServerManager::default_topic_description.name);
if ( nullptr == topic)
{
topic = part->create_topic(DiscoveryServerManager::builtin_defaultTopic.getTopicName().to_string(),
DiscoveryServerManager::builtin_defaultTopic.topicDataType.to_string(),
topic = part->create_topic(DiscoveryServerManager::default_topic_description.name,
DiscoveryServerManager::default_topic_description.type_name,
part->get_default_topic_qos());
manager.setParticipantTopic(part, topic);
}
Expand Down
133 changes: 111 additions & 22 deletions src/DiscoveryServerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ using namespace eprosima::discovery_server;
namespace eprosima {
namespace fastdds {
namespace DSxmlparser {
const char* DATA_TYPE = "dataType";
const char* PROFILES = "profiles";
const char* PROFILE_NAME = "profile_name";
const char* PREFIX = "prefix";
Expand All @@ -53,7 +54,7 @@ const char* TOPIC = "topic";
} // namespace eprosima

/*static members*/
TopicAttributes DiscoveryServerManager::builtin_defaultTopic("HelloWorldTopic", "HelloWorld");
TopicDescriptionItem DiscoveryServerManager::default_topic_description("HelloWorldTopic", "HelloWorld");
const std::regex DiscoveryServerManager::ipv4_regular_expression("^((?:[0-9]{1,3}\\.){3}[0-9]{1,3})?:?(?:(\\d+))?$");
const std::chrono::seconds DiscoveryServerManager::last_snapshot_delay_ = std::chrono::seconds(1);

Expand Down Expand Up @@ -122,6 +123,27 @@ DiscoveryServerManager::DiscoveryServerManager(
{
LOG_ERROR("Error parsing profiles!");
}

// Store Topic Profiles
tinyxml2::XMLElement* topic_profile = profiles->FirstChildElement(s_sTopic.c_str());
while (nullptr != topic_profile)
{
const char* topic_profile_name = topic_profile->Attribute(DSxmlparser::PROFILE_NAME);
if (topic_profile_name != nullptr)
{
auto topic_desc_it = topic_description_profiles_map.find(topic_profile_name);
if (topic_desc_it == topic_description_profiles_map.end())
{
auto &topic_desc = topic_description_profiles_map[topic_profile_name];
if (!fill_topic_description_profile(topic_profile, topic_desc))
{
LOG_ERROR("Error parsing topic profiles");
break;
}
}
}
topic_profile = topic_profile->NextSiblingElement(s_sTopic.c_str());
}
}

// Server processing requires a two pass analysis
Expand Down Expand Up @@ -681,6 +703,66 @@ Topic* DiscoveryServerManager::getParticipantTopicByName(
return returnTopic;
}

bool DiscoveryServerManager::fill_topic_description_profile(
tinyxml2::XMLElement* elem,
TopicDescriptionItem& topic_description)
{
/*
<xs:complexType name="topicDescriptionType">
<xs:all minOccurs="0">
<xs:element name="name" type="stringType" minOccurs="0"/>
<xs:element name="dataType" type="stringType" minOccurs="0"/>
</xs:all>
</xs:complexType>
*/

bool ret = true;
tinyxml2::XMLElement* p_aux0 = elem->FirstChildElement();
const char* name = nullptr;
for (; p_aux0 != NULL; p_aux0 = p_aux0->NextSiblingElement())
{
name = p_aux0->Name();

if (strcmp(name, DSxmlparser::NAME) == 0)
{
// name - stringType
const char* text = p_aux0->GetText();
if (nullptr != text)
{
topic_description.name = text;
}
else
{
LOG_ERROR("<" << p_aux0->Value() << "> GetText XML_ERROR");
ret = false;
break;
}
}
else if (strcmp(name, DSxmlparser::DATA_TYPE) == 0)
{
// dataType - stringType
const char* text = p_aux0->GetText();
if (nullptr != text)
{
topic_description.type_name = text;
}
else
{
LOG_ERROR("<" << p_aux0->Value() << "> GetText XML_ERROR");
ret = false;
break;
}
}
else
{
LOG_ERROR("Invalid element found into 'topicDescriptionType'. Name: " << name);
ret = false;
}
}

return ret;
}

void DiscoveryServerManager::onTerminate()
{
{
Expand Down Expand Up @@ -1302,14 +1384,19 @@ void DiscoveryServerManager::loadSubscriber(

// see if topic is specified
const char* topic_name = sub->Attribute(DSxmlparser::TOPIC);
TopicAttributes topicAttr;
TopicDescriptionItem topic_description;
if (topic_name != nullptr)
{
if (!eprosima::fastdds::rtps::RTPSDomain::get_topic_attributes_from_profile(std::string(
topic_name), topicAttr))
if (topic_name != nullptr)
{
LOG_ERROR("DiscoveryServerManager::loadSubscriber couldn't load topic profile ");
return;
try
{
topic_description = topic_description_profiles_map.at(topic_name);
}
catch (...)
{
LOG_ERROR("Topic " << topic_name << " not found in the topic description profiles");
}
}
}

Expand All @@ -1332,8 +1419,8 @@ void DiscoveryServerManager::loadSubscriber(
endpoint_profile = std::string(profile_name);
}

DelayedEndpointCreation<DataReader> event(creation_time, topicAttr.getTopicName().to_string(),
topicAttr.getTopicDataType().to_string(), topic_name, endpoint_profile, part_guid, pDE,
DelayedEndpointCreation<DataReader> event(creation_time, topic_description.name,
topic_description.type_name, topic_name, endpoint_profile, part_guid, pDE,
participant_creation_event);

if (creation_time == getTime())
Expand All @@ -1349,11 +1436,11 @@ void DiscoveryServerManager::loadSubscriber(

void DiscoveryServerManager::loadPublisher(
GUID_t& part_guid,
tinyxml2::XMLElement* sub,
tinyxml2::XMLElement* pub,
DelayedParticipantCreation* participant_creation_event /*= nullptr*/,
DelayedParticipantDestruction* participant_destruction_event /*= nullptr*/)
{
assert(sub != nullptr);
assert(pub != nullptr);

// check if we need to create an event
std::chrono::steady_clock::time_point creation_time, removal_time;
Expand Down Expand Up @@ -1382,15 +1469,15 @@ void DiscoveryServerManager::loadPublisher(
}

{
const char* creation_time_str = sub->Attribute(s_sCreationTime.c_str());
const char* creation_time_str = pub->Attribute(s_sCreationTime.c_str());
if (creation_time_str != nullptr)
{
int aux;
std::istringstream(creation_time_str) >> aux;
creation_time = getTime() + std::chrono::seconds(aux);
}

const char* removal_time_str = sub->Attribute(s_sRemovalTime.c_str());
const char* removal_time_str = pub->Attribute(s_sRemovalTime.c_str());
if (removal_time_str != nullptr)
{
int aux;
Expand All @@ -1401,22 +1488,24 @@ void DiscoveryServerManager::loadPublisher(

// data_readers are created for debugging purposes
// default topic is the static HelloWorld one
const char* profile_name = sub->Attribute(DSxmlparser::PROFILE_NAME);
const char* profile_name = pub->Attribute(DSxmlparser::PROFILE_NAME);

// see if topic is specified
const char* topic_name = sub->Attribute(DSxmlparser::TOPIC);
TopicAttributes topicAttr;
const char* topic_name = pub->Attribute(DSxmlparser::TOPIC);
TopicDescriptionItem topic_description;
if (topic_name != nullptr)
{
if (!eprosima::fastdds::rtps::RTPSDomain::get_topic_attributes_from_profile(std::string(
topic_name), topicAttr))
try
{
LOG_ERROR("DiscoveryServerManager::loadPublisher couldn't load topic profile ");
return;
topic_description = topic_description_profiles_map.at(topic_name);
}
catch (...)
{
LOG_ERROR("Topic " << topic_name << " not found in the topic description profiles");
}
}

DelayedEndpointDestruction<DataWriter>* pDE = nullptr; // subscriber destruction event
DelayedEndpointDestruction<DataWriter>* pDE = nullptr; // publisher destruction event

if (removal_time != getTime())
{
Expand All @@ -1436,8 +1525,8 @@ void DiscoveryServerManager::loadPublisher(
endpoint_profile = std::string(profile_name);
}

DelayedEndpointCreation<DataWriter> event(creation_time, topicAttr.getTopicName().to_string(),
topicAttr.getTopicDataType().to_string(), topic_name, endpoint_profile, part_guid, pDE,
DelayedEndpointCreation<DataWriter> event(creation_time, topic_description.name,
topic_description.type_name, topic_name, endpoint_profile, part_guid, pDE,
participant_creation_event);

if (creation_time == getTime())
Expand Down
Loading