Paho C++  1.0
The Paho MQTT C++ Client Library
 All Classes Files Functions Variables Typedefs Friends
token.h
Go to the documentation of this file.
1 
8 /*******************************************************************************
9  * Copyright (c) 2013-2017 Frank Pagliughi <fpagliughi@mindspring.com>
10  *
11  * All rights reserved. This program and the accompanying materials
12  * are made available under the terms of the Eclipse Public License v1.0
13  * and Eclipse Distribution License v1.0 which accompany this distribution.
14  *
15  * The Eclipse Public License is available at
16  * http://www.eclipse.org/legal/epl-v10.html
17  * and the Eclipse Distribution License is available at
18  * http://www.eclipse.org/org/documents/edl-v10.php.
19  *
20  * Contributors:
21  * Frank Pagliughi - initial implementation and documentation
22  *******************************************************************************/
23 
24 #ifndef __mqtt_token_h
25 #define __mqtt_token_h
26 
27 #include "MQTTAsync.h"
28 #include "mqtt/iaction_listener.h"
29 #include "mqtt/exception.h"
30 #include "mqtt/types.h"
31 #include "mqtt/buffer_ref.h"
32 #include "mqtt/string_collection.h"
33 #include <vector>
34 #include <thread>
35 #include <mutex>
36 #include <condition_variable>
37 #include <chrono>
38 
39 namespace mqtt {
40 
41 class iasync_client;
42 
44 
49 class token
50 {
52  using guard = std::lock_guard<std::mutex>;
54  using unique_lock = std::unique_lock<std::mutex>;
55 
57  mutable std::mutex lock_;
59  std::condition_variable cond_;
60 
62  iasync_client* cli_;
64  MQTTAsync_token tok_;
66  const_string_collection_ptr topics_;
68  void* userContext_;
74  iaction_listener* listener_;
76  bool complete_;
78  int rc_;
80  string errMsg_;
81 
83  friend class async_client;
84  friend class token_test;
85 
86  friend class connect_options;
87  friend class response_options;
88  friend class delivery_response_options;
89  friend class disconnect_options;
90 
94  void reset();
100  void set_message_id(MQTTAsync_token msgid) {
101  guard g(lock_);
102  tok_ = msgid;
103  }
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* /*cause*/);
135  void on_success(MQTTAsync_successData* rsp);
140  void on_failure(MQTTAsync_failureData* rsp);
141 
146  void check_rc() {
147  if (rc_ != MQTTASYNC_SUCCESS)
148  throw exception(rc_, errMsg_);
149  }
150 
151 public:
153  using ptr_t = std::shared_ptr<token>;
155  using const_ptr_t = std::shared_ptr<const token>;
157  using weak_ptr_t = std::weak_ptr<token>;
158 
163  token(iasync_client& cli);
172  token(iasync_client& cli, void* userContext, iaction_listener& cb);
178  token(iasync_client& cli, const string& topic);
188  token(iasync_client& cli, const string& topic,
189  void* userContext, iaction_listener& cb);
195  token(iasync_client& cli, const_string_collection_ptr topics);
205  token(iasync_client& cli, const_string_collection_ptr topics,
206  void* userContext, iaction_listener& cb);
212  token(iasync_client& cli, MQTTAsync_token tok);
216  virtual ~token() {}
222  static ptr_t create(iasync_client& cli) {
223  return std::make_shared<token>(cli);
224  }
233  static ptr_t create(iasync_client& cli, void* userContext, iaction_listener& cb) {
234  return std::make_shared<token>(cli, userContext, cb);
235  }
241  static ptr_t create(iasync_client& cli, const string& topic) {
242  return std::make_shared<token>(cli, topic);
243  }
253  static ptr_t create(iasync_client& cli, const string& topic,
254  void* userContext, iaction_listener& cb) {
255  return std::make_shared<token>(cli, topic, userContext, cb);
256  }
262  static ptr_t create(iasync_client& cli, const_string_collection_ptr topics) {
263  return std::make_shared<token>(cli, topics);
264  }
274  static ptr_t create(iasync_client& cli, const_string_collection_ptr topics,
275  void* userContext, iaction_listener& cb) {
276  return std::make_shared<token>(cli, topics, userContext, cb);
277  }
283  guard g(lock_);
284  return listener_;
285  }
291  virtual iasync_client* get_client() const { return cli_; }
296  virtual int get_message_id() const {
297  static_assert(sizeof(tok_) <= sizeof(int), "MQTTAsync_token must fit into int");
298  return int(tok_);
299  }
306  virtual const_string_collection_ptr get_topics() const {
307  return topics_;
308  }
313  virtual void* get_user_context() const {
314  guard g(lock_);
315  return userContext_;
316  }
321  virtual bool is_complete() const { return complete_; }
328  virtual int get_return_code() const { return rc_; }
333  virtual void set_action_callback(iaction_listener& listener) {
334  guard g(lock_);
335  listener_ = &listener;
336  }
342  virtual void set_user_context(void* userContext) {
343  guard g(lock_);
344  userContext_ = userContext;
345  }
350  virtual void wait();
356  virtual bool try_wait() {
357  guard g(lock_);
358  if (complete_)
359  check_rc();
360  return complete_;
361  }
369  virtual bool wait_for(long timeout) {
370  return wait_for(std::chrono::milliseconds(timeout));
371  }
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_;}))
383  return false;
384  check_rc();
385  return true;
386  }
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_;}))
397  return false;
398  check_rc();
399  return true;
400  }
401 };
402 
404 using token_ptr = token::ptr_t;
405 
407 using const_token_ptr = token::const_ptr_t;
408 
410 // end namespace mqtt
411 }
412 
413 #endif // __mqtt_token_h
414 
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