1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/////////////////////////////////////////////////////////////////////////////
/// @file response_options.h
/// Implementation of the class 'response_options'
/// @date 26-Aug-2019
/////////////////////////////////////////////////////////////////////////////
/*******************************************************************************
* Copyright (c) 2019-2025 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_response_options_h
#define __mqtt_response_options_h
#include "MQTTAsync.h"
#include "mqtt/delivery_token.h"
#include "mqtt/token.h"
#include "subscribe_options.h"
namespace mqtt {
class token_test;
/////////////////////////////////////////////////////////////////////////////
// response_options
/////////////////////////////////////////////////////////////////////////////
/**
* The response options for various asynchronous calls.
*
* This is an internal data structure, only used within the library.
* Therefore it is not totally fleshed out, but rather only exposes the
* functionality currently required by the library.
*
* Note, too, in the C lib, this became a place to add MQTT v5 options for
* the outgoing calls without breaking the API, so is also referred to as the
* "call options".
*/
class response_options
{
/** The underlying C structure */
MQTTAsync_responseOptions opts_ MQTTAsync_responseOptions_initializer;
/** The token to which we are connected */
token::weak_ptr_t tok_;
/** Packet Properties (Subscribe/Unsubscribe) */
properties props_;
/** A list of subscription options for subscribe-many */
std::vector<MQTTSubscribe_options> subOpts_;
/** The client has special access */
friend class async_client;
/** Update the underlying C struct to match our data */
void update_c_struct();
public:
/**
* Create an empty response object.
* @param mqttVersion The MQTT version for the response.
*/
explicit response_options(int mqttVersion = MQTTVERSION_DEFAULT) {
set_mqtt_version(mqttVersion);
}
/**
* Creates a response object with the specified callbacks.
* @param tok A token to be used as the context.
* @param mqttVersion The MQTT version for the response.
*/
response_options(const token_ptr& tok, int mqttVersion = MQTTVERSION_DEFAULT);
/**
* Copy constructor.
* @param other The other options to copy to this one.
*/
response_options(const response_options& other);
/**
* Move constructor.
* @param other The other options to move into this one.
*/
response_options(response_options&& other);
/**
* Copy operator.
* @param rhs The other options to copy to this one.
*/
response_options& operator=(const response_options& rhs);
/**
* Move operator.
* @param rhs The other options to move into this one.
*/
response_options& operator=(response_options&& rhs);
/**
* Expose the underlying C struct for the unit tests.
*/
#if defined(UNIT_TESTS)
const auto& c_struct() const { return opts_; }
#endif
/**
* Sets the MQTT protocol version used for the response.
* This sets up proper callbacks for MQTT v5 or versions prior to that.
* @param mqttVersion The MQTT version used by the connection.
*/
void set_mqtt_version(int mqttVersion);
/**
* Sets the callback context to a generic token.
* @param tok The token to be used as the callback context.
*/
void set_token(const token_ptr& tok);
/**
* Gets the properties for the response options.
*/
const properties& get_properties() const { return props_; }
/**
* Sets the properties for the response options.
* @param props The properties for the response options.
*/
void set_properties(const properties& props) {
props_ = props;
opts_.properties = props_.c_struct();
}
/**
* Moves the properties for the response options.
* @param props The properties to move into the response options.
*/
void set_properties(properties&& props) {
props_ = std::move(props);
opts_.properties = props_.c_struct();
}
/**
* Gets the options for a single topic subscription.
* @return The subscribe options.
*/
subscribe_options get_subscribe_options() const {
return subscribe_options{opts_.subscribeOptions};
}
/**
* Sets the options for a multi-topic subscription.
* @return The vector of the subscribe options.
*/
std::vector<subscribe_options> get_subscribe_many_options() const;
/**
* Sets the options for a single topic subscription.
* @param opts The subscribe options.
*/
void set_subscribe_options(const subscribe_options& opts);
/**
* Sets the options for a multi-topic subscription.
* @param opts A vector of the subscribe options.
*/
void set_subscribe_many_options(const std::vector<subscribe_options>& opts);
/**
* Sets the options for a multi-topic subscription.
* @param opts A vector of the subscribe options.
* @sa set_subscribe_options
*/
void set_subscribe_options(const std::vector<subscribe_options>& opts) {
set_subscribe_many_options(opts);
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Class to build response options.
*/
class response_options_builder
{
/** The underlying options */
response_options opts_;
public:
/** This class */
using self = response_options_builder;
/**
* Default constructor.
*/
explicit response_options_builder(int mqttVersion = MQTTVERSION_DEFAULT)
: opts_(mqttVersion) {}
/**
* Sets the MQTT protocol version used for the response.
* This sets up proper callbacks for MQTT v5 or versions prior to that.
* @param mqttVersion The MQTT version used by the connection.
*/
auto mqtt_version(int mqttVersion) -> self& {
opts_.set_mqtt_version(mqttVersion);
return *this;
}
/**
* Sets the callback context to a generic token.
* @param tok The token to be used as the callback context.
*/
auto token(const token_ptr& tok) -> self& {
opts_.set_token(tok);
return *this;
}
/**
* Sets the properties for the response options.
* @param props The properties for the response options.
*/
auto properties(mqtt::properties&& props) -> self& {
opts_.set_properties(std::move(props));
return *this;
}
/**
* Sets the properties for the disconnect message.
* @param props The properties for the disconnect message.
*/
auto properties(const mqtt::properties& props) -> self& {
opts_.set_properties(props);
return *this;
}
/**
* Sets the options for a single topic subscription.
* @param opts The subscribe options.
*/
auto subscribe_opts(const subscribe_options& opts) -> self& {
opts_.set_subscribe_options(opts);
return *this;
}
/**
* Sets the options for a multi-topic subscription.
* @param opts A vector of the subscribe options.
*/
auto subscribe_many_opts(const std::vector<subscribe_options>& opts) -> self& {
opts_.set_subscribe_options(opts);
return *this;
}
/**
* Sets the options for a multi-topic subscription.
* @param opts A vector of the subscribe options.
*/
auto subscribe_opts(const std::vector<subscribe_options>& opts) -> self& {
opts_.set_subscribe_options(opts);
return *this;
}
/**
* Finish building the response options and return them.
* @return The response option struct as built.
*/
response_options finalize() { return opts_; }
};
/////////////////////////////////////////////////////////////////////////////
// delivery_response_options
/////////////////////////////////////////////////////////////////////////////
/**
* The response options for asynchronous calls targeted at delivery.
* Each of these objects is tied to a specific delivery_token.
*/
class delivery_response_options
{
/** The underlying C structure */
MQTTAsync_responseOptions opts_;
/** The delivery token to which we are connected */
delivery_token::weak_ptr_t dtok_;
/** The client has special access */
friend class async_client;
public:
/**
* Create an empty delivery response object.
*/
delivery_response_options(int mqttVersion = MQTTVERSION_DEFAULT);
/**
* Creates a response object tied to the specific delivery token.
* @param dtok A delivery token to be used as the context.
* @param mqttVersion The MQTT version for the response
*/
delivery_response_options(
const delivery_token_ptr& dtok, int mqttVersion = MQTTVERSION_DEFAULT
);
/**
* Expose the underlying C struct for the unit tests.
*/
#if defined(UNIT_TESTS)
const MQTTAsync_responseOptions& c_struct() const { return opts_; }
#endif
/**
* Sets the callback context to a delivery token.
* @param dtok The delivery token to be used as the callback context.
*/
void set_token(const delivery_token_ptr& dtok) {
dtok_ = dtok;
opts_.context = dtok.get();
}
};
/////////////////////////////////////////////////////////////////////////////
} // namespace mqtt
#endif // __mqtt_response_options_h
|