SNode.C
Loading...
Searching...
No Matches
iot::mqtt::server::broker::Session Class Reference

#include <Session.h>

Inheritance diagram for iot::mqtt::server::broker::Session:
Collaboration diagram for iot::mqtt::server::broker::Session:

Public Member Functions

 Session ()=default
 
 Session (iot::mqtt::server::Mqtt *mqtt)
 
void sendPublish (iot::mqtt::server::broker::Message &message, uint8_t qoS, bool retain)
 
void publishQueued ()
 
Sessionrenew (iot::mqtt::server::Mqtt *mqtt)
 
void retain ()
 
bool isActive () const
 
bool isOwnedBy (const iot::mqtt::server::Mqtt *mqtt) const
 
nlohmann::json toJson () const final
 
void fromJson (const nlohmann::json &json)
 
- Public Member Functions inherited from iot::mqtt::Session
 Session ()=default
 
 Session (const nlohmann::json &json)
 
virtual ~Session ()=default
 
 Session (const Session &)=default
 
Sessionoperator= (const Session &)=default
 
bool isCleanSession () const
 
void fromJson (const nlohmann::json &json)
 
void clear ()
 

Private Attributes

iot::mqtt::server::Mqttmqtt = nullptr
 
std::deque< MessagemessageQueue
 

Detailed Description

Definition at line 62 of file Session.h.

Constructor & Destructor Documentation

◆ Session() [1/2]

iot::mqtt::server::broker::Session::Session ( )
default

◆ Session() [2/2]

iot::mqtt::server::broker::Session::Session ( iot::mqtt::server::Mqtt mqtt)
explicit

Definition at line 62 of file Session.cpp.

63 : mqtt(mqtt) {
64 }
iot::mqtt::server::Mqtt * mqtt
Definition Session.h:82

Referenced by iot::mqtt::server::broker::Broker::newSession().

Here is the caller graph for this function:

Member Function Documentation

◆ fromJson()

void iot::mqtt::server::broker::Session::fromJson ( const nlohmann::json &  json)

Definition at line 131 of file Session.cpp.

131 {
132 std::transform(json["message_queue"].begin(),
133 json["message_queue"].end(),
134 std::back_inserter(messageQueue),
135 [](const nlohmann::json& jsonMessage) {
136 return Message().fromJson(jsonMessage);
137 });
138
140 }
void fromJson(const nlohmann::json &json)
Definition Session.cpp:89
std::deque< Message > messageQueue
Definition Session.h:84

References iot::mqtt::server::broker::Message::fromJson(), iot::mqtt::Session::fromJson(), iot::mqtt::server::broker::Message::Message(), and messageQueue.

Referenced by iot::mqtt::server::broker::Broker::Broker().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isActive()

bool iot::mqtt::server::broker::Session::isActive ( ) const

Definition at line 113 of file Session.cpp.

113 {
114 return mqtt != nullptr;
115 }

Referenced by iot::mqtt::server::broker::Broker::hasActiveSession(), iot::mqtt::server::broker::Broker::hasRetainedSession(), and sendPublish().

Here is the caller graph for this function:

◆ isOwnedBy()

bool iot::mqtt::server::broker::Session::isOwnedBy ( const iot::mqtt::server::Mqtt mqtt) const

Definition at line 117 of file Session.cpp.

117 {
118 return this->mqtt == mqtt;
119 }

Referenced by iot::mqtt::server::broker::Broker::isActiveSession().

Here is the caller graph for this function:

◆ publishQueued()

void iot::mqtt::server::broker::Session::publishQueued ( )

Definition at line 93 of file Session.cpp.

93 {
94 LOG(INFO) << "MQTT Broker: send queued messages ...";
96 sendPublish(message, message.getQoS(), false);
97 }
98 LOG(INFO) << "MQTT Broker: ... done";
99
100 messageQueue.clear();
101 }
void sendPublish(iot::mqtt::server::broker::Message &message, uint8_t qoS, bool retain)
Definition Session.cpp:66

References iot::mqtt::server::broker::Message::getQoS(), messageQueue, and sendPublish().

Referenced by iot::mqtt::server::broker::Broker::restartSession().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ renew()

Session * iot::mqtt::server::broker::Session::renew ( iot::mqtt::server::Mqtt mqtt)

Definition at line 103 of file Session.cpp.

103 {
104 this->mqtt = mqtt;
105
106 return this;
107 }

Referenced by iot::mqtt::server::broker::Broker::renewSession().

Here is the caller graph for this function:

◆ retain()

void iot::mqtt::server::broker::Session::retain ( )

Definition at line 109 of file Session.cpp.

109 {
110 this->mqtt = nullptr;
111 }

Referenced by iot::mqtt::server::broker::Broker::retainSession().

Here is the caller graph for this function:

◆ sendPublish()

void iot::mqtt::server::broker::Session::sendPublish ( iot::mqtt::server::broker::Message message,
uint8_t  qoS,
bool  retain 
)

Definition at line 66 of file Session.cpp.

66 {
67 LOG(INFO) << "MQTT Broker: TopicName: " << message.getTopic();
68 LOG(INFO) << "MQTT Broker: Message:\n" << iot::mqtt::Mqtt::toHexString(message.getMessage());
69 LOG(DEBUG) << "MQTT Broker: QoS: " << static_cast<uint16_t>(std::min(qoS, message.getQoS()));
70
71 if (isActive()) {
72 LOG(DEBUG) << "MQTT Broker: ClientId: " << mqtt->getClientId();
73 LOG(DEBUG) << "MQTT Broker: OriginClientId: " << message.getOriginClientId();
74
75 if ((mqtt->getReflect() || mqtt->getClientId() != message.getOriginClientId())) {
76 mqtt->sendPublish(message.getTopic(),
77 message.getMessage(),
78 std::min(message.getQoS(), qoS),
79 !mqtt->getReflect() ? message.getOriginRetain() || retain : retain);
80 } else {
81 LOG(INFO) << "MQTT Broker: Suppress reflection to origin to avoid message looping";
82 }
83 } else {
84 if (message.getQoS() == 0) {
85 messageQueue.clear();
86 }
87
88 message.setQoS(std::min(message.getQoS(), qoS));
89 messageQueue.emplace_back(message);
90 }
91 }
static std::string toHexString(const std::vector< char > &data)
Definition Mqtt.cpp:389
void sendPublish(const std::string &topic, const std::string &message, uint8_t qoS, bool retain)
Definition Mqtt.cpp:217
bool getReflect() const
Definition Mqtt.cpp:436
std::string getClientId() const
Definition Mqtt.cpp:392
const std::string & getOriginClientId() const
Definition Message.cpp:62
const std::string & getTopic() const
Definition Message.cpp:66
const std::string & getMessage() const
Definition Message.cpp:74

References iot::mqtt::server::Mqtt::getClientId(), iot::mqtt::server::broker::Message::getMessage(), iot::mqtt::server::broker::Message::getOriginClientId(), iot::mqtt::server::broker::Message::getOriginRetain(), iot::mqtt::server::broker::Message::getQoS(), iot::mqtt::server::Mqtt::getReflect(), iot::mqtt::server::broker::Message::getTopic(), isActive(), messageQueue, iot::mqtt::Mqtt::sendPublish(), iot::mqtt::server::broker::Message::setQoS(), and iot::mqtt::Mqtt::toHexString().

Referenced by publishQueued(), and iot::mqtt::server::broker::Broker::sendPublish().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toJson()

nlohmann::json iot::mqtt::server::broker::Session::toJson ( ) const
finalvirtual

Reimplemented from iot::mqtt::Session.

Definition at line 121 of file Session.cpp.

121 {
122 nlohmann::json json = iot::mqtt::Session::toJson();
123
124 std::transform(messageQueue.begin(), messageQueue.end(), std::back_inserter(json["message_queue"]), [](const Message& message) {
125 return message.toJson();
126 });
127
128 return json;
129 }
virtual nlohmann::json toJson() const
Definition Session.cpp:59

References messageQueue, iot::mqtt::server::broker::Message::toJson(), and iot::mqtt::Session::toJson().

Referenced by iot::mqtt::server::broker::Broker::~Broker().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ messageQueue

std::deque<Message> iot::mqtt::server::broker::Session::messageQueue
private

Definition at line 84 of file Session.h.

Referenced by fromJson(), publishQueued(), sendPublish(), and toJson().

◆ mqtt

iot::mqtt::server::Mqtt* iot::mqtt::server::broker::Session::mqtt = nullptr
private

Definition at line 82 of file Session.h.


The documentation for this class was generated from the following files: