Loading...
Searching...
No Matches
connect_options.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2013-2024 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
*******************************************************************************/
#ifndef __mqtt_connect_options_h
#define __mqtt_connect_options_h
#include <chrono>
#include <map>
#include <vector>
#include "MQTTAsync.h"
#include "mqtt/message.h"
#include "mqtt/platform.h"
#include "mqtt/ssl_options.h"
#include "mqtt/string_collection.h"
#include "mqtt/token.h"
#include "mqtt/topic.h"
#include "mqtt/types.h"
#include "mqtt/will_options.h"
namespace mqtt {
class connect_options
{
static constexpr MQTTAsync_connectOptions DFLT_C_STRUCT
MQTTAsync_connectOptions_initializer;
static constexpr MQTTAsync_connectOptions DFLT_C_STRUCT5
MQTTAsync_connectOptions_initializer5;
static constexpr MQTTAsync_connectOptions DFLT_C_STRUCT_WS
MQTTAsync_connectOptions_initializer_ws;
static constexpr MQTTAsync_connectOptions DFLT_C_STRUCT5_WS
MQTTAsync_connectOptions_initializer5_ws;
MQTTAsync_connectOptions opts_;
will_options will_;
ssl_options ssl_;
string_ref userName_;
binary_ref password_;
token_ptr tok_;
const_string_collection_ptr serverURIs_;
properties props_;
name_value_collection httpHeaders_;
string httpProxy_;
string httpsProxy_;
friend class async_client;
const char* c_str(const string_ref& sr) { return sr.empty() ? nullptr : sr.c_str(); }
const char* c_str(const string& s) { return s.empty() ? nullptr : s.c_str(); }
void update_c_struct();
connect_options(const MQTTAsync_connectOptions& copts) : opts_(copts) {}
public:
using ptr_t = std::shared_ptr<connect_options>;
using const_ptr_t = std::shared_ptr<const connect_options>;
explicit connect_options(int ver = MQTTVERSION_DEFAULT);
connect_options(string_ref userName, binary_ref password, int ver = MQTTVERSION_DEFAULT);
connect_options(const connect_options& opt);
connect_options(connect_options&& opt);
static connect_options v3() { return connect_options(DFLT_C_STRUCT); }
static connect_options v5() { return connect_options(DFLT_C_STRUCT5); }
static connect_options ws() { return connect_options(DFLT_C_STRUCT_WS); }
static connect_options v5_ws() { return connect_options(DFLT_C_STRUCT5_WS); }
connect_options& operator=(const connect_options& opt);
connect_options& operator=(connect_options&& opt);
#if defined(UNIT_TESTS)
const MQTTAsync_connectOptions& c_struct() const { return opts_; }
#endif
std::chrono::seconds get_keep_alive_interval() const {
return std::chrono::seconds(opts_.keepAliveInterval);
}
std::chrono::seconds get_connect_timeout() const {
return std::chrono::seconds(opts_.connectTimeout);
}
string get_user_name() const { return userName_ ? userName_.to_string() : string(); }
binary_ref get_password() const { return password_; }
string get_password_str() const { return password_ ? password_.to_string() : string(); }
int get_max_inflight() const { return opts_.maxInflight; }
string get_will_topic() const { return will_.get_topic(); }
const_message_ptr get_will_message() const { return will_.get_message(); }
const will_options& get_will_options() const { return will_; }
const ssl_options& get_ssl_options() const { return ssl_; }
void set_ssl(const ssl_options& ssl);
void set_ssl(ssl_options&& ssl);
bool is_clean_session() const { return to_bool(opts_.cleansession); }
bool is_clean_start() const { return to_bool(opts_.cleanstart); }
token_ptr get_token() const { return tok_; }
const_string_collection_ptr get_servers() const { return serverURIs_; }
int get_mqtt_version() const { return opts_.MQTTVersion; }
bool get_automatic_reconnect() const { return to_bool(opts_.automaticReconnect); }
std::chrono::seconds get_min_retry_interval() const {
return std::chrono::seconds(opts_.minRetryInterval);
}
std::chrono::seconds get_max_retry_interval() const {
return std::chrono::seconds(opts_.maxRetryInterval);
}
void set_clean_session(bool cleanSession);
void set_clean_start(bool cleanStart);
void set_keep_alive_interval(int keepAliveInterval) {
opts_.keepAliveInterval = keepAliveInterval;
}
template <class Rep, class Period>
void set_keep_alive_interval(const std::chrono::duration<Rep, Period>& interval) {
// TODO: Check range
set_keep_alive_interval((int)to_seconds_count(interval));
}
void set_connect_timeout(int timeout) { opts_.connectTimeout = timeout; }
template <class Rep, class Period>
void set_connect_timeout(const std::chrono::duration<Rep, Period>& timeout) {
// TODO: check range
set_connect_timeout((int)to_seconds_count(timeout));
}
void set_user_name(string_ref userName);
void set_password(binary_ref password);
void set_max_inflight(int n) { opts_.maxInflight = n; }
void set_will(const will_options& will);
void set_will(will_options&& will);
void set_will_message(const message& msg) { set_will(will_options(msg)); }
void set_will_message(const_message_ptr msg) {
if (msg)
set_will(will_options(*msg));
}
void set_token(const token_ptr& tok);
void set_servers(const_string_collection_ptr serverURIs);
void set_mqtt_version(int mqttVersion);
void set_automatic_reconnect(bool on) { opts_.automaticReconnect = to_int(on); }
void set_automatic_reconnect(int minRetryInterval, int maxRetryInterval);
template <class Rep1, class Period1, class Rep2, class Period2>
void set_automatic_reconnect(
const std::chrono::duration<Rep1, Period1>& minRetryInterval,
const std::chrono::duration<Rep2, Period2>& maxRetryInterval
) {
set_automatic_reconnect(
(int)to_seconds_count(minRetryInterval), (int)to_seconds_count(maxRetryInterval)
);
}
const properties& get_properties() const { return props_; }
properties& get_properties() { return props_; }
void set_properties(const properties& props);
void set_properties(properties&& props);
const name_value_collection& get_http_headers() const { return httpHeaders_; }
void set_http_headers(const name_value_collection& httpHeaders) {
httpHeaders_ = httpHeaders;
opts_.httpHeaders = httpHeaders_.empty() ? nullptr : httpHeaders_.c_arr();
}
void set_http_headers(name_value_collection&& httpHeaders) {
httpHeaders_ = std::move(httpHeaders);
opts_.httpHeaders = httpHeaders_.empty() ? nullptr : httpHeaders_.c_arr();
}
string get_http_proxy() const { return httpProxy_; }
void set_http_proxy(const string& httpProxy);
string get_https_proxy() const { return httpsProxy_; }
void set_https_proxy(const string& httpsProxy);
};
using connect_options_ptr = connect_options::ptr_t;
class connect_data
{
PAHO_MQTTPP_EXPORT static constexpr MQTTAsync_connectData DFLT_C_STRUCT
MQTTAsync_connectData_initializer;
MQTTAsync_connectData data_{DFLT_C_STRUCT};
string_ref userName_;
binary_ref password_;
friend class async_client;
void update_c_struct();
connect_data(const MQTTAsync_connectData& cdata);
public:
connect_data();
explicit connect_data(string_ref userName);
connect_data(string_ref userName, binary_ref password);
connect_data(const connect_data& other);
connect_data& operator=(const connect_data& rhs);
string get_user_name() const { return userName_ ? userName_.to_string() : string(); }
binary_ref get_password() const { return password_; }
void set_user_name(string_ref userName);
void set_password(binary_ref password);
};
class connect_options_builder
{
connect_options opts_;
public:
using self = connect_options_builder;
explicit connect_options_builder(int ver = MQTTVERSION_DEFAULT) : opts_(ver) {}
explicit connect_options_builder(const connect_options& opts) : opts_(opts) {}
explicit connect_options_builder(connect_options&& opts) : opts_(std::move(opts)) {}
static connect_options_builder v3() {
return connect_options_builder{connect_options::v3()};
}
static connect_options_builder v5() {
return connect_options_builder{connect_options::v5()};
}
static connect_options_builder ws() {
return connect_options_builder{connect_options::ws()};
}
static connect_options_builder v5_ws() {
return connect_options_builder{connect_options::v5_ws()};
}
auto clean_session(bool on = true) -> self& {
opts_.set_clean_session(on);
return *this;
}
template <class Rep, class Period>
auto keep_alive_interval(const std::chrono::duration<Rep, Period>& interval) -> self& {
opts_.set_keep_alive_interval(interval);
return *this;
}
template <class Rep, class Period>
auto connect_timeout(const std::chrono::duration<Rep, Period>& timeout) -> self& {
opts_.set_connect_timeout(timeout);
return *this;
}
auto user_name(string_ref userName) -> self& {
opts_.set_user_name(userName);
return *this;
}
auto password(binary_ref password) -> self& {
opts_.set_password(password);
return *this;
}
auto max_inflight(int n) -> self& {
opts_.set_max_inflight(n);
return *this;
}
auto will(const will_options& will) -> self& {
opts_.set_will(will);
return *this;
}
auto will(will_options&& will) -> self& {
opts_.set_will(std::move(will));
return *this;
}
auto will(const message& msg) -> self& {
opts_.set_will_message(msg);
return *this;
}
auto ssl(const ssl_options& ssl) -> self& {
opts_.set_ssl(ssl);
return *this;
}
auto ssl(ssl_options&& ssl) -> self& {
opts_.set_ssl(std::move(ssl));
return *this;
}
auto token(const token_ptr& tok) -> self& {
opts_.set_token(tok);
return *this;
}
auto servers(const_string_collection_ptr serverURIs) -> self& {
opts_.set_servers(serverURIs);
return *this;
}
auto mqtt_version(int ver) -> self& {
opts_.set_mqtt_version(ver);
return *this;
}
auto automatic_reconnect(bool on = true) -> self& {
opts_.set_automatic_reconnect(on);
return *this;
}
template <class Rep1, class Period1, class Rep2, class Period2>
auto automatic_reconnect(
const std::chrono::duration<Rep1, Period1>& minRetryInterval,
const std::chrono::duration<Rep2, Period2>& maxRetryInterval
) -> self& {
opts_.set_automatic_reconnect(minRetryInterval, maxRetryInterval);
return *this;
}
auto clean_start(bool on = true) -> self& {
opts_.set_clean_start(on);
return *this;
}
auto properties(const mqtt::properties& props) -> self& {
opts_.set_properties(props);
return *this;
}
auto properties(mqtt::properties&& props) -> self& {
opts_.set_properties(std::move(props));
return *this;
}
auto http_headers(const name_value_collection& headers) -> self& {
opts_.set_http_headers(headers);
return *this;
}
auto http_headers(name_value_collection&& headers) -> self& {
opts_.set_http_headers(std::move(headers));
return *this;
}
auto http_proxy(const string& httpProxy) -> self& {
opts_.set_http_proxy(httpProxy);
return *this;
}
auto https_proxy(const string& httpsProxy) -> self& {
opts_.set_https_proxy(httpsProxy);
return *this;
}
connect_options finalize() { return opts_; }
};
} // namespace mqtt
#endif // __mqtt_connect_options_h
mqtt::buffer_ref::c_str
const char * c_str() const
Definition buffer_ref.h:264
mqtt::buffer_ref::empty
bool empty() const
Definition buffer_ref.h:233
mqtt::connect_data::connect_data
connect_data(const connect_data &other)
mqtt::connect_data::set_user_name
void set_user_name(string_ref userName)
mqtt::connect_data::connect_data
connect_data(string_ref userName, binary_ref password)
mqtt::connect_data::connect_data
connect_data()
mqtt::connect_data::set_password
void set_password(binary_ref password)
mqtt::connect_data::get_user_name
string get_user_name() const
Definition connect_options.h:631
mqtt::connect_data::operator=
connect_data & operator=(const connect_data &rhs)
mqtt::connect_data::connect_data
connect_data(string_ref userName)
mqtt::connect_data::get_password
binary_ref get_password() const
Definition connect_options.h:636
mqtt::connect_data::async_client
friend class async_client
Definition connect_options.h:586
mqtt::connect_options_builder::clean_session
auto clean_session(bool on=true) -> self &
Definition connect_options.h:722
mqtt::connect_options_builder::ssl
auto ssl(ssl_options &&ssl) -> self &
Definition connect_options.h:814
mqtt::connect_options_builder::http_proxy
auto http_proxy(const string &httpProxy) -> self &
Definition connect_options.h:931
mqtt::connect_options_builder::token
auto token(const token_ptr &tok) -> self &
Definition connect_options.h:822
mqtt::connect_options_builder::connect_options_builder
connect_options_builder(int ver=MQTTVERSION_DEFAULT)
Definition connect_options.h:667
mqtt::connect_options_builder::clean_start
auto clean_start(bool on=true) -> self &
Definition connect_options.h:890
mqtt::connect_options_builder::http_headers
auto http_headers(const name_value_collection &headers) -> self &
Definition connect_options.h:914
mqtt::connect_options_builder::will
auto will(const will_options &will) -> self &
Definition connect_options.h:778
mqtt::connect_options_builder::v3
static connect_options_builder v3()
Definition connect_options.h:680
mqtt::connect_options_builder::properties
auto properties(mqtt::properties &&props) -> self &
Definition connect_options.h:906
mqtt::connect_options_builder::http_headers
auto http_headers(name_value_collection &&headers) -> self &
Definition connect_options.h:922
mqtt::connect_options_builder::automatic_reconnect
auto automatic_reconnect(bool on=true) -> self &
Definition connect_options.h:866
mqtt::connect_options_builder::https_proxy
auto https_proxy(const string &httpsProxy) -> self &
Definition connect_options.h:940
mqtt::connect_options_builder::connect_options_builder
connect_options_builder(const connect_options &opts)
Definition connect_options.h:671
mqtt::connect_options_builder::v5
static connect_options_builder v5()
Definition connect_options.h:687
mqtt::connect_options_builder::max_inflight
auto max_inflight(int n) -> self &
Definition connect_options.h:770
mqtt::connect_options_builder::ssl
auto ssl(const ssl_options &ssl) -> self &
Definition connect_options.h:804
mqtt::connect_options_builder::connect_timeout
auto connect_timeout(const std::chrono::duration< Rep, Period > &timeout) -> self &
Definition connect_options.h:745
mqtt::connect_options_builder::password
auto password(binary_ref password) -> self &
Definition connect_options.h:761
mqtt::connect_options_builder::connect_options_builder
connect_options_builder(connect_options &&opts)
Definition connect_options.h:675
mqtt::connect_options_builder::self
connect_options_builder self
Definition connect_options.h:660
mqtt::connect_options_builder::v5_ws
static connect_options_builder v5_ws()
Definition connect_options.h:713
mqtt::connect_options_builder::finalize
connect_options finalize()
Definition connect_options.h:948
mqtt::connect_options_builder::will
auto will(const message &msg) -> self &
Definition connect_options.h:794
mqtt::connect_options_builder::keep_alive_interval
auto keep_alive_interval(const std::chrono::duration< Rep, Period > &interval) -> self &
Definition connect_options.h:734
mqtt::connect_options_builder::properties
auto properties(const mqtt::properties &props) -> self &
Definition connect_options.h:898
mqtt::connect_options_builder::automatic_reconnect
auto automatic_reconnect(const std::chrono::duration< Rep1, Period1 > &minRetryInterval, const std::chrono::duration< Rep2, Period2 > &maxRetryInterval) -> self &
Definition connect_options.h:878
mqtt::connect_options_builder::will
auto will(will_options &&will) -> self &
Definition connect_options.h:786
mqtt::connect_options_builder::ws
static connect_options_builder ws()
Definition connect_options.h:700
mqtt::connect_options_builder::servers
auto servers(const_string_collection_ptr serverURIs) -> self &
Definition connect_options.h:834
mqtt::connect_options_builder::user_name
auto user_name(string_ref userName) -> self &
Definition connect_options.h:753
mqtt::connect_options_builder::mqtt_version
auto mqtt_version(int ver) -> self &
Definition connect_options.h:857
mqtt::connect_options
Definition connect_options.h:50
mqtt::connect_options::get_will_options
const will_options & get_will_options() const
Definition connect_options.h:250
mqtt::connect_options::set_clean_start
void set_clean_start(bool cleanStart)
mqtt::connect_options::set_user_name
void set_user_name(string_ref userName)
mqtt::connect_options::set_properties
void set_properties(properties &&props)
mqtt::connect_options::operator=
connect_options & operator=(connect_options &&opt)
mqtt::connect_options::get_connect_timeout
std::chrono::seconds get_connect_timeout() const
Definition connect_options.h:212
mqtt::connect_options::set_keep_alive_interval
void set_keep_alive_interval(const std::chrono::duration< Rep, Period > &interval)
Definition connect_options.h:372
mqtt::connect_options::set_http_headers
void set_http_headers(name_value_collection &&httpHeaders)
Definition connect_options.h:534
mqtt::connect_options::ws
static connect_options ws()
Definition connect_options.h:173
mqtt::connect_options::set_token
void set_token(const token_ptr &tok)
mqtt::connect_options::operator=
connect_options & operator=(const connect_options &opt)
mqtt::connect_options::connect_options
connect_options(connect_options &&opt)
mqtt::connect_options::set_automatic_reconnect
void set_automatic_reconnect(const std::chrono::duration< Rep1, Period1 > &minRetryInterval, const std::chrono::duration< Rep2, Period2 > &maxRetryInterval)
Definition connect_options.h:489
mqtt::connect_options::set_will
void set_will(will_options &&will)
mqtt::connect_options::set_clean_session
void set_clean_session(bool cleanSession)
mqtt::connect_options::set_connect_timeout
void set_connect_timeout(int timeout)
Definition connect_options.h:382
mqtt::connect_options::v5_ws
static connect_options v5_ws()
Definition connect_options.h:182
mqtt::connect_options::get_http_headers
const name_value_collection & get_http_headers() const
Definition connect_options.h:521
mqtt::connect_options::const_ptr_t
std::shared_ptr< const connect_options > const_ptr_t
Definition connect_options.h:130
mqtt::connect_options::is_clean_session
bool is_clean_session() const
Definition connect_options.h:277
mqtt::connect_options::set_properties
void set_properties(const properties &props)
mqtt::connect_options::get_max_retry_interval
std::chrono::seconds get_max_retry_interval() const
Definition connect_options.h:326
mqtt::connect_options::get_password
binary_ref get_password() const
Definition connect_options.h:224
mqtt::connect_options::connect_options
connect_options(string_ref userName, binary_ref password, int ver=MQTTVERSION_DEFAULT)
mqtt::connect_options::get_properties
const properties & get_properties() const
Definition connect_options.h:501
mqtt::connect_options::set_automatic_reconnect
void set_automatic_reconnect(bool on)
Definition connect_options.h:472
mqtt::connect_options::get_automatic_reconnect
bool get_automatic_reconnect() const
Definition connect_options.h:312
mqtt::connect_options::get_min_retry_interval
std::chrono::seconds get_min_retry_interval() const
Definition connect_options.h:318
mqtt::connect_options::get_https_proxy
string get_https_proxy() const
Definition connect_options.h:553
mqtt::connect_options::get_user_name
string get_user_name() const
Definition connect_options.h:219
mqtt::connect_options::set_keep_alive_interval
void set_keep_alive_interval(int keepAliveInterval)
Definition connect_options.h:361
mqtt::connect_options::set_https_proxy
void set_https_proxy(const string &httpsProxy)
mqtt::connect_options::connect_options
connect_options(const connect_options &opt)
mqtt::connect_options::set_max_inflight
void set_max_inflight(int n)
Definition connect_options.h:409
mqtt::connect_options::get_will_message
const_message_ptr get_will_message() const
Definition connect_options.h:245
mqtt::connect_options::set_will_message
void set_will_message(const_message_ptr msg)
Definition connect_options.h:429
mqtt::connect_options::get_will_topic
string get_will_topic() const
Definition connect_options.h:240
mqtt::connect_options::ptr_t
std::shared_ptr< connect_options > ptr_t
Definition connect_options.h:128
mqtt::connect_options::v3
static connect_options v3()
Definition connect_options.h:159
mqtt::connect_options::get_mqtt_version
int get_mqtt_version() const
Definition connect_options.h:305
mqtt::connect_options::set_servers
void set_servers(const_string_collection_ptr serverURIs)
mqtt::connect_options::set_will
void set_will(const will_options &will)
mqtt::connect_options::get_max_inflight
int get_max_inflight() const
Definition connect_options.h:235
mqtt::connect_options::connect_options
connect_options(int ver=MQTTVERSION_DEFAULT)
mqtt::connect_options::get_ssl_options
const ssl_options & get_ssl_options() const
Definition connect_options.h:255
mqtt::connect_options::async_client
friend class async_client
Definition connect_options.h:101
mqtt::connect_options::set_ssl
void set_ssl(const ssl_options &ssl)
mqtt::connect_options::v5
static connect_options v5()
Definition connect_options.h:164
mqtt::connect_options::get_password_str
string get_password_str() const
Definition connect_options.h:229
mqtt::connect_options::set_connect_timeout
void set_connect_timeout(const std::chrono::duration< Rep, Period > &timeout)
Definition connect_options.h:390
mqtt::connect_options::get_properties
properties & get_properties()
Definition connect_options.h:506
mqtt::connect_options::set_mqtt_version
void set_mqtt_version(int mqttVersion)
mqtt::connect_options::set_automatic_reconnect
void set_automatic_reconnect(int minRetryInterval, int maxRetryInterval)
mqtt::connect_options::get_token
token_ptr get_token() const
Definition connect_options.h:288
mqtt::connect_options::get_keep_alive_interval
std::chrono::seconds get_keep_alive_interval() const
Definition connect_options.h:203
mqtt::connect_options::set_ssl
void set_ssl(ssl_options &&ssl)
mqtt::connect_options::set_password
void set_password(binary_ref password)
mqtt::connect_options::get_http_proxy
string get_http_proxy() const
Definition connect_options.h:542
mqtt::connect_options::set_http_proxy
void set_http_proxy(const string &httpProxy)
mqtt::connect_options::get_servers
const_string_collection_ptr get_servers() const
Definition connect_options.h:296
mqtt::connect_options::set_http_headers
void set_http_headers(const name_value_collection &httpHeaders)
Definition connect_options.h:526
mqtt::connect_options::set_will_message
void set_will_message(const message &msg)
Definition connect_options.h:424
mqtt::connect_options::is_clean_start
bool is_clean_start() const
Definition connect_options.h:283
mqtt::message
Definition message.h:57
mqtt::name_value_collection
Definition string_collection.h:267
mqtt::name_value_collection::empty
bool empty() const
Definition string_collection.h:352
mqtt::properties
Definition properties.h:293
mqtt::ssl_options
Definition ssl_options.h:47
mqtt::will_options
Definition will_options.h:50
PAHO_MQTTPP_EXPORT
#define PAHO_MQTTPP_EXPORT
Definition export.h:40
message.h
mqtt
Definition async_client.h:60
mqtt::binary_ref
buffer_ref< char > binary_ref
Definition buffer_ref.h:305
mqtt::to_seconds_count
long to_seconds_count(const std::chrono::duration< Rep, Period > &dur)
Definition types.h:73
mqtt::to_bool
bool to_bool(int n)
Definition types.h:107
mqtt::token_ptr
token::ptr_t token_ptr
Definition token.h:513
mqtt::const_string_collection_ptr
string_collection::const_ptr_t const_string_collection_ptr
Definition string_collection.h:259
mqtt::const_message_ptr
message::const_ptr_t const_message_ptr
Definition message.h:372
mqtt::connect_options_ptr
connect_options::ptr_t connect_options_ptr
Definition connect_options.h:563
mqtt::string
std::string string
Definition types.h:43
mqtt::to_int
int to_int(bool b)
Definition types.h:113
mqtt::string_ref
buffer_ref< char > string_ref
Definition buffer_ref.h:297
platform.h
ssl_options.h
string_collection.h
token.h
topic.h
types.h
will_options.h