diff --git a/ecal/core/CMakeLists.txt b/ecal/core/CMakeLists.txt index 00207c3e14..7bd9cc3a23 100644 --- a/ecal/core/CMakeLists.txt +++ b/ecal/core/CMakeLists.txt @@ -288,6 +288,12 @@ if (ECAL_CORE_REGISTRATION) src/registration/ecal_registration_provider.h src/registration/ecal_registration_receiver.cpp src/registration/ecal_registration_receiver.h + src/registration/ecal_registration_sample_applier.cpp + src/registration/ecal_registration_sample_applier.h + src/registration/ecal_registration_sample_applier_gates.cpp + src/registration/ecal_registration_sample_applier_gates.h + src/registration/ecal_registration_sample_applier_user.cpp + src/registration/ecal_registration_sample_applier_user.h src/registration/ecal_registration_sender.h src/registration/udp/ecal_registration_receiver_udp.cpp src/registration/udp/ecal_registration_receiver_udp.h diff --git a/ecal/core/src/registration/ecal_registration_receiver.cpp b/ecal/core/src/registration/ecal_registration_receiver.cpp index c644aa1e2e..ed09206072 100644 --- a/ecal/core/src/registration/ecal_registration_receiver.cpp +++ b/ecal/core/src/registration/ecal_registration_receiver.cpp @@ -31,11 +31,6 @@ #if ECAL_CORE_REGISTRATION_SHM #include "registration/shm/ecal_registration_receiver_shm.h" #endif -#include "ecal_global_accessors.h" - -#include "pubsub/ecal_subgate.h" -#include "pubsub/ecal_pubgate.h" -#include "service/ecal_clientgate.h" #include "io/udp/ecal_udp_configurations.h" #include @@ -51,23 +46,29 @@ namespace eCAL ////////////////////////////////////////////////////////////////// std::atomic CRegistrationReceiver::m_created; - CRegistrationReceiver::CRegistrationReceiver() : - m_network(Config::IsNetworkEnabled()), - m_loopback(false), - m_callback_pub(nullptr), - m_callback_sub(nullptr), - m_callback_service(nullptr), - m_callback_client(nullptr), - m_callback_process(nullptr), - m_use_registration_udp(false), - m_use_registration_shm(false), - m_host_group_name(Process::GetHostGroupName()) + CRegistrationReceiver::CRegistrationReceiver() + : m_use_registration_udp(false) + , m_use_registration_shm(false) + , m_sample_applier(Config::IsNetworkEnabled(), false, Process::GetHostGroupName(), Process::GetProcessID()) { + // Connect User registration callback and gates callback with the sample applier + m_sample_applier.SetCustomApplySampleCallback("gates", [](const eCAL::Registration::Sample& sample_) + { + Registration::CSampleApplierGates::ApplySample(sample_); + }); + m_sample_applier.SetCustomApplySampleCallback("custom_registration", [this](const eCAL::Registration::Sample& sample_) + { + m_user_applier.ApplySample(sample_); + }); + } CRegistrationReceiver::~CRegistrationReceiver() { Stop(); + + m_sample_applier.RemCustomApplySampleCallback("custom_registration"); + m_sample_applier.RemCustomApplySampleCallback("gates"); } void CRegistrationReceiver::Start() @@ -80,13 +81,13 @@ namespace eCAL if (m_use_registration_udp) { - m_registration_receiver_udp = std::make_unique([this](const Registration::Sample& sample_) {return this->ApplySample(sample_); }); + m_registration_receiver_udp = std::make_unique([this](const Registration::Sample& sample_) {return m_sample_applier.ApplySample(sample_); }); } #if ECAL_CORE_REGISTRATION_SHM if (m_use_registration_shm) { - m_registration_receiver_shm = std::make_unique([this](const Registration::Sample& sample_) {return this->ApplySample(sample_); }); + m_registration_receiver_shm = std::make_unique([this](const Registration::Sample& sample_) {return m_sample_applier.ApplySample(sample_); }); } #endif @@ -110,266 +111,32 @@ namespace eCAL } #endif - // reset callbacks - m_callback_pub = nullptr; - m_callback_sub = nullptr; - m_callback_service = nullptr; - m_callback_client = nullptr; - m_callback_process = nullptr; - - // finished m_created = false; } void CRegistrationReceiver::EnableLoopback(bool state_) { - m_loopback = state_; - } - - bool CRegistrationReceiver::ApplySample(const Registration::Sample& sample_) - { - if (!m_created) return false; - - if (!AcceptRegistrationSample(sample_)) - { - Logging::Log(log_level_debug1, "CRegistrationReceiver::ApplySample : Incoming sample discarded"); - return false; - } - - // forward all registration samples to outside "customer" (e.g. monitoring, descgate) - { - const std::lock_guard lock(m_callback_custom_apply_sample_map_mtx); - for (const auto& iter : m_callback_custom_apply_sample_map) - { - iter.second(sample_); - } - } - - // forward registration to defined gates - // and store user registration callback - RegistrationCallbackT reg_callback(nullptr); - switch (sample_.cmd_type) - { - case bct_none: - case bct_set_sample: - break; - case bct_reg_process: - case bct_unreg_process: - // unregistration event not implemented currently - reg_callback = m_callback_process; - break; - case bct_reg_service: - case bct_unreg_service: - ApplyServiceRegistration(sample_); - reg_callback = m_callback_service; - break; - case bct_reg_client: - case bct_unreg_client: - // current client implementation doesn't need that information - reg_callback = m_callback_client; - break; - case bct_reg_subscriber: - case bct_unreg_subscriber: - ApplySubscriberRegistration(sample_); - reg_callback = m_callback_sub; - break; - case bct_reg_publisher: - case bct_unreg_publisher: - ApplyPublisherRegistration(sample_); - reg_callback = m_callback_pub; - break; - default: - Logging::Log(log_level_debug1, "CRegistrationReceiver::ApplySample : unknown sample type"); - break; - } - - // call user registration callback - if (reg_callback) - { - std::string reg_sample; - if (SerializeToBuffer(sample_, reg_sample)) - { - reg_callback(reg_sample.c_str(), static_cast(reg_sample.size())); - } - } - - return true; + m_sample_applier.EnableLoopback(state_); } bool CRegistrationReceiver::AddRegistrationCallback(enum eCAL_Registration_Event event_, const RegistrationCallbackT& callback_) { - if (!m_created) return false; - switch (event_) - { - case reg_event_publisher: - m_callback_pub = callback_; - return true; - case reg_event_subscriber: - m_callback_sub = callback_; - return true; - case reg_event_service: - m_callback_service = callback_; - return true; - case reg_event_client: - m_callback_client = callback_; - return true; - case reg_event_process: - m_callback_process = callback_; - return true; - default: - return false; - } + return m_user_applier.AddRegistrationCallback(event_, callback_); } bool CRegistrationReceiver::RemRegistrationCallback(enum eCAL_Registration_Event event_) { - if (!m_created) return false; - switch (event_) - { - case reg_event_publisher: - m_callback_pub = nullptr; - return true; - case reg_event_subscriber: - m_callback_sub = nullptr; - return true; - case reg_event_service: - m_callback_service = nullptr; - return true; - case reg_event_client: - m_callback_client = nullptr; - return true; - case reg_event_process: - m_callback_process = nullptr; - return true; - default: - return false; - } - } - - void CRegistrationReceiver::ApplyServiceRegistration(const eCAL::Registration::Sample& sample_) - { -#if ECAL_CORE_SERVICE - if (g_clientgate() == nullptr) return; - - switch (sample_.cmd_type) - { - // current service implementation processes registration information only (not the unregistration) - case bct_reg_service: - g_clientgate()->ApplyServiceRegistration(sample_); - break; - default: - break; - } -#endif - } - - void CRegistrationReceiver::ApplySubscriberRegistration(const Registration::Sample& sample_) - { -#if ECAL_CORE_PUBLISHER - if (g_pubgate() == nullptr) return; - - switch (sample_.cmd_type) - { - case bct_reg_subscriber: - g_pubgate()->ApplySubRegistration(sample_); - break; - case bct_unreg_subscriber: - g_pubgate()->ApplySubUnregistration(sample_); - break; - default: - break; - } -#endif - } - - void CRegistrationReceiver::ApplyPublisherRegistration(const Registration::Sample& sample_) - { -#if ECAL_CORE_SUBSCRIBER - if (g_subgate() == nullptr) return; - - switch (sample_.cmd_type) - { - case bct_reg_publisher: - g_subgate()->ApplyPubRegistration(sample_); - break; - case bct_unreg_publisher: - g_subgate()->ApplyPubUnregistration(sample_); - break; - default: - break; - } -#endif - } - - bool CRegistrationReceiver::IsHostGroupMember(const Registration::Sample& sample_) - { - std::string host_group_name; - std::string host_name; - switch (sample_.cmd_type) - { - case bct_reg_publisher: - case bct_unreg_publisher: - case bct_reg_subscriber: - case bct_unreg_subscriber: - host_group_name = sample_.topic.hgname; - host_name = sample_.topic.hname; - break; - case bct_reg_service: - case bct_unreg_service: - //host_group_name = sample_.service.hgname; // TODO: we need to add hgname attribute to services - host_name = sample_.service.hname; - break; - case bct_reg_client: - case bct_unreg_client: - //host_group_name = sample_.client.hgname; // TODO: we need to add hgname attribute to clients - host_name = sample_.client.hname; - break; - default: - break; - } - - const std::string& sample_host_group_name = host_group_name.empty() ? host_name : host_group_name; - - if (sample_host_group_name.empty() || m_host_group_name.empty()) - return false; - if (sample_host_group_name != m_host_group_name) - return false; - - return true; - } - - bool CRegistrationReceiver::AcceptRegistrationSample(const Registration::Sample& sample_) - { - // check if the sample is from the same host group - if (IsHostGroupMember(sample_)) - { - // register if the sample is from another process - // or if loopback mode is enabled - return m_loopback || (sample_.topic.pid != Process::GetProcessID()); - } - else - { - // if the sample is from an external host, register only if network mode is enabled - return m_network; - } - - // do not process the registration - return false; + return m_user_applier.RemRegistrationCallback(event_); } void CRegistrationReceiver::SetCustomApplySampleCallback(const std::string& customer_, const ApplySampleCallbackT& callback_) { - const std::lock_guard lock(m_callback_custom_apply_sample_map_mtx); - m_callback_custom_apply_sample_map[customer_] = callback_; + m_sample_applier.SetCustomApplySampleCallback(customer_, callback_); } void CRegistrationReceiver::RemCustomApplySampleCallback(const std::string& customer_) { - const std::lock_guard lock(m_callback_custom_apply_sample_map_mtx); - auto iter = m_callback_custom_apply_sample_map.find(customer_); - if(iter != m_callback_custom_apply_sample_map.end()) - { - m_callback_custom_apply_sample_map.erase(iter); - } + m_sample_applier.RemCustomApplySampleCallback(customer_); } + } diff --git a/ecal/core/src/registration/ecal_registration_receiver.h b/ecal/core/src/registration/ecal_registration_receiver.h index 0bca6c0ec5..2a6d1c6156 100644 --- a/ecal/core/src/registration/ecal_registration_receiver.h +++ b/ecal/core/src/registration/ecal_registration_receiver.h @@ -31,6 +31,9 @@ #include #include "serialization/ecal_struct_sample_registration.h" +#include "registration/ecal_registration_sample_applier.h" +#include "registration/ecal_registration_sample_applier_gates.h" +#include "registration/ecal_registration_sample_applier_user.h" #include #include @@ -51,13 +54,13 @@ namespace eCAL CRegistrationReceiver(); ~CRegistrationReceiver(); + //what about the rest of the rule of 5? + void Start(); void Stop(); void EnableLoopback(bool state_); - bool ApplySample(const Registration::Sample& sample_); - bool AddRegistrationCallback(enum eCAL_Registration_Event event_, const RegistrationCallbackT& callback_); bool RemRegistrationCallback(enum eCAL_Registration_Event event_); @@ -65,24 +68,9 @@ namespace eCAL void SetCustomApplySampleCallback(const std::string& customer_, const ApplySampleCallbackT& callback_); void RemCustomApplySampleCallback(const std::string& customer_); - protected: - void ApplyServiceRegistration(const eCAL::Registration::Sample& sample_); - - void ApplySubscriberRegistration(const eCAL::Registration::Sample& sample_); - void ApplyPublisherRegistration(const eCAL::Registration::Sample& sample_); - - bool IsHostGroupMember(const eCAL::Registration::Sample& sample_); - bool AcceptRegistrationSample(const Registration::Sample& sample_); - + private: + // why is this a static variable? can someone explain? static std::atomic m_created; - bool m_network; - bool m_loopback; - - RegistrationCallbackT m_callback_pub; - RegistrationCallbackT m_callback_sub; - RegistrationCallbackT m_callback_service; - RegistrationCallbackT m_callback_client; - RegistrationCallbackT m_callback_process; std::unique_ptr m_registration_receiver_udp; #if ECAL_CORE_REGISTRATION_SHM @@ -92,9 +80,12 @@ namespace eCAL bool m_use_registration_udp; bool m_use_registration_shm; - std::mutex m_callback_custom_apply_sample_map_mtx; - std::map m_callback_custom_apply_sample_map; + // This class distributes samples to all everyone who is interested in being notified about samples + Registration::CSampleApplier m_sample_applier; - std::string m_host_group_name; + // These classes are interested in being notified about samples + // Possibly remove these from this class + // The custom user callbacks (who receive serialized samples), e.g. registration events. + Registration::CSampleApplierUser m_user_applier; }; } diff --git a/ecal/core/src/registration/ecal_registration_sample_applier.cpp b/ecal/core/src/registration/ecal_registration_sample_applier.cpp new file mode 100644 index 0000000000..41b7a4aa17 --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sample_applier.cpp @@ -0,0 +1,131 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2024 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +#include "registration/ecal_registration_sample_applier.h" + +namespace eCAL +{ + namespace Registration + { + ////////////////////////////////////////////////////////////////// + // CSampleApplier + ////////////////////////////////////////////////////////////////// + CSampleApplier::CSampleApplier(bool network, bool loopback, const std::string& host_group_name, uint32_t pid) + : + m_network(network), + m_loopback(loopback), + m_host_group_name(host_group_name), + m_pid(pid) + { + } + + void CSampleApplier::EnableLoopback(bool state_) + { + m_loopback = state_; + } + + bool CSampleApplier::ApplySample(const Registration::Sample& sample_) + { + if (!AcceptRegistrationSample(sample_)) + { + Logging::Log(log_level_debug1, "CSampleApplier::ApplySample : Incoming sample discarded"); + return false; + } + + // forward all registration samples to outside "customer" (e.g. monitoring, descgate, pub/subgate/client/service gates) + { + const std::lock_guard lock(m_callback_custom_apply_sample_map_mtx); + for (const auto& iter : m_callback_custom_apply_sample_map) + { + iter.second(sample_); + } + } + return true; + } + + bool CSampleApplier::IsHostGroupMember(const Registration::Sample& sample_) + { + std::string host_group_name; + std::string host_name; + switch (sample_.cmd_type) + { + case bct_reg_publisher: + case bct_unreg_publisher: + case bct_reg_subscriber: + case bct_unreg_subscriber: + host_group_name = sample_.topic.hgname; + host_name = sample_.topic.hname; + break; + case bct_reg_service: + case bct_unreg_service: + //host_group_name = sample_.service.hgname; // TODO: we need to add hgname attribute to services + host_name = sample_.service.hname; + break; + case bct_reg_client: + case bct_unreg_client: + //host_group_name = sample_.client.hgname; // TODO: we need to add hgname attribute to clients + host_name = sample_.client.hname; + break; + default: + break; + } + + const std::string& sample_host_group_name = host_group_name.empty() ? host_name : host_group_name; + + if (sample_host_group_name.empty() || m_host_group_name.empty()) + return false; + if (sample_host_group_name != m_host_group_name) + return false; + + return true; + } + + bool CSampleApplier::AcceptRegistrationSample(const Registration::Sample& sample_) + { + // check if the sample is from the same host group + if (IsHostGroupMember(sample_)) + { + // register if the sample is from another process + // or if loopback mode is enabled + return m_loopback || (sample_.topic.pid != m_pid); + } + else + { + // if the sample is from an external host, register only if network mode is enabled + return m_network; + } + } + + void CSampleApplier::SetCustomApplySampleCallback(const std::string& customer_, const ApplySampleCallbackT& callback_) + { + const std::lock_guard lock(m_callback_custom_apply_sample_map_mtx); + m_callback_custom_apply_sample_map[customer_] = callback_; + } + + void CSampleApplier::RemCustomApplySampleCallback(const std::string& customer_) + { + const std::lock_guard lock(m_callback_custom_apply_sample_map_mtx); + auto iter = m_callback_custom_apply_sample_map.find(customer_); + if (iter != m_callback_custom_apply_sample_map.end()) + { + m_callback_custom_apply_sample_map.erase(iter); + } + } + } +} \ No newline at end of file diff --git a/ecal/core/src/registration/ecal_registration_sample_applier.h b/ecal/core/src/registration/ecal_registration_sample_applier.h new file mode 100644 index 0000000000..354f9c982c --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sample_applier.h @@ -0,0 +1,70 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2024 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +/** + * @brief eCAL Sample Applier + * + * This class applies incoming samples to everyone who is interested. +**/ + +#pragma once + +#include +#include + +#include "serialization/ecal_struct_sample_registration.h" + +#include +#include +#include +#include + +namespace eCAL +{ + namespace Registration + { + class CSampleApplier + { + public: + // to be replaced by config version soon! + CSampleApplier(bool network, bool loopback, const std::string& host_group_name, uint32_t pid); + + // to be removed for eCAL 6, but keep until eCAL 5.14 + void EnableLoopback(bool state_); + bool ApplySample(const Registration::Sample& sample_); + + using ApplySampleCallbackT = std::function; + void SetCustomApplySampleCallback(const std::string& customer_, const ApplySampleCallbackT& callback_); + void RemCustomApplySampleCallback(const std::string& customer_); + + private: + bool IsHostGroupMember(const eCAL::Registration::Sample& sample_); + bool AcceptRegistrationSample(const Registration::Sample& sample_); + + bool m_network; + bool m_loopback; + std::string m_host_group_name; + int32_t m_pid; + + std::mutex m_callback_custom_apply_sample_map_mtx; + // We need to check the performance now. Unlike before the pub / subgates also go through the map + std::map m_callback_custom_apply_sample_map; + }; + } +} diff --git a/ecal/core/src/registration/ecal_registration_sample_applier_gates.cpp b/ecal/core/src/registration/ecal_registration_sample_applier_gates.cpp new file mode 100644 index 0000000000..e761409695 --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sample_applier_gates.cpp @@ -0,0 +1,74 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2024 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +#include "registration/ecal_registration_sample_applier_gates.h" +#include "ecal_global_accessors.h" +#include "pubsub/ecal_subgate.h" +#include "pubsub/ecal_pubgate.h" +#include "service/ecal_clientgate.h" + +#include + +namespace eCAL +{ + namespace Registration + { + void CSampleApplierGates::ApplySample(const eCAL::Registration::Sample& sample_) + { + switch (sample_.cmd_type) + { + case bct_none: + case bct_set_sample: + case bct_reg_process: + case bct_unreg_process: + break; +#if ECAL_CORE_SERVICE + case bct_reg_service: + if (g_clientgate() != nullptr) g_clientgate()->ApplyServiceRegistration(sample_); + break; +#endif + case bct_unreg_service: + break; + case bct_reg_client: + case bct_unreg_client: + // current client implementation doesn't need that information + break; +#if ECAL_CORE_PUBLISHER + case bct_reg_subscriber: + if (g_pubgate() != nullptr) g_pubgate()->ApplySubRegistration(sample_); + break; + case bct_unreg_subscriber: + if (g_pubgate() != nullptr) g_pubgate()->ApplySubUnregistration(sample_); + break; +#endif +#if ECAL_CORE_SUBSCRIBER + case bct_reg_publisher: + if (g_subgate() != nullptr) g_subgate()->ApplyPubRegistration(sample_); + break; + case bct_unreg_publisher: + if (g_subgate() != nullptr) g_subgate()->ApplyPubUnregistration(sample_); + break; +#endif + default: + Logging::Log(log_level_debug1, "CGatesApplier::ApplySample : unknown sample type"); + break; + } + } + } +} \ No newline at end of file diff --git a/ecal/core/src/registration/ecal_registration_sample_applier_gates.h b/ecal/core/src/registration/ecal_registration_sample_applier_gates.h new file mode 100644 index 0000000000..c2bf8bf7f3 --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sample_applier_gates.h @@ -0,0 +1,40 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2024 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +/** + * @brief eCAL Sample Applier Gates + * + * This class applies incoming samples to the registration gates +**/ + +#pragma once + +#include "serialization/ecal_struct_sample_registration.h" + +namespace eCAL +{ + namespace Registration + { + class CSampleApplierGates + { + public: + static void ApplySample(const eCAL::Registration::Sample& sample_); + }; + } +} diff --git a/ecal/core/src/registration/ecal_registration_sample_applier_user.cpp b/ecal/core/src/registration/ecal_registration_sample_applier_user.cpp new file mode 100644 index 0000000000..0edae0f70e --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sample_applier_user.cpp @@ -0,0 +1,120 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2024 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +#include "registration/ecal_registration_sample_applier_user.h" +#include "serialization/ecal_serialize_sample_registration.h" + +namespace eCAL +{ + namespace Registration + { + bool CSampleApplierUser::AddRegistrationCallback(eCAL_Registration_Event event_, const RegistrationCallbackT& callback_) + { + switch (event_) + { + case reg_event_publisher: + m_callback_pub = callback_; + return true; + case reg_event_subscriber: + m_callback_sub = callback_; + return true; + case reg_event_service: + m_callback_service = callback_; + return true; + case reg_event_client: + m_callback_client = callback_; + return true; + case reg_event_process: + m_callback_process = callback_; + return true; + default: + return false; + } + } + + bool CSampleApplierUser::RemRegistrationCallback(eCAL_Registration_Event event_) + { + switch (event_) + { + case reg_event_publisher: + m_callback_pub = nullptr; + return true; + case reg_event_subscriber: + m_callback_sub = nullptr; + return true; + case reg_event_service: + m_callback_service = nullptr; + return true; + case reg_event_client: + m_callback_client = nullptr; + return true; + case reg_event_process: + m_callback_process = nullptr; + return true; + default: + return false; + } + } + + void CSampleApplierUser::ApplySample(const eCAL::Registration::Sample& sample_) + { + RegistrationCallbackT reg_callback(nullptr); + switch (sample_.cmd_type) + { + case bct_none: + case bct_set_sample: + break; + case bct_reg_process: + case bct_unreg_process: + // unregistration event not implemented currently + reg_callback = m_callback_process; + break; + case bct_reg_service: + case bct_unreg_service: + reg_callback = m_callback_service; + break; + case bct_reg_client: + case bct_unreg_client: + // current client implementation doesn't need that information + reg_callback = m_callback_client; + break; + case bct_reg_subscriber: + case bct_unreg_subscriber: + reg_callback = m_callback_sub; + break; + case bct_reg_publisher: + case bct_unreg_publisher: + reg_callback = m_callback_pub; + break; + default: + break; + } + + // call user registration callback + if (reg_callback) + { + std::string reg_sample; + if (SerializeToBuffer(sample_, reg_sample)) + { + reg_callback(reg_sample.c_str(), static_cast(reg_sample.size())); + } + } + } + } +} \ No newline at end of file diff --git a/ecal/core/src/registration/ecal_registration_sample_applier_user.h b/ecal/core/src/registration/ecal_registration_sample_applier_user.h new file mode 100644 index 0000000000..f6fcd1f07d --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sample_applier_user.h @@ -0,0 +1,57 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2024 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +/** + * @brief eCAL registration receiver + * + * Receives registration information from the sample applier and forwards them to + * user defined callbacks. +**/ + +#pragma once + +#include +#include + +#include "serialization/ecal_struct_sample_registration.h" + +namespace eCAL +{ + namespace Registration + { + class CSampleApplierUser + { + public: + bool AddRegistrationCallback(enum eCAL_Registration_Event event_, const RegistrationCallbackT& callback_); + bool RemRegistrationCallback(enum eCAL_Registration_Event event_); + + void ApplySample(const eCAL::Registration::Sample& sample_); + + private: + // in the future this may be stored in a map? or somehow differently + RegistrationCallbackT m_callback_pub = nullptr; + RegistrationCallbackT m_callback_sub = nullptr; + RegistrationCallbackT m_callback_service = nullptr; + RegistrationCallbackT m_callback_client = nullptr; + RegistrationCallbackT m_callback_process = nullptr; + + // protect by mutexes? very likeley need to! + }; + } +}