Skip to content

Commit

Permalink
IsSubscribed removed (GetSubscriberCount)
Browse files Browse the repository at this point in the history
  • Loading branch information
rex-schilasky committed Dec 13, 2024
1 parent be1a9dc commit dd361b4
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 149 deletions.
72 changes: 15 additions & 57 deletions ecal/core/include/ecal/ecal_publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <br>
* <br>
* 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.
* <br>
* <br>
* 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
{
Expand All @@ -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.
**/
Expand Down Expand Up @@ -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.
*
Expand All @@ -187,20 +145,20 @@ namespace eCAL
size_t GetSubscriberCount() const;

/**
* @brief Gets name of the connected topic.
* @brief Retrieve the topic name.
*
* @return The topic name.
**/
ECAL_API_EXPORTED_MEMBER
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.
Expand Down
3 changes: 2 additions & 1 deletion ecal/core/src/pubsub/ecal_pubgate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::shared_timed_mutex> 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);
}
}

Expand Down
36 changes: 14 additions & 22 deletions ecal/core/src/pubsub/ecal_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CPublisherImpl>(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
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
}
Expand Down
Loading

0 comments on commit dd361b4

Please sign in to comment.