MQTT callback handler that forwards incoming messages and manages automatic reconnection on connection loss.
More...
#include <Collector.hpp>
|
| | 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 |
| |
|
| 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.
|
| |
|
| 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.
|
| |
MQTT callback handler that forwards incoming messages and manages automatic reconnection on connection loss.
◆ 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
-
| cli | The MQTT asynchronous client. |
| connOpts | The MQTT connection options. |
| topic | The topic to subscribe to. |
| on_message | Callback invoked with each incoming message payload. |
| on_error | Callback invoked on unrecoverable connection errors. |
◆ ~Connector()
| Connector::~Connector |
( |
| ) |
|
|
overridedefault |
◆ connected()
| void Connector::connected |
( |
const std::string & |
cause | ) |
|
|
overrideprivate |
Subscribes to topic_ once the connection is established.
- Parameters
-
| cause | The cause of the connection event. |
◆ connection_lost()
| void Connector::connection_lost |
( |
const std::string & |
cause | ) |
|
|
overrideprivate |
Resets the retry counter and initiates reconnection.
- Parameters
-
| cause | The reason for the connection loss. |
◆ message_arrived()
| void Connector::message_arrived |
( |
mqtt::const_message_ptr |
msg | ) |
|
|
overrideprivate |
Forwards the payload string of msg to on_message_.
- Parameters
-
| msg | The 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
-
| tok | The token associated with the failed action. |
◆ on_success()
| void Connector::on_success |
( |
const mqtt::token & |
tok | ) |
|
|
overrideprivate |
Logs the successful connection attempt.
- Parameters
-
| tok | The token associated with the successful action. |
◆ reconnect()
| void Connector::reconnect |
( |
| ) |
|
|
private |
Waits briefly then calls cli_.connect() again.
◆ 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: