24 #ifndef __mqtt_token_h
25 #define __mqtt_token_h
27 #include "MQTTAsync.h"
36 #include <condition_variable>
52 using guard = std::lock_guard<std::mutex>;
54 using unique_lock = std::unique_lock<std::mutex>;
57 mutable std::mutex lock_;
59 std::condition_variable cond_;
66 const_string_collection_ptr topics_;
84 friend class token_test;
100 void set_message_id(MQTTAsync_token msgid) {
113 static void on_success(
void* tokObj, MQTTAsync_successData* rsp);
123 static void on_failure(
void* tokObj, MQTTAsync_failureData* rsp);
130 static void on_connected(
void* tokObj,
char* );
135 void on_success(MQTTAsync_successData* rsp);
140 void on_failure(MQTTAsync_failureData* rsp);
147 if (rc_ != MQTTASYNC_SUCCESS)
153 using ptr_t = std::shared_ptr<token>;
223 return std::make_shared<token>(cli);
234 return std::make_shared<token>(cli, userContext, cb);
242 return std::make_shared<token>(cli, topic);
255 return std::make_shared<token>(cli, topic, userContext, cb);
263 return std::make_shared<token>(cli, topics);
276 return std::make_shared<token>(cli, topics, userContext, cb);
297 static_assert(
sizeof(tok_) <=
sizeof(
int),
"MQTTAsync_token must fit into int");
335 listener_ = &listener;
344 userContext_ = userContext;
370 return wait_for(std::chrono::milliseconds(timeout));
378 template <
class Rep,
class Period>
379 bool wait_for(
const std::chrono::duration<Rep, Period>& relTime) {
380 unique_lock g(lock_);
381 if (!cond_.wait_for(g, std::chrono::milliseconds(relTime),
382 [
this]{
return complete_;}))
393 template <
class Clock,
class Duration>
394 bool wait_until(
const std::chrono::time_point<Clock, Duration>& absTime) {
395 unique_lock g(lock_);
396 if (!cond_.wait_until(g, absTime, [
this]{
return complete_;}))
404 using token_ptr = token::ptr_t;
407 using const_token_ptr = token::const_ptr_t;
413 #endif // __mqtt_token_h
The response options for asynchronous calls targeted at delivery.
Definition: response_options.h:68
Lightweight client for talking to an MQTT server using non-blocking methods that allow an operation t...
Definition: async_client.h:60
Basic types and type conversions for the Paho MQTT C++ library.
Provides a mechanism for tracking the completion of an asynchronous action.
Definition: iaction_listener.h:48
std::weak_ptr< token > weak_ptr_t
Weak pointer to an object of this class.
Definition: token.h:157
static ptr_t create(iasync_client &cli, const string &topic)
Constructs a token object.
Definition: token.h:241
token(iasync_client &cli)
Constructs a token object.
Definition of the string_collection class for the Paho MQTT C++ library.
Holds the set of options that control how the client connects to a server.
Definition: connect_options.h:46
static ptr_t create(iasync_client &cli, const_string_collection_ptr topics)
Constructs a token object.
Definition: token.h:262
Represents a topic destination, used for publish/subscribe messaging.
Definition: topic.h:42
virtual void wait()
Blocks the current thread until the action this token is associated with has completed.
virtual void set_user_context(void *userContext)
Store some context associated with an action.
Definition: token.h:342
virtual void set_action_callback(iaction_listener &listener)
Register a listener to be notified when an action completes.
Definition: token.h:333
virtual const_string_collection_ptr get_topics() const
Gets the topic string(s) for the action being tracked by this token.
Definition: token.h:306
Declaration of MQTT exception class.
virtual iaction_listener * get_action_callback() const
Gets the action listener for this token.
Definition: token.h:282
virtual int get_return_code() const
Gets the return code from the action.
Definition: token.h:328
static ptr_t create(iasync_client &cli, void *userContext, iaction_listener &cb)
Constructs a token object.
Definition: token.h:233
Base mqtt::exception.
Definition: exception.h:42
bool wait_until(const std::chrono::time_point< Clock, Duration > &absTime)
Waits until an absolute time for the action to complete.
Definition: token.h:394
virtual bool wait_for(long timeout)
Blocks the current thread until the action this token is associated with has completed.
Definition: token.h:369
static ptr_t create(iasync_client &cli)
Constructs a token object.
Definition: token.h:222
Enables an application to communicate with an MQTT server using non-blocking methods.
Definition: iasync_client.h:57
std::shared_ptr< const token > const_ptr_t
Smart/shared pointer to an object of this class.
Definition: token.h:155
virtual int get_message_id() const
Returns the ID of the message that is associated with the token.
Definition: token.h:296
virtual bool is_complete() const
Returns whether or not the action has finished.
Definition: token.h:321
static ptr_t create(iasync_client &cli, const string &topic, void *userContext, iaction_listener &cb)
Constructs a token object.
Definition: token.h:253
Declaration of MQTT iaction_listener class.
virtual bool try_wait()
Non-blocking check to see if the action has completed.
Definition: token.h:356
Buffer reference type for the Paho MQTT C++ library.
std::shared_ptr< token > ptr_t
Smart/shared pointer to an object of this class.
Definition: token.h:153
virtual iasync_client * get_client() const
Returns the MQTT client that is responsible for processing the asynchronous action.
Definition: token.h:291
The response options for various asynchronous calls.
Definition: response_options.h:27
Provides a mechanism for tracking the completion of an asynchronous action.
Definition: token.h:49
virtual ~token()
Virtual destructor.
Definition: token.h:216
static ptr_t create(iasync_client &cli, const_string_collection_ptr topics, void *userContext, iaction_listener &cb)
Constructs a token object.
Definition: token.h:274
Options for disconnecting from an MQTT broker.
Definition: disconnect_options.h:37
bool wait_for(const std::chrono::duration< Rep, Period > &relTime)
Waits a relative amount of time for the action to complete.
Definition: token.h:379
virtual void * get_user_context() const
Retrieve the context associated with an action.
Definition: token.h:313