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

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

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

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

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

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

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

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

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

◆ getClients()

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

Definition at line 278 of file MqttModel.cpp.

278 {
279 return modelMap;
280 }

References modelMap.

◆ getMqtt()

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

Definition at line 282 of file MqttModel.cpp.

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

References modelMap.

◆ instance()

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

Definition at line 180 of file MqttModel.cpp.

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

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

297 {
299 }
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 293 of file MqttModel.cpp.

293 {
295 }
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 268 of file MqttModel.cpp.

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

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

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

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

324 {
325 for (auto& eventReceiver : eventReceiverList) {
326 if (const auto& response = eventReceiver.response.lock()) {
327 sendEvent(response, data, event, id);
328 }
329 }
330 }
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 332 of file MqttModel.cpp.

332 {
333 VLOG(0) << "Server sent event: " << event << "\n" << json.dump(4);
334
335 sendEvent(json.dump(), event, id);
336 }
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 317 of file MqttModel.cpp.

320 {
321 sendEvent(response, json.dump(), event, id);
322 }

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

260 {
261 sendJsonEvent(subscribe{topic, clientId, qos}, "client-subscribed", std::to_string(id++));
262 }

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

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

◆ unsubscribeClient()

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

Definition at line 264 of file MqttModel.cpp.

264 {
265 sendJsonEvent(unsubscribe{clientId, topic}, "client-unsubscribed", std::to_string(id++));
266 }

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 131 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 130 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 132 of file MqttModel.h.

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


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