Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] writer refactoring based on unique ptr #1573

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 57 additions & 70 deletions ecal/core/src/readwrite/ecal_writer.cpp

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions ecal/core/src/readwrite/ecal_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "tcp/ecal_writer_tcp.h"
#endif

#include <memory>
#include <mutex>
#include <string>
#include <atomic>
Expand Down Expand Up @@ -198,18 +199,18 @@ namespace eCAL
bool confirmed = false;
};

SWriterMode udp_mc_mode;
SWriterMode udp_mode;
SWriterMode tcp_mode;
SWriterMode shm_mode;

#if ECAL_CORE_TRANSPORT_UDP
CDataWriterUdpMC udp_mc;
std::unique_ptr<CDataWriterUdpMC> udp;
#endif
#if ECAL_CORE_TRANSPORT_SHM
CDataWriterSHM shm;
std::unique_ptr<CDataWriterSHM> shm;
#endif
#if ECAL_CORE_TRANSPORT_TCP
CDataWriterTCP tcp;
std::unique_ptr<CDataWriterTCP> tcp;
#endif
};
SWriter m_writer;
Expand Down
6 changes: 0 additions & 6 deletions ecal/core/src/readwrite/ecal_writer_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ namespace eCAL
class CDataWriterBase
{
public:
CDataWriterBase() : m_created(false) {};
virtual ~CDataWriterBase() = default;

virtual SWriterInfo GetInfo() = 0;

virtual bool Create(const std::string& host_name_, const std::string& topic_name_, const std::string & topic_id_) = 0;
virtual bool Destroy() = 0;

virtual void AddLocConnection(const std::string& /*process_id_*/, const std::string& /*topic_id_*/, const std::string& /*conn_par_*/) {};
virtual void RemLocConnection(const std::string& /*process_id_*/, const std::string& /*topic_id_*/) {};

Expand All @@ -62,7 +58,5 @@ namespace eCAL
std::string m_host_name;
std::string m_topic_name;
std::string m_topic_id;

std::atomic<bool> m_created;
};
}
53 changes: 13 additions & 40 deletions ecal/core/src/readwrite/shm/ecal_writer_shm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,56 +33,35 @@ namespace eCAL
{
const std::string CDataWriterSHM::m_memfile_base_name = "ecal_";

CDataWriterSHM::~CDataWriterSHM()
CDataWriterSHM::CDataWriterSHM(const std::string& host_name_, const std::string& topic_name_, const std::string& topic_id_)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: parameter 'host_name_' is unused [misc-unused-parameters]

Suggested change
CDataWriterSHM::CDataWriterSHM(const std::string& host_name_, const std::string& topic_name_, const std::string& topic_id_)
CDataWriterSHM::CDataWriterSHM(const std::string& /*host_name_*/, const std::string& topic_name_, const std::string& topic_id_)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: parameter 'topic_id_' is unused [misc-unused-parameters]

Suggested change
CDataWriterSHM::CDataWriterSHM(const std::string& host_name_, const std::string& topic_name_, const std::string& topic_id_)
CDataWriterSHM::CDataWriterSHM(const std::string& host_name_, const std::string& topic_name_, const std::string& /*topic_id_*/)

{
Destroy();
}

SWriterInfo CDataWriterSHM::GetInfo()
{
SWriterInfo info_;

info_.name = "shm";
info_.description = "Local shared memory data writer";

info_.has_mode_local = true;
info_.has_mode_cloud = false;

info_.send_size_max = -1;

return info_;
}

bool CDataWriterSHM::Create(const std::string& /*host_name_*/, const std::string& topic_name_, const std::string & /*topic_id_*/)
{
if (m_created) return true;
m_topic_name = topic_name_;

// init write index and create memory files
m_write_idx = 0;

// set attributes
m_memory_file_attr.min_size = Config::GetMemfileMinsizeBytes();
m_memory_file_attr.reserve = Config::GetMemfileOverprovisioningPercentage();
m_memory_file_attr.timeout_open_ms = PUB_MEMFILE_OPEN_TO;
m_memory_file_attr.timeout_ack_ms = Config::GetMemfileAckTimeoutMs();

// initialize memory file buffer
m_created = SetBufferCount(m_buffer_count);

return m_created;
SetBufferCount(m_buffer_count /*= 1*/);
}

bool CDataWriterSHM::Destroy()
SWriterInfo CDataWriterSHM::GetInfo()
{
if (!m_created) return true;
m_created = false;
SWriterInfo info_;

m_memory_file_vec.clear();
info_.name = "shm";
info_.description = "Local shared memory data writer";

return true;
}
info_.has_mode_local = true;
info_.has_mode_cloud = false;

info_.send_size_max = -1;

return info_;
}

