Skip to content

Commit

Permalink
[core] split GetTopicNames into GetPublishedTopicNames and `GetSu…
Browse files Browse the repository at this point in the history
…bscribedTopicNames`. (#1969)
  • Loading branch information
KerstinKeller authored Jan 30, 2025
1 parent 428e952 commit 55da7f3
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 83 deletions.
27 changes: 19 additions & 8 deletions ecal/core/include/ecal/registration.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace eCAL
*
* @return Set of topic id's.
**/
ECAL_API std::set<STopicId> GetPublisherIDs();
ECAL_API bool GetPublisherIDs(std::set<STopicId>& topic_ids_);

/**
* @brief Get data type information with quality for specific publisher.
Expand Down Expand Up @@ -98,7 +98,7 @@ namespace eCAL
*
* @return Set of topic id's.
**/
ECAL_API std::set<STopicId> GetSubscriberIDs();
ECAL_API bool GetSubscriberIDs(std::set<STopicId>& topic_ids_);

/**
* @brief Get data type information with quality for specific subscriber.
Expand Down Expand Up @@ -130,7 +130,7 @@ namespace eCAL
*
* @return Set of service id's.
**/
ECAL_API std::set<SServiceId> GetServerIDs();
ECAL_API bool GetServerIDs(std::set<SServiceId>& service_ids_);

/**
* @brief Get service method information for a specific server.
Expand All @@ -144,7 +144,7 @@ namespace eCAL
*
* @return Set of service id's.
**/
ECAL_API std::set<SServiceId> GetClientIDs();
ECAL_API bool GetClientIDs(std::set<SServiceId>& service_ids_);

/**
* @brief Get service method information for a specific client.
Expand All @@ -154,25 +154,36 @@ namespace eCAL
ECAL_API bool GetClientInfo(const SServiceId& id_, ServiceMethodInformationSetT& service_method_info_);

/**
* @brief Get all topic names.
* @brief Get all names of topics that are being published.
* This is a convenience function.
* It calls GetPublisherIDs() and filters by name
*
* @param topic_names_ Set to store the topic names.
**/
ECAL_API void GetTopicNames(std::set<std::string>& topic_names_);
ECAL_API bool GetPublishedTopicNames(std::set<std::string>& topic_names_);

/**
* @brief Get all names of topics that are being subscribed
* This is a convenience function.
* It calls GetSubscriberIDs() and filters by name
*
* @param topic_names_ Set to store the topic names.
**/
ECAL_API bool GetSubscribedTopicNames(std::set<std::string>& topic_names_);

/**
* @brief Get the pairs of service name / method name of all eCAL Servers.
*
* @param service_method_names_ Set to store the service/method names (Set { (ServiceName, MethodName) }).
**/
ECAL_API void GetServerMethodNames(std::set<SServiceMethod>& server_method_names_);
ECAL_API bool GetServerMethodNames(std::set<SServiceMethod>& server_method_names_);

/**
* @brief Get the pairs of service name / method name of all eCAL Clients.
*
* @param client_method_names_ Set to store the client/method names (Set { (ClientName, MethodName) }).
**/
ECAL_API void GetClientMethodNames(std::set<SServiceMethod>& client_method_names_);
ECAL_API bool GetClientMethodNames(std::set<SServiceMethod>& client_method_names_);
}
}

59 changes: 39 additions & 20 deletions ecal/core/src/registration/ecal_registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ namespace eCAL
{
namespace Registration
{
std::set<STopicId> GetPublisherIDs()
bool GetPublisherIDs(std::set<STopicId>& topic_ids_)
{
if (g_descgate() == nullptr) return std::set<STopicId>();
return g_descgate()->GetPublisherIDs();
if (g_descgate() == nullptr) return false;
topic_ids_ = std::move(g_descgate()->GetPublisherIDs());
return true;
}

bool GetPublisherInfo(const STopicId& id_, SDataTypeInformation& topic_info_)
Expand All @@ -58,10 +59,11 @@ namespace eCAL
return g_descgate()->RemPublisherEventCallback(token_);
}

std::set<STopicId> GetSubscriberIDs()
bool GetSubscriberIDs(std::set<STopicId>& topic_ids_)
{
if (g_descgate() == nullptr) return std::set<STopicId>();
return g_descgate()->GetSubscriberIDs();
if (g_descgate() == nullptr) return false;
topic_ids_ = std::move(g_descgate()->GetSubscriberIDs());
return true;
}

bool GetSubscriberInfo(const STopicId& id_, SDataTypeInformation& topic_info_)
Expand All @@ -82,10 +84,11 @@ namespace eCAL
return g_descgate()->RemSubscriberEventCallback(token_);
}

std::set<SServiceId> GetServerIDs()
bool GetServerIDs(std::set<SServiceId>& service_ids_)
{
if (g_descgate() == nullptr) return std::set<SServiceId>();
return g_descgate()->GetServerIDs();
if (g_descgate() == nullptr) return false;
service_ids_ = std::move(g_descgate()->GetServerIDs());
return true;
}

