SOIL C++
C++ Unified Device Interface
MQTT::Publisher Class Reference

MQTT Publisher. More...

#include <Publisher.h>

Public Member Functions

 Publisher (std::string id, unsigned int buffer=1024)
 Constructor. More...
 
 ~Publisher ()
 Destructor. More...
 
bool publish (std::string topic, std::string message, int qos, bool retain)
 Publish an MQTT message. More...
 
bool publish (std::vector< std::string > topics, std::vector< std::string > messages, int qos, bool retain)
 Publish multiple MQTT messages. More...
 
void connect (MQTT::Configuration configuration)
 Connect to broker. More...
 
void connect ()
 Connect to broker. More...
 
void reconnect (void)
 Direct Reconnect. More...
 
void disconnect (unsigned int timeout=10000)
 Diconnect from the broker. More...
 
void set_root_topic (std::string root_topic)
 Set new root topic. More...
 
void set_buffer (unsigned int buffer)
 Set message queue size. More...
 
bool is_connected (void)
 Is connected? More...
 
int min_delay_ms (void)
 Minimum delay between to messages in milliseconds. More...
 
void configure (MQTT::Configuration configuration)
 Register new configuration. More...
 

Detailed Description

This class abstracts the process of publishing MQTT messages to an MQTT broker. Currently, the underlying implementation is realized using Paho-MQTT. Upon connection, it spans an own worker thread that continously publishes messages put into a message queue such that the calling thread does not get blocked with the communication process. The purpose of this class is pure publishing, so there is no subscribing functionality implemented.

Todo:
Implement support for authenticating with client certificates.

Definition at line 35 of file Publisher.h.

Constructor & Destructor Documentation

◆ Publisher()

MQTT::Publisher::Publisher ( std::string  id,
unsigned int  buffer = 1024 
)

Constructor for MQTT publisher that should be called from user's code

Parameters
[in]idUnique identifier that is presented to the broker. Be careful if you reuse code across applications that some broker show an unexpected behaviour when using the same id more than once.
[in]bufferSize of the message queue before messages with QoS=0 are discarded.

Definition at line 66 of file Publisher.cpp.

◆ ~Publisher()

MQTT::Publisher::~Publisher ( )

Default destructor

Definition at line 82 of file Publisher.cpp.

Member Function Documentation

◆ configure()

void MQTT::Publisher::configure ( MQTT::Configuration  configuration)

Register a new configuration, without connecting. Changes will only take place after (re-)connecting to the broker.

Parameters
[in]configurationNew configuration object.

Definition at line 196 of file Publisher.cpp.

◆ connect() [1/2]

void MQTT::Publisher::connect ( )

Connect to the broker using the configuration object internally stored.

Definition at line 132 of file Publisher.cpp.

◆ connect() [2/2]

void MQTT::Publisher::connect ( MQTT::Configuration  configuration)

Connect to the broker using the provided configuration object. The internal configuration object will be overridden.

Parameters
[in]configurationConfiguration to apply.

Definition at line 93 of file Publisher.cpp.

◆ disconnect()

void MQTT::Publisher::disconnect ( unsigned int  timeout = 10000)

Disconnect from the broker and internally stop the worker thread. This is a blocking call.

Parameters
[in]timeoutTimeout for the blocking operation in milliseconds.

Definition at line 157 of file Publisher.cpp.

◆ is_connected()

bool MQTT::Publisher::is_connected ( void  )

Checks whether the publisher is currently connected to a broker. Directly calls the underlying implementation of the library if possible.

Returns
True if connected, else false.

Definition at line 184 of file Publisher.cpp.

◆ min_delay_ms()

int MQTT::Publisher::min_delay_ms ( void  )
inline

A minimum delay which should be kept between sending two messages, provided in milliseconds. Defaults to 0 .

Postcondition
This is not enforced in the client, but should be called as property of the publishing by the using code.
Returns
Current delay set.

Definition at line 261 of file Publisher.h.

◆ publish() [1/2]

bool MQTT::Publisher::publish ( std::string  topic,
std::string  message,
int  qos,
bool  retain 
)

This is the core function to publish an MQTT message. It deposits a message into the queue.

Parameters
[in]topicTopic to which the message shall be sent.
[in]messagePrimary content of the messsage to send.
[in]qosMQTT Quality of service level to choose for the message. Check the MQTT specifications for the exact behaviours of 0, 1, and 2 in conjuncation with message retention.
[in]retainFlag whether to retain the message on the broker after disconnection.
Returns
True or false depending whether the message was accepted in the message queue (cf. buffer size).

Definition at line 207 of file Publisher.cpp.

◆ publish() [2/2]

bool MQTT::Publisher::publish ( std::vector< std::string >  topics,
std::vector< std::string >  messages,
int  qos,
bool  retain 
)

This is the multi-message version of the publish function, which deposits all messages at once into the message queue and gains performance by reducing the number of lock operations.

Parameters
[in]topicList of topics to which the messages shall be sent.
[in]messageList of messages, in the correpsonding order of the list of topics.
[in]qosMQTT Quality of service level to choose for the message. Check the MQTT specifications for the exact behaviours of 0, 1, and 2 in conjuncation with message retention.
[in]retainFlag whether to retain the message on the broker after disconnection.
Returns
True or false depending whether the message was accepted in the message queue (cf. buffer size).

Definition at line 224 of file Publisher.cpp.

◆ reconnect()

void MQTT::Publisher::reconnect ( void  )

Direct call of the internal reconnect function. No reconfiguration takes place,

Definition at line 138 of file Publisher.cpp.

◆ set_buffer()

void MQTT::Publisher::set_buffer ( unsigned int  buffer)

Set a new size for the internal message queue.

Parameters
[in]bufferNew size

Definition at line 179 of file Publisher.cpp.

◆ set_root_topic()

void MQTT::Publisher::set_root_topic ( std::string  root_topic)

Override the root topic. This can be done while connected as it only affects preprocessing of messages.

Parameters
[in]root_topicNew root topic.

Definition at line 169 of file Publisher.cpp.


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