bool CDataWriterSHM::SetBufferCount(size_t buffer_count_)
{
// no need to adapt anything
Expand Down Expand Up @@ -128,8 +107,6 @@ namespace eCAL

bool CDataWriterSHM::PrepareWrite(const SWriterAttr& attr_)
{
if (!m_created) return false;

// false signals no rematching / exchanging of
// connection parameters needed
bool ret_state(false);
Expand All @@ -155,8 +132,6 @@ namespace eCAL

bool CDataWriterSHM::Write(CPayloadWriter& payload_, const SWriterAttr& attr_)
{
if (!m_created) return false;

// write content
const bool force_full_write(m_memory_file_vec.size() > 1);
const bool sent = m_memory_file_vec[m_write_idx]->Write(payload_, attr_, force_full_write);
Expand All @@ -170,8 +145,6 @@ namespace eCAL

void CDataWriterSHM::AddLocConnection(const std::string& process_id_, const std::string& /*topic_id_*/, const std::string& /*conn_par_*/)
{
if (!m_created) return;

for (auto& memory_file : m_memory_file_vec)
{
memory_file->Connect(process_id_);
Expand Down
8 changes: 1 addition & 7 deletions ecal/core/src/readwrite/shm/ecal_writer_shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,10 @@ namespace eCAL
class CDataWriterSHM : public CDataWriterBase
{
public:
CDataWriterSHM() = default;
~CDataWriterSHM() override;
CDataWriterSHM(const std::string& host_name_, const std::string& topic_name_, const std::string& topic_id_);

SWriterInfo GetInfo() override;

bool Create(const std::string& host_name_, const std::string& topic_name_, const std::string & topic_id_) override;
// this virtual function is called during construction/destruction,
// so, mark it as final to ensure that no derived classes override it.
bool Destroy() final;

bool SetBufferCount(size_t buffer_count_);

bool PrepareWrite(const SWriterAttr& attr_) override;
Expand Down
48 changes: 13 additions & 35 deletions ecal/core/src/readwrite/tcp/ecal_writer_tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
* @brief tcp writer
**/

#include <ecal/ecal_config.h>

#include "config/ecal_config_reader_hlp.h"
#include "serialization/ecal_serialize_sample_payload.h"

#include <ecal/ecal_config.h>

#include "ecal_writer_tcp.h"
#include "ecal_tcp_pubsub_logger.h"

Expand All @@ -38,31 +38,7 @@ namespace eCAL
std::mutex CDataWriterTCP::g_tcp_writer_executor_mtx;
std::shared_ptr<tcp_pubsub::Executor> CDataWriterTCP::g_tcp_writer_executor;

CDataWriterTCP::CDataWriterTCP() : m_port(0)
{
}

CDataWriterTCP::~CDataWriterTCP()
{
Destroy();
}

SWriterInfo CDataWriterTCP::GetInfo()
{
SWriterInfo info_;

info_.name = "tcp";
info_.description = "tcp data writer";

info_.has_mode_local = true;
info_.has_mode_cloud = true;

info_.send_size_max = -1;

return info_;
}

bool CDataWriterTCP::Create(const std::string& host_name_, const std::string& topic_name_, const std::string& topic_id_)
CDataWriterTCP::CDataWriterTCP(const std::string& host_name_, const std::string& topic_name_, const std::string& topic_id_)
{
{
const std::lock_guard<std::mutex> lock(g_tcp_writer_executor_mtx);
Expand All @@ -80,19 +56,21 @@ namespace eCAL
m_host_name = host_name_;
m_topic_name = topic_name_;
m_topic_id = topic_id_;

return true;
}

bool CDataWriterTCP::Destroy()
SWriterInfo CDataWriterTCP::GetInfo()
{
if(!m_publisher) return true;
SWriterInfo info_;

// destroy publisher
m_publisher = nullptr;
m_port = 0;
info_.name = "tcp";
info_.description = "tcp data writer";

return true;
info_.has_mode_local = true;
info_.has_mode_cloud = true;

info_.send_size_max = -1;

return info_;
}

bool CDataWriterTCP::Write(const void* const buf_, const SWriterAttr& attr_)
Expand Down
10 changes: 2 additions & 8 deletions ecal/core/src/readwrite/tcp/ecal_writer_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,10 @@ namespace eCAL
class CDataWriterTCP : public CDataWriterBase
{
public:
CDataWriterTCP();
~CDataWriterTCP() override;
CDataWriterTCP(const std::string& host_name_, const std::string& topic_name_, const std::string& topic_id_);

SWriterInfo GetInfo() override;

bool Create(const std::string& host_name_, const std::string& topic_name_, const std::string & topic_id_) override;
// this virtual function is called during construction/destruction,
// so, mark it as final to ensure that no derived classes override it.
bool Destroy() final;

bool Write(const void* buf_, const SWriterAttr& attr_) override;

Registration::ConnectionPar GetConnectionParameter() override;
Expand All @@ -59,6 +53,6 @@ namespace eCAL
static std::shared_ptr<tcp_pubsub::Executor> g_tcp_writer_executor;

std::shared_ptr<tcp_pubsub::Publisher> m_publisher;
uint16_t m_port;
uint16_t m_port = 0;
};
}
50 changes: 13 additions & 37 deletions ecal/core/src/readwrite/udp/ecal_writer_udp_mc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,19 @@
* @brief udp data writer
**/

#include <cstddef>
#include <ecal/ecal_config.h>
#include <ecal/ecal_log.h>
#include <memory>
#include <string>

#include "ecal_writer_udp_mc.h"
#include "io/udp/ecal_udp_configurations.h"
#include "serialization/ecal_serialize_sample_payload.h"

#include <cstddef>

namespace eCAL
{
CDataWriterUdpMC::~CDataWriterUdpMC()
{
Destroy();
}

SWriterInfo CDataWriterUdpMC::GetInfo()
{
SWriterInfo info_;

info_.name = "udp";
info_.description = "udp multicast data writer";

info_.has_mode_local = true;
info_.has_mode_cloud = true;

info_.send_size_max = -1;

return info_;
}

bool CDataWriterUdpMC::Create(const std::string & host_name_, const std::string & topic_name_, const std::string & topic_id_)
CDataWriterUdpMC::CDataWriterUdpMC(const std::string& host_name_, const std::string& topic_name_, const std::string& topic_id_)
{
if (m_created) return true;

m_host_name = host_name_;
m_topic_name = topic_name_;
m_topic_id = topic_id_;
Expand All @@ -76,26 +53,25 @@ namespace eCAL
// create udp/sample sender without activated loop-back
attr.loopback = false;
m_sample_sender_no_loopback = std::make_shared<UDP::CSampleSender>(attr);

m_created = true;
return true;
}

bool CDataWriterUdpMC::Destroy()
SWriterInfo CDataWriterUdpMC::GetInfo()
{
if (!m_created) return true;
SWriterInfo info_;

m_sample_sender_loopback.reset();
m_sample_sender_no_loopback.reset();
info_.name = "udp";
info_.description = "udp multicast data writer";

info_.has_mode_local = true;
info_.has_mode_cloud = true;

m_created = false;
return true;
info_.send_size_max = -1;

return info_;
}

bool CDataWriterUdpMC::Write(const void* const buf_, const SWriterAttr& attr_)
{
if (!m_created) return false;

// create new sample
Payload::Sample ecal_sample;
ecal_sample.cmd_type = eCmdType::bct_set_sample;
Expand Down
7 changes: 1 addition & 6 deletions ecal/core/src/readwrite/udp/ecal_writer_udp_mc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,10 @@ namespace eCAL
class CDataWriterUdpMC : public CDataWriterBase
{
public:
~CDataWriterUdpMC() override;
CDataWriterUdpMC(const std::string& host_name_, const std::string& topic_name_, const std::string& topic_id_);

SWriterInfo GetInfo() override;

bool Create(const std::string& host_name_, const std::string& topic_name_, const std::string & topic_id_) override;
// this virtual function is called during construction/destruction,
// so, mark it as final to ensure that no derived classes override it.
bool Destroy() final;

bool Write(const void* buf_, const SWriterAttr& attr_) override;

protected:
Expand Down
Loading