bool GetServerInfo(const SServiceId& id_, ServiceMethodInformationSetT& service_info_)
Expand All @@ -94,10 +97,11 @@ namespace eCAL
return g_descgate()->GetServerInfo(id_, service_info_);
}

std::set<SServiceId> GetClientIDs()
bool GetClientIDs(std::set<SServiceId>& service_ids_)
{
if (g_descgate() == nullptr) return std::set<SServiceId>();
return g_descgate()->GetClientIDs();
if (g_descgate() == nullptr) return false;
service_ids_ = std::move(g_descgate()->GetClientIDs());
return true;
}

bool GetClientInfo(const SServiceId& id_, ServiceMethodInformationSetT& service_info_)
Expand All @@ -106,29 +110,41 @@ namespace eCAL
return g_descgate()->GetClientInfo(id_, service_info_);
}

void GetTopicNames(std::set<std::string>& topic_names_)
bool GetPublishedTopicNames(std::set<std::string>& topic_names_)
{
topic_names_.clear();

// get publisher & subscriber id sets and insert names into the topic_names set
const std::set<STopicId> pub_id_set = GetPublisherIDs();
// get publisher id sets and insert names into the topic_names set
std::set<STopicId> pub_id_set;
bool return_value = GetPublisherIDs(pub_id_set);
for (const auto& pub_id : pub_id_set)
{
topic_names_.insert(pub_id.topic_name);
}
const std::set<STopicId> sub_id_set = GetSubscriberIDs();
return return_value;
}

bool GetSubscribedTopicNames(std::set<std::string>& topic_names_)
{
topic_names_.clear();

// get subscriber id sets and insert names into the topic_names set
std::set<STopicId> sub_id_set;
bool return_value = GetSubscriberIDs(sub_id_set);
for (const auto& sub_id : sub_id_set)
{
topic_names_.insert(sub_id.topic_name);
}
return return_value;
}

void GetServerMethodNames(std::set<SServiceMethod>& server_method_names_)
bool GetServerMethodNames(std::set<SServiceMethod>& server_method_names_)
{
server_method_names_.clear();

// get servers id set and insert names into the server_method_names_ set
const std::set<SServiceId> server_id_set = GetServerIDs();
std::set<SServiceId> server_id_set;
bool return_value = GetServerIDs(server_id_set);
for (const auto& server_id : server_id_set)
{
eCAL::ServiceMethodInformationSetT methods;
Expand All @@ -138,14 +154,16 @@ namespace eCAL
server_method_names_.insert({ server_id.service_name, method.method_name });
}
}
return return_value;
}

void GetClientMethodNames(std::set<SServiceMethod>& client_method_names_)
bool GetClientMethodNames(std::set<SServiceMethod>& client_method_names_)
{
client_method_names_.clear();

// get clients id set and insert names into the client_method_names set
const std::set<SServiceId> client_id_set = GetClientIDs();
std::set<SServiceId> client_id_set;
bool return_value = GetClientIDs(client_id_set);
for (const auto& client_id : client_id_set)
{
eCAL::ServiceMethodInformationSetT methods;
Expand All @@ -155,6 +173,7 @@ namespace eCAL
client_method_names_.insert({ client_id.service_name, method.method_name });
}
}
return return_value;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ int main()
size_t num_sub(0);
while ((num_pub < publisher_number) || (num_sub < subscriber_number))
{
num_pub = eCAL::Registration::GetPublisherIDs().size();
num_sub = eCAL::Registration::GetSubscriberIDs().size();
std::set<eCAL::STopicId> publisher_ids;
std::set<eCAL::STopicId> subscriber_ids;

eCAL::Registration::GetPublisherIDs(publisher_ids);
eCAL::Registration::GetSubscriberIDs(subscriber_ids);

num_pub = publisher_ids.size();
num_sub = subscriber_ids.size();

std::cout << "Registered publisher : " << num_pub << std::endl;
std::cout << "Registered subscriber: " << num_sub << std::endl;
Expand All @@ -183,7 +189,8 @@ int main()
// start time measurement
auto start_time = std::chrono::high_resolution_clock::now();

const auto pub_ids = eCAL::Registration::GetPublisherIDs();
std::set<eCAL::STopicId> pub_ids;
eCAL::Registration::GetPublisherIDs(pub_ids);
num_pub = pub_ids.size();
for (const auto& id : pub_ids)
{
Expand All @@ -200,7 +207,8 @@ int main()
}

// check creation events
const std::set<eCAL::STopicId> publisher_ids = eCAL::Registration::GetPublisherIDs();
std::set<eCAL::STopicId> publisher_ids;
eCAL::Registration::GetPublisherIDs(publisher_ids);
std::cout << "Number of publisher creation events " << created_publisher_num << std::endl;
std::cout << "Size of publisher creation id set " << created_publisher_ids.size() << std::endl;
//std::cout << "Publisher creation id sets are equal " << (publisher_ids == created_publisher_ids) << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main()
start_time = std::chrono::steady_clock::now();
for (run = 0; run < runs; ++run)
{
server_method_id_set = eCAL::Registration::GetServerIDs();
eCAL::Registration::GetServerIDs(server_method_id_set);
}

auto num_services = server_method_id_set.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,13 @@ int main()
start_time = std::chrono::steady_clock::now();
for (run = 0; run < runs; ++run)
{
topic_id_pub_set = eCAL::Registration::GetPublisherIDs();
topic_id_sub_set = eCAL::Registration::GetSubscriberIDs();
eCAL::Registration::GetPublisherIDs(topic_id_pub_set);
eCAL::Registration::GetSubscriberIDs(topic_id_pub_set);
}

auto num_topics = topic_id_pub_set.size() + topic_id_sub_set.size();
auto num_entities = topic_id_pub_set.size() + topic_id_sub_set.size();
auto diff_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_time);
std::cout << "GetTopics : " << static_cast<double>(diff_time.count()) / runs << " ms" << " (" << num_topics << " topics)" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));

