MQTTSuite
Loading...
Searching...
No Matches
Mqtt.cpp
Go to the documentation of this file.
1/*
2 * MQTTSuite - A lightweight MQTT Integration System
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2022, 2023, 2024, 2025, 2026
5 * Tobias Pfeil
6 * 2025, 2026
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation, either version 3 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22/*
23 * MIT License
24 *
25 * Permission is hereby granted, free of charge, to any person obtaining a copy
26 * of this software and associated documentation files (the "Software"), to deal
27 * in the Software without restriction, including without limitation the rights
28 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 * copies of the Software, and to permit persons to whom the Software is
30 * furnished to do so, subject to the following conditions:
31 *
32 * The above copyright notice and this permission notice shall be included in
33 * all copies or substantial portions of the Software.
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41 * THE SOFTWARE.
42 */
43
44#include "Mqtt.h"
45
46#include "lib/MqttMapper.h"
47#include "mqttbroker/lib/MqttModel.h"
48
49#include <iot/mqtt/packets/Publish.h>
50#include <iot/mqtt/packets/Subscribe.h>
51#include <iot/mqtt/packets/Unsubscribe.h>
52#include <iot/mqtt/server/broker/Broker.h>
53
54#ifndef DOXYGEN_SHOULD_SKIP_THIS
55
56#include <functional>
57#include <list>
58
59#endif
60
61namespace mqtt::mqttbroker::lib {
62
64 utils::Timeval when;
65 std::size_t seq;
66 iot::mqtt::packets::Publish publish;
67 utils::Timeval delay;
68 };
69
70 Mqtt::Mqtt(const std::string& connectionName,
71 const std::shared_ptr<iot::mqtt::server::broker::Broker>& broker,
72 const std::shared_ptr<mqtt::lib::MqttMapper>& mqttMapper)
73 : iot::mqtt::server::Mqtt(connectionName, broker)
74 , mqttMapper(mqttMapper)
75 , delayedQueue(this) {
76 }
77
78 bool Mqtt::EarlierFirst::operator()(const ScheduledPublish& a, const ScheduledPublish& b) const {
79 if (a.when != b.when) {
80 return a.when > b.when;
81 }
82
83 return a.seq > b.seq;
84 }
85
87 : mqtt(mqtt) {
88 }
89
91 delayTimer.cancel();
92 }
93
95 const auto now = utils::Timeval::currentTime();
96
97 while (!empty() && top().when <= now) {
98 const iot::mqtt::packets::Publish duePublish = top().publish;
99 pop();
100
101 mqtt->broker->publish(
102 mqtt->clientId, duePublish.getTopic(), duePublish.getMessage(), duePublish.getQoS(), duePublish.getRetain());
103
104 mqtt->onPublish(duePublish);
105 }
106 }
107
109 delayTimer.cancel();
110
111 auto delay = top().when - utils::Timeval::currentTime();
112 if (delay < utils::Timeval{}) {
113 delay = utils::Timeval{};
114 }
115
116 delayTimer = core::timer::Timer::singleshotTimer(
117 [this]() {
119
120 if (!empty()) {
122 }
123 },
124 delay);
125 }
126
127 void Mqtt::DelayedQueue::delayPublish(const utils::Timeval& delay, const iot::mqtt::packets::Publish& publish) {
128 minHeap.push({utils::Timeval::currentTime() + delay, nextSeq++, publish, delay});
130 }
131
132 bool Mqtt::DelayedQueue::empty() const {
133 return minHeap.empty();
134 }
135
137 return minHeap.top();
138 }
139
141 minHeap.pop();
142 }
143
144 void Mqtt::subscribe(const std::string& topic, uint8_t qoS) {
145 broker->subscribe(clientId, topic, qoS);
146 onSubscribe(iot::mqtt::packets::Subscribe(0, {{topic, qoS}}));
147 }
148
149 void Mqtt::unsubscribe(const std::string& topic) {
150 broker->unsubscribe(clientId, topic);
151 onUnsubscribe(iot::mqtt::packets::Unsubscribe(0, {{topic, 0}}));
152 }
153
154 void Mqtt::onConnect([[maybe_unused]] const iot::mqtt::packets::Connect& connect) {
156 }
157
158 void Mqtt::onPublish(const iot::mqtt::packets::Publish& publish) {
159 MqttModel::instance().publishMessage(publish.getTopic(), publish.getMessage(), publish.getQoS(), publish.getRetain());
160
161 if (mqttMapper != nullptr) {
162 mqtt::lib::MqttMapper::MappedPublishes mappedPublishes = mqttMapper->getMappings(publish);
163
164 for (const mqtt::lib::MqttMapper::ScheduledPublish& delayedPublish : mappedPublishes.second) {
165 delayedQueue.delayPublish(delayedPublish.delay, delayedPublish.publish);
166 }
167
168 for (const iot::mqtt::packets::Publish& mappedPublish : mappedPublishes.first) {
169 broker->publish(
170 clientId, mappedPublish.getTopic(), mappedPublish.getMessage(), mappedPublish.getQoS(), mappedPublish.getRetain());
171
172 onPublish(mappedPublish);
173 }
174 }
175 }
176
177 void Mqtt::onSubscribe(const iot::mqtt::packets::Subscribe& subscribe) {
178 for (const iot::mqtt::Topic& topic : subscribe.getTopics()) {
179 MqttModel::instance().subscribeClient(clientId, topic.getName(), topic.getQoS());
180 }
181 }
182
183 void Mqtt::onUnsubscribe(const iot::mqtt::packets::Unsubscribe& unsubscribe) {
184 for (const std::string& topic : unsubscribe.getTopics()) {
186 }
187 }
188
192
193} // namespace mqtt::mqttbroker::lib
MappedPublishes getMappings(const iot::mqtt::packets::Publish &publish)
std::pair< std::vector< iot::mqtt::packets::Publish >, std::vector< ScheduledPublish > > MappedPublishes
Definition MqttMapper.h:83
void publishMessage(const std::string &topic, const std::string &message, uint8_t qoS, bool retain)
static MqttModel & instance()
void unsubscribeClient(const std::string &clientId, const std::string &topic)
void subscribeClient(const std::string &clientId, const std::string &topic, const uint8_t qos)
void disconnectClient(const std::string &clientId)
void delayPublish(const utils::Timeval &delay, const iot::mqtt::packets::Publish &publish)
Definition Mqtt.cpp:127
const ScheduledPublish & top() const
Definition Mqtt.cpp:136
std::priority_queue< ScheduledPublish, std::vector< ScheduledPublish >, EarlierFirst > minHeap
Definition Mqtt.h:105
void unsubscribe(const std::string &topic)
Definition Mqtt.cpp:149
void onPublish(const iot::mqtt::packets::Publish &publish) final
Definition Mqtt.cpp:158
void onConnect(const iot::mqtt::packets::Connect &connect) final
Definition Mqtt.cpp:154
void subscribe(const std::string &topic, uint8_t qoS)
Definition Mqtt.cpp:144
void onDisconnected() final
Definition Mqtt.cpp:189
std::shared_ptr< mqtt::lib::MqttMapper > mqttMapper
Definition Mqtt.h:118
void onSubscribe(const iot::mqtt::packets::Subscribe &subscribe) final
Definition Mqtt.cpp:177
void onUnsubscribe(const iot::mqtt::packets::Unsubscribe &unsubscribe) final
Definition Mqtt.cpp:183
Mqtt(const std::string &connectionName, const std::shared_ptr< iot::mqtt::server::broker::Broker > &broker, const std::shared_ptr< mqtt::lib::MqttMapper > &mqttMapper)
Definition Mqtt.cpp:70
DelayedQueue delayedQueue
Definition Mqtt.h:119
iot::mqtt::packets::Publish publish
Definition MqttMapper.h:80
bool operator()(const ScheduledPublish &a, const ScheduledPublish &b) const
Definition Mqtt.cpp:78
iot::mqtt::packets::Publish publish
Definition Mqtt.cpp:66