123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968 |
- * Copyright (c) 2013-2020 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:
- * and the Eclipse Distribution License is available at
- * http:
- *
- * Contributors:
- * Frank Pagliughi - initial implementation and documentation
- *******************************************************************************/
- #ifndef __mqtt_connect_options_h
- #define __mqtt_connect_options_h
- #include "MQTTAsync.h"
- #include "mqtt/types.h"
- #include "mqtt/message.h"
- #include "mqtt/topic.h"
- #include "mqtt/token.h"
- #include "mqtt/string_collection.h"
- #include "mqtt/will_options.h"
- #include "mqtt/ssl_options.h"
- #include "mqtt/platform.h"
- #include <vector>
- #include <map>
- #include <chrono>
- namespace mqtt {
- * Holds the set of options that control how the client connects to a
- * server.
- */
- class connect_options
- {
-
- PAHO_MQTTPP_EXPORT static const MQTTAsync_connectOptions DFLT_C_STRUCT;
-
- PAHO_MQTTPP_EXPORT static const MQTTAsync_connectOptions DFLT_C_STRUCT5;
-
- PAHO_MQTTPP_EXPORT static const MQTTAsync_connectOptions DFLT_C_STRUCT_WS;
-
- PAHO_MQTTPP_EXPORT static const MQTTAsync_connectOptions DFLT_C_STRUCT5_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;
- /**
- * Gets a pointer to the C-language NUL-terminated strings for the
- * struct.
- * @note In the connect options, by default, the Paho C treats
- * nullptr char arrays as unset values, so we keep that semantic and
- * only set those char arrays if the string is non-empty.
- * @param str The C++ string object.
- * @return Pointer to a NUL terminated string. This is only valid until
- * the next time the string is updated.
- */
- 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();
-
- static connect_options v5();
-
- 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) {
-
- 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) {
-
- 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;
- * The connect options that can be updated before an automatic reconnect.
- */
- class connect_data
- {
-
- PAHO_MQTTPP_EXPORT static const MQTTAsync_connectData DFLT_C_STRUCT;
-
- MQTTAsync_connectData data_;
-
- string_ref userName_;
-
- binary_ref password_;
-
- friend class async_client;
- /**
- * Updates the underlying C structure to match our strings.
- */
- void update_c_struct();
- /**
- * Create data from a C struct
- * This is a deep copy of the data from the C struct.
- * @param cdata The C connect data.
- */
- connect_data(const MQTTAsync_connectData& cdata);
- public:
- /**
- * Creates an empty set of connection data.
- */
- connect_data();
- /**
- * Creates connection data with a user name, but no password.
- * @param userName The user name for reconnecting to the MQTT broker.
- */
- explicit connect_data(string_ref userName);
- /**
- * Creates connection data with a user name and password.
- * @param userName The user name for reconnecting to the MQTT broker.
- * @param password The password for connecting to the MQTT broker.
- */
- connect_data(string_ref userName, binary_ref password);
- /**
- * Copy constructor
- * @param other Another data struct to copy into this one.
- */
- connect_data(const connect_data& other);
- /**
- * Copy the connection data.
- * @param rhs Another data struct to copy into this one.
- * @return A reference to this data
- */
- connect_data& operator=(const connect_data& rhs);
- /**
- * Gets the user name to use for the connection.
- * @return The user name to use for the connection.
- */
- 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 to build connect options.
- */
- 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(const 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_; }
- };
- }
- #endif
|