// GetTopicNames
{
std::set<std::string> topic_names;

start_time = std::chrono::steady_clock::now();
for (run = 0; run < runs; ++run)
{
eCAL::Registration::GetTopicNames(topic_names);
}

auto num_topics = topic_names.size();
auto diff_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_time);
std::cout << "GetTopicsNames : " << static_cast<double>(diff_time.count()) / runs << " ms" << " (" << num_topics << " topics)" << std::endl;
std::cout << std::endl;
std::cout << "GetTopics : " << static_cast<double>(diff_time.count()) / runs << " ms" << " (" << num_entities << " entities)" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ClientsTestFixture : public ::testing::TestWithParam<ClientsTestParams>
TEST_P(ClientsTestFixture, ClientExpiration)
{
std::set<eCAL::SServiceId> id_set;
bool get_client_ids_succeeded = false;

// create simple client and let it expire
{
Expand All @@ -71,7 +72,8 @@ TEST_P(ClientsTestFixture, ClientExpiration)
eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS);

// get all clients
id_set = eCAL::Registration::GetClientIDs();
get_client_ids_succeeded = eCAL::Registration::GetClientIDs(id_set);
EXPECT_TRUE(get_client_ids_succeeded) << "GetClientIDs call failed";

// check size
EXPECT_EQ(id_set.size(), 1);
Expand All @@ -90,7 +92,8 @@ TEST_P(ClientsTestFixture, ClientExpiration)
eCAL::Process::SleepMS(CMN_MONITORING_TIMEOUT_MS);

// get all clients again, client should not be expired
id_set = eCAL::Registration::GetClientIDs();
get_client_ids_succeeded = eCAL::Registration::GetClientIDs(id_set);
EXPECT_TRUE(get_client_ids_succeeded) << "GetClientIDs call failed";

// check size
EXPECT_EQ(id_set.size(), 1);
Expand All @@ -101,7 +104,8 @@ TEST_P(ClientsTestFixture, ClientExpiration)

// get all clients again, all clients
// should be removed from the map
id_set = eCAL::Registration::GetClientIDs();
get_client_ids_succeeded = eCAL::Registration::GetClientIDs(id_set);
EXPECT_TRUE(get_client_ids_succeeded) << "GetClientIDs call failed";

// check size
EXPECT_EQ(id_set.size(), 0);
Expand All @@ -124,7 +128,9 @@ TEST_P(ClientsTestFixture, GetClientIDs)
eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS);

// get client
auto id_set = eCAL::Registration::GetClientIDs();
std::set<eCAL::SServiceId> id_set;
auto call_successful = eCAL::Registration::GetClientIDs(id_set);
EXPECT_TRUE(call_successful);
EXPECT_EQ(1, id_set.size());
if (id_set.size() > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,23 @@ TEST_P(TestFixture, GetPublisherIDsReturnsCorrectNumber)
eCAL::Process::SleepMS(2 * GetParam().configuration.registration.registration_refresh);

// get the list of publisher IDs
const auto pub_ids1 = eCAL::Registration::GetPublisherIDs();
std::set<eCAL::STopicId> pub_ids1;
const auto get_publisher_ids_succeeded = eCAL::Registration::GetPublisherIDs(pub_ids1);

// verify the number of publishers created
EXPECT_TRUE(get_publisher_ids_succeeded) << "GetPublisherIDs call failed";
ASSERT_EQ(pub_ids1.size(), GetParam().publisher_count);
}

// let's finally timeout
eCAL::Process::SleepMS(GetParam().configuration.registration.registration_timeout);

// get the list of publisher IDs
const auto pub_ids2 = eCAL::Registration::GetPublisherIDs();
std::set<eCAL::STopicId> pub_ids2;
const auto get_publisher_ids_succeeded = eCAL::Registration::GetPublisherIDs(pub_ids2);

// all publisher should be timeouted
EXPECT_TRUE(get_publisher_ids_succeeded) << "GetPublisherIDs call failed";
ASSERT_EQ(pub_ids2.size(), 0);
}

Expand Down
Loading

0 comments on commit 55da7f3

Please sign in to comment.