connect_options.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*******************************************************************************
  2. * Copyright (c) 2016 Guilherme M. Ferreira <guilherme.maciel.ferreira@gmail.com>
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Guilherme M. Ferreira - initial implementation and documentation
  15. * Frank Pagliughi - Copy and move operations. Bug fixes.
  16. *******************************************************************************/
  17. #include "mqtt/connect_options.h"
  18. #include <cstring>
  19. namespace mqtt {
  20. /////////////////////////////////////////////////////////////////////////////
  21. const MQTTAsync_connectOptions connect_options::DFLT_C_STRUCT = MQTTAsync_connectOptions_initializer;
  22. connect_options::connect_options() : opts_(DFLT_C_STRUCT)
  23. {
  24. }
  25. connect_options::connect_options(string_ref userName, binary_ref password)
  26. : connect_options()
  27. {
  28. set_user_name(userName);
  29. set_password(password);
  30. }
  31. connect_options::connect_options(const connect_options& opt) : opts_(opt.opts_)
  32. {
  33. if (opts_.will)
  34. set_will(opt.will_);
  35. if (opts_.ssl)
  36. set_ssl(opt.ssl_);
  37. set_user_name(opt.userName_);
  38. set_password(opt.password_);
  39. }
  40. connect_options::connect_options(connect_options&& opt) : opts_(opt.opts_),
  41. will_(std::move(opt.will_)),
  42. ssl_(std::move(opt.ssl_)),
  43. userName_(std::move(opt.userName_)),
  44. password_(std::move(opt.password_))
  45. {
  46. if (opts_.will) {
  47. opts_.will = &will_.opts_;
  48. opts_.willProperties = const_cast<MQTTProperties*>(&will_.props_.c_struct());
  49. }
  50. if (opts_.ssl)
  51. opts_.ssl = &ssl_.opts_;
  52. opts_.username = c_str(userName_);
  53. set_password(password_);
  54. }
  55. connect_options& connect_options::operator=(const connect_options& opt)
  56. {
  57. opts_ = opt.opts_;
  58. if (opts_.will)
  59. set_will(opt.will_);
  60. if (opts_.ssl)
  61. set_ssl(opt.ssl_);
  62. set_user_name(opt.userName_);
  63. set_password(opt.password_);
  64. return *this;
  65. }
  66. connect_options& connect_options::operator=(connect_options&& opt)
  67. {
  68. opts_ = opt.opts_;
  69. if (opts_.will)
  70. set_will(std::move(opt.will_));
  71. if (opts_.ssl)
  72. set_ssl(std::move(opt.ssl_));
  73. userName_ = std::move(opt.userName_);
  74. opts_.username = c_str(userName_);
  75. password_ = std::move(opt.password_);
  76. set_password(password_);
  77. return *this;
  78. }
  79. void connect_options::set_will(const will_options& will)
  80. {
  81. will_ = will;
  82. opts_.will = &will_.opts_;
  83. opts_.willProperties = will_.get_properties().empty()
  84. ? nullptr : const_cast<MQTTProperties*>(&will_.props_.c_struct());
  85. }
  86. void connect_options::set_will(will_options&& will)
  87. {
  88. will_ = will;
  89. opts_.will = &will_.opts_;
  90. opts_.willProperties = will_.get_properties().empty()
  91. ? nullptr : const_cast<MQTTProperties*>(&will_.props_.c_struct());
  92. }
  93. void connect_options::set_user_name(string_ref userName)
  94. {
  95. userName_ = std::move(userName);
  96. opts_.username = c_str(userName_);
  97. }
  98. void connect_options::set_password(binary_ref password)
  99. {
  100. password_ = std::move(password);
  101. if (password_.empty()) {
  102. opts_.binarypwd.len = 0;
  103. opts_.binarypwd.data = nullptr;
  104. }
  105. else {
  106. opts_.binarypwd.len = (int) password_.size();
  107. opts_.binarypwd.data = password_.data();
  108. }
  109. }
  110. void connect_options::set_ssl(const ssl_options& ssl)
  111. {
  112. ssl_ = ssl;
  113. opts_.ssl = &ssl_.opts_;
  114. }
  115. void connect_options::set_ssl(ssl_options&& ssl)
  116. {
  117. ssl_ = ssl;
  118. opts_.ssl = &ssl_.opts_;
  119. }
  120. void connect_options::set_token(const token_ptr& tok)
  121. {
  122. tok_ = tok;
  123. opts_.context = tok_.get();
  124. opts_.onSuccess = nullptr;
  125. opts_.onFailure = nullptr;
  126. opts_.onSuccess5 = nullptr;
  127. opts_.onFailure5 = nullptr;
  128. if (tok) {
  129. if (opts_.MQTTVersion < MQTTVERSION_5) {
  130. opts_.onSuccess = &token::on_success;
  131. opts_.onFailure = &token::on_failure;
  132. }
  133. else {
  134. opts_.onSuccess5 = &token::on_success5;
  135. opts_.onFailure5 = &token::on_failure5;
  136. }
  137. }
  138. }
  139. void connect_options::set_servers(const_string_collection_ptr serverURIs)
  140. {
  141. if (serverURIs) {
  142. serverURIs_ = std::move(serverURIs);
  143. opts_. serverURIcount = (int) serverURIs_->size();
  144. opts_.serverURIs = serverURIs_->c_arr();
  145. }
  146. else {
  147. serverURIs_.reset();
  148. opts_.serverURIcount = 0;
  149. opts_.serverURIs = nullptr;
  150. }
  151. }
  152. void connect_options::set_mqtt_version(int mqttVersion) {
  153. opts_.MQTTVersion = mqttVersion;
  154. if (mqttVersion < MQTTVERSION_5)
  155. opts_.cleanstart = 0;
  156. else
  157. opts_.cleansession = 0;
  158. }
  159. void connect_options::set_automatic_reconnect(int minRetryInterval,
  160. int maxRetryInterval)
  161. {
  162. opts_.automaticReconnect = to_int(true);
  163. opts_.minRetryInterval = minRetryInterval;
  164. opts_.maxRetryInterval = maxRetryInterval;
  165. }
  166. /////////////////////////////////////////////////////////////////////////////
  167. } // end namespace mqtt