AstraLog-HPC
Loading...
Searching...
No Matches
Connector Class Reference

MQTT callback handler that forwards incoming messages and manages automatic reconnection on connection loss. More...

#include <Collector.hpp>

Inheritance diagram for Connector:
Inheritance graph
Collaboration diagram for Connector:
Collaboration graph

Public Member Functions

 Connector (mqtt::async_client &cli, mqtt::connect_options &connOpts, std::string topic, std::function< void(const std::string &)> on_message, std::function< void(const std::string &)> on_error)
 Constructs a Connector.
 
 ~Connector () override=default
 

Private Member Functions

void message_arrived (mqtt::const_message_ptr msg) override
 Forwards the payload string of msg to on_message_.
 
void reconnect ()
 Waits briefly then calls cli_.connect() again.
 
void on_failure (const mqtt::token &tok) override
 Increments the retry counter and reconnects, or calls on_error_ after N_RETRY_ATTEMPTS failures.
 
void on_success (const mqtt::token &tok) override
 Logs the successful connection attempt.
 
void connected (const std::string &cause) override
 Subscribes to topic_ once the connection is established.
 
void connection_lost (const std::string &cause) override
 Resets the retry counter and initiates reconnection.
 

Private Attributes

int nretry_ = 0
 Current retry attempt counter.
 
mqtt::async_client & cli_
 MQTT client used to connect, subscribe, and reconnect.
 
mqtt::connect_options & connOpts_
 Connection options passed to the client on each connect attempt.
 
std::string topic_
 MQTT topic to subscribe to.
 
std::function< void(const std::string &)> on_message_
 Forwards received message payloads to the Collector.
 
std::function< void(const std::string &)> on_error_
 Reports fatal connection errors to the Collector.
 

Detailed Description

MQTT callback handler that forwards incoming messages and manages automatic reconnection on connection loss.

Constructor & Destructor Documentation

◆ Connector()

Connector::Connector ( mqtt::async_client &  cli,
mqtt::connect_options &  connOpts,
std::string  topic,
std::function< void(const std::string &)>  on_message,
std::function< void(const std::string &)>  on_error 
)
inline

Constructs a Connector.

Parameters
cliThe MQTT asynchronous client.
connOptsThe MQTT connection options.
topicThe topic to subscribe to.
on_messageCallback invoked with each incoming message payload.
on_errorCallback invoked on unrecoverable connection errors.

◆ ~Connector()

Connector::~Connector ( )
overridedefault

Member Function Documentation

◆ connected()

void Connector::connected ( const std::string &  cause)
overrideprivate

Subscribes to topic_ once the connection is established.

Parameters
causeThe cause of the connection event.

◆ connection_lost()

void Connector::connection_lost ( const std::string &  cause)
overrideprivate

Resets the retry counter and initiates reconnection.

Parameters
causeThe reason for the connection loss.
Here is the call graph for this function:

◆ message_arrived()

void Connector::message_arrived ( mqtt::const_message_ptr  msg)
overrideprivate

Forwards the payload string of msg to on_message_.

Parameters
msgThe received MQTT message.

◆ on_failure()

void Connector::on_failure ( const mqtt::token &  tok)
overrideprivate

Increments the retry counter and reconnects, or calls on_error_ after N_RETRY_ATTEMPTS failures.

Parameters
tokThe token associated with the failed action.
Here is the call graph for this function:

◆ on_success()

void Connector::on_success ( const mqtt::token &  tok)
overrideprivate

Logs the successful connection attempt.

Parameters
tokThe token associated with the successful action.

◆ reconnect()

void Connector::reconnect ( )
private

Waits briefly then calls cli_.connect() again.

Here is the caller graph for this function:

Member Data Documentation

◆ cli_

mqtt::async_client& Connector::cli_
private

MQTT client used to connect, subscribe, and reconnect.

◆ connOpts_

mqtt::connect_options& Connector::connOpts_
private

Connection options passed to the client on each connect attempt.

◆ nretry_

int Connector::nretry_ = 0
private

Current retry attempt counter.

◆ on_error_

std::function<void(const std::string&)> Connector::on_error_
private

Reports fatal connection errors to the Collector.

◆ on_message_

std::function<void(const std::string&)> Connector::on_message_
private

Forwards received message payloads to the Collector.

◆ topic_

std::string Connector::topic_
private

MQTT topic to subscribe to.


The documentation for this class was generated from the following files: