MQTTSuite
Loading...
Searching...
No Matches
Mqtt.h
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#ifndef MQTTBROKER_LIB_MQTT_H
45#define MQTTBROKER_LIB_MQTT_H
46
47#include <iot/mqtt/server/Mqtt.h>
48
49namespace iot::mqtt {
50 namespace server::broker {
51 class Broker;
52 }
53 namespace packets {
54 class Publish;
55 }
56} // namespace iot::mqtt
57
58namespace mqtt::lib {
59 class MqttMapper;
60}
61
62#ifndef DOXYGEN_SHOULD_SKIP_THIS
63
64#include <cstddef>
65#include <cstdint>
66#include <memory>
67#include <queue>
68#include <string>
69#include <vector>
70
71#endif // DOXYGEN_SHOULD_SKIP_THIS
72
73namespace mqtt::mqttbroker::lib {
74
75 class Mqtt : public iot::mqtt::server::Mqtt {
76 public:
77 explicit Mqtt(const std::string& connectionName,
78 const std::shared_ptr<iot::mqtt::server::broker::Broker>& broker,
79 const std::shared_ptr<mqtt::lib::MqttMapper>& mqttMapper);
80
81 void subscribe(const std::string& topic, uint8_t qoS);
82 void unsubscribe(const std::string& topic);
83
84 private:
85 struct ScheduledPublish;
86
87 struct EarlierFirst {
88 bool operator()(const ScheduledPublish& a, const ScheduledPublish& b) const;
89 };
90
92 public:
93 explicit DelayedQueue(Mqtt* mqtt);
94 ~DelayedQueue();
95
96 void delayPublish(const utils::Timeval& delay, const iot::mqtt::packets::Publish& publish);
97
98 bool empty() const;
99 const ScheduledPublish& top() const;
100 void pop();
101
102 private:
104 std::size_t nextSeq = 0;
105 std::priority_queue<ScheduledPublish, std::vector<ScheduledPublish>, EarlierFirst> minHeap;
106
107 core::timer::Timer delayTimer;
108 void processDue();
109 void armDelayTimer();
110 };
111
112 void onConnect(const iot::mqtt::packets::Connect& connect) final;
113 void onPublish(const iot::mqtt::packets::Publish& publish) final;
114 void onSubscribe(const iot::mqtt::packets::Subscribe& subscribe) final;
115 void onUnsubscribe(const iot::mqtt::packets::Unsubscribe& unsubscribe) final;
116 void onDisconnected() final;
117
118 std::shared_ptr<mqtt::lib::MqttMapper> mqttMapper;
120 };
121
122} // namespace mqtt::mqttbroker::lib
123
124#endif // MQTTBROKER_LIB_MQTT_H
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