Skip to content

Commit

Permalink
cut descgate interfaces to config, registration receiver and registra…
Browse files Browse the repository at this point in the history
…tion provider (to make it testable)
  • Loading branch information
rex-schilasky committed Apr 15, 2024
1 parent 96d7fa8 commit 46d630a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 44 deletions.
36 changes: 8 additions & 28 deletions ecal/core/src/ecal_descgate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
* @brief eCAL description gateway class
**/

#include <ecal/ecal_log.h>
#include <ecal/ecal_config.h>

#include "ecal_globals.h"
#include "ecal_descgate.h"

#include <iostream>

namespace
{
eCAL::Util::DescQualityFlags GetDataTypeInfoQuality(const eCAL::SDataTypeInformation& data_type_info_, bool is_producer_)
Expand All @@ -45,33 +43,15 @@ namespace

namespace eCAL
{
CDescGate::CDescGate() :
m_publisher_info_map (std::chrono::milliseconds(Config::GetMonitoringTimeoutMs())),
m_subscriber_info_map (std::chrono::milliseconds(Config::GetMonitoringTimeoutMs())),
m_service_info_map (std::chrono::milliseconds(Config::GetMonitoringTimeoutMs())),
m_client_info_map (std::chrono::milliseconds(Config::GetMonitoringTimeoutMs()))
CDescGate::CDescGate(const std::chrono::milliseconds& exp_timeout_) :
m_publisher_info_map (exp_timeout_),
m_subscriber_info_map (exp_timeout_),
m_service_info_map (exp_timeout_),
m_client_info_map (exp_timeout_)
{
}
CDescGate::~CDescGate() = default;

void CDescGate::Create()
{
#if ECAL_CORE_REGISTRATION
// utilize registration provider and receiver to get descriptions
g_registration_provider()->SetCustomApplySampleCallback("descgate", [this](const auto& sample_) {this->ApplySample(sample_, tl_none); });
g_registration_receiver()->SetCustomApplySampleCallback("descgate", [this](const auto& sample_) {this->ApplySample(sample_, tl_none); });
#endif
}

void CDescGate::Destroy()
{
#if ECAL_CORE_REGISTRATION
// stop registration provider and receiver utilization to get descriptions
g_registration_provider()->RemCustomApplySampleCallback("descgate");
g_registration_receiver()->RemCustomApplySampleCallback("descgate");
#endif
}

QualityTopicIdMap CDescGate::GetPublisher()
{
return GetTopics(m_publisher_info_map);
Expand Down Expand Up @@ -179,7 +159,7 @@ namespace eCAL
break;
default:
{
Logging::Log(log_level_debug1, "CDescGate::ApplySample : unknown sample type");
std::cerr << "CDescGate::ApplySample : unknown sample type" << '\n';
}
break;
}
Expand Down
16 changes: 8 additions & 8 deletions ecal/core/src/ecal_descgate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
#include <ecal/ecal_types.h>
#include <ecal/ecal_util.h>

#include "serialization/ecal_serialize_sample_registration.h"
#include "serialization/ecal_struct_sample_registration.h"
#include "util/ecal_expmap.h"

#include <chrono>
#include <map>
#include <mutex>
#include <string>
#include <type_traits>

namespace eCAL
{
Expand Down Expand Up @@ -65,21 +65,23 @@ namespace eCAL
}
};

using QualityTopicIdMap = std::map<STopicIdKey, Util::SQualityTopicInfo>;
using QualityTopicIdMap = std::map<STopicIdKey, Util::SQualityTopicInfo>;
using QualityServiceIdMap = std::map<SServiceIdKey, Util::SQualityServiceInfo>;

class CDescGate
{
public:
CDescGate();
CDescGate(const std::chrono::milliseconds& exp_timeout_);
~CDescGate();

void Create();
void Destroy();
// apply samples to description gate
void ApplySample(const Registration::Sample& sample_, eTLayerType layer_);

// get publisher/subscriber maps
QualityTopicIdMap GetPublisher();
QualityTopicIdMap GetSubscriber();

// get service/clients maps
QualityServiceIdMap GetServices();
QualityServiceIdMap GetClients();

Expand Down Expand Up @@ -122,8 +124,6 @@ namespace eCAL
QualityTopicIdMap GetTopics(const SQualityTopicIdMap& topic_map_);
QualityServiceIdMap GetServices(const SQualityServiceIdMap& service_method_info_map_);

void ApplySample(const Registration::Sample& sample_, eTLayerType layer_);

void ApplyTopicDescription(SQualityTopicIdMap& topic_info_map_,
const std::string& topic_name_,
const std::string& topic_id_,
Expand Down
21 changes: 18 additions & 3 deletions ecal/core/src/ecal_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ namespace eCAL
/////////////////////
if (descgate_instance == nullptr)
{
descgate_instance = std::make_unique<CDescGate>();
// create description gate with configured expiration timeout
descgate_instance = std::make_unique<CDescGate>(std::chrono::milliseconds(Config::GetMonitoringTimeoutMs()));
new_initialization = true;
}

Expand Down Expand Up @@ -232,7 +233,14 @@ namespace eCAL
if (registration_provider_instance) registration_provider_instance->Create();
if (registration_receiver_instance) registration_receiver_instance->Create();
#endif
if (descgate_instance) descgate_instance->Create();
if (descgate_instance)
{
#if ECAL_CORE_REGISTRATION
// utilize registration provider and receiver to get descriptions
g_registration_provider()->SetCustomApplySampleCallback("descgate", [](const auto& sample_) {g_descgate()->ApplySample(sample_, tl_none); });
g_registration_receiver()->SetCustomApplySampleCallback("descgate", [](const auto& sample_) {g_descgate()->ApplySample(sample_, tl_none); });
#endif
}
#if defined(ECAL_CORE_REGISTRATION_SHM) || defined(ECAL_CORE_TRANSPORT_SHM)
if (memfile_pool_instance) memfile_pool_instance->Create();
#endif
Expand Down Expand Up @@ -324,7 +332,14 @@ namespace eCAL
#if ECAL_CORE_SUBSCRIBER
if (subgate_instance) subgate_instance->Destroy();
#endif
if (descgate_instance) descgate_instance->Destroy();
if (descgate_instance)
{
#if ECAL_CORE_REGISTRATION
// stop registration provider and receiver utilization to get descriptions
g_registration_provider()->RemCustomApplySampleCallback("descgate");
g_registration_receiver()->RemCustomApplySampleCallback("descgate");
#endif
}
#if ECAL_CORE_REGISTRATION
if (registration_receiver_instance) registration_receiver_instance->Destroy();
if (registration_provider_instance) registration_provider_instance->Destroy();
Expand Down
14 changes: 13 additions & 1 deletion ecal/tests/cpp/util_test/src/util_getclients.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

#include <gtest/gtest.h>

enum { CMN_MONITORING_TIMEOUT = 5000};
enum {
CMN_MONITORING_TIMEOUT = 5000,
CMN_REGISTRATION_REFRESH = 1000
};

TEST(core_cpp_util, ClientExpiration)
{
Expand Down Expand Up @@ -67,6 +70,9 @@ TEST(core_cpp_util, ClientExpiration)
EXPECT_EQ(client_info_map.size(), 1);
}

// let's unregister
eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH);

// get all clients again, all clients
// should be removed from the map
eCAL::Util::GetClients(client_info_map);
Expand Down Expand Up @@ -153,6 +159,9 @@ TEST(core_cpp_util, ClientEqualQualities)
EXPECT_EQ(resp_desc, "foo::resp_desc2");
}

// let's unregister
eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH);

