diff --git a/ecal/core/CMakeLists.txt b/ecal/core/CMakeLists.txt index 699efdd52c..558419d689 100644 --- a/ecal/core/CMakeLists.txt +++ b/ecal/core/CMakeLists.txt @@ -278,15 +278,22 @@ endif() ###################################### if (ECAL_CORE_REGISTRATION) set(ecal_registration_src + src/registration/ecal_process_registration.cpp + src/registration/ecal_process_registration.h src/registration/ecal_registration_provider.cpp src/registration/ecal_registration_provider.h src/registration/ecal_registration_receiver.cpp src/registration/ecal_registration_receiver.h + src/registration/ecal_registration_sender.h + src/registration/ecal_registration_sender_udp.cpp + src/registration/ecal_registration_sender_udp.h ) if(ECAL_CORE_REGISTRATION_SHM) list(APPEND ecal_registration_src src/registration/ecal_registration_receiver_shm.cpp src/registration/ecal_registration_receiver_shm.h + src/registration/ecal_registration_sender_shm.cpp + src/registration/ecal_registration_sender_shm.h src/registration/shm/ecal_memfile_broadcast.cpp src/registration/shm/ecal_memfile_broadcast.h src/registration/shm/ecal_memfile_broadcast_reader.cpp diff --git a/ecal/core/src/registration/ecal_process_registration.cpp b/ecal/core/src/registration/ecal_process_registration.cpp new file mode 100644 index 0000000000..18ca56aa50 --- /dev/null +++ b/ecal/core/src/registration/ecal_process_registration.cpp @@ -0,0 +1,104 @@ +/* ========================= 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 Functions to generate Process Registration / Unregistration samples. + * +**/ + +#include "ecal_process_registration.h" + +#include +#include +#include "ecal_global_accessors.h" +#include "ecal_globals.h" +#include "time/ecal_timegate.h" + +eCAL::Registration::Sample eCAL::Registration::GetProcessRegisterSample() +{ + Registration::Sample process_sample; + process_sample.cmd_type = bct_reg_process; + auto& process_sample_process = process_sample.process; + process_sample_process.hname = eCAL::Process::GetHostName(); + process_sample_process.hgname = eCAL::Process::GetHostGroupName(); + process_sample_process.pid = eCAL::Process::GetProcessID(); + process_sample_process.pname = eCAL::Process::GetProcessName(); + process_sample_process.uname = eCAL::Process::GetUnitName(); + process_sample_process.pparam = eCAL::Process::GetProcessParameter(); + process_sample_process.state.severity = static_cast(g_process_severity); + process_sample_process.state.severity_level = static_cast(g_process_severity_level); + process_sample_process.state.info = g_process_info; +#if ECAL_CORE_TIMEPLUGIN + if (g_timegate() == nullptr) + { + process_sample_process.tsync_state = Registration::eTSyncState::tsync_none; + } + else + { + if (!g_timegate()->IsSynchronized()) + { + process_sample_process.tsync_state = Registration::eTSyncState::tsync_none; + } + else + { + switch (g_timegate()->GetSyncMode()) + { + case CTimeGate::eTimeSyncMode::realtime: + process_sample_process.tsync_state = Registration::eTSyncState::tsync_realtime; + break; + case CTimeGate::eTimeSyncMode::replay: + process_sample_process.tsync_state = Registration::eTSyncState::tsync_replay; + break; + default: + process_sample_process.tsync_state = Registration::eTSyncState::tsync_none; + break; + } + } + process_sample_process.tsync_mod_name = g_timegate()->GetName(); + } +#endif + + // eCAL initialization state + const unsigned int comp_state(g_globals()->GetComponents()); + process_sample_process.component_init_state = static_cast(comp_state); + std::string component_info; + if ((comp_state & eCAL::Init::Publisher) != 0u) component_info += "|pub"; + if ((comp_state & eCAL::Init::Subscriber) != 0u) component_info += "|sub"; + if ((comp_state & eCAL::Init::Logging) != 0u) component_info += "|log"; + if ((comp_state & eCAL::Init::TimeSync) != 0u) component_info += "|time"; + if (!component_info.empty()) component_info = component_info.substr(1); + process_sample_process.component_init_info = component_info; + + process_sample_process.ecal_runtime_version = eCAL::GetVersionString(); + + return process_sample; +} + +eCAL::Registration::Sample eCAL::Registration::GetProcessUnregisterSample() +{ + Registration::Sample process_sample; + process_sample.cmd_type = bct_unreg_process; + auto& process_sample_process = process_sample.process; + process_sample_process.hname = eCAL::Process::GetHostName(); + process_sample_process.pid = eCAL::Process::GetProcessID(); + process_sample_process.pname = eCAL::Process::GetProcessName(); + process_sample_process.uname = eCAL::Process::GetUnitName(); + + return process_sample; +} diff --git a/ecal/core/src/registration/ecal_process_registration.h b/ecal/core/src/registration/ecal_process_registration.h new file mode 100644 index 0000000000..e5961f5f82 --- /dev/null +++ b/ecal/core/src/registration/ecal_process_registration.h @@ -0,0 +1,37 @@ +/* ========================= 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 Functions to generate Process Registration / Unregistration samples. + * +**/ + +#pragma once + +#include "serialization/ecal_struct_sample_registration.h" + +namespace eCAL +{ + namespace Registration + { + Sample GetProcessRegisterSample(); + + Sample GetProcessUnregisterSample(); + } +} \ No newline at end of file diff --git a/ecal/core/src/registration/ecal_registration_provider.cpp b/ecal/core/src/registration/ecal_registration_provider.cpp index 15b836da8c..363ede8fde 100644 --- a/ecal/core/src/registration/ecal_registration_provider.cpp +++ b/ecal/core/src/registration/ecal_registration_provider.cpp @@ -25,17 +25,9 @@ * These information will be send cyclic (registration refresh) via UDP to external eCAL processes. * **/ - -#include -#include - -#include "ecal_def.h" -#include "ecal_globals.h" #include "ecal_registration_provider.h" -#include "io/udp/ecal_udp_configurations.h" -#include "io/udp/ecal_udp_sample_sender.h" - +#include #include #include #include @@ -43,6 +35,16 @@ #include #include +#include +#include +#include "ecal_def.h" + +#include +#include +#if ECAL_CORE_REGISTRATION_SHM +#include +#endif + namespace eCAL { std::atomic CRegistrationProvider::m_created; @@ -66,27 +68,17 @@ namespace eCAL m_use_registration_shm = Config::IsShmRegistrationEnabled(); m_use_registration_udp = !m_use_registration_shm; - if (m_use_registration_udp) - { - // set network attributes - eCAL::UDP::SSenderAttr attr; - attr.address = UDP::GetRegistrationAddress(); - attr.port = UDP::GetRegistrationPort(); - attr.ttl = UDP::GetMulticastTtl(); - attr.broadcast = UDP::IsBroadcast(); - attr.loopback = true; - attr.sndbuf = Config::GetUdpMulticastSndBufSizeBytes(); - - // create udp registration sender - m_reg_sample_snd = std::make_shared(attr); - } - + // TODO Create the registration sender #if ECAL_CORE_REGISTRATION_SHM if (m_use_registration_shm) { - std::cout << "Shared memory monitoring is enabled (domain: " << Config::Experimental::GetShmMonitoringDomain() << " - queue size: " << Config::Experimental::GetShmMonitoringQueueSize() << ")" << '\n'; - m_memfile_broadcast.Create(Config::Experimental::GetShmMonitoringDomain(), Config::Experimental::GetShmMonitoringQueueSize()); - m_memfile_broadcast_writer.Bind(&m_memfile_broadcast); + m_reg_sender = std::make_unique(); + } + else + { +#endif + m_reg_sender = std::make_unique(); +#if ECAL_CORE_REGISTRATION_SHM } #endif @@ -105,28 +97,11 @@ namespace eCAL m_reg_sample_snd_thread->stop(); // add process unregistration sample - AddSample2SampleList(GetProcessUnregisterSample()); + AddSample2SampleList(Registration::GetProcessUnregisterSample()); - if (m_use_registration_udp) - { - // send process unregistration sample over udp - SendSampleList2UDP(); + SendSampleList(); - // destroy udp registration sample sender - m_reg_sample_snd.reset(); - } - -#if ECAL_CORE_REGISTRATION_SHM - if (m_use_registration_shm) - { - // broadcast process unregistration sample over shm - SendSampleList2SHM(); - - // destroy shm registration sample writer - m_memfile_broadcast_writer.Unbind(); - m_memfile_broadcast.Destroy(); - } -#endif + m_reg_sender.reset(); m_created = false; } @@ -150,12 +125,14 @@ namespace eCAL // if registration is forced if (force_) { + SendSampleList(); + // send single registration sample over udp - SendSample2UDP(sample_); + //SendSample2UDP(sample_); #if ECAL_CORE_REGISTRATION_SHM // broadcast (updated) sample list over shm - SendSampleList2SHM(); + //SendSampleList2SHM(); #endif } @@ -184,72 +161,6 @@ namespace eCAL m_sample_list.samples.push_back(sample_); } - bool CRegistrationProvider::SendSample2UDP(const Registration::Sample& sample_) - { - if (!m_created) return(false); - - if (m_use_registration_udp && m_reg_sample_snd) - { - // lock sample buffer - const std::lock_guard lock(m_sample_buffer_mtx); - - // serialize single sample - if (SerializeToBuffer(sample_, m_sample_buffer)) - { - // send single sample over udp - return m_reg_sample_snd->Send("reg_sample", m_sample_buffer) != 0; - } - } - return(false); - } - - bool CRegistrationProvider::SendSampleList2UDP() - { - if (!m_created) return(false); - bool return_value{ true }; - - // lock sample list - const std::lock_guard lock(m_sample_list_mtx); - - // send all (single) samples over udp - if (m_use_registration_udp && m_reg_sample_snd) - { - for (const auto& sample : m_sample_list.samples) - { - return_value &= SendSample2UDP(sample); - } - } - - return return_value; - } - -#if ECAL_CORE_REGISTRATION_SHM - bool CRegistrationProvider::SendSampleList2SHM() - { - if (!m_created) return(false); - - bool return_value{ true }; - - // send sample list over shm - if (m_use_registration_shm) - { - // lock sample list - const std::lock_guard lock(m_sample_list_mtx); - - // serialize whole sample list - if (SerializeToBuffer(m_sample_list, m_sample_list_buffer)) - { - if (!m_sample_list_buffer.empty()) - { - // broadcast sample list over shm - return_value &= m_memfile_broadcast_writer.Write(m_sample_list_buffer.data(), m_sample_list_buffer.size()); - } - } - } - return return_value; - } -#endif - void CRegistrationProvider::ClearSampleList() { // lock sample list @@ -258,6 +169,12 @@ namespace eCAL m_sample_list.samples.clear(); } + void CRegistrationProvider::SendSampleList() + { + std::lock_guard lock(m_sample_list_mtx); + m_reg_sender->SendSampleList(m_sample_list); + } + void CRegistrationProvider::RegisterSendThread() { #if ECAL_CORE_SUBSCRIBER @@ -278,91 +195,12 @@ namespace eCAL if (g_clientgate() != nullptr) g_clientgate()->RefreshRegistrations(); #endif - // send out sample list over udp - SendSampleList2UDP(); - -#if ECAL_CORE_REGISTRATION_SHM - // broadcast sample list over shm - SendSampleList2SHM(); -#endif + SendSampleList(); // clear registration sample list ClearSampleList(); // add process registration sample to internal sample list as first sample (for next registration loop) - AddSample2SampleList(GetProcessRegisterSample()); - } - - Registration::Sample CRegistrationProvider::GetProcessRegisterSample() - { - Registration::Sample process_sample; - process_sample.cmd_type = bct_reg_process; - auto& process_sample_process = process_sample.process; - process_sample_process.hname = Process::GetHostName(); - process_sample_process.hgname = Process::GetHostGroupName(); - process_sample_process.pid = Process::GetProcessID(); - process_sample_process.pname = Process::GetProcessName(); - process_sample_process.uname = Process::GetUnitName(); - process_sample_process.pparam = Process::GetProcessParameter(); - process_sample_process.state.severity = static_cast(g_process_severity); - process_sample_process.state.severity_level = static_cast(g_process_severity_level); - process_sample_process.state.info = g_process_info; -#if ECAL_CORE_TIMEPLUGIN - if (g_timegate() == nullptr) - { - process_sample_process.tsync_state = Registration::eTSyncState::tsync_none; - } - else - { - if (!g_timegate()->IsSynchronized()) - { - process_sample_process.tsync_state = Registration::eTSyncState::tsync_none; - } - else - { - switch (g_timegate()->GetSyncMode()) - { - case CTimeGate::eTimeSyncMode::realtime: - process_sample_process.tsync_state = Registration::eTSyncState::tsync_realtime; - break; - case CTimeGate::eTimeSyncMode::replay: - process_sample_process.tsync_state = Registration::eTSyncState::tsync_replay; - break; - default: - process_sample_process.tsync_state = Registration::eTSyncState::tsync_none; - break; - } - } - process_sample_process.tsync_mod_name = g_timegate()->GetName(); - } -#endif - - // eCAL initialization state - const unsigned int comp_state(g_globals()->GetComponents()); - process_sample_process.component_init_state = static_cast(comp_state); - std::string component_info; - if ((comp_state & Init::Publisher) != 0u) component_info += "|pub"; - if ((comp_state & Init::Subscriber) != 0u) component_info += "|sub"; - if ((comp_state & Init::Logging) != 0u) component_info += "|log"; - if ((comp_state & Init::TimeSync) != 0u) component_info += "|time"; - if (!component_info.empty()) component_info = component_info.substr(1); - process_sample_process.component_init_info = component_info; - - process_sample_process.ecal_runtime_version = GetVersionString(); - - return process_sample; - } - - Registration::Sample CRegistrationProvider::GetProcessUnregisterSample() - { - Registration::Sample process_sample; - process_sample.cmd_type = bct_unreg_process; - auto& process_sample_process = process_sample.process; - process_sample_process.hname = Process::GetHostName(); - process_sample_process.pid = Process::GetProcessID(); - process_sample_process.pname = Process::GetProcessName(); - process_sample_process.uname = Process::GetUnitName(); - - return process_sample; + AddSample2SampleList(Registration::GetProcessRegisterSample()); } } diff --git a/ecal/core/src/registration/ecal_registration_provider.h b/ecal/core/src/registration/ecal_registration_provider.h index 098a9ef2da..422e65277e 100644 --- a/ecal/core/src/registration/ecal_registration_provider.h +++ b/ecal/core/src/registration/ecal_registration_provider.h @@ -30,21 +30,15 @@ #include "io/udp/ecal_udp_sample_sender.h" -#include "util/ecal_thread.h" - -#if ECAL_CORE_REGISTRATION_SHM -#include "shm/ecal_memfile_broadcast.h" -#include "shm/ecal_memfile_broadcast_writer.h" -#endif - -#include "serialization/ecal_serialize_sample_registration.h" - #include #include #include #include #include +#include +#include "util/ecal_thread.h" + namespace eCAL { class CRegistrationProvider @@ -64,40 +58,24 @@ namespace eCAL protected: void AddSample2SampleList(const Registration::Sample& sample_); - bool SendSample2UDP(const Registration::Sample& sample_); - - bool SendSampleList2UDP(); -#if ECAL_CORE_REGISTRATION_SHM - bool SendSampleList2SHM(); -#endif void ClearSampleList(); + void SendSampleList(); void RegisterSendThread(); - Registration::Sample GetProcessRegisterSample(); - Registration::Sample GetProcessUnregisterSample(); - - static std::atomic m_created; - - std::shared_ptr m_reg_sample_snd; - std::shared_ptr m_reg_sample_snd_thread; + static std::atomic m_created; - std::mutex m_sample_buffer_mtx; - std::vector m_sample_buffer; + std::unique_ptr m_reg_sender; - std::mutex m_sample_list_mtx; - Registration::SampleList m_sample_list; + std::shared_ptr m_reg_sample_snd_thread; -#if ECAL_CORE_REGISTRATION_SHM - std::vector m_sample_list_buffer; - CMemoryFileBroadcast m_memfile_broadcast; - CMemoryFileBroadcastWriter m_memfile_broadcast_writer; -#endif + std::mutex m_sample_list_mtx; + Registration::SampleList m_sample_list; - bool m_use_registration_udp; - bool m_use_registration_shm; + bool m_use_registration_udp; + bool m_use_registration_shm; - std::mutex m_callback_custom_apply_sample_map_mtx; + std::mutex m_callback_custom_apply_sample_map_mtx; std::map m_callback_custom_apply_sample_map; }; } diff --git a/ecal/core/src/registration/ecal_registration_sender.h b/ecal/core/src/registration/ecal_registration_sender.h new file mode 100644 index 0000000000..4f9d6e504f --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sender.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 provider + * + * All process internal publisher/subscriber, server/clients register here with all their attributes. + * + * These information will be send cyclic (registration refresh) via UDP to external eCAL processes. + * +**/ + +#pragma once + +#include "serialization/ecal_struct_sample_registration.h" + + +namespace eCAL +{ + class CRegistrationSender + { + public: + CRegistrationSender() = default; + virtual ~CRegistrationSender() = default; + + // Copy constructor + CRegistrationSender(const CRegistrationSender& other) = delete; + + // Copy assignment operator + CRegistrationSender& operator=(const CRegistrationSender & other) = delete; + + // Move constructor + CRegistrationSender(CRegistrationSender && other) noexcept = delete; + + // Move assignment operator + CRegistrationSender& operator=(CRegistrationSender && other) noexcept = delete; + + //virtual bool SendSample(const Registration::Sample& sample_) = 0; + virtual bool SendSampleList(const Registration::SampleList& sample_list) = 0; + }; +} diff --git a/ecal/core/src/registration/ecal_registration_sender_shm.cpp b/ecal/core/src/registration/ecal_registration_sender_shm.cpp new file mode 100644 index 0000000000..ddde350b36 --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sender_shm.cpp @@ -0,0 +1,66 @@ +/* ========================= 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 provider + * + * All process internal publisher/subscriber, server/clients register here with all their attributes. + * + * These information will be send cyclic (registration refresh) via UDP to external eCAL processes. + * +**/ + +#include "registration/ecal_registration_sender_shm.h" +#include "serialization/ecal_serialize_sample_registration.h" + + +eCAL::CRegistrationSenderSHM::CRegistrationSenderSHM() +{ + std::cout << "Shared memory monitoring is enabled (domain: " << Config::Experimental::GetShmMonitoringDomain() << " - queue size: " << Config::Experimental::GetShmMonitoringQueueSize() << ")" << '\n'; + m_memfile_broadcast.Create(Config::Experimental::GetShmMonitoringDomain(), Config::Experimental::GetShmMonitoringQueueSize()); + m_memfile_broadcast_writer.Bind(&m_memfile_broadcast); +} + +eCAL::CRegistrationSenderSHM::~CRegistrationSenderSHM() +{ + m_memfile_broadcast_writer.Unbind(); + m_memfile_broadcast.Destroy(); +} + +/* +bool eCAL::CRegistrationSenderSHM::SendSample(const Registration::Sample& sample_) +{ + return false; +} +*/ + +bool eCAL::CRegistrationSenderSHM::SendSampleList(const Registration::SampleList& sample_list) +{ + bool return_value{true}; + // serialize whole sample list + if (SerializeToBuffer(sample_list, m_sample_list_buffer)) + { + if (!m_sample_list_buffer.empty()) + { + // broadcast sample list over shm + return_value &= m_memfile_broadcast_writer.Write(m_sample_list_buffer.data(), m_sample_list_buffer.size()); + } + } + return return_value; +} diff --git a/ecal/core/src/registration/ecal_registration_sender_shm.h b/ecal/core/src/registration/ecal_registration_sender_shm.h new file mode 100644 index 0000000000..266e644c89 --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sender_shm.h @@ -0,0 +1,58 @@ +/* ========================= 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 provider + * + * All process internal publisher/subscriber, server/clients register here with all their attributes. + * + * These information will be send cyclic (registration refresh) via UDP to external eCAL processes. + * +**/ + +#pragma once + +#include "registration/ecal_registration_sender.h" + +#include "shm/ecal_memfile_broadcast.h" +#include "shm/ecal_memfile_broadcast_writer.h" + +namespace eCAL +{ + class CRegistrationSenderSHM : public CRegistrationSender + { + public: + CRegistrationSenderSHM(); + ~CRegistrationSenderSHM() override; + + // Special member functionss + CRegistrationSenderSHM(const CRegistrationSenderSHM& other) = delete; + CRegistrationSenderSHM& operator=(const CRegistrationSenderSHM& other) = delete; + CRegistrationSenderSHM(CRegistrationSenderSHM&& other) noexcept = delete; + CRegistrationSenderSHM& operator=(CRegistrationSenderSHM&& other) noexcept = delete; + + //bool SendSample(const Registration::Sample& sample_) override; + bool SendSampleList(const Registration::SampleList& sample_list) override; + + private: + std::vector m_sample_list_buffer; + CMemoryFileBroadcast m_memfile_broadcast; + CMemoryFileBroadcastWriter m_memfile_broadcast_writer; + }; +} \ No newline at end of file diff --git a/ecal/core/src/registration/ecal_registration_sender_udp.cpp b/ecal/core/src/registration/ecal_registration_sender_udp.cpp new file mode 100644 index 0000000000..0991483240 --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sender_udp.cpp @@ -0,0 +1,81 @@ +/* ========================= 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 provider + * + * All process internal publisher/subscriber, server/clients register here with all their attributes. + * + * These information will be send cyclic (registration refresh) via UDP to external eCAL processes. + * +**/ + +#include "registration/ecal_registration_sender_udp.h" + +#include "serialization/ecal_serialize_sample_registration.h" +#include "io/udp/ecal_udp_configurations.h" +#include + +namespace +{ + using namespace eCAL; + UDP::SSenderAttr CreateAttributes() + { + eCAL::UDP::SSenderAttr attr; + attr.address = UDP::GetRegistrationAddress(); + attr.port = UDP::GetRegistrationPort(); + attr.ttl = UDP::GetMulticastTtl(); + attr.broadcast = UDP::IsBroadcast(); + attr.loopback = true; + attr.sndbuf = Config::GetUdpMulticastSndBufSizeBytes(); + return attr; + } + +} + +namespace eCAL +{ + CRegistrationSenderUDP::CRegistrationSenderUDP() + : m_reg_sample_snd(CreateAttributes()) + { + } + + CRegistrationSenderUDP::~CRegistrationSenderUDP() = default; + + bool CRegistrationSenderUDP::SendSample(const Registration::Sample& sample_) + { + // serialize single sample + if (SerializeToBuffer(sample_, m_sample_buffer)) + { + // send single sample over udp + return m_reg_sample_snd.Send("reg_sample", m_sample_buffer) != 0; + } + return false; + } + + bool CRegistrationSenderUDP::SendSampleList(const Registration::SampleList& sample_list) + { + bool return_value{ true }; + for (const auto& sample : sample_list.samples) + { + return_value &= SendSample(sample); + } + return return_value; + } +} \ No newline at end of file diff --git a/ecal/core/src/registration/ecal_registration_sender_udp.h b/ecal/core/src/registration/ecal_registration_sender_udp.h new file mode 100644 index 0000000000..65967cd1f9 --- /dev/null +++ b/ecal/core/src/registration/ecal_registration_sender_udp.h @@ -0,0 +1,55 @@ +/* ========================= 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 sender UDP + * + * A device which sends out registration information via UDP + * +**/ + +#pragma once + +#include "registration/ecal_registration_sender.h" + +#include "io/udp/ecal_udp_sample_sender.h" + +namespace eCAL +{ + class CRegistrationSenderUDP : public CRegistrationSender + { + public: + CRegistrationSenderUDP(); + ~CRegistrationSenderUDP() override; + + // Special member functionss + CRegistrationSenderUDP(const CRegistrationSenderUDP& other) = delete; + CRegistrationSenderUDP& operator=(const CRegistrationSenderUDP& other) = delete; + CRegistrationSenderUDP(CRegistrationSenderUDP&& other) noexcept = delete; + CRegistrationSenderUDP& operator=(CRegistrationSenderUDP&& other) noexcept = delete; + + bool SendSampleList(const Registration::SampleList& sample_list) override; + + private: + bool SendSample(const Registration::Sample& sample_); + + UDP::CSampleSender m_reg_sample_snd; + std::vector m_sample_buffer; + }; +} \ No newline at end of file