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

#include <MqttModel.h>

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

Classes

class  MqttModelEntry
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, MqttModelEntry > & 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, MqttModelEntrymodelMap
std::list< EventReceivereventReceiverList
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
uint64_t id = 0

Friends

void to_json (nlohmann::json &j, const MqttModel::MqttModelEntry &mqttModelEntry)

Detailed Description

Definition at line 74 of file MqttModel.h.

Constructor & Destructor Documentation

◆ MqttModel()

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

Definition at line 200 of file MqttModel.cpp.

201 : onlineSinceTimePoint(std::chrono::system_clock::now()) {
202 }
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
Definition MqttModel.h:149

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 210 of file MqttModel.cpp.

212 {
213 auto& eventReceiver = eventReceiverList.emplace_back(response);
214
215 response->getSocketContext()->onDisconnected([this, &eventReceiver]() {
216 eventReceiverList.remove(eventReceiver);
217 });
218
219 /*
220 {
221 "title": "MQTTBroker",
222 "creator": {
223 "name": "Volker Christian",
224 "url": "https://github.com/VolkerChristian/"
225 },
226 "broker": {
227 "name": "MQTTBroker",
228 "url": "https://github.com/SNodeC/mqttsuite/tree/master/mqttbroker"
229 },
230 "suite": {
231 "name": "MQTTSuite",
232 "url": "https://github.com/SNodeC/mqttsuite"
233 },
234 "snodec": {
235 "name": "SNode.C",
236 "url": "https://github.com/SNodeC/snode.c"
237 },
238 "since": "2025-12-25 10:30:00 UTC",
239 "duration": "2 days, 03:45:12"
240 }
241 */
242 sendJsonEvent(response,
243 {
244 {"title", "MQTTBroker"},
245 {"creator", {{"name", "Volker Christian"}, {"url", "https://github.com/VolkerChristian"}}},
246 {"broker", {{"name", "MQTTBroker"}, {"url", "https://github.com/SNodeC/mqttsuite/tree/master/mqttbroker"}}},
247 {"suite", {{"name", "MQTTSuite"}, {"url", "https://github.com/SNodeC/mqttsuite"}}},
248 {"snodec", {{"name", "SNode.C"}, {"url", "https://github.com/SNodeC/snode.c"}}},
249 {"since", onlineSince()},
250 {"duration", onlineDuration()},
251 },
252 "ui-initialize",
253 std::to_string(id++));
254
255 for (const auto& modelMapEntry : modelMap) {
256 sendJsonEvent(response, modelMapEntry.second, "client-connected", std::to_string(id++));
257 }
258
259 for (const auto& [topic, clients] : broker->getSubscriptionTree()) {
260 for (const auto& client : clients) {
261 sendJsonEvent(response, subscribe{topic, client.first, client.second}, "client-subscribed", std::to_string(id++));
262 }
263 }
264
265 for (const auto& [topic, retained] : broker->getRetainTree()) {
266 sendJsonEvent(response, retaine{topic, retained.first, retained.second}, "retained-message-set", std::to_string(id++));
267 }
268 }
std::list< EventReceiver > eventReceiverList
Definition MqttModel.h:148
std::map< std::string, MqttModelEntry > modelMap
Definition MqttModel.h:147
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, id, modelMap, 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 270 of file MqttModel.cpp.

270 {
271 MqttModelEntry& mqttModelEntry = modelMap.emplace(mqtt->getClientId(), mqtt).first->second;
272
273 sendJsonEvent(mqttModelEntry, "client-connected", std::to_string(id++));
274 }

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 276 of file MqttModel.cpp.

276 {
277 if (modelMap.contains(clientId)) {
278 sendJsonEvent(modelMap[clientId], "client-disconnected", std::to_string(id++));
279
280 modelMap.erase(clientId);
281 }
282 }

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 377 of file MqttModel.cpp.

378 {
379 using seconds_duration_type = std::chrono::duration<std::chrono::seconds::rep>::rep;
380
381 seconds_duration_type totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(later - bevore).count();
382
383 // Compute days, hours, minutes, and seconds
384 seconds_duration_type days = totalSeconds / 86400; // 86400 seconds in a day
385 seconds_duration_type remainder = totalSeconds % 86400;
386 seconds_duration_type hours = remainder / 3600;
387 remainder = remainder % 3600;
388 seconds_duration_type minutes = remainder / 60;
389 seconds_duration_type seconds = remainder % 60;
390
391 // Format the components into a string using stringstream
392 std::ostringstream oss;
393 if (days > 0) {
394 oss << days << " day" << (days == 1 ? "" : "s") << ", ";
395 }
396 oss << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2)
397 << std::setfill('0') << seconds;
398
399 return oss.str();
400 }

◆ getClients()

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

Definition at line 302 of file MqttModel.cpp.

302 {
303 return modelMap;
304 }

References modelMap.

◆ getMqtt()

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

Definition at line 306 of file MqttModel.cpp.

306 {
307 Mqtt* mqtt = nullptr;
308
309 auto modelIt = modelMap.find(clientId);
310 if (modelIt != modelMap.end()) {
311 mqtt = modelIt->second.getMqtt();
312 }
313
314 return mqtt;
315 }

References mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::getMqtt(), and modelMap.

Here is the call graph for this function:

◆ instance()

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

Definition at line 204 of file MqttModel.cpp.

204 {
205 static MqttModel mqttModel;
206
207 return mqttModel;
208 }

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 321 of file MqttModel.cpp.

321 {
323 }
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 317 of file MqttModel.cpp.

317 {
319 }
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 292 of file MqttModel.cpp.

292 {
293 if (retain) {
294 if (!message.empty()) {
295 sendJsonEvent(retaine{topic, message, qoS}, "retained-message-set", std::to_string(id++));
296 } else {
297 sendJsonEvent(release{topic}, "retained-message-deleted", std::to_string(id++));
298 }
299 }
300 }

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 325 of file MqttModel.cpp.

328 {
329 if (response->isConnected()) {
330 if (!event.empty()) {
331 response->sendFragment("event:" + event);
332 }
333 if (!id.empty()) {
334 response->sendFragment("id:" + id);
335 }
336 response->sendFragment("data:" + data);
337 response->sendFragment();
338 }
339 }

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 348 of file MqttModel.cpp.

348 {
349 for (auto& eventReceiver : eventReceiverList) {
350 if (const auto& response = eventReceiver.response.lock()) {
351 sendEvent(response, data, event, id);
352 }
353 }
354 }
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 356 of file MqttModel.cpp.

356 {
357 VLOG(0) << "Server sent event: " << event << "\n" << json.dump(4);
358
359 sendEvent(json.dump(), event, id);
360 }
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 341 of file MqttModel.cpp.

344 {
345 sendEvent(response, json.dump(), event, id);
346 }

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 284 of file MqttModel.cpp.

284 {
285 sendJsonEvent(subscribe{topic, clientId, qos}, "client-subscribed", std::to_string(id++));
286 }

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 362 of file MqttModel.cpp.

362 {
363 std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
364 std::tm* tm_ptr = std::gmtime(&time);
365
366 char buffer[100];
367 std::string onlineSince = "Formatting error";
368
369 // Format: "2025-02-02 14:30:00"
370 if (std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_ptr)) {
371 onlineSince = std::string(buffer) + " UTC";
372 }
373
374 return onlineSince;
375 }

◆ unsubscribeClient()

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

Definition at line 288 of file MqttModel.cpp.

288 {
289 sendJsonEvent(unsubscribe{clientId, topic}, "client-unsubscribed", std::to_string(id++));
290 }

References id.

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

Here is the caller graph for this function:

◆ to_json

void to_json ( nlohmann::json & j,
const MqttModel::MqttModelEntry & mqttModelEntry )
friend

Definition at line 99 of file MqttModel.cpp.

99 {
100 const Mqtt* mqtt = mqttModelEntry.getMqtt();
101 const core::socket::stream::SocketConnection* socketConnection = mqtt->getMqttContext()->getSocketConnection();
102
103 j = {{"clientId", mqtt->getClientId()},
104 {"since", mqttModelEntry.onlineSince()},
105 {"duration", mqttModelEntry.onlineDuration()},
106 {"connectionName", mqtt->getConnectionName()},
107 {"cleanSession", mqtt->getCleanSession()},
108 {"connectFlags", mqtt->getConnectFlags()},
109 {"username", mqtt->getUsername()},
110 {"usernameFlag", mqtt->getUsernameFlag()},
111 {"password", mqtt->getPassword()},
112 {"passwordFlag", mqtt->getPasswordFlag()},
113 {"keepAlive", mqtt->getKeepAlive()},
114 {"protocol", mqtt->getProtocol()},
115 {"protocolLevel", mqtt->getLevel()},
116 {"loopPrevention", !mqtt->getReflect()},
117 {"willMessage", mqtt->getWillMessage()},
118 {"willTopic", mqtt->getWillTopic()},
119 {"willQoS", mqtt->getWillQoS()},
120 {"willFlag", mqtt->getWillFlag()},
121 {"willRetain", mqtt->getWillRetain()},
122 {"localAddress", socketConnection->getLocalAddress().toString()},
123 {"remoteAddress", socketConnection->getRemoteAddress().toString()}};
124 }

References mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::getMqtt(), mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::onlineDuration(), and mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::onlineSince().

Member Data Documentation

◆ eventReceiverList

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

Definition at line 148 of file MqttModel.h.

Referenced by addEventReceiver(), and sendEvent().

◆ id

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

◆ modelMap

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

Definition at line 147 of file MqttModel.h.

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

◆ onlineSinceTimePoint

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

Definition at line 149 of file MqttModel.h.

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


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