Skip to content

Commit

Permalink
[core] refactor registration receiver.
Browse files Browse the repository at this point in the history
  • Loading branch information
KerstinKeller committed Jul 12, 2024
1 parent 4d67a9f commit 10d21f3
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 30 deletions.
30 changes: 5 additions & 25 deletions ecal/core/src/registration/ecal_registration_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*
**/

#include "ecal_registration_receiver.h"
#include "registration/ecal_registration_receiver.h"

#include "registration/ecal_registration_receiver_udp.h"
#include "ecal_global_accessors.h"

#include "pubsub/ecal_subgate.h"
Expand Down Expand Up @@ -75,16 +77,7 @@ namespace eCAL

if (m_use_registration_udp)
{
// set network attributes
eCAL::UDP::SReceiverAttr attr;
attr.address = UDP::GetRegistrationAddress();
attr.port = UDP::GetRegistrationPort();
attr.broadcast = UDP::IsBroadcast();
attr.loopback = true;
attr.rcvbuf = Config::GetUdpMulticastRcvBufSizeBytes();

// start registration sample receiver
m_registration_receiver = std::make_shared<UDP::CSampleReceiver>(attr, std::bind(&CRegistrationReceiver::HasSample, this, std::placeholders::_1), std::bind(&CRegistrationReceiver::ApplySerializedSample, this, std::placeholders::_1, std::placeholders::_2));
m_registration_receiver_udp = std::make_unique<CRegistrationReceiverUDP>([this](const Registration::Sample& sample_) {return this->ApplySample(sample_); });
}

#if ECAL_CORE_REGISTRATION_SHM
Expand All @@ -105,13 +98,10 @@ namespace eCAL
{
if(!m_created) return;

// stop network registration receive thread
m_registration_receiver = nullptr;

// stop network registration receive thread
if (m_use_registration_udp)
{
m_registration_receiver = nullptr;
m_registration_receiver_udp = nullptr;
}

#if ECAL_CORE_REGISTRATION_SHM
Expand Down Expand Up @@ -139,16 +129,6 @@ namespace eCAL
m_loopback = state_;
}

bool CRegistrationReceiver::ApplySerializedSample(const char* serialized_sample_data_, size_t serialized_sample_size_)
{
if(!m_created) return false;

Registration::Sample sample;
if (!DeserializeFromBuffer(serialized_sample_data_, serialized_sample_size_, sample)) return false;

return ApplySample(sample);
}

bool CRegistrationReceiver::ApplySample(const Registration::Sample& sample_)
{
if (!m_created) return false;
Expand Down
9 changes: 4 additions & 5 deletions ecal/core/src/registration/ecal_registration_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <cstddef>
#include <ecal/ecal.h>

#include "io/udp/ecal_udp_sample_receiver.h"
#include "serialization/ecal_struct_sample_registration.h"

#if ECAL_CORE_REGISTRATION_SHM
Expand All @@ -47,6 +46,8 @@

namespace eCAL
{
class CRegistrationReceiverUDP;

class CRegistrationReceiver
{
public:
Expand All @@ -69,8 +70,6 @@ namespace eCAL
void RemCustomApplySampleCallback(const std::string& customer_);

protected:
bool ApplySerializedSample(const char* serialized_sample_data_, size_t serialized_sample_size_);

void ApplySubscriberRegistration(const eCAL::Registration::Sample& sample_);
void ApplyPublisherRegistration(const eCAL::Registration::Sample& sample_);

Expand All @@ -85,8 +84,8 @@ namespace eCAL
RegistrationCallbackT m_callback_service;
RegistrationCallbackT m_callback_client;
RegistrationCallbackT m_callback_process;
std::shared_ptr<UDP::CSampleReceiver> m_registration_receiver;

std::unique_ptr<CRegistrationReceiverUDP> m_registration_receiver_udp;

#if ECAL_CORE_REGISTRATION_SHM
CMemoryFileBroadcast m_memfile_broadcast;
Expand Down
59 changes: 59 additions & 0 deletions ecal/core/src/registration/ecal_registration_receiver_udp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* ========================= 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_receiver_udp.h"

#include "io/udp/ecal_udp_receiver_attr.h"
#include "io/udp/ecal_udp_sample_receiver.h"
#include "io/udp/ecal_udp_configurations.h"
#include "serialization/ecal_serialize_sample_registration.h"
#include <ecal/ecal_config.h>

namespace
{
using namespace eCAL;
UDP::SReceiverAttr CreateAttributes()
{
// set network attributes
eCAL::UDP::SReceiverAttr attr;
attr.address = UDP::GetRegistrationAddress();
attr.port = UDP::GetRegistrationPort();
attr.broadcast = UDP::IsBroadcast();
attr.loopback = true;
attr.rcvbuf = Config::GetUdpMulticastRcvBufSizeBytes();
return attr;
}

}

using namespace eCAL;

eCAL::CRegistrationReceiverUDP::CRegistrationReceiverUDP(RegistrationApplySampleCallbackT apply_sample_callback)
: m_registration_receiver(std::make_unique<UDP::CSampleReceiver>(
CreateAttributes(),
[](const std::string& sample_name_) {return true; },
[apply_sample_callback](const char* serialized_sample_data_, size_t serialized_sample_size_) {
Registration::Sample sample;
if (!DeserializeFromBuffer(serialized_sample_data_, serialized_sample_size_, sample)) return false;
return apply_sample_callback(sample);
}
))
{}

eCAL::CRegistrationReceiverUDP::~CRegistrationReceiverUDP() = default;
52 changes: 52 additions & 0 deletions ecal/core/src/registration/ecal_registration_receiver_udp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* ========================= 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 UDP registration receiver
*
* Handles UDP samples coming from other processes
*
**/

#include <memory>
#include <registration/ecal_registration_types.h>

namespace eCAL
{
namespace UDP
{
class CSampleReceiver;
}

class CRegistrationReceiverUDP
{
public:
CRegistrationReceiverUDP(RegistrationApplySampleCallbackT apply_sample_callback);
~CRegistrationReceiverUDP();

// Special member functionss
CRegistrationReceiverUDP(const CRegistrationReceiverUDP& other) = delete;
CRegistrationReceiverUDP& operator=(const CRegistrationReceiverUDP& other) = delete;
CRegistrationReceiverUDP(CRegistrationReceiverUDP&& other) noexcept = delete;
CRegistrationReceiverUDP& operator=(CRegistrationReceiverUDP&& other) noexcept = delete;

private:
std::unique_ptr<UDP::CSampleReceiver> m_registration_receiver;
};
}
33 changes: 33 additions & 0 deletions ecal/core/src/registration/ecal_registration_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ========================= 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 =================================
*/

#pragma once

#include <functional>
#include <serialization/ecal_struct_sample_registration.h>

namespace eCAL {
/**
* @brief Apply sample callback type.
*
* @param sample_ The sample protocol buffer registration payload buffer.
* @param sample_size_ The payload buffer size.
**/
using RegistrationApplySampleCallbackT = std::function<bool(const Registration::Sample&)>;
}

0 comments on commit 10d21f3

Please sign in to comment.