// get all clients again, all clients
// should be removed from the map
eCAL::Util::GetClients(client_info_map);
Expand Down Expand Up @@ -218,6 +227,9 @@ TEST(core_cpp_util, ClientDifferentQualities)
EXPECT_EQ(client_info_map.size(), 1);
}

// let's unregister
eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH);

// get all clients again, all clients
// should be removed from the map
eCAL::Util::GetClients(client_info_map);
Expand Down
14 changes: 13 additions & 1 deletion ecal/tests/cpp/util_test/src/util_getservices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

#include <gtest/gtest.h>

enum { CMN_MONITORING_TIMEOUT = 5000 };
enum {
CMN_MONITORING_TIMEOUT = 5000,
CMN_REGISTRATION_REFRESH = 1000
};

TEST(core_cpp_util, ServiceExpiration)
{
Expand Down Expand Up @@ -63,6 +66,9 @@ TEST(core_cpp_util, ServiceExpiration)
EXPECT_EQ(service_info_map.size(), 1);
}

// let's unregister
eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH);

// get all services again, all services
// should be removed from the map
eCAL::Util::GetServices(service_info_map);
Expand Down Expand Up @@ -141,6 +147,9 @@ TEST(core_cpp_util, ServiceEqualQualities)
EXPECT_EQ(resp_desc, "foo::resp_desc2");
}

// let's unregister
eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH);

// get all services again, all services
// should be removed from the map
eCAL::Util::GetServices(service_info_map);
Expand Down Expand Up @@ -198,6 +207,9 @@ TEST(core_cpp_util, ServiceDifferentQualities)
EXPECT_EQ(service_info_map.size(), 1);
}

// let's unregister
eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH);

// get all services again, all services
// should be removed from the map
eCAL::Util::GetServices(service_info_map);
Expand Down
9 changes: 6 additions & 3 deletions ecal/tests/cpp/util_test/src/util_gettopics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

#include <gtest/gtest.h>

enum { CMN_MONITORING_TIMEOUT = 5000 };
enum {
CMN_MONITORING_TIMEOUT = 5000,
CMN_REGISTRATION_REFRESH = 1000
};

TEST(core_cpp_util, GetTopics)
{
Expand Down Expand Up @@ -108,8 +111,8 @@ TEST(core_cpp_util, GetTopics)
}
}

// let's unregister them
eCAL::Process::SleepMS(CMN_MONITORING_TIMEOUT + 2000);
// let's unregister
eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH);

// get all topics again, now all topics
// should be removed from the map
Expand Down

0 comments on commit 46d630a

Please sign in to comment.