SOIL C++
C++ Unified Device Interface
|
#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... | |
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.
Definition at line 35 of file Publisher.h.
MQTT::Publisher::Publisher | ( | std::string | id, |
unsigned int | buffer = 1024 |
||
) |
Constructor for MQTT publisher that should be called from user's code
[in] | id | Unique 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] | buffer | Size of the message queue before messages with QoS=0 are discarded. |
Definition at line 66 of file Publisher.cpp.
MQTT::Publisher::~Publisher | ( | ) |
Default destructor
Definition at line 82 of file Publisher.cpp.
void MQTT::Publisher::configure | ( | MQTT::Configuration | configuration | ) |
Register a new configuration, without connecting. Changes will only take place after (re-)connecting to the broker.
[in] | configuration | New configuration object. |
Definition at line 196 of file Publisher.cpp.
void MQTT::Publisher::connect | ( | ) |
Connect to the broker using the configuration object internally stored.
Definition at line 132 of file Publisher.cpp.
void MQTT::Publisher::connect | ( | MQTT::Configuration | configuration | ) |
Connect to the broker using the provided configuration object. The internal configuration object will be overridden.
[in] | configuration | Configuration to apply. |
Definition at line 93 of file Publisher.cpp.
void MQTT::Publisher::disconnect | ( | unsigned int | timeout = 10000 | ) |
Disconnect from the broker and internally stop the worker thread. This is a blocking call.
[in] | timeout | Timeout for the blocking operation in milliseconds. |
Definition at line 157 of file Publisher.cpp.
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.
Definition at line 184 of file Publisher.cpp.
|
inline |
A minimum delay which should be kept between sending two messages, provided in milliseconds. Defaults to 0
.
Definition at line 261 of file Publisher.h.
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.
[in] | topic | Topic to which the message shall be sent. |
[in] | message | Primary content of the messsage to send. |
[in] | qos | MQTT 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] | retain | Flag whether to retain the message on the broker after disconnection. |
Definition at line 207 of file Publisher.cpp.
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.
[in] | topic | List of topics to which the messages shall be sent. |
[in] | message | List of messages, in the correpsonding order of the list of topics. |
[in] | qos | MQTT 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] | retain | Flag whether to retain the message on the broker after disconnection. |
Definition at line 224 of file Publisher.cpp.
void MQTT::Publisher::reconnect | ( | void | ) |
Direct call of the internal reconnect function. No reconfiguration takes place,
Definition at line 138 of file Publisher.cpp.
void MQTT::Publisher::set_buffer | ( | unsigned int | buffer | ) |
Set a new size for the internal message queue.
[in] | buffer | New size |
Definition at line 179 of file Publisher.cpp.
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.
[in] | root_topic | New root topic. |
Definition at line 169 of file Publisher.cpp.