MQTTSuite
Loading...
Searching...
No Matches
mqtt::mqttbroker::lib::MqttModel Class Reference

#include <MqttModel.h>

Collaboration diagram for mqtt::mqttbroker::lib::MqttModel:

Classes

class  EventReceiver

Public Member Functions

void addEventReceiver (const std::shared_ptr< express::Response > &response, const std::string &lastEventId, const std::shared_ptr< iot::mqtt::server::broker::Broker > &broker)
void connectClient (Mqtt *mqtt)
void disconnectClient (const std::string &clientId)
void subscribeClient (const std::string &clientId, const std::string &topic, const uint8_t qos)
void unsubscribeClient (const std::string &clientId, const std::string &topic)
void publishMessage (const std::string &topic, const std::string &message, uint8_t qoS, bool retain)
const std::map< std::string, Mqtt * > & getClients () const
MqttgetMqtt (const std::string &clientId) const
std::string onlineSince () const
std::string onlineDuration () const

Static Public Member Functions

static MqttModelinstance ()

Private Member Functions

 MqttModel ()
void sendEvent (const std::string &data, const std::string &event="", const std::string &id="") const
void sendJsonEvent (const nlohmann::json &json, const std::string &event="", const std::string &id="") const

Static Private Member Functions

static void sendEvent (const std::shared_ptr< express::Response > &response, const std::string &data, const std::string &event, const std::string &id)
static void sendJsonEvent (const std::shared_ptr< express::Response > &response, const nlohmann::json &json, const std::string &event="", const std::string &id="")
static std::string timePointToString (const std::chrono::time_point< std::chrono::system_clock > &timePoint)
static std::string durationToString (const std::chrono::time_point< std::chrono::system_clock > &bevore, const std::chrono::time_point< std::chrono::system_clock > &later=std::chrono::system_clock::now())

Private Attributes

std::map< std::string, Mqtt * > modelMap
std::list< EventReceivereventReceiverList
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
std::uint64_t nextEventReceiverId = 0
uint64_t id = 0

Detailed Description

Definition at line 75 of file MqttModel.h.

Constructor & Destructor Documentation

◆ MqttModel()

mqtt::mqttbroker::lib::MqttModel::MqttModel ( )
private

Definition at line 175 of file MqttModel.cpp.

176 : onlineSinceTimePoint(std::chrono::system_clock::now()) {
177 }
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
Definition MqttModel.h:135

References onlineSinceTimePoint.

Member Function Documentation

◆ addEventReceiver()

void mqtt::mqttbroker::lib::MqttModel::addEventReceiver ( const std::shared_ptr< express::Response > & response,
const std::string & lastEventId,
const std::shared_ptr< iot::mqtt::server::broker::Broker > & broker )

Definition at line 185 of file MqttModel.cpp.

187 {
188 const std::uint64_t eventReceiverId = nextEventReceiverId++;
189
190 eventReceiverList.emplace_back(eventReceiverId, response);
191
192 response->getSocketContext()->onDisconnected([this, eventReceiverId]() {
193 eventReceiverList.remove_if([eventReceiverId](const EventReceiver& eventReceiver) {
194 return eventReceiver.getId() == eventReceiverId;
195 });
196 });
197
198 /*
199 {
200 "title": "MQTTBroker",
201 "creator": {
202 "name": "Volker Christian",
203 "url": "https://github.com/VolkerChristian/"
204 },
205 "broker": {
206 "name": "MQTTBroker",
207 "url": "https://github.com/SNodeC/mqttsuite/tree/master/mqttbroker"
208 },
209 "suite": {
210 "name": "MQTTSuite",
211 "url": "https://github.com/SNodeC/mqttsuite"
212 },
213 "snodec": {
214 "name": "SNode.C",
215 "url": "https://github.com/SNodeC/snode.c"
216 },
217 "since": "2025-12-25 10:30:00 UTC",
218 "duration": "2 days, 03:45:12"
219 }
220 */
221 sendJsonEvent(response,
222 {
223 {"title", "MQTTBroker"},
224 {"creator", {{"name", "Volker Christian"}, {"url", "https://github.com/VolkerChristian"}}},
225 {"broker", {{"name", "MQTTBroker"}, {"url", "https://github.com/SNodeC/mqttsuite/tree/master/mqttbroker"}}},
226 {"suite", {{"name", "MQTTSuite"}, {"url", "https://github.com/SNodeC/mqttsuite"}}},
227 {"snodec", {{"name", "SNode.C"}, {"url", "https://github.com/SNodeC/snode.c"}}},
228 {"since", onlineSince()},
229 {"duration", onlineDuration()},
230 },
231 "ui-initialize",
232 std::to_string(id++));
233
234 for (const auto& modelMapEntry : modelMap) {
235 sendJsonEvent(response, modelMapEntry.second, "client-connected", std::to_string(id++));
236 }
237
238 for (const auto& [topic, clients] : broker->getSubscriptionTree()) {
239 for (const auto& client : clients) {
240 sendJsonEvent(response, subscribe{topic, client.first, client.second}, "client-subscribed", std::to_string(id++));
241 }
242 }
243
244 for (const auto& [topic, retained] : broker->getRetainTree()) {
245 sendJsonEvent(response, retaine{topic, retained.first, retained.second}, "retained-message-set", std::to_string(id++));
246 }
247 }
std::map< std::string, Mqtt * > modelMap
Definition MqttModel.h:133
std::list< EventReceiver > eventReceiverList
Definition MqttModel.h:134
static void sendJsonEvent(const std::shared_ptr< express::Response > &response, const nlohmann::json &json, const std::string &event="", const std::string &id="")
std::string onlineDuration() const
std::string onlineSince() const

References eventReceiverList, mqtt::mqttbroker::lib::MqttModel::EventReceiver::getId(), id, modelMap, nextEventReceiverId, onlineDuration(), and onlineSince().

Referenced by getRouter().

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

◆ connectClient()

void mqtt::mqttbroker::lib::MqttModel::connectClient ( Mqtt * mqtt)

Definition at line 249 of file MqttModel.cpp.

249 {
250 modelMap.emplace(mqtt->getClientId(), mqtt);
251
252 sendJsonEvent(mqtt, "client-connected", std::to_string(id++));
253 }

References id, and modelMap.

Referenced by mqtt::mqttbroker::lib::Mqtt::onConnect().

Here is the caller graph for this function:

◆ disconnectClient()

void mqtt::mqttbroker::lib::MqttModel::disconnectClient ( const std::string & clientId)

Definition at line 255 of file MqttModel.cpp.

255 {
256 if (modelMap.contains(clientId)) {
257 sendJsonEvent(modelMap[clientId], "client-disconnected", std::to_string(id++));
258
259 modelMap.erase(clientId);
260 }
261 }

References id, and modelMap.

Referenced by mqtt::mqttbroker::lib::Mqtt::onDisconnected().

Here is the caller graph for this function:

◆ durationToString()

std::string mqtt::mqttbroker::lib::MqttModel::durationToString ( const std::chrono::time_point< std::chrono::system_clock > & bevore,
const std::chrono::time_point< std::chrono::system_clock > & later = std::chrono::system_clock::now() )
staticprivate

Definition at line 356 of file MqttModel.cpp.

357 {
358 using seconds_duration_type = std::chrono::duration<std::chrono::seconds::rep>::rep;
359
360 seconds_duration_type totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(later - bevore).count();
361
362 // Compute days, hours, minutes, and seconds
363 seconds_duration_type days = totalSeconds / 86400; // 86400 seconds in a day
364 seconds_duration_type remainder = totalSeconds % 86400;
365 seconds_duration_type hours = remainder / 3600;
366 remainder = remainder % 3600;
367 seconds_duration_type minutes = remainder / 60;
368 seconds_duration_type seconds = remainder % 60;
369
370 // Format the components into a string using stringstream
371 std::ostringstream oss;
372 if (days > 0) {
373 oss << days << " day" << (days == 1 ? "" : "s") << ", ";
374 }
375 oss << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2)
376 << std::setfill('0') << seconds;
377
378 return oss.str();
379 }

◆ getClients()

const std::map< std::string, Mqtt * > & mqtt::mqttbroker::lib::MqttModel::getClients ( ) const

Definition at line 281 of file MqttModel.cpp.

281 {
282 return modelMap;
283 }

References modelMap.

◆ getMqtt()

Mqtt * mqtt::mqttbroker::lib::MqttModel::getMqtt ( const std::string & clientId) const

Definition at line 285 of file MqttModel.cpp.

285 {
286 Mqtt* mqtt = nullptr;
287
288 auto modelIt = modelMap.find(clientId);
289 if (modelIt != modelMap.end()) {
290 mqtt = modelIt->second;
291 }
292
293 return mqtt;
294 }

References modelMap.

◆ instance()

MqttModel & mqtt::mqttbroker::lib::MqttModel::instance ( )
static

Definition at line 179 of file MqttModel.cpp.

179 {
180 static MqttModel mqttModel;
181
182 return mqttModel;
183 }

Referenced by getRouter(), mqtt::mqttbroker::lib::Mqtt::onConnect(), mqtt::mqttbroker::lib::Mqtt::onDisconnected(), mqtt::mqttbroker::lib::Mqtt::onPublish(), mqtt::mqttbroker::lib::Mqtt::onSubscribe(), and mqtt::mqttbroker::lib::Mqtt::onUnsubscribe().

Here is the caller graph for this function:

◆ onlineDuration()

std::string mqtt::mqttbroker::lib::MqttModel::onlineDuration ( ) const

Definition at line 300 of file MqttModel.cpp.

300 {
302 }
static std::string durationToString(const std::chrono::time_point< std::chrono::system_clock > &bevore, const std::chrono::time_point< std::chrono::system_clock > &later=std::chrono::system_clock::now())

References onlineSinceTimePoint.

Referenced by addEventReceiver().

Here is the caller graph for this function:

◆ onlineSince()

std::string mqtt::mqttbroker::lib::MqttModel::onlineSince ( ) const

Definition at line 296 of file MqttModel.cpp.

296 {
298 }
static std::string timePointToString(const std::chrono::time_point< std::chrono::system_clock > &timePoint)

References onlineSinceTimePoint.

Referenced by addEventReceiver().

Here is the caller graph for this function:

◆ publishMessage()

void mqtt::mqttbroker::lib::MqttModel::publishMessage ( const std::string & topic,
const std::string & message,
uint8_t qoS,
bool retain )

Definition at line 271 of file MqttModel.cpp.

271 {
272 if (retain) {
273 if (!message.empty()) {
274 sendJsonEvent(retaine{topic, message, qoS}, "retained-message-set", std::to_string(id++));
275 } else {
276 sendJsonEvent(release{topic}, "retained-message-deleted", std::to_string(id++));
277 }
278 }
279 }

References id.

Referenced by mqtt::mqttbroker::lib::Mqtt::onPublish().

Here is the caller graph for this function:

◆ sendEvent() [1/2]

void mqtt::mqttbroker::lib::MqttModel::sendEvent ( const std::shared_ptr< express::Response > & response,
const std::string & data,
const std::string & event,
const std::string & id )
staticprivate

Definition at line 304 of file MqttModel.cpp.

307 {
308 if (response->isConnected()) {
309 if (!event.empty()) {
310 response->sendFragment("event:" + event);
311 }
312 if (!id.empty()) {
313 response->sendFragment("id:" + id);
314 }
315 response->sendFragment("data:" + data);
316 response->sendFragment();
317 }
318 }

Referenced by sendEvent(), and sendJsonEvent().

Here is the caller graph for this function:

◆ sendEvent() [2/2]

void mqtt::mqttbroker::lib::MqttModel::sendEvent ( const std::string & data,
const std::string & event = "",
const std::string & id = "" ) const
private

Definition at line 327 of file MqttModel.cpp.

327 {
328 for (auto& eventReceiver : eventReceiverList) {
329 if (const auto& response = eventReceiver.response.lock()) {
330 sendEvent(response, data, event, id);
331 }
332 }
333 }
static void sendEvent(const std::shared_ptr< express::Response > &response, const std::string &data, const std::string &event, const std::string &id)

References eventReceiverList, mqtt::mqttbroker::lib::MqttModel::EventReceiver::response, and sendEvent().

Referenced by sendJsonEvent().

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

◆ sendJsonEvent() [1/2]

void mqtt::mqttbroker::lib::MqttModel::sendJsonEvent ( const nlohmann::json & json,
const std::string & event = "",
const std::string & id = "" ) const
private

Definition at line 335 of file MqttModel.cpp.

335 {
336 VLOG(0) << "Server sent event: " << event << "\n" << json.dump(4);
337
338 sendEvent(json.dump(), event, id);
339 }
nlohmann::json json

References sendEvent().

Here is the call graph for this function:

◆ sendJsonEvent() [2/2]

void mqtt::mqttbroker::lib::MqttModel::sendJsonEvent ( const std::shared_ptr< express::Response > & response,
const nlohmann::json & json,
const std::string & event = "",
const std::string & id = "" )
staticprivate

Definition at line 320 of file MqttModel.cpp.

323 {
324 sendEvent(response, json.dump(), event, id);
325 }

References sendEvent().

Here is the call graph for this function:

◆ subscribeClient()

void mqtt::mqttbroker::lib::MqttModel::subscribeClient ( const std::string & clientId,
const std::string & topic,
const uint8_t qos )

Definition at line 263 of file MqttModel.cpp.

263 {
264 sendJsonEvent(subscribe{topic, clientId, qos}, "client-subscribed", std::to_string(id++));
265 }

References id.

Referenced by mqtt::mqttbroker::lib::Mqtt::onSubscribe().

Here is the caller graph for this function:

◆ timePointToString()

std::string mqtt::mqttbroker::lib::MqttModel::timePointToString ( const std::chrono::time_point< std::chrono::system_clock > & timePoint)
staticprivate

Definition at line 341 of file MqttModel.cpp.

341 {
342 std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
343 std::tm* tm_ptr = std::gmtime(&time);
344
345 char buffer[100];
346 std::string onlineSince = "Formatting error";
347
348 // Format: "2025-02-02 14:30:00"
349 if (std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_ptr)) {
350 onlineSince = std::string(buffer) + " UTC";
351 }
352
353 return onlineSince;
354 }

◆ unsubscribeClient()

void mqtt::mqttbroker::lib::MqttModel::unsubscribeClient ( const std::string & clientId,
const std::string & topic )

Definition at line 267 of file MqttModel.cpp.

267 {
268 sendJsonEvent(unsubscribe{clientId, topic}, "client-unsubscribed", std::to_string(id++));
269 }

References id.

Referenced by mqtt::mqttbroker::lib::Mqtt::onUnsubscribe().

Here is the caller graph for this function:

Member Data Documentation

◆ eventReceiverList

std::list<EventReceiver> mqtt::mqttbroker::lib::MqttModel::eventReceiverList
private

Definition at line 134 of file MqttModel.h.

Referenced by addEventReceiver(), and sendEvent().

◆ id

uint64_t mqtt::mqttbroker::lib::MqttModel::id = 0
private

◆ modelMap

std::map<std::string, Mqtt*> mqtt::mqttbroker::lib::MqttModel::modelMap
private

Definition at line 133 of file MqttModel.h.

Referenced by addEventReceiver(), connectClient(), disconnectClient(), getClients(), and getMqtt().

◆ nextEventReceiverId

std::uint64_t mqtt::mqttbroker::lib::MqttModel::nextEventReceiverId = 0
private

Definition at line 136 of file MqttModel.h.

Referenced by addEventReceiver().

◆ onlineSinceTimePoint

std::chrono::time_point<std::chrono::system_clock> mqtt::mqttbroker::lib::MqttModel::onlineSinceTimePoint
private

Definition at line 135 of file MqttModel.h.

Referenced by MqttModel(), onlineDuration(), and onlineSince().


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