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
|
/////////////////////////////////////////////////////////////////////////////
/// @file event.h
/// Declaration of MQTT event-related classes
/// @date July 6, 2024
/// @author Frank Pagliughi
/////////////////////////////////////////////////////////////////////////////
/*******************************************************************************
* Copyright (c) 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_event_h
#define __mqtt_event_h
#include <variant>
#include "mqtt/message.h"
#include "mqtt/properties.h"
#include "mqtt/reason_code.h"
#include "mqtt/types.h"
namespace mqtt {
/////////////////////////////////////////////////////////////////////////////
/** Event for when the client is connected/reconnected */
struct connected_event
{
string cause;
};
/** Event for when the connection is lost */
struct connection_lost_event
{
string cause;
};
/** Event for when we receive a DISCONNECT packet from the server */
struct disconnected_event
{
properties props;
ReasonCode reasonCode;
};
/** Event for when the consumer queue is shutdown from another thread */
struct shutdown_event
{
};
/* Event for when a message arrives is just a message pointer */
/////////////////////////////////////////////////////////////////////////////
/**
* An MQTT event.
*
* This is used by the client consumer to pass events and state changes from
* the client to the application without the need for any additional
* callbacks or client queries.
*
* Each instance carries the relevant data for specific event that caused
* it. For example an incoming message event contains a shared pointer to
* the message that arrived.
*
* The supported event types are:
* @li **message** A message arrived from the server.
* @li **connected** The client connected. If the client was configured for
* automatic reconnects, this can be from a reconnection. (No data)
* @li **connection lost** The client lost the connection. (No data)
* @li **disconnected** (v5) The client received a DISCONNECT packet from
* the server. This includes the reason code and properties for the
* disconnect.
*/
class event
{
public:
/** The variant type for any possible event. */
using event_type = std::variant<
const_message_ptr, connected_event, connection_lost_event, disconnected_event,
shutdown_event>;
private:
event_type evt_{};
public:
/**
* Constructs an empty event.
* This shows as a message, but the message pointer is null.
*/
event() {}
/**
* Constructs an event from an event type variant.
* @param evt The event type variant.
*/
event(event_type evt) : evt_{std::move(evt)} {}
/**
* Constructs a message event.
* @param msg A shared message pointer.
*/
event(message_ptr msg) : evt_{std::move(msg)} {}
/**
* Constructs a message event.
* @param msg A shared const message pointer.
*/
event(const_message_ptr msg) : evt_{std::move(msg)} {}
/**
* Constructs a 'connected' event.
* @param evt A connected event.
*/
event(connected_event evt) : evt_{std::move(evt)} {}
/**
* Constructs a 'connection lost' event.
* @param evt A connection lost event.
*/
event(connection_lost_event evt) : evt_{std::move(evt)} {}
/**
* Constructs a 'disconnected' event.
* @param evt A disconnected event.
*/
event(disconnected_event evt) : evt_{std::move(evt)} {}
/**
* Constructs a 'shutdown' event.
* @param evt A shutdown event.
*/
event(shutdown_event evt) : evt_{std::move(evt)} {}
/**
* Copy constructor.
* @param evt The event to copy.
*/
event(const event& evt) : evt_{evt.evt_} {}
/**
* Move constructor.
* @param evt The event to move.
*/
event(event&& evt) : evt_{std::move(evt.evt_)} {}
/**
* Assignment from an event type variant.
* @param evt The event type variant.
* @return A reference to this object.
*/
event& operator=(event_type evt) {
evt_ = std::move(evt);
return *this;
}
/**
* Copy assignment.
* @param rhs The event to copy.
* @return A reference to this object.
*/
event& operator=(const event& rhs) {
if (&rhs != this)
evt_ = rhs.evt_;
return *this;
}
/**
* Move assignment.
* @param rhs The event to move.
* @return A reference to this object.
*/
event& operator=(event&& rhs) {
if (&rhs != this)
evt_ = std::move(rhs.evt_);
return *this;
}
/**
* Determines if this event is an incoming message.
* @return @em true if this event is an incoming message, @em false
* otherwise.
*/
bool is_message() const { return std::holds_alternative<const_message_ptr>(evt_); }
/**
* Determines if this event is a client (re)connection.
* @return @em true if this event is a client connection, @em false
* otherwise.
*/
bool is_connected() const { return std::holds_alternative<connected_event>(evt_); }
/**
* Determines if this event is a client connection lost.
* @return @em true if this event is a client connection lost, @em false
* otherwise.
*/
bool is_connection_lost() const {
return std::holds_alternative<connection_lost_event>(evt_);
}
/**
* Determines if this event is a client disconnected.
* @return @em true if this event is a client disconnected, @em false
* otherwise.
*/
bool is_disconnected() const { return std::holds_alternative<disconnected_event>(evt_); }
/**
* Determines if this event is an internal shutdown request.
* @return @em true if this event is a shutdown request, @em false
* otherwise.
*/
bool is_shutdown() const { return std::holds_alternative<disconnected_event>(evt_); }
/**
* Determines if this is any type of client disconnect or shutdown.
* @return @em true if this event is any type of client disconnect such
* as a 'connection lost', 'disconnected', or shutdown event.
*/
bool is_any_disconnect() const {
return std::holds_alternative<connection_lost_event>(evt_) ||
std::holds_alternative<disconnected_event>(evt_) ||
std::holds_alternative<shutdown_event>(evt_);
}
/**
* Gets the message from the event, iff this is a message event.
* @return A message pointer, if this is a message event.
* @throw std::bad_variant_access if this is not a 'message' event.
*/
const_message_ptr get_message() { return std::get<const_message_ptr>(evt_); }
/**
* Gets the underlying information for a disconnected event iff this is
* a 'disconnected' event.
* This contains the reason code and properties that the server sent in
* the DISCONNECT packet.
* @return The disconnected event object containing information about
* why the server disconnected.
* @throw std::bad_variant_access if this is not a 'disconnected' event.
*/
disconnected_event get_disconnected() { return std::get<disconnected_event>(evt_); }
/**
* Gets a pointer to the message in the event, iff this is a message
* event.
* @return A pointer to a message pointer, if this is a message event.
* Returns nulltr if this is not a message event.
*/
constexpr std::add_pointer_t<const_message_ptr> get_message_if() noexcept {
return std::get_if<const_message_ptr>(&evt_);
}
/**
* Gets a pointer the underlying information for a disconnected event,
* iff this is a 'disconnected' event.
* This contains the reason code and properties that the server sent in
* the DISCONNECT packet.
* @return The disconnected event object containing information about
* why the server disconnected.
* @throw std::bad_variant_access if this is not a 'disconnected' event.
*/
constexpr std::add_pointer_t<disconnected_event> get_disconnected_if() noexcept {
return std::get_if<disconnected_event>(&evt_);
}
};
/////////////////////////////////////////////////////////////////////////////
} // namespace mqtt
#endif // __mqtt_event_h
|