Paho C++  1.0
The Paho MQTT C++ Client Library
 All Classes Files Functions Variables Typedefs Friends
connect_options.h
Go to the documentation of this file.
1 
8 /*******************************************************************************
9  * Copyright (c) 2013-2016 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_connect_options_h
25 #define __mqtt_connect_options_h
26 
27 #include "MQTTAsync.h"
28 #include "mqtt/types.h"
29 #include "mqtt/message.h"
30 #include "mqtt/topic.h"
31 #include "mqtt/token.h"
32 #include "mqtt/string_collection.h"
33 #include "mqtt/will_options.h"
34 #include "mqtt/ssl_options.h"
35 #include <vector>
36 #include <chrono>
37 
38 namespace mqtt {
39 
41 
47 {
49  static const MQTTAsync_connectOptions DFLT_C_STRUCT ;
50 
52  MQTTAsync_connectOptions opts_;
53 
55  will_options will_;
56 
58  ssl_options ssl_;
59 
61  string_ref userName_;
62 
64  binary_ref password_;
65 
67  token_ptr tok_;
68 
70  const_string_collection_ptr serverURIs_;
71 
73  friend class async_client;
74  friend class connect_options_test;
75  friend class token_test;
76 
87  const char* c_str(const string_ref& sr) {
88  return sr.empty() ? nullptr : sr.c_str();
89  }
90 
91 public:
93  using ptr_t = std::shared_ptr<connect_options>;
95  using const_ptr_t = std::shared_ptr<const connect_options>;
96 
100  connect_options();
106  connect_options(string_ref userName, binary_ref password);
111  connect_options(const connect_options& opt);
131  std::chrono::seconds get_keep_alive_interval() const {
132  return std::chrono::seconds(opts_.keepAliveInterval);
133  }
140  std::chrono::seconds get_connect_timeout() const {
141  return std::chrono::seconds(opts_.connectTimeout);
142  }
147  string get_user_name() const { return userName_ ? userName_.to_string() : string(); }
152  binary_ref get_password() const { return password_; }
157  string get_password_str() const {
158  return password_ ? password_.to_string() : string();
159  }
165  int get_max_inflight() const { return opts_.maxInflight; }
170  string get_will_topic() const {
171  return will_.get_topic();
172  }
177  const_message_ptr get_will_message() const {
178  return will_.get_message();
179  }
184  const will_options& get_will_options() const { return will_; }
189  const ssl_options& get_ssl_options() const { return ssl_; }
196  void set_ssl(const ssl_options& ssl);
202  bool is_clean_session() const { return to_bool(opts_.cleansession); }
207  token_ptr get_token() const { return tok_; }
215  const_string_collection_ptr get_servers() const { return serverURIs_; }
224  int get_mqtt_version() const { return opts_.MQTTVersion; }
231  bool get_automatic_reconnect() const { return to_bool(opts_.automaticReconnect); }
237  std::chrono::seconds get_min_retry_interval() const {
238  return std::chrono::seconds(opts_.minRetryInterval);
239  }
245  std::chrono::seconds get_max_retry_interval() const {
246  return std::chrono::seconds(opts_.maxRetryInterval);
247  }
248 
254  void set_clean_session(bool cleanSession) {
255  opts_.cleansession = to_int(cleanSession);
256  }
261  void set_connection_timeout(int timeout) {
262  opts_.connectTimeout = timeout;
263  }
271  void set_keep_alive_interval(int keepAliveInterval) {
272  opts_.keepAliveInterval = keepAliveInterval;
273  }
281  template <class Rep, class Period>
282  void set_keep_alive_interval(const std::chrono::duration<Rep, Period>& interval) {
283  // TODO: Check range
284  set_keep_alive_interval((int) to_seconds_count(interval));
285  }
292  void set_connect_timeout(int to) {
293  opts_.connectTimeout = to;
294  }
301  template <class Rep, class Period>
302  void set_connect_timeout(const std::chrono::duration<Rep, Period>& to) {
303  // TODO: check range
304  set_connect_timeout((int) to_seconds_count(to));
305  }
310  void set_user_name(string_ref userName);
314  void set_password(binary_ref password);
320  void set_max_inflight(int n) { opts_.maxInflight = n; }
325  void set_will(const will_options& will);
330  void set_token(const token_ptr& tok);
339  void set_servers(const_string_collection_ptr serverURIs);
348  void set_mqtt_version(int mqttVersion) { opts_.MQTTVersion = mqttVersion; }
354  void set_automatic_reconnect(bool on) {
355  opts_.automaticReconnect = on ? !0 : 0;
356  }
364  void set_automatic_reconnect(int minRetryInterval, int maxRetryInterval);
372  template <class Rep1, class Period1, class Rep2, class Period2>
373  void set_automatic_reconnect(const std::chrono::duration<Rep1, Period1>& minRetryInterval,
374  const std::chrono::duration<Rep2, Period2>& maxRetryInterval) {
375  set_automatic_reconnect((int) to_seconds_count(minRetryInterval),
376  (int) to_seconds_count(maxRetryInterval));
377  }
382  string to_string() const;
383 };
384 
386 using connect_options_ptr = connect_options::ptr_t;
387 
389 // end namespace mqtt
390 }
391 
392 #endif // __mqtt_connect_options_h
393 
Lightweight client for talking to an MQTT server using non-blocking methods that allow an operation t...
Definition: async_client.h:60
std::shared_ptr< connect_options > ptr_t
Smart/shared pointer to an object of this class.
Definition: connect_options.h:93
void set_clean_session(bool cleanSession)
Sets whether the server should remember state for the client across reconnects.
Definition: connect_options.h:254
Basic types and type conversions for the Paho MQTT C++ library.
string get_will_topic() const
Gets the topic to be used for last will and testament (LWT).
Definition: connect_options.h:170
string get_password_str() const
Gets the password to use for the connection.
Definition: connect_options.h:157
void set_max_inflight(int n)
Sets the maximum number of messages that can be in-flight simultaneously.
Definition: connect_options.h:320
const_message_ptr get_message() const
Gets the LWT message as a message object.
Definition: will_options.h:193
int get_max_inflight() const
Gets the maximum number of messages that can be in-flight simultaneously.
Definition: connect_options.h:165
Holds the set of SSL options for connection.
Definition: ssl_options.h:41
Declaration of MQTT will_options class.
void set_password(binary_ref password)
Sets the password to use for the connection.
connect_options()
Constructs a new object using the default values.
void set_keep_alive_interval(int keepAliveInterval)
Sets the "keep alive" interval.
Definition: connect_options.h:271
std::shared_ptr< const connect_options > const_ptr_t
Smart/shared pointer to a const object of this class.
Definition: connect_options.h:95
const_message_ptr get_will_message() const
Gets the message to be sent as last will and testament (LWT).
Definition: connect_options.h:177
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
const char * c_str() const
Gets the data buffer as NUL-terminated C string.
Definition: buffer_ref.h:257
connect_options & operator=(const connect_options &opt)
Copy assignment.
bool is_clean_session() const
Returns whether the server should remember state for the client across reconnects.
Definition: connect_options.h:202
void set_servers(const_string_collection_ptr serverURIs)
Sets the list of servers to which the client will connect.
Declaration of MQTT message class.
std::chrono::seconds get_max_retry_interval() const
Gets the maximum retry interval for automatic reconnect.
Definition: connect_options.h:245
void set_connection_timeout(int timeout)
Sets the connection timeout value.
Definition: connect_options.h:261
bool get_automatic_reconnect() const
Determines if the options have been configured for automatic reconnect.
Definition: connect_options.h:231
void set_token(const token_ptr &tok)
Sets the callback context to a delivery token.
std::chrono::seconds get_min_retry_interval() const
Gets the minimum retry interval for automatic reconnect.
Definition: connect_options.h:237
void set_mqtt_version(int mqttVersion)
Sets the version of MQTT to be used on the connect.
Definition: connect_options.h:348
void set_automatic_reconnect(const std::chrono::duration< Rep1, Period1 > &minRetryInterval, const std::chrono::duration< Rep2, Period2 > &maxRetryInterval)
Enable or disable automatic reconnects.
Definition: connect_options.h:373
void set_ssl(const ssl_options &ssl)
Sets the SSL for the connection.
string to_string() const
Gets a string representation of the object.
string get_user_name() const
Gets the user name to use for the connection.
Definition: connect_options.h:147
void set_automatic_reconnect(bool on)
Enable or disable automatic reconnects.
Definition: connect_options.h:354
bool empty() const
Determines if the buffer is empty.
Definition: buffer_ref.h:226
token_ptr get_token() const
Gets the token used as the callback context.
Definition: connect_options.h:207
void set_will(const will_options &will)
Sets the "Last Will and Testament" (LWT) for the connection.
int get_mqtt_version() const
Gets the version of MQTT to be used on the connect.
Definition: connect_options.h:224
void set_user_name(string_ref userName)
Sets the user name to use for the connection.
binary_ref get_password() const
Gets the password to use for the connection.
Definition: connect_options.h:152
string get_topic() const
Gets the LWT message topic name.
Definition: will_options.h:168
void set_connect_timeout(int to)
Sets the connect timeout in seconds.
Definition: connect_options.h:292
std::chrono::seconds get_connect_timeout() const
Gets the connection timeout.
Definition: connect_options.h:140
void set_keep_alive_interval(const std::chrono::duration< Rep, Period > &interval)
Sets the "keep alive" interval with a chrono duration.
Definition: connect_options.h:282
Declaration of MQTT topic class.
const ssl_options & get_ssl_options() const
Get the SSL options to use for the connection.
Definition: connect_options.h:189
std::chrono::seconds get_keep_alive_interval() const
Gets the "keep alive" interval.
Definition: connect_options.h:131
const will_options & get_will_options() const
Get the LWT options to use for the connection.
Definition: connect_options.h:184
Holds the set of options that govern the Last Will and Testament feature.
Definition: will_options.h:47
Declaration of MQTT ssl_options class.
const_string_collection_ptr get_servers() const
Gets the list of servers to which the client will connect.
Definition: connect_options.h:215
Declaration of MQTT token class.
void set_connect_timeout(const std::chrono::duration< Rep, Period > &to)
Sets the connect timeout with a chrono duration.
Definition: connect_options.h:302
const blob & to_string() const
Gets the data buffer as a string.
Definition: buffer_ref.h:251