Paho C++  1.0
The Paho MQTT C++ Client Library
 All Classes Files Functions Variables Typedefs Friends
message.h
Go to the documentation of this file.
1 
8 /*******************************************************************************
9  * Copyright (c) 2013-2017 Frank Pagliughi <fpagliughi@mindspring.com>
10  *
11  * All rights reserved. This program and the accompanying materials
12  * are made available under the terms of the Eclipse Public License v1.0
13  * and Eclipse Distribution License v1.0 which accompany this distribution.
14  *
15  * The Eclipse Public License is available at
16  * http://www.eclipse.org/legal/epl-v10.html
17  * and the Eclipse Distribution License is available at
18  * http://www.eclipse.org/org/documents/edl-v10.php.
19  *
20  * Contributors:
21  * Frank Pagliughi - initial implementation and documentation
22  *******************************************************************************/
23 
24 #ifndef __mqtt_message_h
25 #define __mqtt_message_h
26 
27 #include "MQTTAsync.h"
28 #include "mqtt/buffer_ref.h"
29 #include "mqtt/exception.h"
30 #include <memory>
31 
32 namespace mqtt {
33 
35 
52 class message
53 {
54 public:
56  static constexpr int DFLT_QOS = 0;
58  static constexpr bool DFLT_RETAINED = false;
59 
60 private:
62  static const MQTTAsync_message DFLT_C_STRUCT;
63 
65  static const string EMPTY_STR;
67  static const binary EMPTY_BIN;
68 
70  MQTTAsync_message msg_;
72  string_ref topic_;
74  binary_ref payload_;
75 
77  friend class async_client;
78  friend class message_test;
79 
84  void set_duplicate(bool dup) { msg_.dup = to_int(dup); }
85 
86 public:
88  using ptr_t = std::shared_ptr<message>;
90  using const_ptr_t = std::shared_ptr<const message>;
91 
96  message();
106  message(string_ref topic, const void* payload, size_t len,
107  int qos, bool retained);
115  message(string_ref topic, const void* payload, size_t len)
116  : message(std::move(topic), payload, len, DFLT_QOS, DFLT_RETAINED) {}
125  message(string_ref topic, binary_ref payload, int qos, bool retained);
133  : message(std::move(topic), std::move(payload), DFLT_QOS, DFLT_RETAINED) {}
139  message(string_ref topic, const MQTTAsync_message& msg);
144  message(const message& other);
149  message(message&& other);
153  ~message() {}
154 
164  static ptr_t create(string_ref topic, const void* payload, size_t len,
165  int qos, bool retained) {
166  return std::make_shared<message>(std::move(topic), payload, len,
167  qos, retained);
168  }
176  static ptr_t create(string_ref topic, const void* payload, size_t len) {
177  return std::make_shared<message>(std::move(topic), payload, len,
179  }
188  static ptr_t create(string_ref topic, binary_ref payload, int qos, bool retained) {
189  return std::make_shared<message>(std::move(topic), std::move(payload),
190  qos, retained);
191  }
199  return std::make_shared<message>(std::move(topic), std::move(payload),
201  }
207  static ptr_t create(string_ref topic, const MQTTAsync_message& msg) {
208  return std::make_shared<message>(std::move(topic), msg);
209  }
215  message& operator=(const message& rhs);
221  message& operator=(message&& rhs);
227  topic_ = topic_ ? std::move(topic) : string_ref(string());
228  }
233  const string_ref& get_topic_ref() const { return topic_; }
238  const string& get_topic() const {
239  return topic_ ? topic_.str() : EMPTY_STR;
240  }
244  void clear_payload();
248  const binary_ref& get_payload_ref() const { return payload_; }
252  const binary& get_payload() const {
253  return payload_ ? payload_.str() : EMPTY_BIN;
254  }
258  const string& get_payload_str() const {
259  return payload_ ? payload_.str() : EMPTY_STR;
260  }
265  int get_qos() const { return msg_.qos; }
272  bool is_duplicate() const { return to_bool(msg_.dup); }
279  bool is_retained() const { return to_bool(msg_.retained); }
287  void set_payload(binary_ref payload);
293  void set_payload(const void* payload, size_t n) {
294  set_payload(binary_ref(static_cast<const binary_ref::value_type*>(payload), n));
295  }
300  void set_qos(int qos) {
301  validate_qos(qos);
302  msg_.qos = qos;
303  }
309  void set_retained(bool retained) { msg_.retained = to_int(retained); }
314  string to_string() const { return get_payload_str(); }
320  static void validate_qos(int qos) {
321  if (qos < 0 || qos > 2)
322  throw exception(MQTTASYNC_BAD_QOS, "Bad QoS");
323  }
324 };
325 
327 using message_ptr = message::ptr_t;
328 
330 using const_message_ptr = message::const_ptr_t;
331 
339 inline message_ptr make_message(string_ref topic, const void* payload, size_t len) {
340  return mqtt::message::create(std::move(topic), payload, len);
341 }
342 
352 inline message_ptr make_message(string_ref topic, const void* payload, size_t len,
353  int qos, bool retained) {
354  return mqtt::message::create(std::move(topic), payload, len, qos, retained);
355 }
356 
363 inline message_ptr make_message(string_ref topic, binary_ref payload) {
364  return mqtt::message::create(std::move(topic), std::move(payload));
365 }
366 
374 inline message_ptr make_message(string_ref topic, binary_ref payload,
375  int qos, bool retained) {
376  return mqtt::message::create(std::move(topic), std::move(payload), qos, retained);
377 }
378 
380 // end namespace mqtt
381 }
382 
383 #endif // __mqtt_message_h
384 
const string & get_topic() const
Gets the topic for the message.
Definition: message.h:238
void set_payload(const void *payload, size_t n)
Sets the payload of this message to be the specified byte array.
Definition: message.h:293
void set_topic(string_ref topic)
Sets the topic string.
Definition: message.h:226
Lightweight client for talking to an MQTT server using non-blocking methods that allow an operation t...
Definition: async_client.h:60
void set_qos(int qos)
Sets the quality of service for this message.
Definition: message.h:300
static ptr_t create(string_ref topic, binary_ref payload, int qos, bool retained)
Constructs a message from a byte buffer.
Definition: message.h:188
void set_payload(binary_ref payload)
Sets the payload of this message to be the specified buffer.
bool is_duplicate() const
Returns whether or not this message might be a duplicate of one which has already been received...
Definition: message.h:272
string to_string() const
Returns a string representation of this messages payload.
Definition: message.h:314
static ptr_t create(string_ref topic, const MQTTAsync_message &msg)
Constructs a message as a copy of the C message struct.
Definition: message.h:207
const blob & str() const
Gets the data buffer as a string.
Definition: buffer_ref.h:246
message(string_ref topic, const void *payload, size_t len)
Constructs a message with the specified array as a payload, and all other values set to defaults...
Definition: message.h:115
Represents a topic destination, used for publish/subscribe messaging.
Definition: topic.h:42
int get_qos() const
Returns the quality of service for this message.
Definition: message.h:265
Declaration of MQTT exception class.
void set_retained(bool retained)
Whether or not the publish message should be retained by the broker.
Definition: message.h:309
std::shared_ptr< message > ptr_t
Smart/shared pointer to this class.
Definition: message.h:88
message & operator=(const message &rhs)
Copies another message to this one.
Base mqtt::exception.
Definition: exception.h:42
const string & get_payload_str() const
Gets the payload as a string.
Definition: message.h:258
message(string_ref topic, binary_ref payload)
Constructs a message from a byte buffer.
Definition: message.h:132
const binary_ref & get_payload_ref() const
Gets the payload reference.
Definition: message.h:248
An MQTT message holds everything required for an MQTT PUBLISH message.
Definition: message.h:52
const string_ref & get_topic_ref() const
Gets the topic reference for the message.
Definition: message.h:233
Buffer reference type for the Paho MQTT C++ library.
static constexpr bool DFLT_RETAINED
The default retained flag.
Definition: message.h:58
~message()
Destroys a message and frees all associated resources.
Definition: message.h:153
static ptr_t create(string_ref topic, const void *payload, size_t len)
Constructs a message with the specified array as a payload, and all other values set to defaults...
Definition: message.h:176
bool is_retained() const
Returns whether or not this message should be/was retained by the server.
Definition: message.h:279
static void validate_qos(int qos)
Determines if the QOS value is a valid one.
Definition: message.h:320
const binary & get_payload() const
Gets the payload.
Definition: message.h:252
static ptr_t create(string_ref topic, binary_ref payload)
Constructs a message from a byte buffer.
Definition: message.h:198
message()
Constructs a message with an empty payload, and all other values set to defaults. ...
static constexpr int DFLT_QOS
The default QoS for a message.
Definition: message.h:56
static ptr_t create(string_ref topic, const void *payload, size_t len, int qos, bool retained)
Constructs a message with the specified array as a payload, and all other values set to defaults...
Definition: message.h:164
std::shared_ptr< const message > const_ptr_t
Smart/shared pointer to this class.
Definition: message.h:90
void clear_payload()
Clears the payload, resetting it to be empty.