From 291e9d6945d1096d3fcd06829bddef3663635ae3 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:17:01 +0100 Subject: [PATCH 01/19] legacy CPublisher moved to eCAL::v5::CPublisher new eCAL::CPublisher with reduced API all tests and samples use new CPublisher all apps, lang bindings adapted to use old v5::CPublisher --- .../play_core/src/measurement_container.h | 3 +- ecal/core/CMakeLists.txt | 2 + ecal/core/include/ecal/ecal_publisher.h | 113 +----- ecal/core/include/ecal/ecal_publisher_v5.h | 323 ++++++++++++++++++ .../include/ecal/msg/protobuf/publisher.h | 10 +- ecal/core/include/ecal/msg/publisher.h | 4 +- ecal/core/src/cimpl/ecal_publisher_cimpl.cpp | 23 +- ecal/core/src/pubsub/ecal_publisher.cpp | 123 ++----- ecal/core/src/pubsub/ecal_publisher_v5.cpp | 248 ++++++++++++++ .../counter_snd/src/counter_snd.cpp | 5 +- .../datarate_snd/src/datarate_snd.cpp | 7 +- .../dynsize_snd/src/dynsize_snd.cpp | 2 +- .../latency_snd/src/latency_snd.cpp | 4 +- .../multiple_snd/src/multiple_snd.cpp | 6 +- .../performance_snd/src/performance_snd.cpp | 7 +- .../cpp/benchmarks/perftool/src/publisher.cpp | 2 +- .../src/pubsub_throughput.cpp | 2 +- .../binary/binary_snd/src/binary_snd.cpp | 4 +- .../src/proto_publisher_test.cpp | 72 ---- .../src/pubsub_callback_topicid.cpp | 4 +- .../src/pubsub_connection_test.cpp | 8 +- .../pubsub_test/src/pubsub_multibuffer.cpp | 2 +- .../tests/cpp/pubsub_test/src/pubsub_test.cpp | 65 +--- .../cpp/pubsub_test/src/pubsub_test_shm.cpp | 8 +- .../cpp/pubsub_test/src/pubsub_test_udp.cpp | 14 +- .../src/registration_gettopics.cpp | 18 +- .../csharp/Continental.eCAL.Core/ecal_clr.cpp | 6 +- lang/csharp/Continental.eCAL.Core/ecal_clr.h | 3 +- lang/python/core/src/ecal_clang.cpp | 11 +- 29 files changed, 669 insertions(+), 430 deletions(-) create mode 100644 ecal/core/include/ecal/ecal_publisher_v5.h create mode 100644 ecal/core/src/pubsub/ecal_publisher_v5.cpp diff --git a/app/play/play_core/src/measurement_container.h b/app/play/play_core/src/measurement_container.h index 1925da8ce8..dad5036590 100644 --- a/app/play/play_core/src/measurement_container.h +++ b/app/play/play_core/src/measurement_container.h @@ -24,6 +24,7 @@ #include #include +#include #include #include "continuity_report.h" @@ -90,7 +91,7 @@ class MeasurementContainer private: struct PublisherInfo { - eCAL::CPublisher publisher_; + eCAL::v5::CPublisher publisher_; long long message_counter_; PublisherInfo(const std::string& topic_name, const eCAL::SDataTypeInformation& info_) diff --git a/ecal/core/CMakeLists.txt b/ecal/core/CMakeLists.txt index c849a4f225..7ae90eee8e 100644 --- a/ecal/core/CMakeLists.txt +++ b/ecal/core/CMakeLists.txt @@ -205,6 +205,7 @@ endif() if(ECAL_CORE_PUBLISHER) set(ecal_pub_src src/pubsub/ecal_publisher.cpp + src/pubsub/ecal_publisher_v5.cpp src/pubsub/ecal_pubgate.cpp src/pubsub/ecal_pubgate.h ) @@ -537,6 +538,7 @@ set(ecal_header_cmn include/ecal/ecal_process_severity.h include/ecal/ecal_registration.h include/ecal/ecal_publisher.h + include/ecal/ecal_publisher_v5.h include/ecal/ecal_server.h include/ecal/ecal_service_info.h include/ecal/ecal_subscriber.h diff --git a/ecal/core/include/ecal/ecal_publisher.h b/ecal/core/include/ecal/ecal_publisher.h index f48e94a84d..c74670ac35 100644 --- a/ecal/core/include/ecal/ecal_publisher.h +++ b/ecal/core/include/ecal/ecal_publisher.h @@ -74,12 +74,6 @@ namespace eCAL ECAL_API_EXPORTED_MEMBER static constexpr long long DEFAULT_TIME_ARGUMENT = -1; /*!< Use DEFAULT_TIME_ARGUMENT in the `Send()` function to let eCAL determine the send timestamp */ - /** - * @brief Constructor. - **/ - ECAL_API_EXPORTED_MEMBER - CPublisher(); - /** * @brief Constructor. * @@ -90,15 +84,6 @@ namespace eCAL ECAL_API_EXPORTED_MEMBER CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_ = GetPublisherConfiguration()); - /** - * @brief Constructor. - * - * @param topic_name_ Unique topic name. - * @param config_ Optional configuration parameters. - **/ - ECAL_API_EXPORTED_MEMBER - explicit CPublisher(const std::string& topic_name_, const Publisher::Configuration& config_ = GetPublisherConfiguration()); - /** * @brief Destructor. **/ @@ -127,78 +112,6 @@ namespace eCAL ECAL_API_EXPORTED_MEMBER CPublisher& operator=(CPublisher&& rhs) noexcept; - /** - * @brief Creates this object. - * - * @param topic_name_ Unique topic name. - * @param data_type_info_ Topic data type information (encoding, type, descriptor). - * @param config_ Optional configuration parameters. - * - * @return True if it succeeds, false if it fails. - **/ - ECAL_API_EXPORTED_MEMBER - bool Create(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_ = GetPublisherConfiguration()); - - /** - * @brief Creates this object. - * - * @param topic_name_ Unique topic name. - * - * @return True if it succeeds, false if it fails. - **/ - ECAL_API_EXPORTED_MEMBER - bool Create(const std::string& topic_name_); - - /** - * @brief Destroys this object. - * - * @return True if it succeeds, false if it fails. - **/ - ECAL_API_EXPORTED_MEMBER - bool Destroy(); - - /** - * @brief Setup topic information. - * - * @param data_type_info_ Topic data type information attributes. - * - * @return True if it succeeds, false if it fails. - **/ - ECAL_API_EXPORTED_MEMBER - bool SetDataTypeInformation(const SDataTypeInformation& data_type_info_); - - /** - * @brief Sets publisher attribute. - * - * @param attr_name_ Attribute name. - * @param attr_value_ Attribute value. - * - * @return True if it succeeds, false if it fails. - **/ - ECAL_API_EXPORTED_MEMBER - bool SetAttribute(const std::string& attr_name_, const std::string& attr_value_); - - /** - * @brief Removes publisher attribute. - * - * @param attr_name_ Attribute name. - * - * @return True if it succeeds, false if it fails. - * @experimental - **/ - ECAL_API_EXPORTED_MEMBER - bool ClearAttribute(const std::string& attr_name_); - - /** - * @brief Set the specific topic filter id. - * - * @param filter_id_ The topic id for subscriber side filtering (0 == no id). - * - * @return True if it succeeds, false if it fails. - **/ - ECAL_API_EXPORTED_MEMBER - bool SetID(long long filter_id_); - /** * @brief Send a message to all subscribers. * @@ -242,7 +155,7 @@ namespace eCAL * @return True if succeeded, false if not. **/ ECAL_API_EXPORTED_MEMBER - bool AddEventCallback(eCAL_Publisher_Event type_, PubEventCallbackT callback_); + bool AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_); /** * @brief Remove callback function for publisher events. @@ -254,14 +167,6 @@ namespace eCAL ECAL_API_EXPORTED_MEMBER bool RemEventCallback(eCAL_Publisher_Event type_); - /** - * @brief Query if the publisher is created. - * - * @return True if created, false if not. - **/ - ECAL_API_EXPORTED_MEMBER - bool IsCreated() const { return(m_datawriter != nullptr); } - /** * @brief Query if the publisher is subscribed. * @@ -289,10 +194,10 @@ namespace eCAL /** * @brief Gets a unique ID of this Publisher * - * @return The topic id. + * @return The publisher id. **/ ECAL_API_EXPORTED_MEMBER - Registration::STopicId GetId() const; + Registration::STopicId GetPublisherId() const; /** * @brief Gets description of the connected topic. @@ -302,19 +207,7 @@ namespace eCAL ECAL_API_EXPORTED_MEMBER SDataTypeInformation GetDataTypeInformation() const; - /** - * @brief Dump the whole class state into a string. - * - * @param indent_ Indentation used for dump. - * - * @return The dump string. - **/ - ECAL_API_EXPORTED_MEMBER - std::string Dump(const std::string& indent_ = "") const; - private: - // class members std::shared_ptr m_datawriter; - long long m_filter_id; }; } diff --git a/ecal/core/include/ecal/ecal_publisher_v5.h b/ecal/core/include/ecal/ecal_publisher_v5.h new file mode 100644 index 0000000000..ac7beef8f5 --- /dev/null +++ b/ecal/core/include/ecal/ecal_publisher_v5.h @@ -0,0 +1,323 @@ +/* ========================= 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 ================================= +*/ + +/** + * @file ecal_publisher_v5.h + * @brief eCAL publisher interface (deprecated eCAL5 version) +**/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace eCAL +{ + class CDataWriter; + + namespace v5 + { + /** + * @brief eCAL publisher class. + * + * The CPublisher class is used to send topics to matching eCAL subscribers. The topic is created automatically by the constructor + * or by the Create member function. + *
+ *
+ * For sending the topic payload the publisher class provides an overloaded Send method. The first one is sending the payload as + * a std::string. The second needs a preallocated buffer described by a buffer address and a buffer length. The publisher is not + * taking the ownership for the allocated memory buffer. + *
+ *
+ * An optional time stamp can be attached to the topic payload. + * + **/ + /** + * @code + * // create publisher, topic name "A" + * eCAL::CPublisher pub("A"); + * + * // send string + * std::string send_s = "Hello World "; + * + * // send content + * size_t snd_len = pub.Send(send_s); + * @endcode + **/ + class ECAL_API_CLASS CPublisher + { + public: + ECAL_API_EXPORTED_MEMBER + static constexpr long long DEFAULT_TIME_ARGUMENT = -1; /*!< Use DEFAULT_TIME_ARGUMENT in the `Send()` function to let eCAL determine the send timestamp */ + + /** + * @brief Constructor. + **/ + ECAL_API_EXPORTED_MEMBER + CPublisher(); + + /** + * @brief Constructor. + * + * @param topic_name_ Unique topic name. + * @param data_type_info_ Topic data type information (encoding, type, descriptor). + * @param config_ Optional configuration parameters. + **/ + ECAL_API_EXPORTED_MEMBER + CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_ = GetPublisherConfiguration()); + + /** + * @brief Constructor. + * + * @param topic_name_ Unique topic name. + * @param config_ Optional configuration parameters. + **/ + ECAL_API_EXPORTED_MEMBER + explicit CPublisher(const std::string& topic_name_, const Publisher::Configuration& config_ = GetPublisherConfiguration()); + + /** + * @brief Destructor. + **/ + ECAL_API_EXPORTED_MEMBER + virtual ~CPublisher(); + + /** + * @brief CPublishers are non-copyable + **/ + CPublisher(const CPublisher&) = delete; + + /** + * @brief CPublishers are non-copyable + **/ + CPublisher& operator=(const CPublisher&) = delete; + + /** + * @brief CPublishers are move-enabled + **/ + ECAL_API_EXPORTED_MEMBER + CPublisher(CPublisher&& rhs) noexcept; + + /** + * @brief CPublishers are move-enabled + **/ + ECAL_API_EXPORTED_MEMBER + CPublisher& operator=(CPublisher&& rhs) noexcept; + + /** + * @brief Creates this object. + * + * @param topic_name_ Unique topic name. + * @param data_type_info_ Topic data type information (encoding, type, descriptor). + * @param config_ Optional configuration parameters. + * + * @return True if it succeeds, false if it fails. + **/ + ECAL_API_EXPORTED_MEMBER + bool Create(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_ = GetPublisherConfiguration()); + + /** + * @brief Creates this object. + * + * @param topic_name_ Unique topic name. + * + * @return True if it succeeds, false if it fails. + **/ + ECAL_API_EXPORTED_MEMBER + bool Create(const std::string& topic_name_); + + /** + * @brief Destroys this object. + * + * @return True if it succeeds, false if it fails. + **/ + ECAL_API_EXPORTED_MEMBER + bool Destroy(); + + /** + * @brief Setup topic information. + * + * @param data_type_info_ Topic data type information attributes. + * + * @return True if it succeeds, false if it fails. + **/ + ECAL_API_EXPORTED_MEMBER + bool SetDataTypeInformation(const SDataTypeInformation& data_type_info_); + + /** + * @brief Sets publisher attribute. + * + * @param attr_name_ Attribute name. + * @param attr_value_ Attribute value. + * + * @return True if it succeeds, false if it fails. + **/ + ECAL_API_EXPORTED_MEMBER + bool SetAttribute(const std::string& attr_name_, const std::string& attr_value_); + + /** + * @brief Removes publisher attribute. + * + * @param attr_name_ Attribute name. + * + * @return True if it succeeds, false if it fails. + * @experimental + **/ + ECAL_API_EXPORTED_MEMBER + bool ClearAttribute(const std::string& attr_name_); + + /** + * @brief Set the specific topic filter id. + * + * @param filter_id_ The topic id for subscriber side filtering (0 == no id). + * + * @return True if it succeeds, false if it fails. + **/ + ECAL_API_EXPORTED_MEMBER + bool SetID(long long filter_id_); + + /** + * @brief Send a message to all subscribers. + * + * @param buf_ Pointer to content buffer. + * @param len_ Length of buffer. + * @param time_ Send time (-1 = use eCAL system time in us, default = -1). + * + * @return Number of bytes sent. + **/ + ECAL_API_EXPORTED_MEMBER + size_t Send(const void* buf_, size_t len_, long long time_ = DEFAULT_TIME_ARGUMENT); + + /** + * @brief Send a message to all subscribers. + * + * @param payload_ Payload. + * @param time_ Send time (-1 = use eCAL system time in us, default = -1). + * + * @return Number of bytes sent. + **/ + ECAL_API_EXPORTED_MEMBER + size_t Send(CPayloadWriter& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); + + /** + * @brief Send a message to all subscribers. + * + * @param s_ String that contains content to send. + * @param time_ Send time (-1 = use eCAL system time in us, default = -1). + * + * @return Number of bytes sent. + **/ + ECAL_API_EXPORTED_MEMBER + size_t Send(const std::string& s_, long long time_ = DEFAULT_TIME_ARGUMENT); + + /** + * @brief Add callback function for publisher events. + * + * @param type_ The event type to react on. + * @param callback_ The callback function to add. + * + * @return True if succeeded, false if not. + **/ + ECAL_API_EXPORTED_MEMBER + bool AddEventCallback(eCAL_Publisher_Event type_, PubEventCallbackT callback_); + + /** + * @brief Remove callback function for publisher events. + * + * @param type_ The event type to remove. + * + * @return True if succeeded, false if not. + **/ + ECAL_API_EXPORTED_MEMBER + bool RemEventCallback(eCAL_Publisher_Event type_); + + /** + * @brief Query if the publisher is created. + * + * @return True if created, false if not. + **/ + ECAL_API_EXPORTED_MEMBER + bool IsCreated() const { return(m_datawriter != nullptr); } + + /** + * @brief Query if the publisher is subscribed. + * + * @return true if subscribed, false if not. + **/ + ECAL_API_EXPORTED_MEMBER + bool IsSubscribed() const; + + /** + * @brief Query the number of subscribers. + * + * @return Number of subscribers. + **/ + ECAL_API_EXPORTED_MEMBER + size_t GetSubscriberCount() const; + + /** + * @brief Gets name of the connected topic. + * + * @return The topic name. + **/ + ECAL_API_EXPORTED_MEMBER + std::string GetTopicName() const; + + /** + * @brief Gets a unique ID of this Publisher + * + * @return The topic id. + **/ + ECAL_API_EXPORTED_MEMBER + Registration::STopicId GetId() const; + + /** + * @brief Gets description of the connected topic. + * + * @return The topic information. + **/ + ECAL_API_EXPORTED_MEMBER + SDataTypeInformation GetDataTypeInformation() const; + + /** + * @brief Dump the whole class state into a string. + * + * @param indent_ Indentation used for dump. + * + * @return The dump string. + **/ + ECAL_API_EXPORTED_MEMBER + std::string Dump(const std::string& indent_ = "") const; + + private: + // class members + std::shared_ptr m_datawriter; + long long m_filter_id; + }; + } +} diff --git a/ecal/core/include/ecal/msg/protobuf/publisher.h b/ecal/core/include/ecal/msg/protobuf/publisher.h index daab06f0bd..2592859eff 100644 --- a/ecal/core/include/ecal/msg/protobuf/publisher.h +++ b/ecal/core/include/ecal/msg/protobuf/publisher.h @@ -54,7 +54,7 @@ namespace eCAL * **/ template - class CPublisher : public eCAL::CPublisher + class CPublisher : public eCAL::v5::CPublisher { class CPayload : public eCAL::CPayloadWriter { @@ -92,7 +92,7 @@ namespace eCAL /** * @brief Constructor. **/ - CPublisher() : eCAL::CPublisher() + CPublisher() : eCAL::v5::CPublisher() { } @@ -107,7 +107,7 @@ namespace eCAL // where the vtable is not created yet, or it's destructed. // Probably we can handle the Message publishers differently. One message publisher class and then one class for payloads and getting type // descriptor information. - explicit CPublisher(const std::string& topic_name_, const eCAL::Publisher::Configuration& config_ = GetPublisherConfiguration()) : eCAL::CPublisher(topic_name_, CPublisher::GetDataTypeInformation(), config_) + explicit CPublisher(const std::string& topic_name_, const eCAL::Publisher::Configuration& config_ = GetPublisherConfiguration()) : eCAL::v5::CPublisher(topic_name_, CPublisher::GetDataTypeInformation(), config_) { } @@ -146,7 +146,7 @@ namespace eCAL **/ bool Create(const std::string& topic_name_, const eCAL::Publisher::Configuration& config_ = GetPublisherConfiguration()) { - return(eCAL::CPublisher::Create(topic_name_, GetDataTypeInformation(), config_)); + return(eCAL::v5::CPublisher::Create(topic_name_, GetDataTypeInformation(), config_)); } /** @@ -160,7 +160,7 @@ namespace eCAL size_t Send(const T& msg_, long long time_ = DEFAULT_TIME_ARGUMENT) { CPayload payload{ msg_ }; - return eCAL::CPublisher::Send(payload, time_); + return eCAL::v5::CPublisher::Send(payload, time_); } private: diff --git a/ecal/core/include/ecal/msg/publisher.h b/ecal/core/include/ecal/msg/publisher.h index 5497c9bf95..2236c64f6c 100644 --- a/ecal/core/include/ecal/msg/publisher.h +++ b/ecal/core/include/ecal/msg/publisher.h @@ -25,7 +25,7 @@ #pragma once #include -#include +#include #include #include @@ -43,7 +43,7 @@ namespace eCAL * **/ template - class CMsgPublisher : public CPublisher + class CMsgPublisher : public v5::CPublisher { public: /** diff --git a/ecal/core/src/cimpl/ecal_publisher_cimpl.cpp b/ecal/core/src/cimpl/ecal_publisher_cimpl.cpp index cc32d00e35..b7ff5a97fe 100644 --- a/ecal/core/src/cimpl/ecal_publisher_cimpl.cpp +++ b/ecal/core/src/cimpl/ecal_publisher_cimpl.cpp @@ -23,6 +23,7 @@ **/ #include +#include #include #include "ecal_common_cimpl.h" @@ -52,14 +53,14 @@ extern "C" { ECALC_API ECAL_HANDLE eCAL_Pub_New() { - auto* pub = new eCAL::CPublisher; // NOLINT(*-owning-memory) + auto* pub = new eCAL::v5::CPublisher; // NOLINT(*-owning-memory) return(pub); } ECALC_API int eCAL_Pub_Create(ECAL_HANDLE handle_, const char* topic_name_, const char* topic_type_name_, const char* topic_type_encoding_, const char* topic_desc_, int topic_desc_len_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); const eCAL::SDataTypeInformation topic_info = { topic_type_name_, topic_type_encoding_, std::string(topic_desc_, static_cast(topic_desc_len_)) }; if (!pub->Create(topic_name_, topic_info)) return(0); return(1); @@ -68,7 +69,7 @@ extern "C" ECALC_API int eCAL_Pub_Destroy(ECAL_HANDLE handle_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); delete pub; // NOLINT(*-owning-memory) return(1); } @@ -76,7 +77,7 @@ extern "C" ECALC_API int eCAL_Pub_SetAttribute(ECAL_HANDLE handle_, const char* attr_name_, int attr_name_len_, const char* attr_value_, int attr_value_len_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); if (pub->SetAttribute(std::string(attr_name_, static_cast(attr_name_len_)), std::string(attr_value_, static_cast(attr_value_len_)))) return(1); return(0); } @@ -84,7 +85,7 @@ extern "C" ECALC_API int eCAL_Pub_ClearAttribute(ECAL_HANDLE handle_, const char* attr_name_, int attr_name_len_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); if (pub->ClearAttribute(std::string(attr_name_, static_cast(attr_name_len_)))) return(1); return(0); } @@ -92,7 +93,7 @@ extern "C" ECALC_API int eCAL_Pub_SetID(ECAL_HANDLE handle_, long long id_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); if (pub->SetID(id_)) return(1); return(0); } @@ -100,7 +101,7 @@ extern "C" ECALC_API int eCAL_Pub_IsSubscribed(ECAL_HANDLE handle_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); if (pub->IsSubscribed()) return(1); return(0); } @@ -108,7 +109,7 @@ extern "C" ECALC_API int eCAL_Pub_Send(ECAL_HANDLE handle_, const void* const buf_, int buf_len_, long long time_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); const size_t ret = pub->Send(buf_, static_cast(buf_len_), time_); if (static_cast(ret) == buf_len_) { @@ -120,7 +121,7 @@ extern "C" ECALC_API int eCAL_Pub_AddEventCallback(ECAL_HANDLE handle_, enum eCAL_Publisher_Event type_, PubEventCallbackCT callback_, void* par_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); auto callback = std::bind(g_pub_event_callback, std::placeholders::_1, std::placeholders::_2, callback_, par_); if (pub->AddEventCallback(type_, callback)) return(1); return(0); @@ -129,7 +130,7 @@ extern "C" ECALC_API int eCAL_Pub_RemEventCallback(ECAL_HANDLE handle_, enum eCAL_Publisher_Event type_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); if (pub->RemEventCallback(type_)) return(1); return(0); } @@ -137,7 +138,7 @@ extern "C" ECALC_API int eCAL_Pub_Dump(ECAL_HANDLE handle_, void* buf_, int buf_len_) { if (handle_ == nullptr) return(0); - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); const std::string dump = pub->Dump(); if (!dump.empty()) { diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index 28c4e2f805..f98d3637c7 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -38,33 +38,34 @@ namespace eCAL { - CPublisher::CPublisher() : - m_datawriter(nullptr), - m_filter_id(0) - { - } - CPublisher::CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_) - : CPublisher() { - CPublisher::Create(topic_name_, data_type_info_, config_); - } + if (topic_name_.empty()) return; - CPublisher::CPublisher(const std::string& topic_name_, const Publisher::Configuration& config_) - : CPublisher(topic_name_, SDataTypeInformation{}, config_) - {} + // create datawriter + m_datawriter = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); + + // register datawriter + g_pubgate()->Register(topic_name_, m_datawriter); + } CPublisher::~CPublisher() { - CPublisher::Destroy(); + if (m_datawriter == nullptr) return; + + // unregister datawriter + if (g_pubgate() != nullptr) g_pubgate()->Unregister(m_datawriter->GetTopicName(), m_datawriter); +#ifndef NDEBUG + // log it + eCAL::Logging::Log(log_level_debug1, std::string(m_datawriter->GetTopicName() + "::CPublisher::Destroy")); +#endif } /** * @brief CPublisher are move-enabled **/ CPublisher::CPublisher(CPublisher&& rhs) noexcept : - m_datawriter(std::move(rhs.m_datawriter)), - m_filter_id(rhs.m_filter_id) + m_datawriter(std::move(rhs.m_datawriter)) { rhs.m_datawriter = nullptr; } @@ -74,80 +75,11 @@ namespace eCAL **/ CPublisher& CPublisher::operator=(CPublisher&& rhs) noexcept { - // Call destroy, to clean up the current state, then afterwards move all elements - Destroy(); - m_datawriter = std::move(rhs.m_datawriter); - m_filter_id = rhs.m_filter_id; - rhs.m_datawriter = nullptr; - return *this; } - bool CPublisher::Create(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_) - { - if (m_datawriter != nullptr) return(false); - if (topic_name_.empty()) return(false); - - // create datawriter - m_datawriter = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); - - // register datawriter - g_pubgate()->Register(topic_name_, m_datawriter); - - // we made it :-) - return(true); - } - - bool CPublisher::Create(const std::string& topic_name_) - { - return Create(topic_name_, SDataTypeInformation()); - } - - bool CPublisher::Destroy() - { - if (m_datawriter == nullptr) return(false); - - // unregister datawriter - if(g_pubgate() != nullptr) g_pubgate()->Unregister(m_datawriter->GetTopicName(), m_datawriter); -#ifndef NDEBUG - // log it - eCAL::Logging::Log(log_level_debug1, std::string(m_datawriter->GetTopicName() + "::CPublisher::Destroy")); -#endif - - // stop & destroy datawriter - m_datawriter->Stop(); - m_datawriter.reset(); - - // we made it :-) - return(true); - } - - bool CPublisher::SetDataTypeInformation(const SDataTypeInformation& data_type_info_) - { - if (m_datawriter == nullptr) return false; - return m_datawriter->SetDataTypeInformation(data_type_info_); - } - - bool CPublisher::SetAttribute(const std::string& attr_name_, const std::string& attr_value_) - { - if(m_datawriter == nullptr) return false; - return m_datawriter->SetAttribute(attr_name_, attr_value_); - } - - bool CPublisher::ClearAttribute(const std::string& attr_name_) - { - if(m_datawriter == nullptr) return false; - return m_datawriter->ClearAttribute(attr_name_); - } - - bool CPublisher::SetID(long long filter_id_) - { - m_filter_id = filter_id_; - return true; - } - size_t CPublisher::Send(const void* const buf_, const size_t len_, const long long time_ /* = DEFAULT_TIME_ARGUMENT */) { CBufferPayloadWriter payload{ buf_, len_ }; @@ -171,7 +103,7 @@ namespace eCAL // send content via data writer layer const long long write_time = (time_ == DEFAULT_TIME_ARGUMENT) ? eCAL::Time::GetMicroSeconds() : time_; - const size_t written_bytes = m_datawriter->Write(payload_, write_time, m_filter_id); + const size_t written_bytes = m_datawriter->Write(payload_, write_time, 0); // return number of bytes written return written_bytes; @@ -182,11 +114,11 @@ namespace eCAL return(Send(s_.data(), s_.size(), time_)); } - bool CPublisher::AddEventCallback(eCAL_Publisher_Event type_, PubEventCallbackT callback_) + bool CPublisher::AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_) { if (m_datawriter == nullptr) return(false); RemEventCallback(type_); - return(m_datawriter->AddEventCallback(type_, std::move(callback_))); + return(m_datawriter->AddEventCallback(type_, callback_)); } bool CPublisher::RemEventCallback(eCAL_Publisher_Event type_) @@ -217,7 +149,7 @@ namespace eCAL return(m_datawriter->GetTopicName()); } - Registration::STopicId CPublisher::GetId() const + Registration::STopicId CPublisher::GetPublisherId() const { if (m_datawriter == nullptr) return{}; return(m_datawriter->GetId()); @@ -228,17 +160,4 @@ namespace eCAL if (m_datawriter == nullptr) return(SDataTypeInformation{}); return(m_datawriter->GetDataTypeInformation()); } - - std::string CPublisher::Dump(const std::string& indent_ /* = "" */) const - { - std::stringstream out; - - out << indent_ << "----------------------" << '\n'; - out << indent_ << " class CPublisher" << '\n'; - out << indent_ << "----------------------" << '\n'; - if((m_datawriter != nullptr) && m_datawriter->IsCreated()) out << indent_ << m_datawriter->Dump(" "); - out << '\n'; - - return(out.str()); - } -} \ No newline at end of file +} diff --git a/ecal/core/src/pubsub/ecal_publisher_v5.cpp b/ecal/core/src/pubsub/ecal_publisher_v5.cpp new file mode 100644 index 0000000000..3a746a81e9 --- /dev/null +++ b/ecal/core/src/pubsub/ecal_publisher_v5.cpp @@ -0,0 +1,248 @@ +/* ========================= 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 common data publisher based on eCAL +**/ + +#include +#include + +#include "ecal_globals.h" +#include "readwrite/ecal_writer.h" +#include "readwrite/ecal_writer_buffer_payload.h" + +#include "config/builder/writer_attribute_builder.h" +#include "ecal/ecal_config.h" + +#include +#include +#include +#include +#include + +namespace eCAL +{ + namespace v5 + { + CPublisher::CPublisher() : + m_datawriter(nullptr), + m_filter_id(0) + { + } + + CPublisher::CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_) + : CPublisher() + { + CPublisher::Create(topic_name_, data_type_info_, config_); + } + + CPublisher::CPublisher(const std::string& topic_name_, const Publisher::Configuration& config_) + : CPublisher(topic_name_, SDataTypeInformation{}, config_) + {} + + CPublisher::~CPublisher() + { + CPublisher::Destroy(); + } + + /** + * @brief CPublisher are move-enabled + **/ + CPublisher::CPublisher(CPublisher&& rhs) noexcept : + m_datawriter(std::move(rhs.m_datawriter)), + m_filter_id(rhs.m_filter_id) + { + rhs.m_datawriter = nullptr; + } + + /** + * @brief CPublisher are move-enabled + **/ + CPublisher& CPublisher::operator=(CPublisher&& rhs) noexcept + { + // Call destroy, to clean up the current state, then afterwards move all elements + Destroy(); + + m_datawriter = std::move(rhs.m_datawriter); + m_filter_id = rhs.m_filter_id; + + rhs.m_datawriter = nullptr; + + return *this; + } + + bool CPublisher::Create(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_) + { + if (m_datawriter != nullptr) return(false); + if (topic_name_.empty()) return(false); + + // create datawriter + m_datawriter = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); + + // register datawriter + g_pubgate()->Register(topic_name_, m_datawriter); + + // we made it :-) + return(true); + } + + bool CPublisher::Create(const std::string& topic_name_) + { + return Create(topic_name_, SDataTypeInformation()); + } + + bool CPublisher::Destroy() + { + if (m_datawriter == nullptr) return(false); + + // unregister datawriter + if(g_pubgate() != nullptr) g_pubgate()->Unregister(m_datawriter->GetTopicName(), m_datawriter); + #ifndef NDEBUG + // log it + eCAL::Logging::Log(log_level_debug1, std::string(m_datawriter->GetTopicName() + "::CPublisher::Destroy")); + #endif + + // stop & destroy datawriter + m_datawriter->Stop(); + m_datawriter.reset(); + + // we made it :-) + return(true); + } + + bool CPublisher::SetDataTypeInformation(const SDataTypeInformation& data_type_info_) + { + if (m_datawriter == nullptr) return false; + return m_datawriter->SetDataTypeInformation(data_type_info_); + } + + bool CPublisher::SetAttribute(const std::string& attr_name_, const std::string& attr_value_) + { + if(m_datawriter == nullptr) return false; + return m_datawriter->SetAttribute(attr_name_, attr_value_); + } + + bool CPublisher::ClearAttribute(const std::string& attr_name_) + { + if(m_datawriter == nullptr) return false; + return m_datawriter->ClearAttribute(attr_name_); + } + + bool CPublisher::SetID(long long filter_id_) + { + m_filter_id = filter_id_; + return true; + } + + size_t CPublisher::Send(const void* const buf_, const size_t len_, const long long time_ /* = DEFAULT_TIME_ARGUMENT */) + { + CBufferPayloadWriter payload{ buf_, len_ }; + return Send(payload, time_); + } + + size_t CPublisher::Send(CPayloadWriter& payload_, long long time_) + { + if (m_datawriter == nullptr) return 0; + + // in an optimization case the + // publisher can send an empty package + // or we do not have any subscription at all + // then the data writer will only do some statistics + // for the monitoring layer and return + if (!IsSubscribed()) + { + m_datawriter->RefreshSendCounter(); + return(payload_.GetSize()); + } + + // send content via data writer layer + const long long write_time = (time_ == DEFAULT_TIME_ARGUMENT) ? eCAL::Time::GetMicroSeconds() : time_; + const size_t written_bytes = m_datawriter->Write(payload_, write_time, m_filter_id); + + // return number of bytes written + return written_bytes; + } + + size_t CPublisher::Send(const std::string& s_, long long time_) + { + return(Send(s_.data(), s_.size(), time_)); + } + + bool CPublisher::AddEventCallback(eCAL_Publisher_Event type_, PubEventCallbackT callback_) + { + if (m_datawriter == nullptr) return(false); + RemEventCallback(type_); + return(m_datawriter->AddEventCallback(type_, std::move(callback_))); + } + + bool CPublisher::RemEventCallback(eCAL_Publisher_Event type_) + { + if (m_datawriter == nullptr) return(false); + return(m_datawriter->RemEventCallback(type_)); + } + + bool CPublisher::IsSubscribed() const + { + #if ECAL_CORE_REGISTRATION + if(m_datawriter == nullptr) return(false); + return(m_datawriter->IsSubscribed()); + #else // ECAL_CORE_REGISTRATION + return(true); + #endif // ECAL_CORE_REGISTRATION + } + + size_t CPublisher::GetSubscriberCount() const + { + if (m_datawriter == nullptr) return(0); + return(m_datawriter->GetSubscriberCount()); + } + + std::string CPublisher::GetTopicName() const + { + if(m_datawriter == nullptr) return(""); + return(m_datawriter->GetTopicName()); + } + + Registration::STopicId CPublisher::GetId() const + { + if (m_datawriter == nullptr) return{}; + return(m_datawriter->GetId()); + } + + SDataTypeInformation CPublisher::GetDataTypeInformation() const + { + if (m_datawriter == nullptr) return(SDataTypeInformation{}); + return(m_datawriter->GetDataTypeInformation()); + } + + std::string CPublisher::Dump(const std::string& indent_ /* = "" */) const + { + std::stringstream out; + + out << indent_ << "----------------------" << '\n'; + out << indent_ << " class CPublisher" << '\n'; + out << indent_ << "----------------------" << '\n'; + if((m_datawriter != nullptr) && m_datawriter->IsCreated()) out << indent_ << m_datawriter->Dump(" "); + out << '\n'; + + return(out.str()); + } + } +} diff --git a/ecal/samples/cpp/benchmarks/counter_snd/src/counter_snd.cpp b/ecal/samples/cpp/benchmarks/counter_snd/src/counter_snd.cpp index 6833ec518f..6610e02124 100644 --- a/ecal/samples/cpp/benchmarks/counter_snd/src/counter_snd.cpp +++ b/ecal/samples/cpp/benchmarks/counter_snd/src/counter_snd.cpp @@ -1,6 +1,6 @@ /* ========================= eCAL LICENSE ================================= * - * Copyright (C) 2016 - 2019 Continental Corporation + * 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. @@ -83,9 +83,6 @@ int main(int argc, char **argv) } } - // destroy publisher - pub.Destroy(); - // finalize eCAL API eCAL::Finalize(); diff --git a/ecal/samples/cpp/benchmarks/datarate_snd/src/datarate_snd.cpp b/ecal/samples/cpp/benchmarks/datarate_snd/src/datarate_snd.cpp index df987b2228..c997e9b638 100644 --- a/ecal/samples/cpp/benchmarks/datarate_snd/src/datarate_snd.cpp +++ b/ecal/samples/cpp/benchmarks/datarate_snd/src/datarate_snd.cpp @@ -1,6 +1,6 @@ /* ========================= eCAL LICENSE ================================= * - * Copyright (C) 2016 - 2019 Continental Corporation + * 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. @@ -74,7 +74,7 @@ int main(int argc, char **argv) pub_config.layer.shm.acknowledge_timeout_ms = acknowledge_time; // new publisher - eCAL::CPublisher pub(topic_name, pub_config); + eCAL::CPublisher pub(topic_name, eCAL::SDataTypeInformation(), pub_config); // default send string size *= 1024 * 1024; @@ -94,9 +94,6 @@ int main(int argc, char **argv) if(sleep > 0) std::this_thread::sleep_for(std::chrono::milliseconds(sleep)); } - // destroy publisher - pub.Destroy(); - // finalize eCAL API eCAL::Finalize(); diff --git a/ecal/samples/cpp/benchmarks/dynsize_snd/src/dynsize_snd.cpp b/ecal/samples/cpp/benchmarks/dynsize_snd/src/dynsize_snd.cpp index 6aac3a5896..a7b5232746 100644 --- a/ecal/samples/cpp/benchmarks/dynsize_snd/src/dynsize_snd.cpp +++ b/ecal/samples/cpp/benchmarks/dynsize_snd/src/dynsize_snd.cpp @@ -27,7 +27,7 @@ int main() eCAL::Initialize("dynsize_snd"); // publisher for topic "Performance" - eCAL::CPublisher pub("Performance"); + eCAL::CPublisher pub("Performance", eCAL::SDataTypeInformation()); // prepare snd_buf const int MAX_BUFSIZE(10*1024*1024); diff --git a/ecal/samples/cpp/benchmarks/latency_snd/src/latency_snd.cpp b/ecal/samples/cpp/benchmarks/latency_snd/src/latency_snd.cpp index 33f25c277e..f358dddaf9 100644 --- a/ecal/samples/cpp/benchmarks/latency_snd/src/latency_snd.cpp +++ b/ecal/samples/cpp/benchmarks/latency_snd/src/latency_snd.cpp @@ -1,6 +1,6 @@ /* ========================= eCAL LICENSE ================================= * - * Copyright (C) 2016 - 2019 Continental Corporation + * 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. @@ -59,7 +59,7 @@ void do_run(const int runs, int snd_size /*kB*/, int mem_buffer, bool zero_copy) pub_config.layer.shm.acknowledge_timeout_ms = 100; // create publisher - eCAL::CPublisher pub("ping", pub_config); + eCAL::CPublisher pub("ping", eCAL::SDataTypeInformation(), pub_config); // prepare send buffer CBinaryPayload payload(snd_size * 1024); diff --git a/ecal/samples/cpp/benchmarks/multiple_snd/src/multiple_snd.cpp b/ecal/samples/cpp/benchmarks/multiple_snd/src/multiple_snd.cpp index 4a33e06548..e0bff385bc 100644 --- a/ecal/samples/cpp/benchmarks/multiple_snd/src/multiple_snd.cpp +++ b/ecal/samples/cpp/benchmarks/multiple_snd/src/multiple_snd.cpp @@ -1,6 +1,6 @@ /* ========================= eCAL LICENSE ================================= * - * Copyright (C) 2016 - 2019 Continental Corporation + * 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. @@ -64,9 +64,7 @@ int main() tname << "PUB_" << i; // publisher topic name - std::shared_ptr pub = std::make_shared(); - - pub->Create(tname.str()); + std::shared_ptr pub = std::make_shared(tname.str(), eCAL::SDataTypeInformation()); struct SPubCount pub_count; pub_count.pub = std::move(pub); pub_vec[i] = pub_count; diff --git a/ecal/samples/cpp/benchmarks/performance_snd/src/performance_snd.cpp b/ecal/samples/cpp/benchmarks/performance_snd/src/performance_snd.cpp index fdd392d0ad..a6073ceb5b 100644 --- a/ecal/samples/cpp/benchmarks/performance_snd/src/performance_snd.cpp +++ b/ecal/samples/cpp/benchmarks/performance_snd/src/performance_snd.cpp @@ -1,6 +1,6 @@ /* ========================= eCAL LICENSE ================================= * - * Copyright (C) 2016 - 2019 Continental Corporation + * 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. @@ -62,7 +62,7 @@ int main(int argc, char **argv) std::cout << std::endl; // create publisher - eCAL::CPublisher pub("Performance", pub_config); + eCAL::CPublisher pub("Performance", eCAL::SDataTypeInformation(), pub_config); // counter long long msgs (0); @@ -110,9 +110,6 @@ int main(int argc, char **argv) } } - // destroy publisher - pub.Destroy(); - // finalize eCAL API eCAL::Finalize(); diff --git a/ecal/samples/cpp/benchmarks/perftool/src/publisher.cpp b/ecal/samples/cpp/benchmarks/perftool/src/publisher.cpp index c3a161439c..eb2a5dc0b0 100644 --- a/ecal/samples/cpp/benchmarks/perftool/src/publisher.cpp +++ b/ecal/samples/cpp/benchmarks/perftool/src/publisher.cpp @@ -41,7 +41,7 @@ #endif // WIN32 Publisher::Publisher(const std::string& topic_name, double frequency, std::size_t payload_size, bool quiet, bool log_print_verbose_times) - : ecal_pub (topic_name) + : ecal_pub (topic_name, eCAL::SDataTypeInformation()) , frequency_ (frequency) , payload_ (payload_size) , period_ (std::chrono::nanoseconds(static_cast(1e9 / frequency))) diff --git a/ecal/samples/cpp/benchmarks/pubsub_throughput/src/pubsub_throughput.cpp b/ecal/samples/cpp/benchmarks/pubsub_throughput/src/pubsub_throughput.cpp index 4d5ab63208..2fd5250c7a 100644 --- a/ecal/samples/cpp/benchmarks/pubsub_throughput/src/pubsub_throughput.cpp +++ b/ecal/samples/cpp/benchmarks/pubsub_throughput/src/pubsub_throughput.cpp @@ -62,7 +62,7 @@ void throughput_test(int snd_size, int snd_loops, eCAL::TLayer::eTransportLayer pub_config.layer.shm.acknowledge_timeout_ms = 100; // create publisher - eCAL::CPublisher pub("throughput", pub_config); + eCAL::CPublisher pub("throughput", eCAL::SDataTypeInformation(), pub_config); // create subscriber eCAL::CSubscriber sub("throughput"); diff --git a/ecal/samples/cpp/pubsub/binary/binary_snd/src/binary_snd.cpp b/ecal/samples/cpp/pubsub/binary/binary_snd/src/binary_snd.cpp index af9bcb57c9..095c0eafbd 100644 --- a/ecal/samples/cpp/pubsub/binary/binary_snd/src/binary_snd.cpp +++ b/ecal/samples/cpp/pubsub/binary/binary_snd/src/binary_snd.cpp @@ -1,6 +1,6 @@ /* ========================= eCAL LICENSE ================================= * - * Copyright (C) 2016 - 2019 Continental Corporation + * 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. @@ -28,7 +28,7 @@ int main() eCAL::Initialize("binary_snd"); // publisher for topic "blob" - eCAL::CPublisher pub("blob"); + eCAL::CPublisher pub("blob", eCAL::SDataTypeInformation()); // create binary buffer std::vector bin_buffer(1024); diff --git a/ecal/tests/cpp/pubsub_proto_test/src/proto_publisher_test.cpp b/ecal/tests/cpp/pubsub_proto_test/src/proto_publisher_test.cpp index fe351029db..e69de29bb2 100644 --- a/ecal/tests/cpp/pubsub_proto_test/src/proto_publisher_test.cpp +++ b/ecal/tests/cpp/pubsub_proto_test/src/proto_publisher_test.cpp @@ -1,72 +0,0 @@ -/* ========================= eCAL LICENSE ================================= - * - * Copyright (C) 2016 - 2019 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 ================================= -*/ - -// std headers -// used libraries -#include -// own project -#include -#include - -#include -#include - -class ProtoPublisherTest : public ::testing::Test { -public: - - ProtoPublisherTest() { - eCAL::Initialize(); - } - - ~ProtoPublisherTest() override { - eCAL::Finalize(); - } - -}; -using core_cpp_pubsub_proto_pub = ProtoPublisherTest; - -TEST_F(core_cpp_pubsub_proto_pub, ProtoPublisherTest_MoveAssignment) -{ - eCAL::protobuf::CPublisher person_pub("ProtoPublisherTest"); - - ASSERT_TRUE(person_pub.IsCreated()); - - eCAL::protobuf::CPublisherperson_moved; - - ASSERT_FALSE(person_moved.IsCreated()); - - person_moved = std::move(person_pub); - - // New Subscriber must be initialized - ASSERT_TRUE(person_moved.IsCreated()); - // Old subscriber is not initialized - ASSERT_FALSE(person_pub.IsCreated()); -} - -TEST_F(core_cpp_pubsub_proto_pub, ProtoPublisherTest_MoveConstruction) -{ - eCAL::protobuf::CPublisher person_pub("ProtoPublisherTest"); - - ASSERT_TRUE(person_pub.IsCreated()); - - eCAL::protobuf::CPublisherperson_moved{ std::move(person_pub) }; - - ASSERT_TRUE(person_moved.IsCreated()); - ASSERT_FALSE(person_pub.IsCreated()); -} diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp index 94d8ec26b3..821268f33f 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp @@ -128,7 +128,7 @@ TEST_P(TestFixture, OnePubSub) callback_datatype_info = datatype_info_; } ); - const auto pub_id = publisher.GetId(); + const auto pub_id = publisher.GetTopicId(); // let them match eCAL::Process::SleepMS(2 * config.registration.registration_refresh); @@ -175,7 +175,7 @@ TEST_P(TestFixture, MultiplePubSub) for (int i = 0; i < num_publishers; ++i) { auto& publisher = publishers[i]; - const auto pub_id = publisher.GetId(); + const auto pub_id = publisher.GetTopicId(); const auto pub_datatype_info = publisher.GetDataTypeInformation(); // send data diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp index 357639ab06..fdf9d3b388 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp @@ -41,7 +41,7 @@ TEST(core_cpp_pubsub, TestSubscriberIsPublishedTiming) auto publisher_function = [&do_start_publication, &publication_finished, &subscriber_seen_at_publication_start]() { eCAL::Publisher::Configuration pub_config; pub_config.layer.shm.acknowledge_timeout_ms = 500; - eCAL::CPublisher pub("blob", pub_config); + eCAL::CPublisher pub("blob", eCAL::SDataTypeInformation(), pub_config); int pub_count(0); const auto max_pub_count(1000); @@ -140,7 +140,7 @@ TEST(core_cpp_pubsub, TestPublisherIsSubscribedTiming) auto publisher_function = [&do_start_publication, &publication_finished]() { eCAL::Publisher::Configuration pub_config; pub_config.layer.shm.acknowledge_timeout_ms = 500; - eCAL::CPublisher pub("blob", pub_config); + eCAL::CPublisher pub("blob", eCAL::SDataTypeInformation(), pub_config); int cnt(0); const auto max_runs(1000); @@ -235,7 +235,7 @@ TEST(core_cpp_pubsub, TestChainedPublisherSubscriberCallback) // Publisher1 in thread 1 auto publisher1_function = [&publisher1_sent_count, &message_count]() { - eCAL::CPublisher pub1("topic1"); + eCAL::CPublisher pub1("topic1", eCAL::SDataTypeInformation()); while (!pub1.IsSubscribed()) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); @@ -249,7 +249,7 @@ TEST(core_cpp_pubsub, TestChainedPublisherSubscriberCallback) }; // Publisher2 - eCAL::CPublisher pub2("topic2"); + eCAL::CPublisher pub2("topic2", eCAL::SDataTypeInformation()); // Subscriber1 with callback that triggers Publisher2 eCAL::CSubscriber sub1("topic1"); diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_multibuffer.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_multibuffer.cpp index 826de1c97e..0011c44499 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_multibuffer.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_multibuffer.cpp @@ -91,7 +91,7 @@ std::vector multibuffer_pub_sub_test(int buffer_count, bool zero_copy, int pub_config.layer.shm.memfile_buffer_count = buffer_count; // create publisher for topic "A" - eCAL::CPublisher pub("A", pub_config); + eCAL::CPublisher pub("A", eCAL::SDataTypeInformation(), pub_config); std::atomic received_count{ 0 }; std::atomic received_bytes{ 0 }; diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp index 44f7b9474d..5eef9cf773 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp @@ -71,7 +71,7 @@ TEST(core_cpp_pubsub, LeakedPubSub) sub.AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2)); // create publisher - eCAL::CPublisher pub("foo"); + eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); // let's match them eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS); @@ -115,7 +115,7 @@ TEST(core_cpp_pubsub, CallbackDestruction) std::shared_ptr sub; // create publisher - eCAL::CPublisher pub("foo"); + eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); // start publishing thread std::atomic pub_stop(false); @@ -161,45 +161,6 @@ TEST(core_cpp_pubsub, CallbackDestruction) EXPECT_EQ(0, eCAL::Finalize()); } -TEST(core_cpp_pubsub, CreateDestroy) -{ - // initialize eCAL API - eCAL::Initialize("pubsub_test"); - - // create publisher for topic "foo" - eCAL::CPublisher pub; - - // check state - EXPECT_EQ(false, pub.IsCreated()); - - // create - EXPECT_EQ(true, pub.Create("foo")); - - // check state - EXPECT_EQ(true, pub.IsCreated()); - - // create subscriber for topic "foo" - eCAL::CSubscriber sub; - - // check state - EXPECT_EQ(false, sub.IsCreated()); - - // create - EXPECT_EQ(true, sub.Create("foo")); - - // check state - EXPECT_EQ(true, sub.IsCreated()); - - // destroy publisher - EXPECT_EQ(true, pub.Destroy()); - - // destroy subscriber - EXPECT_EQ(true, sub.Destroy()); - - // finalize eCAL API - eCAL::Finalize(); -} - TEST(core_cpp_pubsub, SimpleMessage1) { // default send / receive strings @@ -210,7 +171,7 @@ TEST(core_cpp_pubsub, SimpleMessage1) eCAL::Initialize("pubsub_test"); // create publisher for topic "foo" - eCAL::CPublisher pub("foo"); + eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); // create subscriber for topic "foo" eCAL::CSubscriber sub("foo"); @@ -232,9 +193,6 @@ TEST(core_cpp_pubsub, SimpleMessage1) EXPECT_EQ(false, sub.ReceiveBuffer(recv_s, nullptr, DATA_FLOW_TIME_MS)); EXPECT_EQ(0, recv_s.size()); - // destroy publisher - pub.Destroy(); - // destroy subscriber sub.Destroy(); @@ -255,7 +213,7 @@ TEST(core_cpp_pubsub, SimpleMessage2) eCAL::CSubscriber sub("foo"); // create publisher for topic "foo" - eCAL::CPublisher pub("foo"); + eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); // let's match them eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS); @@ -268,9 +226,6 @@ TEST(core_cpp_pubsub, SimpleMessage2) EXPECT_EQ(true, sub.ReceiveBuffer(recv_s, nullptr, DATA_FLOW_TIME_MS)); EXPECT_EQ(send_s.size(), recv_s.size()); - // destroy publisher - pub.Destroy(); - // destroy subscriber sub.Destroy(); @@ -290,7 +245,7 @@ TEST(core_cpp_pubsub, SimpleMessageCB) eCAL::CSubscriber sub("foo"); // create publisher for topic "foo" - eCAL::CPublisher pub("foo"); + eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); // add callback EXPECT_EQ(true, sub.AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2))); @@ -347,9 +302,6 @@ TEST(core_cpp_pubsub, SimpleMessageCB) // check callback receive EXPECT_EQ(0, g_callback_received_bytes); - // destroy publisher - pub.Destroy(); - // finalize eCAL API eCAL::Finalize(); } @@ -366,7 +318,7 @@ TEST(core_cpp_pubsub, DynamicSizeCB) eCAL::CSubscriber sub("foo"); // create publisher for topic "foo" - eCAL::CPublisher pub("foo"); + eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); // add callback EXPECT_EQ(true, sub.AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2))); @@ -397,9 +349,6 @@ TEST(core_cpp_pubsub, DynamicSizeCB) // check callback receive EXPECT_EQ(send_s.size(), g_callback_received_bytes); - // destroy publisher - pub.Destroy(); - // destroy subscriber sub.Destroy(); @@ -421,7 +370,7 @@ TEST(core_cpp_pubsub, DynamicCreate) // create publisher for topic "foo" eCAL::CPublisher* pub; - pub = new eCAL::CPublisher("foo"); + pub = new eCAL::CPublisher("foo", eCAL::SDataTypeInformation()); // add callback EXPECT_EQ(true, sub->AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2))); diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_test_shm.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_test_shm.cpp index 6e573ad8f2..7f91a9519e 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_test_shm.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_test_shm.cpp @@ -65,11 +65,11 @@ TEST(core_cpp_pubsub, ZeroPayloadMessageSHM) pub_config.layer.tcp.enable = false; // create publisher for topic "A" (no zero copy) - eCAL::CPublisher pub1("A", pub_config); + eCAL::CPublisher pub1("A", eCAL::SDataTypeInformation(), pub_config); // switch on zero copy pub_config.layer.shm.zero_copy_mode = true; - eCAL::CPublisher pub2("A", pub_config); + eCAL::CPublisher pub2("A", eCAL::SDataTypeInformation(), pub_config); // add callback @@ -102,10 +102,6 @@ TEST(core_cpp_pubsub, ZeroPayloadMessageSHM) // destroy subscriber sub.Destroy(); - // destroy publisher - pub1.Destroy(); - pub2.Destroy(); - // finalize eCAL API eCAL::Finalize(); } diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_test_udp.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_test_udp.cpp index ea4b4d7d2e..eca36fa63f 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_test_udp.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_test_udp.cpp @@ -65,7 +65,7 @@ TEST(core_cpp_pubsub, ZeroPayloadMessageUDP) pub_config.layer.tcp.enable = false; // create publisher for topic "A" - eCAL::CPublisher pub("A", pub_config); + eCAL::CPublisher pub("A", eCAL::SDataTypeInformation(), pub_config); // add callback EXPECT_EQ(true, sub.AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2))); @@ -86,12 +86,6 @@ TEST(core_cpp_pubsub, ZeroPayloadMessageUDP) EXPECT_EQ(send_s.size(), g_callback_received_bytes); EXPECT_EQ(2, g_callback_received_count); - // destroy subscriber - sub.Destroy(); - - // destroy publisher - pub.Destroy(); - // finalize eCAL API eCAL::Finalize(); } @@ -139,12 +133,6 @@ TEST(core_cpp_pubsub, MultipleSendsUDP) ++timestamp; } - // destroy subscriber - sub.Destroy(); - - // destroy publisher - pub.Destroy(); - // finalize eCAL API eCAL::Finalize(); } diff --git a/ecal/tests/cpp/registration_test_public/src/registration_gettopics.cpp b/ecal/tests/cpp/registration_test_public/src/registration_gettopics.cpp index bb4d4869e7..1cc72185d2 100644 --- a/ecal/tests/cpp/registration_test_public/src/registration_gettopics.cpp +++ b/ecal/tests/cpp/registration_test_public/src/registration_gettopics.cpp @@ -52,13 +52,13 @@ TEST(core_cpp_registration_public, GetTopics) eCAL::SDataTypeInformation info_B2 { "typeB2" ,"", "descB2" }; // create 3 publisher - eCAL::CPublisher pub1("A1", info_A1); - eCAL::CPublisher pub2("A2", info_A2); - eCAL::CPublisher pub3("A3", info_A3); + auto pub1 = std::make_shared("A1", info_A1); + auto pub2 = std::make_shared("A2", info_A2); + auto pub3 = std::make_shared("A3", info_A3); // create 2 subscriber - eCAL::CSubscriber sub1("B1", info_B1); - eCAL::CSubscriber sub2("B2", info_B2); + auto sub1 = std::make_shared("B1", info_B1); + auto sub2 = std::make_shared("B2", info_B2); // let's register eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS); @@ -103,8 +103,8 @@ TEST(core_cpp_registration_public, GetTopics) // now destroy publisher pub1 and subscriber sub1 // the entities pub12 and sub12 should replace them // by overwriting their type names and descriptions - pub1.Destroy(); - sub1.Destroy(); + pub1.reset(); + sub1.reset(); // let's register eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS); @@ -133,7 +133,7 @@ TEST(core_cpp_registration_public, GetTopics) eCAL::Finalize(); } -// This test creates a reall big number of publishers. +// This test creates a real big number of publishers. // It then checks, if they have all been seen using GetTopics() // And the count is back to 0 upon completion. TEST(core_cpp_registration_public, GetTopicsParallel) @@ -153,7 +153,7 @@ TEST(core_cpp_registration_public, GetTopicsParallel) { std::vector> publishers; for (int pub_count = 0; pub_count < max_publisher_count; pub_count++) { - std::unique_ptr publisher = std::make_unique(topic_name + std::to_string(pub_count)); + std::unique_ptr publisher = std::make_unique(topic_name + std::to_string(pub_count), eCAL::SDataTypeInformation()); publishers.push_back(std::move(publisher)); } std::this_thread::sleep_for(std::chrono::milliseconds(waiting_time_thread)); diff --git a/lang/csharp/Continental.eCAL.Core/ecal_clr.cpp b/lang/csharp/Continental.eCAL.Core/ecal_clr.cpp index 80aeece79a..1343b81d59 100644 --- a/lang/csharp/Continental.eCAL.Core/ecal_clr.cpp +++ b/lang/csharp/Continental.eCAL.Core/ecal_clr.cpp @@ -131,7 +131,7 @@ void Logger::Log(System::String^ message_) ///////////////////////////////////////////////////////////////////////////// // Publisher ///////////////////////////////////////////////////////////////////////////// -Publisher::Publisher() : m_pub(new ::eCAL::CPublisher()) +Publisher::Publisher() : m_pub(new ::eCAL::v5::CPublisher()) { } @@ -141,7 +141,7 @@ Publisher::Publisher(System::String^ topic_name_, System::String^ topic_type_, S topic_info.name = StringToStlString(topic_type_); topic_info.encoding = StringToStlString(topic_encoding_); topic_info.descriptor = StringToStlString(topic_desc_); - m_pub = new ::eCAL::CPublisher(StringToStlString(topic_name_), topic_info); + m_pub = new ::eCAL::v5::CPublisher(StringToStlString(topic_name_), topic_info); } Publisher::Publisher(System::String^ topic_name_, System::String^ topic_type_, System::String^ topic_encoding_, array^ topic_desc_) @@ -150,7 +150,7 @@ Publisher::Publisher(System::String^ topic_name_, System::String^ topic_type_, S topic_info.name = StringToStlString(topic_type_); topic_info.encoding = StringToStlString(topic_encoding_); topic_info.descriptor = ByteArrayToStlString(topic_desc_); - m_pub = new ::eCAL::CPublisher(StringToStlString(topic_name_), topic_info); + m_pub = new ::eCAL::v5::CPublisher(StringToStlString(topic_name_), topic_info); } Publisher::~Publisher() diff --git a/lang/csharp/Continental.eCAL.Core/ecal_clr.h b/lang/csharp/Continental.eCAL.Core/ecal_clr.h index a272b65238..ef8e4e1154 100644 --- a/lang/csharp/Continental.eCAL.Core/ecal_clr.h +++ b/lang/csharp/Continental.eCAL.Core/ecal_clr.h @@ -25,6 +25,7 @@ #pragma once #include #include +#include using namespace System; using namespace System::Collections::Generic; @@ -215,7 +216,7 @@ namespace Continental System::String^ Dump(); private: - ::eCAL::CPublisher* m_pub; + ::eCAL::v5::CPublisher* m_pub; }; diff --git a/lang/python/core/src/ecal_clang.cpp b/lang/python/core/src/ecal_clang.cpp index a1e73e3a02..a84728c940 100644 --- a/lang/python/core/src/ecal_clang.cpp +++ b/lang/python/core/src/ecal_clang.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "ecal_clang.h" @@ -300,7 +301,7 @@ ECAL_HANDLE pub_create(const char* topic_name_, const char* topic_type_, const c topic_info.encoding = topic_enc_; topic_info.descriptor = std::string(topic_desc_, static_cast(topic_desc_length_)); - auto* pub = new eCAL::CPublisher; + auto* pub = new eCAL::v5::CPublisher; if (!pub->Create(topic_name_, topic_info)) { delete pub; @@ -314,7 +315,7 @@ ECAL_HANDLE pub_create(const char* topic_name_, const char* topic_type_, const c /****************************************/ bool pub_destroy(ECAL_HANDLE handle_) { - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); if(pub != nullptr) { delete pub; @@ -331,7 +332,7 @@ bool pub_destroy(ECAL_HANDLE handle_) /****************************************/ int pub_send(ECAL_HANDLE handle_, const char* payload_, const int length_, const long long time_) { - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); if(pub != nullptr) { const size_t ret = pub->Send(payload_, static_cast(length_), time_); @@ -363,7 +364,7 @@ static void g_pub_event_callback(const char* topic_name_, const struct eCAL::SPu bool pub_add_event_callback(ECAL_HANDLE handle_, enum eCAL_Publisher_Event type_, const PubEventCallbackCT callback_, void* par_) { - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); auto callback = std::bind(g_pub_event_callback, std::placeholders::_1, std::placeholders::_2, callback_, par_); return(pub->AddEventCallback(type_, callback)); @@ -374,7 +375,7 @@ bool pub_add_event_callback(ECAL_HANDLE handle_, enum eCAL_Publisher_Event type_ /****************************************/ bool pub_rem_event_callback(ECAL_HANDLE handle_, enum eCAL_Publisher_Event type_) { - auto* pub = static_cast(handle_); + auto* pub = static_cast(handle_); return(pub->RemEventCallback(type_)); } From 586c28c9efb1d8ea638edf95ec1251d01ee4c5b9 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:26:33 +0100 Subject: [PATCH 02/19] readwrite/ecal_writer.cpp/h moved and renamed to pubsub/ecal_publisher_impl.cpp/h --- ecal/core/CMakeLists.txt | 4 ++-- ecal/core/src/pubsub/ecal_pubgate.h | 2 +- ecal/core/src/pubsub/ecal_publisher.cpp | 2 +- .../ecal_publisher_impl.cpp} | 15 ++++++++------- .../ecal_publisher_impl.h} | 8 ++++---- ecal/core/src/pubsub/ecal_publisher_v5.cpp | 2 +- 6 files changed, 17 insertions(+), 16 deletions(-) rename ecal/core/src/{readwrite/ecal_writer.cpp => pubsub/ecal_publisher_impl.cpp} (98%) rename ecal/core/src/{readwrite/ecal_writer.h => pubsub/ecal_publisher_impl.h} (96%) diff --git a/ecal/core/CMakeLists.txt b/ecal/core/CMakeLists.txt index 7ae90eee8e..86d89e4183 100644 --- a/ecal/core/CMakeLists.txt +++ b/ecal/core/CMakeLists.txt @@ -205,6 +205,8 @@ endif() if(ECAL_CORE_PUBLISHER) set(ecal_pub_src src/pubsub/ecal_publisher.cpp + src/pubsub/ecal_publisher_impl.cpp + src/pubsub/ecal_publisher_impl.h src/pubsub/ecal_publisher_v5.cpp src/pubsub/ecal_pubgate.cpp src/pubsub/ecal_pubgate.h @@ -228,8 +230,6 @@ set(ecal_readwrite_src if(ECAL_CORE_PUBLISHER) set(ecal_writer_src - src/readwrite/ecal_writer.cpp - src/readwrite/ecal_writer.h src/readwrite/ecal_writer_base.h src/readwrite/ecal_writer_buffer_payload.h src/readwrite/ecal_writer_data.h diff --git a/ecal/core/src/pubsub/ecal_pubgate.h b/ecal/core/src/pubsub/ecal_pubgate.h index cc73d36bc7..f22a001eeb 100644 --- a/ecal/core/src/pubsub/ecal_pubgate.h +++ b/ecal/core/src/pubsub/ecal_pubgate.h @@ -23,7 +23,7 @@ #pragma once -#include "readwrite/ecal_writer.h" +#include "ecal_publisher_impl.h" #include "serialization/ecal_struct_sample_registration.h" #include diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index f98d3637c7..18f1e3edd3 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -24,7 +24,7 @@ #include #include "ecal_globals.h" -#include "readwrite/ecal_writer.h" +#include "ecal_publisher_impl.h" #include "readwrite/ecal_writer_buffer_payload.h" #include "config/builder/writer_attribute_builder.h" diff --git a/ecal/core/src/readwrite/ecal_writer.cpp b/ecal/core/src/pubsub/ecal_publisher_impl.cpp similarity index 98% rename from ecal/core/src/readwrite/ecal_writer.cpp rename to ecal/core/src/pubsub/ecal_publisher_impl.cpp index 9d6ceaca15..1f6a952afe 100644 --- a/ecal/core/src/readwrite/ecal_writer.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_impl.cpp @@ -30,15 +30,16 @@ #include "registration/ecal_registration_provider.h" #endif -#include "ecal_writer.h" -#include "ecal_writer_base.h" -#include "ecal_writer_buffer_payload.h" +#include "ecal_publisher_impl.h" #include "ecal_global_accessors.h" -#include "ecal_transport_layer.h" -#include "config/builder/shm_attribute_builder.h" -#include "config/builder/tcp_attribute_builder.h" -#include "config/builder/udp_attribute_builder.h" +#include "readwrite/ecal_writer_base.h" +#include "readwrite/ecal_writer_buffer_payload.h" +#include "readwrite/ecal_transport_layer.h" + +#include "readwrite/config/builder/shm_attribute_builder.h" +#include "readwrite/config/builder/tcp_attribute_builder.h" +#include "readwrite/config/builder/udp_attribute_builder.h" #include #include diff --git a/ecal/core/src/readwrite/ecal_writer.h b/ecal/core/src/pubsub/ecal_publisher_impl.h similarity index 96% rename from ecal/core/src/readwrite/ecal_writer.h rename to ecal/core/src/pubsub/ecal_publisher_impl.h index 14fffc4175..e646b063f9 100644 --- a/ecal/core/src/readwrite/ecal_writer.h +++ b/ecal/core/src/pubsub/ecal_publisher_impl.h @@ -30,18 +30,18 @@ #include "serialization/ecal_serialize_sample_registration.h" #include "util/frequency_calculator.h" -#include "config/attributes/writer_attributes.h" +#include "readwrite/config/attributes/writer_attributes.h" #if ECAL_CORE_TRANSPORT_UDP -#include "udp/ecal_writer_udp.h" +#include "readwrite/udp/ecal_writer_udp.h" #endif #if ECAL_CORE_TRANSPORT_SHM -#include "shm/ecal_writer_shm.h" +#include "readwrite/shm/ecal_writer_shm.h" #endif #if ECAL_CORE_TRANSPORT_TCP -#include "tcp/ecal_writer_tcp.h" +#include "readwrite/tcp/ecal_writer_tcp.h" #endif #include diff --git a/ecal/core/src/pubsub/ecal_publisher_v5.cpp b/ecal/core/src/pubsub/ecal_publisher_v5.cpp index 3a746a81e9..79568823ac 100644 --- a/ecal/core/src/pubsub/ecal_publisher_v5.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_v5.cpp @@ -25,7 +25,7 @@ #include #include "ecal_globals.h" -#include "readwrite/ecal_writer.h" +#include "ecal_publisher_impl.h" #include "readwrite/ecal_writer_buffer_payload.h" #include "config/builder/writer_attribute_builder.h" From 3ea43f064fdffb90472115d4a3d5bbe81e38979b Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:41:06 +0100 Subject: [PATCH 03/19] eCAL::CDataWriter renamed to eCAL::CPublisherImpl --- ecal/core/include/ecal/ecal_publisher.h | 4 +- ecal/core/include/ecal/ecal_publisher_v5.h | 8 +- ecal/core/src/pubsub/ecal_pubgate.cpp | 42 +++++----- ecal/core/src/pubsub/ecal_pubgate.h | 10 +-- ecal/core/src/pubsub/ecal_publisher.cpp | 60 +++++++------- ecal/core/src/pubsub/ecal_publisher_impl.cpp | 68 ++++++++-------- ecal/core/src/pubsub/ecal_publisher_impl.h | 8 +- ecal/core/src/pubsub/ecal_publisher_v5.cpp | 84 ++++++++++---------- 8 files changed, 140 insertions(+), 144 deletions(-) diff --git a/ecal/core/include/ecal/ecal_publisher.h b/ecal/core/include/ecal/ecal_publisher.h index c74670ac35..f7e037bca0 100644 --- a/ecal/core/include/ecal/ecal_publisher.h +++ b/ecal/core/include/ecal/ecal_publisher.h @@ -39,7 +39,7 @@ namespace eCAL { - class CDataWriter; + class CPublisherImpl; /** * @brief eCAL publisher class. @@ -208,6 +208,6 @@ namespace eCAL SDataTypeInformation GetDataTypeInformation() const; private: - std::shared_ptr m_datawriter; + std::shared_ptr m_publisher_impl; }; } diff --git a/ecal/core/include/ecal/ecal_publisher_v5.h b/ecal/core/include/ecal/ecal_publisher_v5.h index ac7beef8f5..2c2fbef67c 100644 --- a/ecal/core/include/ecal/ecal_publisher_v5.h +++ b/ecal/core/include/ecal/ecal_publisher_v5.h @@ -39,7 +39,7 @@ namespace eCAL { - class CDataWriter; + class CPublisherImpl; namespace v5 { @@ -262,7 +262,7 @@ namespace eCAL * @return True if created, false if not. **/ ECAL_API_EXPORTED_MEMBER - bool IsCreated() const { return(m_datawriter != nullptr); } + bool IsCreated() const { return(m_publisher_impl != nullptr); } /** * @brief Query if the publisher is subscribed. @@ -316,8 +316,8 @@ namespace eCAL private: // class members - std::shared_ptr m_datawriter; - long long m_filter_id; + std::shared_ptr m_publisher_impl; + long long m_filter_id; }; } } diff --git a/ecal/core/src/pubsub/ecal_pubgate.cpp b/ecal/core/src/pubsub/ecal_pubgate.cpp index 19e52073f7..f50349eb42 100644 --- a/ecal/core/src/pubsub/ecal_pubgate.cpp +++ b/ecal/core/src/pubsub/ecal_pubgate.cpp @@ -55,39 +55,35 @@ namespace eCAL if(!m_created) return; // stop & destroy all remaining publisher - const std::unique_lock lock(m_topic_name_datawriter_sync); - for (const auto& datawriter : m_topic_name_datawriter_map) - { - datawriter.second->Stop(); - } - m_topic_name_datawriter_map.clear(); + const std::unique_lock lock(m_topic_name_publisher_sync); + m_topic_name_publisher_map.clear(); m_created = false; } - bool CPubGate::Register(const std::string& topic_name_, const std::shared_ptr& datawriter_) + bool CPubGate::Register(const std::string& topic_name_, const std::shared_ptr& publisher_) { if(!m_created) return(false); // register writer and multicast group - const std::unique_lock lock(m_topic_name_datawriter_sync); - m_topic_name_datawriter_map.emplace(std::pair>(topic_name_, datawriter_)); + const std::unique_lock lock(m_topic_name_publisher_sync); + m_topic_name_publisher_map.emplace(std::pair>(topic_name_, publisher_)); return(true); } - bool CPubGate::Unregister(const std::string& topic_name_, const std::shared_ptr& datawriter_) + bool CPubGate::Unregister(const std::string& topic_name_, const std::shared_ptr& publisher_) { if(!m_created) return(false); bool ret_state = false; - const std::unique_lock lock(m_topic_name_datawriter_sync); - auto res = m_topic_name_datawriter_map.equal_range(topic_name_); + const std::unique_lock lock(m_topic_name_publisher_sync); + auto res = m_topic_name_publisher_map.equal_range(topic_name_); for(auto iter = res.first; iter != res.second; ++iter) { - if(iter->second == datawriter_) + if(iter->second == publisher_) { - m_topic_name_datawriter_map.erase(iter); + m_topic_name_publisher_map.erase(iter); ret_state = true; break; } @@ -110,7 +106,7 @@ namespace eCAL const auto& subscription_info = ecal_sample_.identifier; const SDataTypeInformation& topic_information = ecal_topic.tdatatype; - CDataWriter::SLayerStates layer_states; + CPublisherImpl::SLayerStates layer_states; for (const auto& layer : ecal_topic.tlayer) { // transport layer versions 0 and 1 did not support dynamic layer enable feature @@ -146,9 +142,9 @@ namespace eCAL #endif // register subscriber - const std::shared_lock lock(m_topic_name_datawriter_sync); - auto res = m_topic_name_datawriter_map.equal_range(topic_name); - for(TopicNameDataWriterMapT::const_iterator iter = res.first; iter != res.second; ++iter) + const std::shared_lock lock(m_topic_name_publisher_sync); + auto res = m_topic_name_publisher_map.equal_range(topic_name); + for(TopicNamePublisherMapT::const_iterator iter = res.first; iter != res.second; ++iter) { iter->second->ApplySubscription(subscription_info, topic_information, layer_states, reader_par); } @@ -167,9 +163,9 @@ namespace eCAL const auto& subscription_info = ecal_sample_.identifier; // unregister subscriber - const std::shared_lock lock(m_topic_name_datawriter_sync); - auto res = m_topic_name_datawriter_map.equal_range(topic_name); - for (TopicNameDataWriterMapT::const_iterator iter = res.first; iter != res.second; ++iter) + const std::shared_lock lock(m_topic_name_publisher_sync); + auto res = m_topic_name_publisher_map.equal_range(topic_name); + for (TopicNamePublisherMapT::const_iterator iter = res.first; iter != res.second; ++iter) { iter->second->RemoveSubscription(subscription_info); } @@ -180,8 +176,8 @@ namespace eCAL if (!m_created) return; // read reader registrations - const std::shared_lock lock(m_topic_name_datawriter_sync); - for (const auto& iter : m_topic_name_datawriter_map) + const std::shared_lock lock(m_topic_name_publisher_sync); + for (const auto& iter : m_topic_name_publisher_map) { iter.second->GetRegistration(reg_sample_list_.push_back()); } diff --git a/ecal/core/src/pubsub/ecal_pubgate.h b/ecal/core/src/pubsub/ecal_pubgate.h index f22a001eeb..7b1f93b5f1 100644 --- a/ecal/core/src/pubsub/ecal_pubgate.h +++ b/ecal/core/src/pubsub/ecal_pubgate.h @@ -44,8 +44,8 @@ namespace eCAL void Start(); void Stop(); - bool Register(const std::string& topic_name_, const std::shared_ptr& datawriter_); - bool Unregister(const std::string& topic_name_, const std::shared_ptr& datawriter_); + bool Register(const std::string& topic_name_, const std::shared_ptr& publisher_); + bool Unregister(const std::string& topic_name_, const std::shared_ptr& publisher_); void ApplySubRegistration(const Registration::Sample& ecal_sample_); void ApplySubUnregistration(const Registration::Sample& ecal_sample_); @@ -55,8 +55,8 @@ namespace eCAL protected: static std::atomic m_created; - using TopicNameDataWriterMapT = std::multimap>; - std::shared_timed_mutex m_topic_name_datawriter_sync; - TopicNameDataWriterMapT m_topic_name_datawriter_map; + using TopicNamePublisherMapT = std::multimap>; + std::shared_timed_mutex m_topic_name_publisher_sync; + TopicNamePublisherMapT m_topic_name_publisher_map; }; } diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index 18f1e3edd3..e731c23a7b 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -18,7 +18,7 @@ */ /** - * @brief common data publisher based on eCAL + * @brief eCAL publisher interface **/ #include @@ -42,22 +42,22 @@ namespace eCAL { if (topic_name_.empty()) return; - // create datawriter - m_datawriter = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); + // create publisher + m_publisher_impl = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); - // register datawriter - g_pubgate()->Register(topic_name_, m_datawriter); + // register publisher + g_pubgate()->Register(topic_name_, m_publisher_impl); } CPublisher::~CPublisher() { - if (m_datawriter == nullptr) return; + if (m_publisher_impl == nullptr) return; - // unregister datawriter - if (g_pubgate() != nullptr) g_pubgate()->Unregister(m_datawriter->GetTopicName(), m_datawriter); + // unregister publisher + if (g_pubgate() != nullptr) g_pubgate()->Unregister(m_publisher_impl->GetTopicName(), m_publisher_impl); #ifndef NDEBUG // log it - eCAL::Logging::Log(log_level_debug1, std::string(m_datawriter->GetTopicName() + "::CPublisher::Destroy")); + eCAL::Logging::Log(log_level_debug1, std::string(m_publisher_impl->GetTopicName() + "::CPublisher::Destroy")); #endif } @@ -65,9 +65,9 @@ namespace eCAL * @brief CPublisher are move-enabled **/ CPublisher::CPublisher(CPublisher&& rhs) noexcept : - m_datawriter(std::move(rhs.m_datawriter)) + m_publisher_impl(std::move(rhs.m_publisher_impl)) { - rhs.m_datawriter = nullptr; + rhs.m_publisher_impl = nullptr; } /** @@ -75,8 +75,8 @@ namespace eCAL **/ CPublisher& CPublisher::operator=(CPublisher&& rhs) noexcept { - m_datawriter = std::move(rhs.m_datawriter); - rhs.m_datawriter = nullptr; + m_publisher_impl = std::move(rhs.m_publisher_impl); + rhs.m_publisher_impl = nullptr; return *this; } @@ -88,7 +88,7 @@ namespace eCAL size_t CPublisher::Send(CPayloadWriter& payload_, long long time_) { - if (m_datawriter == nullptr) return 0; + if (m_publisher_impl == nullptr) return 0; // in an optimization case the // publisher can send an empty package @@ -97,13 +97,13 @@ namespace eCAL // for the monitoring layer and return if (!IsSubscribed()) { - m_datawriter->RefreshSendCounter(); + m_publisher_impl->RefreshSendCounter(); return(payload_.GetSize()); } // send content via data writer layer const long long write_time = (time_ == DEFAULT_TIME_ARGUMENT) ? eCAL::Time::GetMicroSeconds() : time_; - const size_t written_bytes = m_datawriter->Write(payload_, write_time, 0); + const size_t written_bytes = m_publisher_impl->Write(payload_, write_time, 0); // return number of bytes written return written_bytes; @@ -116,22 +116,22 @@ namespace eCAL bool CPublisher::AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_) { - if (m_datawriter == nullptr) return(false); + if (m_publisher_impl == nullptr) return(false); RemEventCallback(type_); - return(m_datawriter->AddEventCallback(type_, callback_)); + return(m_publisher_impl->AddEventCallback(type_, callback_)); } bool CPublisher::RemEventCallback(eCAL_Publisher_Event type_) { - if (m_datawriter == nullptr) return(false); - return(m_datawriter->RemEventCallback(type_)); + if (m_publisher_impl == nullptr) return(false); + return(m_publisher_impl->RemEventCallback(type_)); } bool CPublisher::IsSubscribed() const { #if ECAL_CORE_REGISTRATION - if(m_datawriter == nullptr) return(false); - return(m_datawriter->IsSubscribed()); + if(m_publisher_impl == nullptr) return(false); + return(m_publisher_impl->IsSubscribed()); #else // ECAL_CORE_REGISTRATION return(true); #endif // ECAL_CORE_REGISTRATION @@ -139,25 +139,25 @@ namespace eCAL size_t CPublisher::GetSubscriberCount() const { - if (m_datawriter == nullptr) return(0); - return(m_datawriter->GetSubscriberCount()); + if (m_publisher_impl == nullptr) return(0); + return(m_publisher_impl->GetSubscriberCount()); } std::string CPublisher::GetTopicName() const { - if(m_datawriter == nullptr) return(""); - return(m_datawriter->GetTopicName()); + if(m_publisher_impl == nullptr) return(""); + return(m_publisher_impl->GetTopicName()); } Registration::STopicId CPublisher::GetPublisherId() const { - if (m_datawriter == nullptr) return{}; - return(m_datawriter->GetId()); + if (m_publisher_impl == nullptr) return{}; + return(m_publisher_impl->GetId()); } SDataTypeInformation CPublisher::GetDataTypeInformation() const { - if (m_datawriter == nullptr) return(SDataTypeInformation{}); - return(m_datawriter->GetDataTypeInformation()); + if (m_publisher_impl == nullptr) return(SDataTypeInformation{}); + return(m_publisher_impl->GetDataTypeInformation()); } } diff --git a/ecal/core/src/pubsub/ecal_publisher_impl.cpp b/ecal/core/src/pubsub/ecal_publisher_impl.cpp index 1f6a952afe..7cf9073e7d 100644 --- a/ecal/core/src/pubsub/ecal_publisher_impl.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_impl.cpp @@ -18,7 +18,7 @@ */ /** - * @brief common eCAL data writer + * @brief eCAL publisher implementation **/ #include @@ -80,14 +80,14 @@ namespace } // function to log the states of SLayerState - void logLayerState(const std::string& layerName, const eCAL::CDataWriter::SLayerState& state) { + void logLayerState(const std::string& layerName, const eCAL::CPublisherImpl::SLayerState& state) { std::cout << layerName << " - Read Enabled: " << boolToString(state.read_enabled) << ", Write Enabled: " << boolToString(state.write_enabled) << ", Write Active : " << boolToString(state.active) << std::endl; } // function to log the states of SLayerStates - void logLayerStates(const eCAL::CDataWriter::SLayerStates& states) { + void logLayerStates(const eCAL::CPublisherImpl::SLayerStates& states) { std::cout << "Logging Layer States:" << std::endl; logLayerState("UDP", states.udp); logLayerState("SHM", states.shm); @@ -98,7 +98,7 @@ namespace namespace eCAL { - CDataWriter::CDataWriter(const SDataTypeInformation& topic_info_, const eCAL::eCALWriter::SAttributes& attr_) : + CPublisherImpl::CPublisherImpl(const SDataTypeInformation& topic_info_, const eCAL::eCALWriter::SAttributes& attr_) : m_topic_info(topic_info_), m_attributes(attr_), m_frequency_calculator(3.0f), @@ -118,7 +118,7 @@ namespace eCAL m_created = true; } - CDataWriter::~CDataWriter() + CPublisherImpl::~CPublisherImpl() { #ifndef NDEBUG // log it @@ -128,7 +128,7 @@ namespace eCAL Stop(); } - bool CDataWriter::Stop() + bool CPublisherImpl::Stop() { if (!m_created) return false; #ifndef NDEBUG @@ -160,7 +160,7 @@ namespace eCAL return true; } - size_t CDataWriter::Write(CPayloadWriter& payload_, long long time_, long long filter_id_) + size_t CPublisherImpl::Write(CPayloadWriter& payload_, long long time_, long long filter_id_) { // get payload buffer size (one time, to avoid multiple computations) const size_t payload_buf_size(payload_.GetSize()); @@ -355,7 +355,7 @@ namespace eCAL else return 0; } - bool CDataWriter::SetDataTypeInformation(const SDataTypeInformation& topic_info_) + bool CPublisherImpl::SetDataTypeInformation(const SDataTypeInformation& topic_info_) { m_topic_info = topic_info_; @@ -367,7 +367,7 @@ namespace eCAL return(true); } - bool CDataWriter::SetAttribute(const std::string& attr_name_, const std::string& attr_value_) + bool CPublisherImpl::SetAttribute(const std::string& attr_name_, const std::string& attr_value_) { m_attr[attr_name_] = attr_value_; @@ -379,7 +379,7 @@ namespace eCAL return(true); } - bool CDataWriter::ClearAttribute(const std::string& attr_name_) + bool CPublisherImpl::ClearAttribute(const std::string& attr_name_) { m_attr.erase(attr_name_); @@ -391,7 +391,7 @@ namespace eCAL return(true); } - bool CDataWriter::AddEventCallback(eCAL_Publisher_Event type_, PubEventCallbackT callback_) + bool CPublisherImpl::AddEventCallback(eCAL_Publisher_Event type_, PubEventCallbackT callback_) { if (!m_created) return(false); @@ -408,7 +408,7 @@ namespace eCAL return(true); } - bool CDataWriter::RemEventCallback(eCAL_Publisher_Event type_) + bool CPublisherImpl::RemEventCallback(eCAL_Publisher_Event type_) { if (!m_created) return(false); @@ -425,7 +425,7 @@ namespace eCAL return(true); } - void CDataWriter::ApplySubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_, const SLayerStates& sub_layer_states_, const std::string& reader_par_) + void CPublisherImpl::ApplySubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_, const SLayerStates& sub_layer_states_, const std::string& reader_par_) { // collect layer states std::vector pub_layers; @@ -540,7 +540,7 @@ namespace eCAL #endif } - void CDataWriter::RemoveSubscription(const SSubscriptionInfo& subscription_info_) + void CPublisherImpl::RemoveSubscription(const SSubscriptionInfo& subscription_info_) { // remove subscription #if ECAL_CORE_TRANSPORT_UDP @@ -577,7 +577,7 @@ namespace eCAL #endif } - void CDataWriter::RefreshSendCounter() + void CPublisherImpl::RefreshSendCounter() { // increase write clock m_clock++; @@ -591,17 +591,17 @@ namespace eCAL } } - bool CDataWriter::IsSubscribed() const + bool CPublisherImpl::IsSubscribed() const { return m_connection_count > 0; } - size_t CDataWriter::GetSubscriberCount() const + size_t CPublisherImpl::GetSubscriberCount() const { return m_connection_count; } - std::string CDataWriter::Dump(const std::string& indent_ /* = "" */) + std::string CPublisherImpl::Dump(const std::string& indent_ /* = "" */) { std::stringstream out; @@ -625,7 +625,7 @@ namespace eCAL return(out.str()); } - void CDataWriter::Register() + void CPublisherImpl::Register() { #if ECAL_CORE_REGISTRATION Registration::Sample registration_sample; @@ -639,7 +639,7 @@ namespace eCAL #endif // ECAL_CORE_REGISTRATION } - void CDataWriter::Unregister() + void CPublisherImpl::Unregister() { #if ECAL_CORE_REGISTRATION Registration::Sample unregistration_sample; @@ -653,12 +653,12 @@ namespace eCAL #endif // ECAL_CORE_REGISTRATION } - void CDataWriter::GetRegistration(Registration::Sample& sample) + void CPublisherImpl::GetRegistration(Registration::Sample& sample) { GetRegistrationSample(sample); } - void CDataWriter::GetRegistrationSample(Registration::Sample& ecal_reg_sample) + void CPublisherImpl::GetRegistrationSample(Registration::Sample& ecal_reg_sample) { ecal_reg_sample.cmd_type = bct_reg_publisher; @@ -752,7 +752,7 @@ namespace eCAL ecal_reg_sample_topic.connections_ext = static_cast(ext_connections); } - void CDataWriter::GetUnregistrationSample(Registration::Sample& ecal_unreg_sample) + void CPublisherImpl::GetUnregistrationSample(Registration::Sample& ecal_unreg_sample) { ecal_unreg_sample.cmd_type = bct_unreg_publisher; @@ -768,7 +768,7 @@ namespace eCAL ecal_reg_sample_topic.uname = m_attributes.unit_name; } - void CDataWriter::FireConnectEvent(const std::string& tid_, const SDataTypeInformation& tinfo_) + void CPublisherImpl::FireConnectEvent(const std::string& tid_, const SDataTypeInformation& tinfo_) { const std::lock_guard lock(m_event_callback_map_mtx); auto iter = m_event_callback_map.find(pub_event_connected); @@ -784,7 +784,7 @@ namespace eCAL } } - void CDataWriter::FireUpdateEvent(const std::string& tid_, const SDataTypeInformation& tinfo_) + void CPublisherImpl::FireUpdateEvent(const std::string& tid_, const SDataTypeInformation& tinfo_) { const std::lock_guard lock(m_event_callback_map_mtx); auto iter = m_event_callback_map.find(pub_event_update_connection); @@ -800,7 +800,7 @@ namespace eCAL } } - void CDataWriter::FireDisconnectEvent() + void CPublisherImpl::FireDisconnectEvent() { const std::lock_guard lock(m_event_callback_map_mtx); auto iter = m_event_callback_map.find(pub_event_disconnected); @@ -814,7 +814,7 @@ namespace eCAL } } - size_t CDataWriter::GetConnectionCount() + size_t CPublisherImpl::GetConnectionCount() { // no need to lock map here for now, map locked by caller size_t count(0); @@ -828,7 +828,7 @@ namespace eCAL return count; } - bool CDataWriter::StartUdpLayer() + bool CPublisherImpl::StartUdpLayer() { #if ECAL_CORE_TRANSPORT_UDP if (m_layers.udp.write_enabled) return false; @@ -854,7 +854,7 @@ namespace eCAL #endif // ECAL_CORE_TRANSPORT_UDP } - bool CDataWriter::StartShmLayer() + bool CPublisherImpl::StartShmLayer() { #if ECAL_CORE_TRANSPORT_SHM if (m_layers.shm.write_enabled) return false; @@ -880,7 +880,7 @@ namespace eCAL #endif // ECAL_CORE_TRANSPORT_SHM } - bool CDataWriter::StartTcpLayer() + bool CPublisherImpl::StartTcpLayer() { #if ECAL_CORE_TRANSPORT_TCP if (m_layers.tcp.write_enabled) return false; @@ -906,7 +906,7 @@ namespace eCAL #endif // ECAL_CORE_TRANSPORT_TCP } - void CDataWriter::StopAllLayer() + void CPublisherImpl::StopAllLayer() { #if ECAL_CORE_TRANSPORT_UDP // flag disabled @@ -933,7 +933,7 @@ namespace eCAL #endif } - size_t CDataWriter::PrepareWrite(long long id_, size_t len_) + size_t CPublisherImpl::PrepareWrite(long long id_, size_t len_) { // store id m_id = id_; @@ -952,7 +952,7 @@ namespace eCAL return snd_hash; } - TLayer::eTransportLayer CDataWriter::DetermineTransportLayer2Start(const std::vector& enabled_pub_layer_, const std::vector& enabled_sub_layer_, bool same_host_) + TLayer::eTransportLayer CPublisherImpl::DetermineTransportLayer2Start(const std::vector& enabled_pub_layer_, const std::vector& enabled_sub_layer_, bool same_host_) { // determine the priority list to use const Publisher::Configuration::LayerPriorityVector& layer_priority_vector = same_host_ ? m_attributes.layer_priority_local : m_attributes.layer_priority_remote; @@ -972,7 +972,7 @@ namespace eCAL return TLayer::eTransportLayer::tlayer_none; } - int32_t CDataWriter::GetFrequency() + int32_t CPublisherImpl::GetFrequency() { const auto frequency_time = std::chrono::steady_clock::now(); const std::lock_guard lock(m_frequency_calculator_mtx); diff --git a/ecal/core/src/pubsub/ecal_publisher_impl.h b/ecal/core/src/pubsub/ecal_publisher_impl.h index e646b063f9..61d10fafd3 100644 --- a/ecal/core/src/pubsub/ecal_publisher_impl.h +++ b/ecal/core/src/pubsub/ecal_publisher_impl.h @@ -18,7 +18,7 @@ */ /** - * @brief common eCAL data writer + * @brief eCAL publisher implementation **/ #pragma once @@ -55,7 +55,7 @@ namespace eCAL { - class CDataWriter + class CPublisherImpl { public: struct SLayerState @@ -74,8 +74,8 @@ namespace eCAL using SSubscriptionInfo = Registration::SampleIdentifier; - CDataWriter(const SDataTypeInformation& topic_info_, const eCAL::eCALWriter::SAttributes& attr_); - ~CDataWriter(); + CPublisherImpl(const SDataTypeInformation& topic_info_, const eCAL::eCALWriter::SAttributes& attr_); + ~CPublisherImpl(); bool Stop(); diff --git a/ecal/core/src/pubsub/ecal_publisher_v5.cpp b/ecal/core/src/pubsub/ecal_publisher_v5.cpp index 79568823ac..91a639cf7f 100644 --- a/ecal/core/src/pubsub/ecal_publisher_v5.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_v5.cpp @@ -18,7 +18,7 @@ */ /** - * @brief common data publisher based on eCAL + * @brief eCAL publisher interface (deprecated eCAL5 version) **/ #include @@ -42,7 +42,7 @@ namespace eCAL namespace v5 { CPublisher::CPublisher() : - m_datawriter(nullptr), + m_publisher_impl(nullptr), m_filter_id(0) { } @@ -66,10 +66,10 @@ namespace eCAL * @brief CPublisher are move-enabled **/ CPublisher::CPublisher(CPublisher&& rhs) noexcept : - m_datawriter(std::move(rhs.m_datawriter)), + m_publisher_impl(std::move(rhs.m_publisher_impl)), m_filter_id(rhs.m_filter_id) { - rhs.m_datawriter = nullptr; + rhs.m_publisher_impl = nullptr; } /** @@ -80,24 +80,24 @@ namespace eCAL // Call destroy, to clean up the current state, then afterwards move all elements Destroy(); - m_datawriter = std::move(rhs.m_datawriter); + m_publisher_impl = std::move(rhs.m_publisher_impl); m_filter_id = rhs.m_filter_id; - rhs.m_datawriter = nullptr; + rhs.m_publisher_impl = nullptr; return *this; } bool CPublisher::Create(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_) { - if (m_datawriter != nullptr) return(false); + if (m_publisher_impl != nullptr) return(false); if (topic_name_.empty()) return(false); - // create datawriter - m_datawriter = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); + // create publisher + m_publisher_impl = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); - // register datawriter - g_pubgate()->Register(topic_name_, m_datawriter); + // register publisher + g_pubgate()->Register(topic_name_, m_publisher_impl); // we made it :-) return(true); @@ -110,18 +110,18 @@ namespace eCAL bool CPublisher::Destroy() { - if (m_datawriter == nullptr) return(false); + if (m_publisher_impl == nullptr) return(false); - // unregister datawriter - if(g_pubgate() != nullptr) g_pubgate()->Unregister(m_datawriter->GetTopicName(), m_datawriter); + // unregister publisher + if(g_pubgate() != nullptr) g_pubgate()->Unregister(m_publisher_impl->GetTopicName(), m_publisher_impl); #ifndef NDEBUG // log it - eCAL::Logging::Log(log_level_debug1, std::string(m_datawriter->GetTopicName() + "::CPublisher::Destroy")); + eCAL::Logging::Log(log_level_debug1, std::string(m_publisher_impl->GetTopicName() + "::CPublisher::Destroy")); #endif - // stop & destroy datawriter - m_datawriter->Stop(); - m_datawriter.reset(); + // stop & destroy publisher + m_publisher_impl->Stop(); + m_publisher_impl.reset(); // we made it :-) return(true); @@ -129,20 +129,20 @@ namespace eCAL bool CPublisher::SetDataTypeInformation(const SDataTypeInformation& data_type_info_) { - if (m_datawriter == nullptr) return false; - return m_datawriter->SetDataTypeInformation(data_type_info_); + if (m_publisher_impl == nullptr) return false; + return m_publisher_impl->SetDataTypeInformation(data_type_info_); } bool CPublisher::SetAttribute(const std::string& attr_name_, const std::string& attr_value_) { - if(m_datawriter == nullptr) return false; - return m_datawriter->SetAttribute(attr_name_, attr_value_); + if(m_publisher_impl == nullptr) return false; + return m_publisher_impl->SetAttribute(attr_name_, attr_value_); } bool CPublisher::ClearAttribute(const std::string& attr_name_) { - if(m_datawriter == nullptr) return false; - return m_datawriter->ClearAttribute(attr_name_); + if(m_publisher_impl == nullptr) return false; + return m_publisher_impl->ClearAttribute(attr_name_); } bool CPublisher::SetID(long long filter_id_) @@ -159,7 +159,7 @@ namespace eCAL size_t CPublisher::Send(CPayloadWriter& payload_, long long time_) { - if (m_datawriter == nullptr) return 0; + if (m_publisher_impl == nullptr) return 0; // in an optimization case the // publisher can send an empty package @@ -168,13 +168,13 @@ namespace eCAL // for the monitoring layer and return if (!IsSubscribed()) { - m_datawriter->RefreshSendCounter(); + m_publisher_impl->RefreshSendCounter(); return(payload_.GetSize()); } // send content via data writer layer const long long write_time = (time_ == DEFAULT_TIME_ARGUMENT) ? eCAL::Time::GetMicroSeconds() : time_; - const size_t written_bytes = m_datawriter->Write(payload_, write_time, m_filter_id); + const size_t written_bytes = m_publisher_impl->Write(payload_, write_time, m_filter_id); // return number of bytes written return written_bytes; @@ -187,22 +187,22 @@ namespace eCAL bool CPublisher::AddEventCallback(eCAL_Publisher_Event type_, PubEventCallbackT callback_) { - if (m_datawriter == nullptr) return(false); + if (m_publisher_impl == nullptr) return(false); RemEventCallback(type_); - return(m_datawriter->AddEventCallback(type_, std::move(callback_))); + return(m_publisher_impl->AddEventCallback(type_, std::move(callback_))); } bool CPublisher::RemEventCallback(eCAL_Publisher_Event type_) { - if (m_datawriter == nullptr) return(false); - return(m_datawriter->RemEventCallback(type_)); + if (m_publisher_impl == nullptr) return(false); + return(m_publisher_impl->RemEventCallback(type_)); } bool CPublisher::IsSubscribed() const { #if ECAL_CORE_REGISTRATION - if(m_datawriter == nullptr) return(false); - return(m_datawriter->IsSubscribed()); + if(m_publisher_impl == nullptr) return(false); + return(m_publisher_impl->IsSubscribed()); #else // ECAL_CORE_REGISTRATION return(true); #endif // ECAL_CORE_REGISTRATION @@ -210,26 +210,26 @@ namespace eCAL size_t CPublisher::GetSubscriberCount() const { - if (m_datawriter == nullptr) return(0); - return(m_datawriter->GetSubscriberCount()); + if (m_publisher_impl == nullptr) return(0); + return(m_publisher_impl->GetSubscriberCount()); } std::string CPublisher::GetTopicName() const { - if(m_datawriter == nullptr) return(""); - return(m_datawriter->GetTopicName()); + if(m_publisher_impl == nullptr) return(""); + return(m_publisher_impl->GetTopicName()); } Registration::STopicId CPublisher::GetId() const { - if (m_datawriter == nullptr) return{}; - return(m_datawriter->GetId()); + if (m_publisher_impl == nullptr) return{}; + return(m_publisher_impl->GetId()); } SDataTypeInformation CPublisher::GetDataTypeInformation() const { - if (m_datawriter == nullptr) return(SDataTypeInformation{}); - return(m_datawriter->GetDataTypeInformation()); + if (m_publisher_impl == nullptr) return(SDataTypeInformation{}); + return(m_publisher_impl->GetDataTypeInformation()); } std::string CPublisher::Dump(const std::string& indent_ /* = "" */) const @@ -239,7 +239,7 @@ namespace eCAL out << indent_ << "----------------------" << '\n'; out << indent_ << " class CPublisher" << '\n'; out << indent_ << "----------------------" << '\n'; - if((m_datawriter != nullptr) && m_datawriter->IsCreated()) out << indent_ << m_datawriter->Dump(" "); + if((m_publisher_impl != nullptr) && m_publisher_impl->IsCreated()) out << indent_ << m_publisher_impl->Dump(" "); out << '\n'; return(out.str()); From 6c1328373b3815751c9a25e209f34cf3204ff386 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Thu, 12 Dec 2024 10:01:20 +0100 Subject: [PATCH 04/19] minor renaming and default arguments --- ecal/core/include/ecal/ecal_publisher.h | 10 +++++----- ecal/core/src/pubsub/ecal_publisher.cpp | 4 ++-- .../cpp/benchmarks/dynsize_snd/src/dynsize_snd.cpp | 2 +- .../benchmarks/multiple_snd/src/multiple_snd.cpp | 2 +- .../cpp/benchmarks/perftool/src/publisher.cpp | 2 +- .../pubsub/binary/binary_snd/src/binary_snd.cpp | 2 +- .../pubsub_test/src/pubsub_callback_topicid.cpp | 4 ++-- .../cpp/pubsub_test/src/pubsub_connection_test.cpp | 4 ++-- ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp | 14 +++++++------- .../src/registration_gettopics.cpp | 2 +- 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/ecal/core/include/ecal/ecal_publisher.h b/ecal/core/include/ecal/ecal_publisher.h index f7e037bca0..110a6683a6 100644 --- a/ecal/core/include/ecal/ecal_publisher.h +++ b/ecal/core/include/ecal/ecal_publisher.h @@ -82,7 +82,7 @@ namespace eCAL * @param config_ Optional configuration parameters. **/ ECAL_API_EXPORTED_MEMBER - CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_ = GetPublisherConfiguration()); + CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_ = SDataTypeInformation(), const Publisher::Configuration& config_ = GetPublisherConfiguration()); /** * @brief Destructor. @@ -127,7 +127,7 @@ namespace eCAL /** * @brief Send a message to all subscribers. * - * @param payload_ Payload. + * @param payload_ Payload writer. * @param time_ Send time (-1 = use eCAL system time in us, default = -1). * * @return Number of bytes sent. @@ -138,13 +138,13 @@ namespace eCAL /** * @brief Send a message to all subscribers. * - * @param s_ String that contains content to send. - * @param time_ Send time (-1 = use eCAL system time in us, default = -1). + * @param payload_ Payload string. + * @param time_ Send time (-1 = use eCAL system time in us, default = -1). * * @return Number of bytes sent. **/ ECAL_API_EXPORTED_MEMBER - size_t Send(const std::string& s_, long long time_ = DEFAULT_TIME_ARGUMENT); + size_t Send(const std::string& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); /** * @brief Add callback function for publisher events. diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index e731c23a7b..509d0052fd 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -109,9 +109,9 @@ namespace eCAL return written_bytes; } - size_t CPublisher::Send(const std::string& s_, long long time_) + size_t CPublisher::Send(const std::string& payload_, long long time_) { - return(Send(s_.data(), s_.size(), time_)); + return(Send(payload_.data(), payload_.size(), time_)); } bool CPublisher::AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_) diff --git a/ecal/samples/cpp/benchmarks/dynsize_snd/src/dynsize_snd.cpp b/ecal/samples/cpp/benchmarks/dynsize_snd/src/dynsize_snd.cpp index a7b5232746..6aac3a5896 100644 --- a/ecal/samples/cpp/benchmarks/dynsize_snd/src/dynsize_snd.cpp +++ b/ecal/samples/cpp/benchmarks/dynsize_snd/src/dynsize_snd.cpp @@ -27,7 +27,7 @@ int main() eCAL::Initialize("dynsize_snd"); // publisher for topic "Performance" - eCAL::CPublisher pub("Performance", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub("Performance"); // prepare snd_buf const int MAX_BUFSIZE(10*1024*1024); diff --git a/ecal/samples/cpp/benchmarks/multiple_snd/src/multiple_snd.cpp b/ecal/samples/cpp/benchmarks/multiple_snd/src/multiple_snd.cpp index e0bff385bc..127598235e 100644 --- a/ecal/samples/cpp/benchmarks/multiple_snd/src/multiple_snd.cpp +++ b/ecal/samples/cpp/benchmarks/multiple_snd/src/multiple_snd.cpp @@ -64,7 +64,7 @@ int main() tname << "PUB_" << i; // publisher topic name - std::shared_ptr pub = std::make_shared(tname.str(), eCAL::SDataTypeInformation()); + std::shared_ptr pub = std::make_shared(tname.str()); struct SPubCount pub_count; pub_count.pub = std::move(pub); pub_vec[i] = pub_count; diff --git a/ecal/samples/cpp/benchmarks/perftool/src/publisher.cpp b/ecal/samples/cpp/benchmarks/perftool/src/publisher.cpp index eb2a5dc0b0..c3a161439c 100644 --- a/ecal/samples/cpp/benchmarks/perftool/src/publisher.cpp +++ b/ecal/samples/cpp/benchmarks/perftool/src/publisher.cpp @@ -41,7 +41,7 @@ #endif // WIN32 Publisher::Publisher(const std::string& topic_name, double frequency, std::size_t payload_size, bool quiet, bool log_print_verbose_times) - : ecal_pub (topic_name, eCAL::SDataTypeInformation()) + : ecal_pub (topic_name) , frequency_ (frequency) , payload_ (payload_size) , period_ (std::chrono::nanoseconds(static_cast(1e9 / frequency))) diff --git a/ecal/samples/cpp/pubsub/binary/binary_snd/src/binary_snd.cpp b/ecal/samples/cpp/pubsub/binary/binary_snd/src/binary_snd.cpp index 095c0eafbd..206bd33a62 100644 --- a/ecal/samples/cpp/pubsub/binary/binary_snd/src/binary_snd.cpp +++ b/ecal/samples/cpp/pubsub/binary/binary_snd/src/binary_snd.cpp @@ -28,7 +28,7 @@ int main() eCAL::Initialize("binary_snd"); // publisher for topic "blob" - eCAL::CPublisher pub("blob", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub("blob"); // create binary buffer std::vector bin_buffer(1024); diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp index 821268f33f..ea2054ac83 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp @@ -128,7 +128,7 @@ TEST_P(TestFixture, OnePubSub) callback_datatype_info = datatype_info_; } ); - const auto pub_id = publisher.GetTopicId(); + const auto pub_id = publisher.GetPublisherId(); // let them match eCAL::Process::SleepMS(2 * config.registration.registration_refresh); @@ -175,7 +175,7 @@ TEST_P(TestFixture, MultiplePubSub) for (int i = 0; i < num_publishers; ++i) { auto& publisher = publishers[i]; - const auto pub_id = publisher.GetTopicId(); + const auto pub_id = publisher.GetPublisherId(); const auto pub_datatype_info = publisher.GetDataTypeInformation(); // send data diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp index fdf9d3b388..0da9ef6ac5 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp @@ -235,7 +235,7 @@ TEST(core_cpp_pubsub, TestChainedPublisherSubscriberCallback) // Publisher1 in thread 1 auto publisher1_function = [&publisher1_sent_count, &message_count]() { - eCAL::CPublisher pub1("topic1", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub1("topic1"); while (!pub1.IsSubscribed()) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); @@ -249,7 +249,7 @@ TEST(core_cpp_pubsub, TestChainedPublisherSubscriberCallback) }; // Publisher2 - eCAL::CPublisher pub2("topic2", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub2("topic2"); // Subscriber1 with callback that triggers Publisher2 eCAL::CSubscriber sub1("topic1"); diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp index 5eef9cf773..1d61a35fbd 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp @@ -71,7 +71,7 @@ TEST(core_cpp_pubsub, LeakedPubSub) sub.AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2)); // create publisher - eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub("foo"); // let's match them eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS); @@ -115,7 +115,7 @@ TEST(core_cpp_pubsub, CallbackDestruction) std::shared_ptr sub; // create publisher - eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub("foo"); // start publishing thread std::atomic pub_stop(false); @@ -171,7 +171,7 @@ TEST(core_cpp_pubsub, SimpleMessage1) eCAL::Initialize("pubsub_test"); // create publisher for topic "foo" - eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub("foo"); // create subscriber for topic "foo" eCAL::CSubscriber sub("foo"); @@ -213,7 +213,7 @@ TEST(core_cpp_pubsub, SimpleMessage2) eCAL::CSubscriber sub("foo"); // create publisher for topic "foo" - eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub("foo"); // let's match them eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS); @@ -245,7 +245,7 @@ TEST(core_cpp_pubsub, SimpleMessageCB) eCAL::CSubscriber sub("foo"); // create publisher for topic "foo" - eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub("foo"); // add callback EXPECT_EQ(true, sub.AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2))); @@ -318,7 +318,7 @@ TEST(core_cpp_pubsub, DynamicSizeCB) eCAL::CSubscriber sub("foo"); // create publisher for topic "foo" - eCAL::CPublisher pub("foo", eCAL::SDataTypeInformation()); + eCAL::CPublisher pub("foo"); // add callback EXPECT_EQ(true, sub.AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2))); @@ -370,7 +370,7 @@ TEST(core_cpp_pubsub, DynamicCreate) // create publisher for topic "foo" eCAL::CPublisher* pub; - pub = new eCAL::CPublisher("foo", eCAL::SDataTypeInformation()); + pub = new eCAL::CPublisher("foo"); // add callback EXPECT_EQ(true, sub->AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2))); diff --git a/ecal/tests/cpp/registration_test_public/src/registration_gettopics.cpp b/ecal/tests/cpp/registration_test_public/src/registration_gettopics.cpp index 1cc72185d2..aad32a42e2 100644 --- a/ecal/tests/cpp/registration_test_public/src/registration_gettopics.cpp +++ b/ecal/tests/cpp/registration_test_public/src/registration_gettopics.cpp @@ -153,7 +153,7 @@ TEST(core_cpp_registration_public, GetTopicsParallel) { std::vector> publishers; for (int pub_count = 0; pub_count < max_publisher_count; pub_count++) { - std::unique_ptr publisher = std::make_unique(topic_name + std::to_string(pub_count), eCAL::SDataTypeInformation()); + std::unique_ptr publisher = std::make_unique(topic_name + std::to_string(pub_count)); publishers.push_back(std::move(publisher)); } std::this_thread::sleep_for(std::chrono::milliseconds(waiting_time_thread)); From 9806e039ebb171fbe51ba551bb03d7e98e574d2d Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Thu, 12 Dec 2024 10:52:06 +0100 Subject: [PATCH 05/19] ecal proto and capnp publisher still base on eCAL::v5::CPublisher --- ecal/core/include/ecal/msg/capnproto/publisher.h | 12 ++++++------ ecal/core/include/ecal/msg/protobuf/publisher.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ecal/core/include/ecal/msg/capnproto/publisher.h b/ecal/core/include/ecal/msg/capnproto/publisher.h index 60192fd4ae..319ab927b6 100644 --- a/ecal/core/include/ecal/msg/capnproto/publisher.h +++ b/ecal/core/include/ecal/msg/capnproto/publisher.h @@ -24,7 +24,7 @@ #pragma once -#include +#include #include // capnp includes @@ -50,7 +50,7 @@ namespace eCAL * **/ template - class CPublisher : public eCAL::CPublisher + class CPublisher : public eCAL::v5::CPublisher { class CPayload : public eCAL::CPayloadWriter { @@ -89,7 +89,7 @@ namespace eCAL * @brief Constructor. **/ CPublisher() - : eCAL::CPublisher() + : eCAL::v5::CPublisher() , builder(std::make_unique()) , root_builder(builder->initRoot()) { @@ -102,7 +102,7 @@ namespace eCAL * @param config_ Optional configuration parameters. **/ CPublisher(const std::string& topic_name_, const eCAL::Publisher::Configuration& config_ = {}) - : eCAL::CPublisher(topic_name_, GetDataTypeInformation(), config_) + : eCAL::v5::CPublisher(topic_name_, GetDataTypeInformation(), config_) , builder(std::make_unique()) , root_builder(builder->initRoot()) { @@ -152,7 +152,7 @@ namespace eCAL **/ bool Create(const std::string& topic_name_, const eCAL::Publisher::Configuration& config_ = {}) { - return(eCAL::CPublisher::Create(topic_name_, GetDataTypeInformation(), config_)); + return(eCAL::v5::CPublisher::Create(topic_name_, GetDataTypeInformation(), config_)); } typename message_type::Builder GetBuilder() @@ -163,7 +163,7 @@ namespace eCAL void Send() { CPayload payload{ *builder }; - eCAL::CPublisher::Send(payload); + eCAL::v5::CPublisher::Send(payload); } private: diff --git a/ecal/core/include/ecal/msg/protobuf/publisher.h b/ecal/core/include/ecal/msg/protobuf/publisher.h index 2592859eff..18ab976b6d 100644 --- a/ecal/core/include/ecal/msg/protobuf/publisher.h +++ b/ecal/core/include/ecal/msg/protobuf/publisher.h @@ -27,7 +27,7 @@ #include #include #include -#include +#include // protobuf includes #ifdef _MSC_VER From 0a9970ce7c6b0aac8933f01d545ccbd21c44feae Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Thu, 12 Dec 2024 11:20:21 +0100 Subject: [PATCH 06/19] master merged in config internal adaption --- ecal/core/src/pubsub/ecal_publisher_v5.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ecal/core/src/pubsub/ecal_publisher_v5.cpp b/ecal/core/src/pubsub/ecal_publisher_v5.cpp index 91a639cf7f..c20fb07499 100644 --- a/ecal/core/src/pubsub/ecal_publisher_v5.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_v5.cpp @@ -26,10 +26,10 @@ #include "ecal_globals.h" #include "ecal_publisher_impl.h" -#include "readwrite/ecal_writer_buffer_payload.h" +#include "ecal_config_internal.h" #include "config/builder/writer_attribute_builder.h" -#include "ecal/ecal_config.h" +#include "readwrite/ecal_writer_buffer_payload.h" #include #include @@ -94,7 +94,7 @@ namespace eCAL if (topic_name_.empty()) return(false); // create publisher - m_publisher_impl = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); + m_publisher_impl = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, eCAL::GetTransportLayerConfiguration(), eCAL::GetRegistrationConfiguration())); // register publisher g_pubgate()->Register(topic_name_, m_publisher_impl); From 76494ddae017320fd6db096f48440a25b2ebe893 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Thu, 12 Dec 2024 21:05:43 +0100 Subject: [PATCH 07/19] shared_ptr handling, commenting --- ecal/core/include/ecal/ecal_publisher.h | 342 ++++++++++++------------ ecal/core/src/pubsub/ecal_publisher.cpp | 25 +- 2 files changed, 181 insertions(+), 186 deletions(-) diff --git a/ecal/core/include/ecal/ecal_publisher.h b/ecal/core/include/ecal/ecal_publisher.h index 110a6683a6..88668d09e5 100644 --- a/ecal/core/include/ecal/ecal_publisher.h +++ b/ecal/core/include/ecal/ecal_publisher.h @@ -24,11 +24,12 @@ #pragma once -#include #include #include -#include + +#include #include +#include #include #include @@ -41,173 +42,176 @@ namespace eCAL { class CPublisherImpl; - /** - * @brief eCAL publisher class. - * - * The CPublisher class is used to send topics to matching eCAL subscribers. The topic is created automatically by the constructor - * or by the Create member function. - *
- *
- * For sending the topic payload the publisher class provides an overloaded Send method. The first one is sending the payload as - * a std::string. The second needs a preallocated buffer described by a buffer address and a buffer length. The publisher is not - * taking the ownership for the allocated memory buffer. - *
- *
- * An optional time stamp can be attached to the topic payload. - * - **/ - /** - * @code - * // create publisher, topic name "A" - * eCAL::CPublisher pub("A"); - * - * // send string - * std::string send_s = "Hello World "; - * - * // send content - * size_t snd_len = pub.Send(send_s); - * @endcode - **/ - class ECAL_API_CLASS CPublisher + inline namespace v6 { - public: - ECAL_API_EXPORTED_MEMBER - static constexpr long long DEFAULT_TIME_ARGUMENT = -1; /*!< Use DEFAULT_TIME_ARGUMENT in the `Send()` function to let eCAL determine the send timestamp */ - - /** - * @brief Constructor. - * - * @param topic_name_ Unique topic name. - * @param data_type_info_ Topic data type information (encoding, type, descriptor). - * @param config_ Optional configuration parameters. - **/ - ECAL_API_EXPORTED_MEMBER - CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_ = SDataTypeInformation(), const Publisher::Configuration& config_ = GetPublisherConfiguration()); - - /** - * @brief Destructor. - **/ - ECAL_API_EXPORTED_MEMBER - virtual ~CPublisher(); - - /** - * @brief CPublishers are non-copyable - **/ - CPublisher(const CPublisher&) = delete; - - /** - * @brief CPublishers are non-copyable - **/ - CPublisher& operator=(const CPublisher&) = delete; - - /** - * @brief CPublishers are move-enabled - **/ - ECAL_API_EXPORTED_MEMBER - CPublisher(CPublisher&& rhs) noexcept; - - /** - * @brief CPublishers are move-enabled - **/ - ECAL_API_EXPORTED_MEMBER - CPublisher& operator=(CPublisher&& rhs) noexcept; - - /** - * @brief Send a message to all subscribers. - * - * @param buf_ Pointer to content buffer. - * @param len_ Length of buffer. - * @param time_ Send time (-1 = use eCAL system time in us, default = -1). - * - * @return Number of bytes sent. - **/ - ECAL_API_EXPORTED_MEMBER - size_t Send(const void* buf_, size_t len_, long long time_ = DEFAULT_TIME_ARGUMENT); - - /** - * @brief Send a message to all subscribers. - * - * @param payload_ Payload writer. - * @param time_ Send time (-1 = use eCAL system time in us, default = -1). - * - * @return Number of bytes sent. - **/ - ECAL_API_EXPORTED_MEMBER - size_t Send(CPayloadWriter& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); - /** - * @brief Send a message to all subscribers. - * - * @param payload_ Payload string. - * @param time_ Send time (-1 = use eCAL system time in us, default = -1). - * - * @return Number of bytes sent. - **/ - ECAL_API_EXPORTED_MEMBER - size_t Send(const std::string& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); - - /** - * @brief Add callback function for publisher events. - * - * @param type_ The event type to react on. - * @param callback_ The callback function to add. - * - * @return True if succeeded, false if not. - **/ - ECAL_API_EXPORTED_MEMBER - bool AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_); - - /** - * @brief Remove callback function for publisher events. - * - * @param type_ The event type to remove. - * - * @return True if succeeded, false if not. - **/ - ECAL_API_EXPORTED_MEMBER - bool RemEventCallback(eCAL_Publisher_Event type_); - - /** - * @brief Query if the publisher is subscribed. - * - * @return true if subscribed, false if not. - **/ - ECAL_API_EXPORTED_MEMBER - bool IsSubscribed() const; - - /** - * @brief Query the number of subscribers. - * - * @return Number of subscribers. - **/ - ECAL_API_EXPORTED_MEMBER - size_t GetSubscriberCount() const; - - /** - * @brief Gets name of the connected topic. - * - * @return The topic name. - **/ - ECAL_API_EXPORTED_MEMBER - std::string GetTopicName() const; - - /** - * @brief Gets a unique ID of this Publisher - * - * @return The publisher id. - **/ - ECAL_API_EXPORTED_MEMBER - Registration::STopicId GetPublisherId() const; - - /** - * @brief Gets description of the connected topic. - * - * @return The topic information. - **/ - ECAL_API_EXPORTED_MEMBER - SDataTypeInformation GetDataTypeInformation() const; - - private: - std::shared_ptr m_publisher_impl; - }; + * @brief eCAL publisher class. + * + * The CPublisher class is used to send topics to matching eCAL subscribers. The topic is created automatically by the constructor + * or by the Create member function. + *
+ *
+ * For sending the topic payload the publisher class provides an overloaded Send method. The first one is sending the payload as + * a std::string. The second needs a preallocated buffer described by a buffer address and a buffer length. The publisher is not + * taking the ownership for the allocated memory buffer. + *
+ *
+ * An optional time stamp can be attached to the topic payload. + * + **/ + /** + * @code + * // create publisher, topic name "A" + * eCAL::CPublisher pub("A"); + * + * // send string + * std::string send_s = "Hello World "; + * + * // send content + * size_t snd_len = pub.Send(send_s); + * @endcode + **/ + class ECAL_API_CLASS CPublisher + { + public: + ECAL_API_EXPORTED_MEMBER + static constexpr long long DEFAULT_TIME_ARGUMENT = -1; /*!< Use DEFAULT_TIME_ARGUMENT in the `Send()` function to let eCAL determine the send timestamp */ + + /** + * @brief Constructor. + * + * @param topic_name_ Unique topic name. + * @param data_type_info_ Topic data type information (encoding, type, descriptor). + * @param config_ Optional configuration parameters. + **/ + ECAL_API_EXPORTED_MEMBER + CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_ = SDataTypeInformation(), const Publisher::Configuration& config_ = GetPublisherConfiguration()); + + /** + * @brief Destructor. + **/ + ECAL_API_EXPORTED_MEMBER + virtual ~CPublisher(); + + /** + * @brief CPublishers are non-copyable + **/ + CPublisher(const CPublisher&) = delete; + + /** + * @brief CPublishers are non-copyable + **/ + CPublisher& operator=(const CPublisher&) = delete; + + /** + * @brief CPublishers are move-enabled + **/ + ECAL_API_EXPORTED_MEMBER + CPublisher(CPublisher&& rhs) noexcept; + + /** + * @brief CPublishers are move-enabled + **/ + ECAL_API_EXPORTED_MEMBER + CPublisher& operator=(CPublisher&& rhs) noexcept; + + /** + * @brief Send a message to all subscribers. + * + * @param buf_ Pointer to content buffer. + * @param len_ Length of buffer. + * @param time_ Send time (-1 = use eCAL system time in us, default = -1). + * + * @return Number of bytes sent. + **/ + ECAL_API_EXPORTED_MEMBER + size_t Send(const void* buf_, size_t len_, long long time_ = DEFAULT_TIME_ARGUMENT); + + /** + * @brief Send a message to all subscribers. + * + * @param payload_ Payload writer. + * @param time_ Send time (-1 = use eCAL system time in us, default = -1). + * + * @return Number of bytes sent. + **/ + ECAL_API_EXPORTED_MEMBER + size_t Send(CPayloadWriter& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); + + /** + * @brief Send a message to all subscribers. + * + * @param payload_ Payload string. + * @param time_ Send time (-1 = use eCAL system time in us, default = -1). + * + * @return Number of bytes sent. + **/ + ECAL_API_EXPORTED_MEMBER + size_t Send(const std::string& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); + + /** + * @brief Add callback function for publisher events. + * + * @param type_ The event type to react on. + * @param callback_ The callback function to add. + * + * @return True if succeeded, false if not. + **/ + ECAL_API_EXPORTED_MEMBER + bool AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_); + + /** + * @brief Remove callback function for publisher events. + * + * @param type_ The event type to remove. + * + * @return True if succeeded, false if not. + **/ + ECAL_API_EXPORTED_MEMBER + bool RemEventCallback(eCAL_Publisher_Event type_); + + /** + * @brief Query if the publisher is subscribed. + * + * @return true if subscribed, false if not. + **/ + ECAL_API_EXPORTED_MEMBER + bool IsSubscribed() const; + + /** + * @brief Query the number of subscribers. + * + * @return Number of subscribers. + **/ + ECAL_API_EXPORTED_MEMBER + size_t GetSubscriberCount() const; + + /** + * @brief Gets name of the connected topic. + * + * @return The topic name. + **/ + ECAL_API_EXPORTED_MEMBER + std::string GetTopicName() const; + + /** + * @brief Gets a unique ID of this Publisher + * + * @return The publisher id. + **/ + ECAL_API_EXPORTED_MEMBER + Registration::STopicId GetPublisherId() const; + + /** + * @brief Gets description of the connected topic. + * + * @return The topic information. + **/ + ECAL_API_EXPORTED_MEMBER + SDataTypeInformation GetDataTypeInformation() const; + + private: + std::shared_ptr m_publisher_impl; + }; + } } diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index 87e53e3468..b5637d5c17 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -40,43 +40,34 @@ namespace eCAL { CPublisher::CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const Publisher::Configuration& config_) { - if (topic_name_.empty()) return; - - // create publisher + // create publisher implementation m_publisher_impl = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); // register publisher - g_pubgate()->Register(topic_name_, m_publisher_impl); + if(g_pubgate() != nullptr) g_pubgate()->Register(topic_name_, m_publisher_impl); } CPublisher::~CPublisher() { - if (m_publisher_impl == nullptr) return; - // unregister publisher if (g_pubgate() != nullptr) g_pubgate()->Unregister(m_publisher_impl->GetTopicName(), m_publisher_impl); + #ifndef NDEBUG - // log it eCAL::Logging::Log(log_level_debug1, std::string(m_publisher_impl->GetTopicName() + "::CPublisher::Destroy")); #endif } - /** - * @brief CPublisher are move-enabled - **/ CPublisher::CPublisher(CPublisher&& rhs) noexcept : - m_publisher_impl(std::move(rhs.m_publisher_impl)) + m_publisher_impl(std::move(rhs.m_publisher_impl)) { - rhs.m_publisher_impl = nullptr; } - /** - * @brief CPublisher are move-enabled - **/ CPublisher& CPublisher::operator=(CPublisher&& rhs) noexcept { - m_publisher_impl = std::move(rhs.m_publisher_impl); - rhs.m_publisher_impl = nullptr; + if (this != &rhs) + { + m_publisher_impl = std::move(rhs.m_publisher_impl); + } return *this; } From 396dd5e10e051ed4e384379de67c90f7c6405201 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 13 Dec 2024 07:06:30 +0100 Subject: [PATCH 08/19] check for nullptr in destructor --- ecal/core/src/pubsub/ecal_publisher.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index b5637d5c17..67da6f2cd8 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -49,6 +49,9 @@ namespace eCAL CPublisher::~CPublisher() { + // could be already destroyed by move + if (m_publisher_impl == nullptr) return; + // unregister publisher if (g_pubgate() != nullptr) g_pubgate()->Unregister(m_publisher_impl->GetTopicName(), m_publisher_impl); From 6d696be20eb2b953eb3bf4ed49d3f85baef5f466 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:25:07 +0100 Subject: [PATCH 09/19] remove pointer checks --- ecal/core/src/pubsub/ecal_publisher.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index 67da6f2cd8..664bedeebf 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -82,8 +82,6 @@ namespace eCAL size_t CPublisher::Send(CPayloadWriter& payload_, long long time_) { - if (m_publisher_impl == nullptr) return 0; - // in an optimization case the // publisher can send an empty package // or we do not have any subscription at all @@ -110,21 +108,18 @@ namespace eCAL bool CPublisher::AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_) { - if (m_publisher_impl == nullptr) return(false); RemEventCallback(type_); return(m_publisher_impl->AddEventCallback(type_, callback_)); } bool CPublisher::RemEventCallback(eCAL_Publisher_Event type_) { - if (m_publisher_impl == nullptr) return(false); return(m_publisher_impl->RemEventCallback(type_)); } bool CPublisher::IsSubscribed() const { #if ECAL_CORE_REGISTRATION - if(m_publisher_impl == nullptr) return(false); return(m_publisher_impl->IsSubscribed()); #else // ECAL_CORE_REGISTRATION return(true); @@ -133,25 +128,21 @@ namespace eCAL size_t CPublisher::GetSubscriberCount() const { - if (m_publisher_impl == nullptr) return(0); return(m_publisher_impl->GetSubscriberCount()); } std::string CPublisher::GetTopicName() const { - if(m_publisher_impl == nullptr) return(""); return(m_publisher_impl->GetTopicName()); } Registration::STopicId CPublisher::GetPublisherId() const { - if (m_publisher_impl == nullptr) return{}; return(m_publisher_impl->GetId()); } SDataTypeInformation CPublisher::GetDataTypeInformation() const { - if (m_publisher_impl == nullptr) return(SDataTypeInformation{}); return(m_publisher_impl->GetDataTypeInformation()); } } From 4e37e22835f8283d3d20182d60a0bb73764b65af Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:34:13 +0100 Subject: [PATCH 10/19] return value type of CPublisher::Send changed to boolean --- ecal/core/include/ecal/ecal_publisher.h | 12 ++++++------ ecal/core/src/pubsub/ecal_publisher.cpp | 11 ++++++----- ecal/core/src/pubsub/ecal_publisher_impl.cpp | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/ecal/core/include/ecal/ecal_publisher.h b/ecal/core/include/ecal/ecal_publisher.h index 88668d09e5..d086db3f80 100644 --- a/ecal/core/include/ecal/ecal_publisher.h +++ b/ecal/core/include/ecal/ecal_publisher.h @@ -122,10 +122,10 @@ namespace eCAL * @param len_ Length of buffer. * @param time_ Send time (-1 = use eCAL system time in us, default = -1). * - * @return Number of bytes sent. + * @return True if succeeded, false if not. **/ ECAL_API_EXPORTED_MEMBER - size_t Send(const void* buf_, size_t len_, long long time_ = DEFAULT_TIME_ARGUMENT); + bool Send(const void* buf_, size_t len_, long long time_ = DEFAULT_TIME_ARGUMENT); /** * @brief Send a message to all subscribers. @@ -133,10 +133,10 @@ namespace eCAL * @param payload_ Payload writer. * @param time_ Send time (-1 = use eCAL system time in us, default = -1). * - * @return Number of bytes sent. + * @return True if succeeded, false if not. **/ ECAL_API_EXPORTED_MEMBER - size_t Send(CPayloadWriter& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); + bool Send(CPayloadWriter& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); /** * @brief Send a message to all subscribers. @@ -144,10 +144,10 @@ namespace eCAL * @param payload_ Payload string. * @param time_ Send time (-1 = use eCAL system time in us, default = -1). * - * @return Number of bytes sent. + * @return True if succeeded, false if not. **/ ECAL_API_EXPORTED_MEMBER - size_t Send(const std::string& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); + bool Send(const std::string& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); /** * @brief Add callback function for publisher events. diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index 664bedeebf..ba1ee61175 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -74,13 +74,13 @@ namespace eCAL return *this; } - size_t CPublisher::Send(const void* const buf_, const size_t len_, const long long time_ /* = DEFAULT_TIME_ARGUMENT */) + bool CPublisher::Send(const void* const buf_, const size_t len_, const long long time_ /* = DEFAULT_TIME_ARGUMENT */) { CBufferPayloadWriter payload{ buf_, len_ }; return Send(payload, time_); } - size_t CPublisher::Send(CPayloadWriter& payload_, long long time_) + bool CPublisher::Send(CPayloadWriter& payload_, long long time_) { // in an optimization case the // publisher can send an empty package @@ -97,11 +97,12 @@ namespace eCAL const long long write_time = (time_ == DEFAULT_TIME_ARGUMENT) ? eCAL::Time::GetMicroSeconds() : time_; const size_t written_bytes = m_publisher_impl->Write(payload_, write_time, 0); - // return number of bytes written - return written_bytes; + // TODO: change CPublisherImpl::Write to return a bool + // return true if we have written something + return written_bytes > 0; } - size_t CPublisher::Send(const std::string& payload_, long long time_) + bool CPublisher::Send(const std::string& payload_, long long time_) { return(Send(payload_.data(), payload_.size(), time_)); } diff --git a/ecal/core/src/pubsub/ecal_publisher_impl.cpp b/ecal/core/src/pubsub/ecal_publisher_impl.cpp index 7cf9073e7d..fae641681e 100644 --- a/ecal/core/src/pubsub/ecal_publisher_impl.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_impl.cpp @@ -106,7 +106,7 @@ namespace eCAL { #ifndef NDEBUG // log it - Logging::Log(log_level_debug1, m_attributes.topic_name + "::CDataWriter::Constructor"); + Logging::Log(log_level_debug2, m_attributes.topic_name + "::CDataWriter::Constructor"); #endif // build topic id From b74825d928b6a64f041aaf6b3e414488108b22f7 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:56:48 +0100 Subject: [PATCH 11/19] tests adapted (boolean return of CPublisher::Send() --- .../pubsub_test/src/pubsub_multibuffer.cpp | 2 +- .../tests/cpp/pubsub_test/src/pubsub_test.cpp | 22 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_multibuffer.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_multibuffer.cpp index 0011c44499..cd04ac99af 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_multibuffer.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_multibuffer.cpp @@ -117,7 +117,7 @@ std::vector multibuffer_pub_sub_test(int buffer_count, bool zero_copy, int // run publications for (int i = 0; i < publications; ++i) { - EXPECT_EQ(PAYLOAD_SIZE_BYTE, pub.Send(binary_payload)); + EXPECT_EQ(true, pub.Send(binary_payload)); eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); } diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp index 1d61a35fbd..dd8d66f06f 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_test.cpp @@ -180,7 +180,7 @@ TEST(core_cpp_pubsub, SimpleMessage1) eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS); // send content - EXPECT_EQ(send_s.size(), pub.Send(send_s)); + EXPECT_EQ(true, pub.Send(send_s)); // receive content with DATA_FLOW_TIME_MS timeout recv_s.clear(); @@ -219,7 +219,7 @@ TEST(core_cpp_pubsub, SimpleMessage2) eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS); // send content - EXPECT_EQ(send_s.size(), pub.Send(send_s)); + EXPECT_EQ(true, pub.Send(send_s)); // receive content with DATA_FLOW_TIME_MS timeout recv_s.clear(); @@ -255,7 +255,7 @@ TEST(core_cpp_pubsub, SimpleMessageCB) // send content g_callback_received_bytes = 0; - EXPECT_EQ(send_s.size(), pub.Send(send_s)); + EXPECT_EQ(true, pub.Send(send_s)); // let the data flow eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); @@ -268,7 +268,7 @@ TEST(core_cpp_pubsub, SimpleMessageCB) // send content g_callback_received_bytes = 0; - EXPECT_EQ(send_s.size(), pub.Send(send_s)); + EXPECT_EQ(true, pub.Send(send_s)); // let the data flow eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); @@ -281,7 +281,7 @@ TEST(core_cpp_pubsub, SimpleMessageCB) // send content g_callback_received_bytes = 0; - EXPECT_EQ(send_s.size(), pub.Send(send_s)); + EXPECT_EQ(true, pub.Send(send_s)); // let the data flow eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); @@ -294,7 +294,7 @@ TEST(core_cpp_pubsub, SimpleMessageCB) // send content g_callback_received_bytes = 0; - EXPECT_EQ(send_s.size(), pub.Send(send_s)); + EXPECT_EQ(true, pub.Send(send_s)); // let the data flow eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); @@ -328,7 +328,7 @@ TEST(core_cpp_pubsub, DynamicSizeCB) // send content g_callback_received_bytes = 0; - EXPECT_EQ(send_s.size(), pub.Send(send_s)); + EXPECT_EQ(true, pub.Send(send_s)); // let the data flow eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); @@ -341,7 +341,7 @@ TEST(core_cpp_pubsub, DynamicSizeCB) // send content g_callback_received_bytes = 0; - EXPECT_EQ(send_s.size(), pub.Send(send_s)); + EXPECT_EQ(true, pub.Send(send_s)); // let the data flow eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); @@ -380,7 +380,7 @@ TEST(core_cpp_pubsub, DynamicCreate) // send content g_callback_received_bytes = 0; - EXPECT_EQ(send_s.size(), pub->Send(send_s)); + EXPECT_EQ(true, pub->Send(send_s)); // let the data flow eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); @@ -403,7 +403,7 @@ TEST(core_cpp_pubsub, DynamicCreate) // send content g_callback_received_bytes = 0; - EXPECT_EQ(send_s.size(), pub->Send(send_s)); + EXPECT_EQ(true, pub->Send(send_s)); // let the data flow eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); @@ -425,7 +425,7 @@ TEST(core_cpp_pubsub, DynamicCreate) // send content g_callback_received_bytes = 0; - EXPECT_EQ(send_s.size(), pub->Send(send_s)); + EXPECT_EQ(true, pub->Send(send_s)); // let the data flow eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); From c912abb129002e8ec97aa8b02eb24dfa9d98b75f Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 13 Dec 2024 12:27:56 +0100 Subject: [PATCH 12/19] Publisher::Send call for message publisher change to return boolean value as well RemEventCallback -> RemoveEventCallback --- ecal/core/include/ecal/ecal_publisher.h | 2 +- ecal/core/include/ecal/msg/capnproto/publisher.h | 4 ++-- .../core/include/ecal/msg/protobuf/dynamic_publisher.h | 4 ++-- ecal/core/include/ecal/msg/protobuf/publisher.h | 6 +++--- ecal/core/include/ecal/msg/publisher.h | 10 +++++----- ecal/core/src/pubsub/ecal_publisher.cpp | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/ecal/core/include/ecal/ecal_publisher.h b/ecal/core/include/ecal/ecal_publisher.h index d086db3f80..1a1eac0ed8 100644 --- a/ecal/core/include/ecal/ecal_publisher.h +++ b/ecal/core/include/ecal/ecal_publisher.h @@ -168,7 +168,7 @@ namespace eCAL * @return True if succeeded, false if not. **/ ECAL_API_EXPORTED_MEMBER - bool RemEventCallback(eCAL_Publisher_Event type_); + bool RemoveEventCallback(eCAL_Publisher_Event type_); /** * @brief Query if the publisher is subscribed. diff --git a/ecal/core/include/ecal/msg/capnproto/publisher.h b/ecal/core/include/ecal/msg/capnproto/publisher.h index 319ab927b6..b49c2f2b7c 100644 --- a/ecal/core/include/ecal/msg/capnproto/publisher.h +++ b/ecal/core/include/ecal/msg/capnproto/publisher.h @@ -160,10 +160,10 @@ namespace eCAL return root_builder; } - void Send() + bool Send() { CPayload payload{ *builder }; - eCAL::v5::CPublisher::Send(payload); + return eCAL::v5::CPublisher::Send(payload); } private: diff --git a/ecal/core/include/ecal/msg/protobuf/dynamic_publisher.h b/ecal/core/include/ecal/msg/protobuf/dynamic_publisher.h index 2ff702f89a..46d55bd08f 100644 --- a/ecal/core/include/ecal/msg/protobuf/dynamic_publisher.h +++ b/ecal/core/include/ecal/msg/protobuf/dynamic_publisher.h @@ -86,9 +86,9 @@ namespace eCAL * * @param time_ Optional time stamp. * - * @return Number of bytes sent. + * @return True if succeeded, false if not. **/ - size_t Send(long long time_ = -1) + bool Send(long long time_ = -1) { return CMsgPublisher::Send(*m_msg, time_); } diff --git a/ecal/core/include/ecal/msg/protobuf/publisher.h b/ecal/core/include/ecal/msg/protobuf/publisher.h index 18ab976b6d..d3acf442c9 100644 --- a/ecal/core/include/ecal/msg/protobuf/publisher.h +++ b/ecal/core/include/ecal/msg/protobuf/publisher.h @@ -155,12 +155,12 @@ namespace eCAL * @param msg_ The message object. * @param time_ Time stamp. * - * @return Number of bytes sent. + * @return True if succeeded, false if not. **/ - size_t Send(const T& msg_, long long time_ = DEFAULT_TIME_ARGUMENT) + bool Send(const T& msg_, long long time_ = DEFAULT_TIME_ARGUMENT) { CPayload payload{ msg_ }; - return eCAL::v5::CPublisher::Send(payload, time_); + return (eCAL::v5::CPublisher::Send(payload, time_) > 0); } private: diff --git a/ecal/core/include/ecal/msg/publisher.h b/ecal/core/include/ecal/msg/publisher.h index 2236c64f6c..900a9b57b0 100644 --- a/ecal/core/include/ecal/msg/publisher.h +++ b/ecal/core/include/ecal/msg/publisher.h @@ -130,9 +130,9 @@ namespace eCAL * @param msg_ The message object. * @param time_ Optional time stamp. * - * @return Number of bytes sent. + * @return True if succeeded, false if not. **/ - size_t Send(const T& msg_, long long time_ = DEFAULT_TIME_ARGUMENT) + bool Send(const T& msg_, long long time_ = DEFAULT_TIME_ARGUMENT) { // this is an optimization ... // if there is no subscription we do not waste time for @@ -141,7 +141,7 @@ namespace eCAL // counting and frequency calculation for the monitoring layer if (!IsSubscribed()) { - return(CPublisher::Send(nullptr, 0, time_)); + return(eCAL::v5::CPublisher::Send(nullptr, 0, time_) > 0); } // if we have a subscription allocate memory for the @@ -153,13 +153,13 @@ namespace eCAL m_buffer.resize(size); if (Serialize(msg_, m_buffer.data(), m_buffer.size())) { - return(CPublisher::Send(m_buffer.data(), size, time_)); + return(eCAL::v5::CPublisher::Send(m_buffer.data(), size, time_) > 0); } } else { // send a zero payload length message to trigger the subscriber side - return(CPublisher::Send(nullptr, 0, time_)); + return(eCAL::v5::CPublisher::Send(nullptr, 0, time_) > 0); } return(0); } diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index ba1ee61175..90f4cfff4a 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -109,11 +109,11 @@ namespace eCAL bool CPublisher::AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_) { - RemEventCallback(type_); + RemoveEventCallback(type_); return(m_publisher_impl->AddEventCallback(type_, callback_)); } - bool CPublisher::RemEventCallback(eCAL_Publisher_Event type_) + bool CPublisher::RemoveEventCallback(eCAL_Publisher_Event type_) { return(m_publisher_impl->RemEventCallback(type_)); } From be1a9dc4efc7f5bdc01061d6fdcfb01c5df338ef Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 13 Dec 2024 13:43:17 +0100 Subject: [PATCH 13/19] proto pub test fixed removed test added again --- .../src/proto_publisher_test.cpp | 72 +++++++++++++++++++ .../src/proto_subscriber_test.cpp | 9 ++- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/ecal/tests/cpp/pubsub_proto_test/src/proto_publisher_test.cpp b/ecal/tests/cpp/pubsub_proto_test/src/proto_publisher_test.cpp index e69de29bb2..a186107ac0 100644 --- a/ecal/tests/cpp/pubsub_proto_test/src/proto_publisher_test.cpp +++ b/ecal/tests/cpp/pubsub_proto_test/src/proto_publisher_test.cpp @@ -0,0 +1,72 @@ +/* ========================= 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 ================================= +*/ + +// std headers +// used libraries +#include +// own project +#include +#include + +#include +#include + +class ProtoPublisherTest : public ::testing::Test { +public: + + ProtoPublisherTest() { + eCAL::Initialize(); + } + + ~ProtoPublisherTest() override { + eCAL::Finalize(); + } + +}; +using core_cpp_pubsub_proto_pub = ProtoPublisherTest; + +TEST_F(core_cpp_pubsub_proto_pub, ProtoPublisherTest_MoveAssignment) +{ + eCAL::protobuf::CPublisher person_pub("ProtoPublisherTest"); + + ASSERT_TRUE(person_pub.IsCreated()); + + eCAL::protobuf::CPublisherperson_moved; + + ASSERT_FALSE(person_moved.IsCreated()); + + person_moved = std::move(person_pub); + + // New Subscriber must be initialized + ASSERT_TRUE(person_moved.IsCreated()); + // Old subscriber is not initialized + ASSERT_FALSE(person_pub.IsCreated()); +} + +TEST_F(core_cpp_pubsub_proto_pub, ProtoPublisherTest_MoveConstruction) +{ + eCAL::protobuf::CPublisher person_pub("ProtoPublisherTest"); + + ASSERT_TRUE(person_pub.IsCreated()); + + eCAL::protobuf::CPublisherperson_moved{ std::move(person_pub) }; + + ASSERT_TRUE(person_moved.IsCreated()); + ASSERT_FALSE(person_pub.IsCreated()); +} diff --git a/ecal/tests/cpp/pubsub_proto_test/src/proto_subscriber_test.cpp b/ecal/tests/cpp/pubsub_proto_test/src/proto_subscriber_test.cpp index 50032dc0ae..3c2f937719 100644 --- a/ecal/tests/cpp/pubsub_proto_test/src/proto_subscriber_test.cpp +++ b/ecal/tests/cpp/pubsub_proto_test/src/proto_subscriber_test.cpp @@ -47,7 +47,7 @@ class ProtoSubscriberTest : public ::testing::Test { eCAL::Finalize(); } - size_t SendPerson(eCAL::protobuf::CPublisher& pub) + bool SendPerson(eCAL::protobuf::CPublisher& pub) { p.set_id(1); p.set_name("Max"); @@ -87,11 +87,10 @@ TEST_F(core_cpp_pubsub_proto_sub, ProtoSubscriberTest_SendReceive) std::this_thread::sleep_for(std::chrono::milliseconds(2000)); - auto bytes_send = SendPerson(person_pub); + ASSERT_TRUE(SendPerson(person_pub)); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // assert that the OnPerson callback has been called once. ASSERT_EQ(1, received_callbacks); - ASSERT_EQ(bytes_send, GetPersonSize()); } TEST_F(core_cpp_pubsub_proto_sub, ProtoSubscriberTest_MoveAssignment) @@ -119,7 +118,7 @@ TEST_F(core_cpp_pubsub_proto_sub, ProtoSubscriberTest_MoveAssignment) std::this_thread::sleep_for(std::chrono::milliseconds(2000)); - SendPerson(person_pub); + ASSERT_TRUE(SendPerson(person_pub)); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // assert that the OnPerson callback has been called once. ASSERT_EQ(1, received_callbacks); @@ -145,7 +144,7 @@ TEST_F(core_cpp_pubsub_proto_sub, ProtoSubscriberTest_MoveConstruction) std::this_thread::sleep_for(std::chrono::milliseconds(2000)); - SendPerson(person_pub); + ASSERT_TRUE(SendPerson(person_pub)); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // assert that the OnPerson callback has been called once. ASSERT_EQ(1, received_callbacks); From dd361b41f5f051c47f462567375778f0aeaa9682 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 13 Dec 2024 16:28:01 +0100 Subject: [PATCH 14/19] IsSubscribed removed (GetSubscriberCount) --- ecal/core/include/ecal/ecal_publisher.h | 72 +++------- ecal/core/src/pubsub/ecal_pubgate.cpp | 3 +- ecal/core/src/pubsub/ecal_publisher.cpp | 36 ++--- ecal/core/src/pubsub/ecal_publisher_impl.cpp | 126 ++++++++++-------- ecal/core/src/pubsub/ecal_publisher_impl.h | 26 ++-- .../src/pubsub_callback_topicid.cpp | 4 +- .../src/pubsub_connection_test.cpp | 6 +- 7 files changed, 124 insertions(+), 149 deletions(-) diff --git a/ecal/core/include/ecal/ecal_publisher.h b/ecal/core/include/ecal/ecal_publisher.h index 1a1eac0ed8..357459be8b 100644 --- a/ecal/core/include/ecal/ecal_publisher.h +++ b/ecal/core/include/ecal/ecal_publisher.h @@ -46,30 +46,6 @@ namespace eCAL { /** * @brief eCAL publisher class. - * - * The CPublisher class is used to send topics to matching eCAL subscribers. The topic is created automatically by the constructor - * or by the Create member function. - *
- *
- * For sending the topic payload the publisher class provides an overloaded Send method. The first one is sending the payload as - * a std::string. The second needs a preallocated buffer described by a buffer address and a buffer length. The publisher is not - * taking the ownership for the allocated memory buffer. - *
- *
- * An optional time stamp can be attached to the topic payload. - * - **/ - /** - * @code - * // create publisher, topic name "A" - * eCAL::CPublisher pub("A"); - * - * // send string - * std::string send_s = "Hello World "; - * - * // send content - * size_t snd_len = pub.Send(send_s); - * @endcode **/ class ECAL_API_CLASS CPublisher { @@ -87,6 +63,17 @@ namespace eCAL ECAL_API_EXPORTED_MEMBER CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_ = SDataTypeInformation(), const Publisher::Configuration& config_ = GetPublisherConfiguration()); + /** + * @brief Constructor. + * + * @param topic_name_ Unique topic name. + * @param data_type_info_ Topic data type information (encoding, type, descriptor). + * @param event_callback_ The publisher event callback funtion. + * @param config_ Optional configuration parameters. + **/ + ECAL_API_EXPORTED_MEMBER + CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const PubEventIDCallbackT event_callback_, const Publisher::Configuration& config_ = GetPublisherConfiguration()); + /** * @brief Destructor. **/ @@ -149,35 +136,6 @@ namespace eCAL ECAL_API_EXPORTED_MEMBER bool Send(const std::string& payload_, long long time_ = DEFAULT_TIME_ARGUMENT); - /** - * @brief Add callback function for publisher events. - * - * @param type_ The event type to react on. - * @param callback_ The callback function to add. - * - * @return True if succeeded, false if not. - **/ - ECAL_API_EXPORTED_MEMBER - bool AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_); - - /** - * @brief Remove callback function for publisher events. - * - * @param type_ The event type to remove. - * - * @return True if succeeded, false if not. - **/ - ECAL_API_EXPORTED_MEMBER - bool RemoveEventCallback(eCAL_Publisher_Event type_); - - /** - * @brief Query if the publisher is subscribed. - * - * @return true if subscribed, false if not. - **/ - ECAL_API_EXPORTED_MEMBER - bool IsSubscribed() const; - /** * @brief Query the number of subscribers. * @@ -187,7 +145,7 @@ namespace eCAL size_t GetSubscriberCount() const; /** - * @brief Gets name of the connected topic. + * @brief Retrieve the topic name. * * @return The topic name. **/ @@ -195,12 +153,12 @@ namespace eCAL std::string GetTopicName() const; /** - * @brief Gets a unique ID of this Publisher + * @brief Retrieve the topic id. * - * @return The publisher id. + * @return The topic id. **/ ECAL_API_EXPORTED_MEMBER - Registration::STopicId GetPublisherId() const; + Registration::STopicId GetTopicId() const; /** * @brief Gets description of the connected topic. diff --git a/ecal/core/src/pubsub/ecal_pubgate.cpp b/ecal/core/src/pubsub/ecal_pubgate.cpp index f50349eb42..cd8fcc62be 100644 --- a/ecal/core/src/pubsub/ecal_pubgate.cpp +++ b/ecal/core/src/pubsub/ecal_pubgate.cpp @@ -161,13 +161,14 @@ namespace eCAL if (topic_name.empty()) return; const auto& subscription_info = ecal_sample_.identifier; + const SDataTypeInformation& topic_information = ecal_topic.tdatatype; // unregister subscriber const std::shared_lock lock(m_topic_name_publisher_sync); auto res = m_topic_name_publisher_map.equal_range(topic_name); for (TopicNamePublisherMapT::const_iterator iter = res.first; iter != res.second; ++iter) { - iter->second->RemoveSubscription(subscription_info); + iter->second->RemoveSubscription(subscription_info, topic_information); } } diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index 90f4cfff4a..bd66f2698a 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -47,6 +47,18 @@ namespace eCAL if(g_pubgate() != nullptr) g_pubgate()->Register(topic_name_, m_publisher_impl); } + CPublisher::CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const PubEventIDCallbackT event_callback_, const Publisher::Configuration& config_) + { + // create publisher implementation + m_publisher_impl = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); + + // register publisher + if (g_pubgate() != nullptr) g_pubgate()->Register(topic_name_, m_publisher_impl); + + // add event callback for all current event types + m_publisher_impl->AddEventIDCallback(event_callback_); + } + CPublisher::~CPublisher() { // could be already destroyed by move @@ -87,7 +99,7 @@ namespace eCAL // or we do not have any subscription at all // then the data writer will only do some statistics // for the monitoring layer and return - if (!IsSubscribed()) + if (GetSubscriberCount() == 0) { m_publisher_impl->RefreshSendCounter(); return(payload_.GetSize()); @@ -107,26 +119,6 @@ namespace eCAL return(Send(payload_.data(), payload_.size(), time_)); } - bool CPublisher::AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT& callback_) - { - RemoveEventCallback(type_); - return(m_publisher_impl->AddEventCallback(type_, callback_)); - } - - bool CPublisher::RemoveEventCallback(eCAL_Publisher_Event type_) - { - return(m_publisher_impl->RemEventCallback(type_)); - } - - bool CPublisher::IsSubscribed() const - { -#if ECAL_CORE_REGISTRATION - return(m_publisher_impl->IsSubscribed()); -#else // ECAL_CORE_REGISTRATION - return(true); -#endif // ECAL_CORE_REGISTRATION - } - size_t CPublisher::GetSubscriberCount() const { return(m_publisher_impl->GetSubscriberCount()); @@ -137,7 +129,7 @@ namespace eCAL return(m_publisher_impl->GetTopicName()); } - Registration::STopicId CPublisher::GetPublisherId() const + Registration::STopicId CPublisher::GetTopicId() const { return(m_publisher_impl->GetId()); } diff --git a/ecal/core/src/pubsub/ecal_publisher_impl.cpp b/ecal/core/src/pubsub/ecal_publisher_impl.cpp index fae641681e..22c15f1a46 100644 --- a/ecal/core/src/pubsub/ecal_publisher_impl.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_impl.cpp @@ -141,13 +141,13 @@ namespace eCAL // clear subscriber maps { - const std::lock_guard lock(m_connection_map_mtx); + const std::lock_guard lock(m_connection_map_mutex); m_connection_map.clear(); } // clear event callback map { - const std::lock_guard lock(m_event_callback_map_mtx); + const std::lock_guard lock(m_event_callback_map_mutex); m_event_callback_map.clear(); } @@ -391,7 +391,7 @@ namespace eCAL return(true); } - bool CPublisherImpl::AddEventCallback(eCAL_Publisher_Event type_, PubEventCallbackT callback_) + bool CPublisherImpl::AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT callback_) { if (!m_created) return(false); @@ -401,7 +401,7 @@ namespace eCAL // log it Logging::Log(log_level_debug2, m_attributes.topic_name + "::CDataWriter::AddEventCallback"); #endif - const std::lock_guard lock(m_event_callback_map_mtx); + const std::lock_guard lock(m_event_callback_map_mutex); m_event_callback_map[type_] = std::move(callback_); } @@ -418,13 +418,29 @@ namespace eCAL // log it Logging::Log(log_level_debug2, m_attributes.topic_name + "::CDataWriter::RemEventCallback"); #endif - const std::lock_guard lock(m_event_callback_map_mtx); + const std::lock_guard lock(m_event_callback_map_mutex); m_event_callback_map[type_] = nullptr; } return(true); } + bool CPublisherImpl::AddEventIDCallback(const PubEventIDCallbackT callback_) + { + if (!m_created) return false; + const std::lock_guard lock(m_event_id_callback_mutex); + m_event_id_callback = callback_; + return true; + } + + bool CPublisherImpl::RemEventIDCallback() + { + if (!m_created) return false; + const std::lock_guard lock(m_event_id_callback_mutex); + m_event_id_callback = nullptr; + return true; + } + void CPublisherImpl::ApplySubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_, const SLayerStates& sub_layer_states_, const std::string& reader_par_) { // collect layer states @@ -488,7 +504,7 @@ namespace eCAL bool is_new_connection = false; bool is_updated_connection = false; { - const std::lock_guard lock(m_connection_map_mtx); + const std::lock_guard lock(m_connection_map_mutex); auto subscription_info_iter = m_connection_map.find(subscription_info_); if (subscription_info_iter == m_connection_map.end()) @@ -526,12 +542,12 @@ namespace eCAL if (is_new_connection) { // fire connect event - FireConnectEvent(subscription_info_.entity_id, data_type_info_); + FireConnectEvent(subscription_info_, data_type_info_); } else if (is_updated_connection) { // fire update event - FireUpdateEvent(subscription_info_.entity_id, data_type_info_); + FireUpdateEvent(subscription_info_, data_type_info_); } #ifndef NDEBUG @@ -540,7 +556,7 @@ namespace eCAL #endif } - void CPublisherImpl::RemoveSubscription(const SSubscriptionInfo& subscription_info_) + void CPublisherImpl::RemoveSubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_) { // remove subscription #if ECAL_CORE_TRANSPORT_UDP @@ -553,23 +569,18 @@ namespace eCAL if (m_writer_tcp) m_writer_tcp->RemoveSubscription(subscription_info_.host_name, subscription_info_.process_id, subscription_info_.entity_id); #endif - // remove key from connection map - bool last_connection_gone(false); { - const std::lock_guard lock(m_connection_map_mtx); + const std::lock_guard lock(m_connection_map_mutex); + // remove key from connection map m_connection_map.erase(subscription_info_); - last_connection_gone = m_connection_map.empty(); // update connection count m_connection_count = GetConnectionCount(); } - if (last_connection_gone) - { - // fire disconnect event - FireDisconnectEvent(); - } + // fire disconnect event + FireDisconnectEvent(subscription_info_, data_type_info_); #ifndef NDEBUG // log it @@ -586,7 +597,7 @@ namespace eCAL { // we should think about if we would like to potentially use the `time_` variable to tick with (but we would need the same base for checking incoming samples then.... const auto send_time = std::chrono::steady_clock::now(); - const std::lock_guard lock(m_frequency_calculator_mtx); + const std::lock_guard lock(m_frequency_calculator_mutex); m_frequency_calculator.addTick(send_time); } } @@ -738,7 +749,7 @@ namespace eCAL size_t loc_connections(0); size_t ext_connections(0); { - const std::lock_guard lock(m_connection_map_mtx); + const std::lock_guard lock(m_connection_map_mutex); for (const auto& sub : m_connection_map) { if (sub.first.host_name == m_attributes.host_name) @@ -768,50 +779,53 @@ namespace eCAL ecal_reg_sample_topic.uname = m_attributes.unit_name; } - void CPublisherImpl::FireConnectEvent(const std::string& tid_, const SDataTypeInformation& tinfo_) + void CPublisherImpl::FireEvent(const eCAL_Publisher_Event type_, const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& tinfo_) { - const std::lock_guard lock(m_event_callback_map_mtx); - auto iter = m_event_callback_map.find(pub_event_connected); - if (iter != m_event_callback_map.end() && iter->second) + SPubEventCallbackData data; + data.type = type_; + data.time = std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); + data.clock = 0; + data.tid = subscription_info_.entity_id; + data.tdatatype = tinfo_; + + // new event handling with topic id { - SPubEventCallbackData data; - data.type = pub_event_connected; - data.time = std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); - data.clock = 0; - data.tid = tid_; - data.tdatatype = tinfo_; - (iter->second)(m_attributes.topic_name.c_str(), &data); + Registration::STopicId topic_id; + topic_id.topic_id.entity_id = subscription_info_.entity_id; + topic_id.topic_id.process_id = subscription_info_.process_id; + topic_id.topic_id.host_name = subscription_info_.host_name; + topic_id.topic_name = m_attributes.topic_name; + const std::lock_guard lock(m_event_id_callback_mutex); + if (m_event_id_callback) + { + m_event_id_callback(topic_id, data); + } } - } - void CPublisherImpl::FireUpdateEvent(const std::string& tid_, const SDataTypeInformation& tinfo_) - { - const std::lock_guard lock(m_event_callback_map_mtx); - auto iter = m_event_callback_map.find(pub_event_update_connection); - if (iter != m_event_callback_map.end() && iter->second) + // deprecated event handling with topic name { - SPubEventCallbackData data; - data.type = pub_event_update_connection; - data.time = std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); - data.clock = 0; - data.tid = tid_; - data.tdatatype = tinfo_; - (iter->second)(m_attributes.topic_name.c_str(), &data); + const std::lock_guard lock(m_event_callback_map_mutex); + auto iter = m_event_callback_map.find(type_); + if (iter != m_event_callback_map.end() && iter->second) + { + (iter->second)(m_attributes.topic_name.c_str(), &data); + } } } - void CPublisherImpl::FireDisconnectEvent() + void CPublisherImpl::FireConnectEvent(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& tinfo_) { - const std::lock_guard lock(m_event_callback_map_mtx); - auto iter = m_event_callback_map.find(pub_event_disconnected); - if (iter != m_event_callback_map.end() && iter->second) - { - SPubEventCallbackData data; - data.type = pub_event_disconnected; - data.time = std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); - data.clock = 0; - (iter->second)(m_attributes.topic_name.c_str(), &data); - } + FireEvent(pub_event_connected, subscription_info_, tinfo_); + } + + void CPublisherImpl::FireUpdateEvent(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& tinfo_) + { + FireEvent(pub_event_update_connection, subscription_info_, tinfo_); + } + + void CPublisherImpl::FireDisconnectEvent(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& tinfo_) + { + FireEvent(pub_event_disconnected, subscription_info_, tinfo_); } size_t CPublisherImpl::GetConnectionCount() @@ -975,7 +989,7 @@ namespace eCAL int32_t CPublisherImpl::GetFrequency() { const auto frequency_time = std::chrono::steady_clock::now(); - const std::lock_guard lock(m_frequency_calculator_mtx); + const std::lock_guard lock(m_frequency_calculator_mutex); return static_cast(m_frequency_calculator.getFrequency(frequency_time) * 1000); } } diff --git a/ecal/core/src/pubsub/ecal_publisher_impl.h b/ecal/core/src/pubsub/ecal_publisher_impl.h index 61d10fafd3..0587301597 100644 --- a/ecal/core/src/pubsub/ecal_publisher_impl.h +++ b/ecal/core/src/pubsub/ecal_publisher_impl.h @@ -83,14 +83,19 @@ namespace eCAL bool SetDataTypeInformation(const SDataTypeInformation& topic_info_); - bool AddEventCallback(eCAL_Publisher_Event type_, PubEventCallbackT callback_); + // deprecated event callback interface + bool AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT callback_); bool RemEventCallback(eCAL_Publisher_Event type_); + // future event callback interface + bool AddEventIDCallback(const PubEventIDCallbackT callback_); + bool RemEventIDCallback(); + bool SetAttribute(const std::string& attr_name_, const std::string& attr_value_); bool ClearAttribute(const std::string& attr_name_); void ApplySubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_, const SLayerStates& sub_layer_states_, const std::string& reader_par_); - void RemoveSubscription(const SSubscriptionInfo& subscription_info_); + void RemoveSubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_); void GetRegistration(Registration::Sample& sample); void RefreshSendCounter(); @@ -128,9 +133,11 @@ namespace eCAL void StopAllLayer(); - void FireConnectEvent(const std::string& tid_, const SDataTypeInformation& tinfo_); - void FireUpdateEvent(const std::string& tid_, const SDataTypeInformation& tinfo_); - void FireDisconnectEvent(); + void FireEvent(const eCAL_Publisher_Event type_, const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& tinfo_); + + void FireConnectEvent (const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& tinfo_); + void FireUpdateEvent (const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& tinfo_); + void FireDisconnectEvent(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& tinfo_); size_t GetConnectionCount(); @@ -155,18 +162,21 @@ namespace eCAL bool state = false; }; using SSubscriptionMapT = std::map; - mutable std::mutex m_connection_map_mtx; + mutable std::mutex m_connection_map_mutex; SSubscriptionMapT m_connection_map; std::atomic m_connection_count{ 0 }; using EventCallbackMapT = std::map; - std::mutex m_event_callback_map_mtx; + std::mutex m_event_callback_map_mutex; EventCallbackMapT m_event_callback_map; + std::mutex m_event_id_callback_mutex; + PubEventIDCallbackT m_event_id_callback; + long long m_id = 0; long long m_clock = 0; - std::mutex m_frequency_calculator_mtx; + std::mutex m_frequency_calculator_mutex; ResettableFrequencyCalculator m_frequency_calculator; #if ECAL_CORE_TRANSPORT_UDP diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp index ea2054ac83..821268f33f 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_callback_topicid.cpp @@ -128,7 +128,7 @@ TEST_P(TestFixture, OnePubSub) callback_datatype_info = datatype_info_; } ); - const auto pub_id = publisher.GetPublisherId(); + const auto pub_id = publisher.GetTopicId(); // let them match eCAL::Process::SleepMS(2 * config.registration.registration_refresh); @@ -175,7 +175,7 @@ TEST_P(TestFixture, MultiplePubSub) for (int i = 0; i < num_publishers; ++i) { auto& publisher = publishers[i]; - const auto pub_id = publisher.GetPublisherId(); + const auto pub_id = publisher.GetTopicId(); const auto pub_datatype_info = publisher.GetDataTypeInformation(); // send data diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp index 0da9ef6ac5..c28f1159a2 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_connection_test.cpp @@ -51,7 +51,7 @@ TEST(core_cpp_pubsub, TestSubscriberIsPublishedTiming) { if (pub_count == 0) { - subscriber_seen_at_publication_start = pub.IsSubscribed(); + subscriber_seen_at_publication_start = pub.GetSubscriberCount() > 0; } pub.Send(std::to_string(pub_count)); @@ -146,7 +146,7 @@ TEST(core_cpp_pubsub, TestPublisherIsSubscribedTiming) const auto max_runs(1000); while (eCAL::Ok()) { - if (pub.IsSubscribed()) + if (pub.GetSubscriberCount() > 0) { do_start_publication = true; } @@ -236,7 +236,7 @@ TEST(core_cpp_pubsub, TestChainedPublisherSubscriberCallback) // Publisher1 in thread 1 auto publisher1_function = [&publisher1_sent_count, &message_count]() { eCAL::CPublisher pub1("topic1"); - while (!pub1.IsSubscribed()) + while (pub1.GetSubscriberCount() == 0) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } From cd8d045d47151574525c23b5a5fee9f8188905d7 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 13 Dec 2024 16:50:04 +0100 Subject: [PATCH 15/19] pointer check --- ecal/core/src/pubsub/ecal_publisher.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index bd66f2698a..0fe1de4f15 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -94,7 +94,9 @@ namespace eCAL bool CPublisher::Send(CPayloadWriter& payload_, long long time_) { - // in an optimization case the + if (m_publisher_impl == nullptr) return false; + + // in an optimization case the // publisher can send an empty package // or we do not have any subscription at all // then the data writer will only do some statistics @@ -121,21 +123,25 @@ namespace eCAL size_t CPublisher::GetSubscriberCount() const { + if (m_publisher_impl == nullptr) return 0; return(m_publisher_impl->GetSubscriberCount()); } std::string CPublisher::GetTopicName() const { + if (m_publisher_impl == nullptr) return ""; return(m_publisher_impl->GetTopicName()); } Registration::STopicId CPublisher::GetTopicId() const { + if (m_publisher_impl == nullptr) return Registration::STopicId(); return(m_publisher_impl->GetId()); } SDataTypeInformation CPublisher::GetDataTypeInformation() const { + if (m_publisher_impl == nullptr) return SDataTypeInformation(); return(m_publisher_impl->GetDataTypeInformation()); } } From 443eb1683b769f711496ebed1532249e08cc3726 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Mon, 16 Dec 2024 11:13:58 +0100 Subject: [PATCH 16/19] renaming, Send() return bool instead size --- ecal/core/src/pubsub/ecal_pubgate.cpp | 8 ++-- ecal/core/src/pubsub/ecal_pubgate.h | 4 +- ecal/core/src/pubsub/ecal_publisher.cpp | 9 ++-- ecal/core/src/pubsub/ecal_publisher_impl.cpp | 48 ++----------------- ecal/core/src/pubsub/ecal_publisher_impl.h | 10 ++-- ecal/core/src/pubsub/ecal_publisher_v5.cpp | 6 +-- ...ecal_registration_sample_applier_gates.cpp | 4 +- .../cpp/pubsub_test/src/pubsub_test_shm.cpp | 8 ++-- .../cpp/pubsub_test/src/pubsub_test_udp.cpp | 4 +- 9 files changed, 28 insertions(+), 73 deletions(-) diff --git a/ecal/core/src/pubsub/ecal_pubgate.cpp b/ecal/core/src/pubsub/ecal_pubgate.cpp index cd8fcc62be..c4fd19a01a 100644 --- a/ecal/core/src/pubsub/ecal_pubgate.cpp +++ b/ecal/core/src/pubsub/ecal_pubgate.cpp @@ -92,7 +92,7 @@ namespace eCAL return(ret_state); } - void CPubGate::ApplySubRegistration(const Registration::Sample& ecal_sample_) + void CPubGate::ApplySubscriberRegistration(const Registration::Sample& ecal_sample_) { if(!m_created) return; @@ -146,11 +146,11 @@ namespace eCAL auto res = m_topic_name_publisher_map.equal_range(topic_name); for(TopicNamePublisherMapT::const_iterator iter = res.first; iter != res.second; ++iter) { - iter->second->ApplySubscription(subscription_info, topic_information, layer_states, reader_par); + iter->second->ApplySubscriberRegistration(subscription_info, topic_information, layer_states, reader_par); } } - void CPubGate::ApplySubUnregistration(const Registration::Sample& ecal_sample_) + void CPubGate::ApplySubscriberUnregistration(const Registration::Sample& ecal_sample_) { if (!m_created) return; @@ -168,7 +168,7 @@ namespace eCAL auto res = m_topic_name_publisher_map.equal_range(topic_name); for (TopicNamePublisherMapT::const_iterator iter = res.first; iter != res.second; ++iter) { - iter->second->RemoveSubscription(subscription_info, topic_information); + iter->second->ApplySubscriberUnregistration(subscription_info, topic_information); } } diff --git a/ecal/core/src/pubsub/ecal_pubgate.h b/ecal/core/src/pubsub/ecal_pubgate.h index 7b1f93b5f1..c959e1f86e 100644 --- a/ecal/core/src/pubsub/ecal_pubgate.h +++ b/ecal/core/src/pubsub/ecal_pubgate.h @@ -47,8 +47,8 @@ namespace eCAL bool Register(const std::string& topic_name_, const std::shared_ptr& publisher_); bool Unregister(const std::string& topic_name_, const std::shared_ptr& publisher_); - void ApplySubRegistration(const Registration::Sample& ecal_sample_); - void ApplySubUnregistration(const Registration::Sample& ecal_sample_); + void ApplySubscriberRegistration(const Registration::Sample& ecal_sample_); + void ApplySubscriberUnregistration(const Registration::Sample& ecal_sample_); void GetRegistrations(Registration::SampleList& reg_sample_list_); diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index 0fe1de4f15..bbad8d80c9 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -104,16 +104,13 @@ namespace eCAL if (GetSubscriberCount() == 0) { m_publisher_impl->RefreshSendCounter(); - return(payload_.GetSize()); + // we return false here to indicate that we did not really send something + return false; } // send content via data writer layer const long long write_time = (time_ == DEFAULT_TIME_ARGUMENT) ? eCAL::Time::GetMicroSeconds() : time_; - const size_t written_bytes = m_publisher_impl->Write(payload_, write_time, 0); - - // TODO: change CPublisherImpl::Write to return a bool - // return true if we have written something - return written_bytes > 0; + return m_publisher_impl->Write(payload_, write_time, 0); } bool CPublisher::Send(const std::string& payload_, long long time_) diff --git a/ecal/core/src/pubsub/ecal_publisher_impl.cpp b/ecal/core/src/pubsub/ecal_publisher_impl.cpp index 22c15f1a46..00bdc0ec1a 100644 --- a/ecal/core/src/pubsub/ecal_publisher_impl.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_impl.cpp @@ -125,16 +125,7 @@ namespace eCAL Logging::Log(log_level_debug1, m_attributes.topic_name + "::CDataWriter::Destructor"); #endif - Stop(); - } - - bool CPublisherImpl::Stop() - { - if (!m_created) return false; -#ifndef NDEBUG - // log it - Logging::Log(log_level_debug1, m_attributes.topic_name + "::CDataWriter::Stop"); -#endif + if (!m_created) return; // stop all transport layer StopAllLayer(); @@ -156,11 +147,9 @@ namespace eCAL // and unregister Unregister(); - - return true; } - size_t CPublisherImpl::Write(CPayloadWriter& payload_, long long time_, long long filter_id_) + bool CPublisherImpl::Write(CPayloadWriter& payload_, long long time_, long long filter_id_) { // get payload buffer size (one time, to avoid multiple computations) const size_t payload_buf_size(payload_.GetSize()); @@ -351,8 +340,7 @@ namespace eCAL #endif // ECAL_CORE_TRANSPORT_TCP // return success - if (written) return payload_buf_size; - else return 0; + return written; } bool CPublisherImpl::SetDataTypeInformation(const SDataTypeInformation& topic_info_) @@ -441,7 +429,7 @@ namespace eCAL return true; } - void CPublisherImpl::ApplySubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_, const SLayerStates& sub_layer_states_, const std::string& reader_par_) + void CPublisherImpl::ApplySubscriberRegistration(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_, const SLayerStates& sub_layer_states_, const std::string& reader_par_) { // collect layer states std::vector pub_layers; @@ -466,8 +454,6 @@ namespace eCAL #endif // determine if we need to start a transport layer - // if a new layer gets activated, we reregister for SHM and TCP to force the exchange of connection parameter - // without this forced registration we would need one additional registration loop for these two layers to establish the connection const TLayer::eTransportLayer layer2activate = DetermineTransportLayer2Start(pub_layers, sub_layers, m_attributes.host_name == subscription_info_.host_name); switch (layer2activate) { @@ -556,7 +542,7 @@ namespace eCAL #endif } - void CPublisherImpl::RemoveSubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_) + void CPublisherImpl::ApplySubscriberUnregistration(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_) { // remove subscription #if ECAL_CORE_TRANSPORT_UDP @@ -612,30 +598,6 @@ namespace eCAL return m_connection_count; } - std::string CPublisherImpl::Dump(const std::string& indent_ /* = "" */) - { - std::stringstream out; - - out << '\n'; - out << indent_ << "--------------------------" << '\n'; - out << indent_ << " class CDataWriter " << '\n'; - out << indent_ << "--------------------------" << '\n'; - out << indent_ << "m_host_name: " << m_attributes.host_name << '\n'; - out << indent_ << "m_host_group_name: " << m_attributes.host_group_name << '\n'; - out << indent_ << "m_topic_name: " << m_attributes.topic_name << '\n'; - out << indent_ << "m_topic_id: " << m_topic_id << '\n'; - out << indent_ << "m_topic_info.encoding: " << m_topic_info.encoding << '\n'; - out << indent_ << "m_topic_info.name: " << m_topic_info.name << '\n'; - out << indent_ << "m_topic_info.desc: " << m_topic_info.descriptor << '\n'; - out << indent_ << "m_id: " << m_id << '\n'; - out << indent_ << "m_clock: " << m_clock << '\n'; - out << indent_ << "frequency [mHz]: " << GetFrequency() << '\n'; - out << indent_ << "m_created: " << m_created << '\n'; - out << std::endl; - - return(out.str()); - } - void CPublisherImpl::Register() { #if ECAL_CORE_REGISTRATION diff --git a/ecal/core/src/pubsub/ecal_publisher_impl.h b/ecal/core/src/pubsub/ecal_publisher_impl.h index 0587301597..f6f033a37b 100644 --- a/ecal/core/src/pubsub/ecal_publisher_impl.h +++ b/ecal/core/src/pubsub/ecal_publisher_impl.h @@ -77,9 +77,7 @@ namespace eCAL CPublisherImpl(const SDataTypeInformation& topic_info_, const eCAL::eCALWriter::SAttributes& attr_); ~CPublisherImpl(); - bool Stop(); - - size_t Write(CPayloadWriter& payload_, long long time_, long long filter_id_); + bool Write(CPayloadWriter& payload_, long long time_, long long filter_id_); bool SetDataTypeInformation(const SDataTypeInformation& topic_info_); @@ -94,8 +92,8 @@ namespace eCAL bool SetAttribute(const std::string& attr_name_, const std::string& attr_value_); bool ClearAttribute(const std::string& attr_name_); - void ApplySubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_, const SLayerStates& sub_layer_states_, const std::string& reader_par_); - void RemoveSubscription(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_); + void ApplySubscriberRegistration(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_, const SLayerStates& sub_layer_states_, const std::string& reader_par_); + void ApplySubscriberUnregistration(const SSubscriptionInfo& subscription_info_, const SDataTypeInformation& data_type_info_); void GetRegistration(Registration::Sample& sample); void RefreshSendCounter(); @@ -118,8 +116,6 @@ namespace eCAL const std::string& GetTopicName() const { return(m_attributes.topic_name); } const SDataTypeInformation& GetDataTypeInformation() const { return m_topic_info; } - std::string Dump(const std::string& indent_ = ""); - protected: void Register(); void Unregister(); diff --git a/ecal/core/src/pubsub/ecal_publisher_v5.cpp b/ecal/core/src/pubsub/ecal_publisher_v5.cpp index c20fb07499..37e2ff2e16 100644 --- a/ecal/core/src/pubsub/ecal_publisher_v5.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_v5.cpp @@ -119,8 +119,7 @@ namespace eCAL eCAL::Logging::Log(log_level_debug1, std::string(m_publisher_impl->GetTopicName() + "::CPublisher::Destroy")); #endif - // stop & destroy publisher - m_publisher_impl->Stop(); + // destroy publisher m_publisher_impl.reset(); // we made it :-) @@ -239,7 +238,8 @@ namespace eCAL out << indent_ << "----------------------" << '\n'; out << indent_ << " class CPublisher" << '\n'; out << indent_ << "----------------------" << '\n'; - if((m_publisher_impl != nullptr) && m_publisher_impl->IsCreated()) out << indent_ << m_publisher_impl->Dump(" "); + out << indent_ << "DUMP NOT SUPPORTED ANYMORE" << '\n'; + out << indent_ << "----------------------" << '\n'; out << '\n'; return(out.str()); diff --git a/ecal/core/src/registration/ecal_registration_sample_applier_gates.cpp b/ecal/core/src/registration/ecal_registration_sample_applier_gates.cpp index e761409695..7b64f47efa 100644 --- a/ecal/core/src/registration/ecal_registration_sample_applier_gates.cpp +++ b/ecal/core/src/registration/ecal_registration_sample_applier_gates.cpp @@ -51,10 +51,10 @@ namespace eCAL break; #if ECAL_CORE_PUBLISHER case bct_reg_subscriber: - if (g_pubgate() != nullptr) g_pubgate()->ApplySubRegistration(sample_); + if (g_pubgate() != nullptr) g_pubgate()->ApplySubscriberRegistration(sample_); break; case bct_unreg_subscriber: - if (g_pubgate() != nullptr) g_pubgate()->ApplySubUnregistration(sample_); + if (g_pubgate() != nullptr) g_pubgate()->ApplySubscriberUnregistration(sample_); break; #endif #if ECAL_CORE_SUBSCRIBER diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_test_shm.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_test_shm.cpp index 7f91a9519e..176acd6ac9 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_test_shm.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_test_shm.cpp @@ -82,17 +82,17 @@ TEST(core_cpp_pubsub, ZeroPayloadMessageSHM) g_callback_received_count = 0; // send without zero copy - EXPECT_EQ(send_s.size(), pub1.Send(send_s)); + EXPECT_TRUE(pub1.Send(send_s)); eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); - EXPECT_EQ(send_s.size(), pub1.Send(nullptr, 0)); + EXPECT_TRUE(pub1.Send(nullptr, 0)); eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); // send with zero copy - EXPECT_EQ(send_s.size(), pub2.Send(send_s)); + EXPECT_TRUE(pub2.Send(send_s)); eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); - EXPECT_EQ(send_s.size(), pub2.Send(nullptr, 0)); + EXPECT_TRUE(pub2.Send(nullptr, 0)); eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); // check callback receive diff --git a/ecal/tests/cpp/pubsub_test/src/pubsub_test_udp.cpp b/ecal/tests/cpp/pubsub_test/src/pubsub_test_udp.cpp index eca36fa63f..d6784c1b45 100644 --- a/ecal/tests/cpp/pubsub_test/src/pubsub_test_udp.cpp +++ b/ecal/tests/cpp/pubsub_test/src/pubsub_test_udp.cpp @@ -76,10 +76,10 @@ TEST(core_cpp_pubsub, ZeroPayloadMessageUDP) g_callback_received_bytes = 0; g_callback_received_count = 0; - EXPECT_EQ(send_s.size(), pub.Send(send_s)); + EXPECT_TRUE(pub.Send(send_s)); eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); - EXPECT_EQ(send_s.size(), pub.Send(nullptr, 0)); + EXPECT_TRUE(pub.Send(nullptr, 0)); eCAL::Process::SleepMS(DATA_FLOW_TIME_MS); // check callback receive From 4122a128458960e2a5268c4cc968a04e9feb96fa Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:49:18 +0100 Subject: [PATCH 17/19] _sync -> _mutex Constructor handling (code duplicates) --- ecal/core/src/pubsub/ecal_pubgate.cpp | 12 ++++++------ ecal/core/src/pubsub/ecal_pubgate.h | 2 +- ecal/core/src/pubsub/ecal_publisher.cpp | 9 ++------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/ecal/core/src/pubsub/ecal_pubgate.cpp b/ecal/core/src/pubsub/ecal_pubgate.cpp index c4fd19a01a..88906a720c 100644 --- a/ecal/core/src/pubsub/ecal_pubgate.cpp +++ b/ecal/core/src/pubsub/ecal_pubgate.cpp @@ -55,7 +55,7 @@ namespace eCAL if(!m_created) return; // stop & destroy all remaining publisher - const std::unique_lock lock(m_topic_name_publisher_sync); + const std::unique_lock lock(m_topic_name_publisher_mutex); m_topic_name_publisher_map.clear(); m_created = false; @@ -66,7 +66,7 @@ namespace eCAL if(!m_created) return(false); // register writer and multicast group - const std::unique_lock lock(m_topic_name_publisher_sync); + const std::unique_lock lock(m_topic_name_publisher_mutex); m_topic_name_publisher_map.emplace(std::pair>(topic_name_, publisher_)); return(true); @@ -77,7 +77,7 @@ namespace eCAL if(!m_created) return(false); bool ret_state = false; - const std::unique_lock lock(m_topic_name_publisher_sync); + const std::unique_lock lock(m_topic_name_publisher_mutex); auto res = m_topic_name_publisher_map.equal_range(topic_name_); for(auto iter = res.first; iter != res.second; ++iter) { @@ -142,7 +142,7 @@ namespace eCAL #endif // register subscriber - const std::shared_lock lock(m_topic_name_publisher_sync); + const std::shared_lock lock(m_topic_name_publisher_mutex); auto res = m_topic_name_publisher_map.equal_range(topic_name); for(TopicNamePublisherMapT::const_iterator iter = res.first; iter != res.second; ++iter) { @@ -164,7 +164,7 @@ namespace eCAL const SDataTypeInformation& topic_information = ecal_topic.tdatatype; // unregister subscriber - const std::shared_lock lock(m_topic_name_publisher_sync); + const std::shared_lock lock(m_topic_name_publisher_mutex); auto res = m_topic_name_publisher_map.equal_range(topic_name); for (TopicNamePublisherMapT::const_iterator iter = res.first; iter != res.second; ++iter) { @@ -177,7 +177,7 @@ namespace eCAL if (!m_created) return; // read reader registrations - const std::shared_lock lock(m_topic_name_publisher_sync); + const std::shared_lock lock(m_topic_name_publisher_mutex); for (const auto& iter : m_topic_name_publisher_map) { iter.second->GetRegistration(reg_sample_list_.push_back()); diff --git a/ecal/core/src/pubsub/ecal_pubgate.h b/ecal/core/src/pubsub/ecal_pubgate.h index c959e1f86e..b3d4937508 100644 --- a/ecal/core/src/pubsub/ecal_pubgate.h +++ b/ecal/core/src/pubsub/ecal_pubgate.h @@ -56,7 +56,7 @@ namespace eCAL static std::atomic m_created; using TopicNamePublisherMapT = std::multimap>; - std::shared_timed_mutex m_topic_name_publisher_sync; + std::shared_timed_mutex m_topic_name_publisher_mutex; TopicNamePublisherMapT m_topic_name_publisher_map; }; } diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index bbad8d80c9..efa4d7f402 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -47,14 +47,9 @@ namespace eCAL if(g_pubgate() != nullptr) g_pubgate()->Register(topic_name_, m_publisher_impl); } - CPublisher::CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const PubEventIDCallbackT event_callback_, const Publisher::Configuration& config_) + CPublisher::CPublisher(const std::string& topic_name_, const SDataTypeInformation& data_type_info_, const PubEventIDCallbackT event_callback_, const Publisher::Configuration& config_) : + CPublisher(topic_name_, data_type_info_, config_) { - // create publisher implementation - m_publisher_impl = std::make_shared(data_type_info_, BuildWriterAttributes(topic_name_, config_, GetTransportLayerConfiguration(), GetRegistrationConfiguration())); - - // register publisher - if (g_pubgate() != nullptr) g_pubgate()->Register(topic_name_, m_publisher_impl); - // add event callback for all current event types m_publisher_impl->AddEventIDCallback(event_callback_); } From c684aa1d207a99e7f33f08d105eec865adb809ee Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:31:36 +0100 Subject: [PATCH 18/19] GetId -> GetTopicId RemEventCallback -> RemoveEventCallback std::move(const callback) -> = callback check m_event_id_callback before creating callback structure --- ecal/core/src/pubsub/ecal_publisher.cpp | 2 +- ecal/core/src/pubsub/ecal_publisher_impl.cpp | 11 +++++------ ecal/core/src/pubsub/ecal_publisher_impl.h | 4 ++-- ecal/core/src/pubsub/ecal_publisher_v5.cpp | 4 ++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index efa4d7f402..f743d774d8 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -128,7 +128,7 @@ namespace eCAL Registration::STopicId CPublisher::GetTopicId() const { if (m_publisher_impl == nullptr) return Registration::STopicId(); - return(m_publisher_impl->GetId()); + return(m_publisher_impl->GetTopicId()); } SDataTypeInformation CPublisher::GetDataTypeInformation() const diff --git a/ecal/core/src/pubsub/ecal_publisher_impl.cpp b/ecal/core/src/pubsub/ecal_publisher_impl.cpp index 00bdc0ec1a..c1eb320020 100644 --- a/ecal/core/src/pubsub/ecal_publisher_impl.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_impl.cpp @@ -390,13 +390,13 @@ namespace eCAL Logging::Log(log_level_debug2, m_attributes.topic_name + "::CDataWriter::AddEventCallback"); #endif const std::lock_guard lock(m_event_callback_map_mutex); - m_event_callback_map[type_] = std::move(callback_); + m_event_callback_map[type_] = callback_; } return(true); } - bool CPublisherImpl::RemEventCallback(eCAL_Publisher_Event type_) + bool CPublisherImpl::RemoveEventCallback(eCAL_Publisher_Event type_) { if (!m_created) return(false); @@ -751,6 +751,7 @@ namespace eCAL data.tdatatype = tinfo_; // new event handling with topic id + if(m_event_id_callback) { Registration::STopicId topic_id; topic_id.topic_id.entity_id = subscription_info_.entity_id; @@ -758,10 +759,8 @@ namespace eCAL topic_id.topic_id.host_name = subscription_info_.host_name; topic_id.topic_name = m_attributes.topic_name; const std::lock_guard lock(m_event_id_callback_mutex); - if (m_event_id_callback) - { - m_event_id_callback(topic_id, data); - } + // call event callback + m_event_id_callback(topic_id, data); } // deprecated event handling with topic name diff --git a/ecal/core/src/pubsub/ecal_publisher_impl.h b/ecal/core/src/pubsub/ecal_publisher_impl.h index f6f033a37b..509e980297 100644 --- a/ecal/core/src/pubsub/ecal_publisher_impl.h +++ b/ecal/core/src/pubsub/ecal_publisher_impl.h @@ -83,7 +83,7 @@ namespace eCAL // deprecated event callback interface bool AddEventCallback(eCAL_Publisher_Event type_, const PubEventCallbackT callback_); - bool RemEventCallback(eCAL_Publisher_Event type_); + bool RemoveEventCallback(eCAL_Publisher_Event type_); // future event callback interface bool AddEventIDCallback(const PubEventIDCallbackT callback_); @@ -103,7 +103,7 @@ namespace eCAL bool IsSubscribed() const; size_t GetSubscriberCount() const; - Registration::STopicId GetId() const + Registration::STopicId GetTopicId() const { Registration::STopicId id; id.topic_name = m_attributes.topic_name; diff --git a/ecal/core/src/pubsub/ecal_publisher_v5.cpp b/ecal/core/src/pubsub/ecal_publisher_v5.cpp index 37e2ff2e16..6727d70206 100644 --- a/ecal/core/src/pubsub/ecal_publisher_v5.cpp +++ b/ecal/core/src/pubsub/ecal_publisher_v5.cpp @@ -194,7 +194,7 @@ namespace eCAL bool CPublisher::RemEventCallback(eCAL_Publisher_Event type_) { if (m_publisher_impl == nullptr) return(false); - return(m_publisher_impl->RemEventCallback(type_)); + return(m_publisher_impl->RemoveEventCallback(type_)); } bool CPublisher::IsSubscribed() const @@ -222,7 +222,7 @@ namespace eCAL Registration::STopicId CPublisher::GetId() const { if (m_publisher_impl == nullptr) return{}; - return(m_publisher_impl->GetId()); + return(m_publisher_impl->GetTopicId()); } SDataTypeInformation CPublisher::GetDataTypeInformation() const From 23e7f634746f2bab85a17d8bc3dcfa69a75eaef6 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:35:35 +0100 Subject: [PATCH 19/19] minor logging fix --- ecal/core/src/pubsub/ecal_publisher.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecal/core/src/pubsub/ecal_publisher.cpp b/ecal/core/src/pubsub/ecal_publisher.cpp index f743d774d8..0f767402a3 100644 --- a/ecal/core/src/pubsub/ecal_publisher.cpp +++ b/ecal/core/src/pubsub/ecal_publisher.cpp @@ -63,7 +63,7 @@ namespace eCAL if (g_pubgate() != nullptr) g_pubgate()->Unregister(m_publisher_impl->GetTopicName(), m_publisher_impl); #ifndef NDEBUG - eCAL::Logging::Log(log_level_debug1, std::string(m_publisher_impl->GetTopicName() + "::CPublisher::Destroy")); + eCAL::Logging::Log(log_level_debug1, std::string(m_publisher_impl->GetTopicName() + "::CPublisher::Destructor")); #endif }