Class CSubscriber

Inheritance Relationships

Derived Type

Class Documentation

class eCAL::CSubscriber

eCAL subscriber class.

The CSubscriber class is used to receive topics from matching eCAL publishers.

For receiving the topic payload the subscriber class provides a callback interface and overloaded receive methods.

// create subscriber, topic name "A"
eCAL::CSubscriber sub("A");

//---------------------------------------------------------------------------------------
// receive content with 100 ms timeout as standard string and read receive time stamp
//---------------------------------------------------------------------------------------
std::string rcv_s;
long long time = 0;
size_t rcv_len = sub.Receive(rcv_s, &time, 100);

//---------------------------------------------------------------------------------------
// receive content with a callback functions
//---------------------------------------------------------------------------------------
void OnReceive(const char* topic_name_, const struct eCAL::SReceiveCallbackData* data_)
{
  size_t      size = data_->size;
  const void* data = data_->buf;
  long long   time = data_->time;
}

// add callback
sub.AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2));

// idle main thread
while(eCAL::Ok())
{
  // sleep 100 ms
  std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

Subclassed by eCAL::CMsgSubscriber< T >

Public Functions

CSubscriber()

Constructor.

CSubscriber(const std::string &topic_name_, const std::string &topic_type_ = "", const std::string &topic_desc_ = "")

Constructor.

Parameters
  • topic_name_: Unique topic name.

  • topic_type_: Type name (optional for type checking).

  • topic_desc_: Type description (optional for description checking).

~CSubscriber()

Destructor.

CSubscriber(const CSubscriber&) = delete

CSubscribers are non-copyable.

CSubscriber &operator=(const CSubscriber&) = delete

CSubscribers are non-copyable.

CSubscriber(CSubscriber &&rhs) noexcept

CSubscribers are move-enabled.

CSubscriber &operator=(CSubscriber &&rhs) noexcept

CSubscribers are move-enabled.

bool Create(const std::string &topic_name_, const std::string &topic_type_ = "", const std::string &topic_desc_ = "")

Creates this object.

Return

true if it succeeds, false if it fails.

Parameters
  • topic_name_: Unique topic name.

  • topic_type_: Type name (optional for type checking).

  • topic_desc_: Type description (optional for description checking).

bool Destroy()

Destroys this object.

Return

true if it succeeds, false if it fails.

bool SetQOS(const QOS::SReaderQOS &qos_)

Set subscriber quality of service attributes.

Return

True if it succeeds, false if it fails.

Parameters
  • qos_: Quality of service policies.

QOS::SReaderQOS GetQOS()

Get current subscriber quality of service attributes.

Return

Quality of service attributes.

bool SetID(const std::set<long long> &id_set_)

Set a set of id’s to prefiltering topics (see CPublisher::SetID).

Return

True if it succeeds, false if it fails.

Parameters
  • id_set_: Set of id’s.

size_t Receive(std::string &buf_, long long *time_ = nullptr, int rcv_timeout_ = 0) const

Receive a message from the publisher.

Return

Length of received buffer.

Parameters
  • [out] buf_: Standard string for copying message content.

  • [out] time_: Time from publisher in us (default = nullptr).

  • rcv_timeout_: Maximum time before receive operation returns (in milliseconds, -1 means infinite).

bool AddReceiveCallback(ReceiveCallbackT callback_)

Add callback function for incoming receives.

Return

True if succeeded, false if not.

Parameters
  • callback_: The callback function to add.

bool RemReceiveCallback()

Remove callback function for incoming receives.

Return

True if succeeded, false if not.

bool AddEventCallback(eCAL_Subscriber_Event type_, SubEventCallbackT callback_)

Add callback function for subscriber events.

Return

True if succeeded, false if not.

Parameters
  • type_: The event type to react on.

  • callback_: The callback function to add.

bool RemEventCallback(eCAL_Subscriber_Event type_)

Remove callback function for subscriber events.

Return

True if succeeded, false if not.

Parameters
  • type_: The event type to remove.

bool IsCreated() const

Query if this object is created.

Return

true if created, false if not.

std::string GetTopicName() const

Gets name of the connected topic.

Return

The topic name.

std::string GetTypeName() const

Gets type of the connected topic.

Return

The type name.

std::string GetDescription() const

Gets description of the connected topic.

Return

The description.

bool SetTimeout(int timeout_)

Set the timeout parameter for triggering the timeout callback.

Return

True if succeeded, false if not.

Parameters
  • timeout_: The timeout in milliseconds.

std::string Dump(const std::string &indent_ = "") const

Dump the whole class state into a string.

Return

The dump sting.

Parameters
  • indent_: Indentation used for dump.