Skip to content

Commit 66c426e

Browse files
authored
Expose get entity by guid function (#264)
* Expose get entity by guid function Signed-off-by: Raul Sanchez-Mateos <[email protected]> * Fix doxygen description Signed-off-by: Raul Sanchez-Mateos <[email protected]> --------- Signed-off-by: Raul Sanchez-Mateos <[email protected]>
1 parent 8bf93a3 commit 66c426e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

include/fastdds_statistics_backend/StatisticsBackend.hpp

+19
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ class StatisticsBackend
203203
EntityKind entity_type,
204204
EntityId entity_id = EntityId::all());
205205

206+
/**
207+
* @brief Get the EntityId for a given GUID in string format.
208+
*
209+
* @param guid The GUID in string format.
210+
* @return The EntityId corresponding to the given GUID.
211+
* @throws eprosima::statistics_backend::BadParameter if the GUID is not found.
212+
*/
213+
FASTDDS_STATISTICS_BACKEND_DllAPI
214+
static EntityId get_entity_by_guid(
215+
const std::string& guid);
216+
206217
/**
207218
* @brief Returns whether the entity is active.
208219
*
@@ -612,6 +623,14 @@ class StatisticsBackend
612623
FASTDDS_STATISTICS_BACKEND_DllAPI
613624
static std::string deserialize_guid(
614625
fastdds::statistics::detail::GUID_s data);
626+
627+
/**
628+
* @brief Serialize entity guid from string.
629+
* @param guid_str Entity guid.
630+
*/
631+
FASTDDS_STATISTICS_BACKEND_DllAPI
632+
static fastdds::statistics::detail::GUID_s serialize_guid(
633+
const std::string& guid_str);
615634
};
616635

617636
} // namespace statistics_backend

src/cpp/StatisticsBackend.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,15 @@ std::vector<EntityId> StatisticsBackend::get_entities(
508508
return StatisticsBackendData::get_instance()->database_->get_entity_ids(entity_type, entity_id);
509509
}
510510

511+
EntityId StatisticsBackend::get_entity_by_guid(
512+
const std::string& guid)
513+
{
514+
return StatisticsBackendData::get_instance()->database_->get_entity_by_guid(
515+
StatisticsBackendData::get_instance()->database_->get_entity_kind_by_guid(
516+
StatisticsBackend::serialize_guid(guid)),
517+
guid).second;
518+
}
519+
511520
bool StatisticsBackend::is_active(
512521
EntityId entity_id)
513522
{
@@ -994,5 +1003,37 @@ std::string StatisticsBackend::deserialize_guid(
9941003
return ss.str();
9951004
}
9961005

1006+
fastdds::statistics::detail::GUID_s StatisticsBackend::serialize_guid(
1007+
const std::string& guid_str)
1008+
{
1009+
fastdds::statistics::detail::GUID_s guid_s;
1010+
std::istringstream iss(guid_str);
1011+
std::string byte_str;
1012+
1013+
// Parse the guidPrefix part
1014+
uint8_t guid_prefix_size = static_cast<uint8_t>(fastdds::rtps::GuidPrefix_t::size);
1015+
for (uint8_t i = 0; i < guid_prefix_size; ++i)
1016+
{
1017+
if (i == (guid_prefix_size - 1))
1018+
{
1019+
std::getline(iss, byte_str, '|');
1020+
}
1021+
else
1022+
{
1023+
std::getline(iss, byte_str, '.');
1024+
}
1025+
guid_s.guidPrefix().value()[i] = static_cast<uint8_t>(std::stoul(byte_str, nullptr, 16));
1026+
}
1027+
1028+
// Parse the entityId part
1029+
for (uint8_t i = 0; i < static_cast<uint8_t>(fastdds::rtps::EntityId_t::size); ++i)
1030+
{
1031+
std::getline(iss, byte_str, '.');
1032+
guid_s.entityId().value()[i] = static_cast<uint8_t>(std::stoul(byte_str, nullptr, 16));
1033+
}
1034+
1035+
return guid_s;
1036+
}
1037+
9971038
} // namespace statistics_backend
9981039
} // namespace eprosima

0 commit comments

Comments
 (0)