MQTTSuite
Loading...
Searching...
No Matches
mqtt::bridge::lib::SSEDistributor Class Reference

#include <SSEDistributor.h>

Collaboration diagram for mqtt::bridge::lib::SSEDistributor:

Classes

class  EventReceiver
class  Event

Public Member Functions

 SSEDistributor (const SSEDistributor &)=delete
SSEDistributoroperator= (const SSEDistributor &)=delete
 SSEDistributor (SSEDistributor &&)=delete
SSEDistributoroperator= (SSEDistributor &&)=delete
 ~SSEDistributor ()=default
void addEventReceiver (const std::shared_ptr< express::Response > &response, const std::string &lastEventId)
void bridgesStarting ()
void bridgesStarted ()
void bridgesStopping ()
void bridgesStopped ()
void bridgeDisabled (const std::string &bridgeName)
void bridgeStarting (const std::string &bridgeName)
void bridgeStarted (const std::string &bridgeName)
void bridgeStopping (const std::string &bridgeName)
void bridgeStopped (const std::string &bridgeName)
void brokerDisabled (const std::string &bridgeName, const std::string &instanceName)
void brokerConnecting (const std::string &bridgeName, const std::string &instanceName)
void brokerConnected (const std::string &bridgeName, const std::string &instanceName)
void brokerDisconnecting (const std::string &bridgeName, const std::string &instanceName)
void brokerDisconnected (const std::string &bridgeName, const std::string &instanceName)

Static Public Member Functions

static SSEDistributorinstance ()

Private Member Functions

 SSEDistributor ()
void sendEvent (const std::string &data, const std::string &event="", const std::string &id="")
void sendJsonEvent (const nlohmann::json &json, const std::string &event="", const std::string &id="")
std::string bridgesStartedAt () 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::list< EventReceivereventReceiverList
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
std::chrono::time_point< std::chrono::system_clock > bridgesStartTimePoint
uint64_t id = 0
std::list< EventreplayEvents

Detailed Description

Definition at line 63 of file SSEDistributor.h.

Constructor & Destructor Documentation

◆ SSEDistributor() [1/3]

mqtt::bridge::lib::SSEDistributor::SSEDistributor ( )
private

Definition at line 64 of file SSEDistributor.cpp.

65 : onlineSinceTimePoint(std::chrono::system_clock::now()) {
66 }
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint

References onlineSinceTimePoint.

◆ SSEDistributor() [2/3]

mqtt::bridge::lib::SSEDistributor::SSEDistributor ( const SSEDistributor & )
delete

◆ SSEDistributor() [3/3]

mqtt::bridge::lib::SSEDistributor::SSEDistributor ( SSEDistributor && )
delete

◆ ~SSEDistributor()

mqtt::bridge::lib::SSEDistributor::~SSEDistributor ( )
default

Member Function Documentation

◆ addEventReceiver()

void mqtt::bridge::lib::SSEDistributor::addEventReceiver ( const std::shared_ptr< express::Response > & response,
const std::string & lastEventId )

Definition at line 74 of file SSEDistributor.cpp.

75 {
76 auto& eventReceiver = eventReceiverList.emplace_back(response);
77
78 response->getSocketContext()->onDisconnected([this, &eventReceiver]() {
79 eventReceiverList.remove(eventReceiver);
80 });
81
82 for (const auto& event : replayEvents) {
83 sendEvent(response, event.getData(), event.getEvent(), event.getId());
84 }
85 }
static void sendEvent(const std::shared_ptr< express::Response > &response, const std::string &data, const std::string &event, const std::string &id)
std::list< EventReceiver > eventReceiverList

References eventReceiverList, mqtt::bridge::lib::SSEDistributor::Event::getData(), mqtt::bridge::lib::SSEDistributor::Event::getEvent(), mqtt::bridge::lib::SSEDistributor::Event::getId(), replayEvents, and sendEvent().

Referenced by main().

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

◆ bridgeDisabled()

void mqtt::bridge::lib::SSEDistributor::bridgeDisabled ( const std::string & bridgeName)

Definition at line 144 of file SSEDistributor.cpp.

144 {
146 {{"at", timePointToString(std::chrono::system_clock::now())}, {"name", bridgeName}}, "bridge_disabled", std::to_string(id++));
147 }
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)

References id, sendJsonEvent(), and timePointToString().

Here is the call graph for this function:

◆ bridgesStarted()

void mqtt::bridge::lib::SSEDistributor::bridgesStarted ( )

Definition at line 132 of file SSEDistributor.cpp.

132 {
133 sendJsonEvent({{"at", timePointToString(std::chrono::system_clock::now())}}, "bridges_started", std::to_string(id++));
134 }

References id, sendJsonEvent(), and timePointToString().

Referenced by mqtt::bridge::lib::BridgeStore::mqttConnected().

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

◆ bridgesStartedAt()

std::string mqtt::bridge::lib::SSEDistributor::bridgesStartedAt ( ) const
private

Definition at line 199 of file SSEDistributor.cpp.

199 {
201 }
std::chrono::time_point< std::chrono::system_clock > bridgesStartTimePoint

References bridgesStartTimePoint, and timePointToString().

Here is the call graph for this function:

◆ bridgesStarting()

void mqtt::bridge::lib::SSEDistributor::bridgesStarting ( )

Definition at line 126 of file SSEDistributor.cpp.

126 {
127 replayEvents.clear();
128
129 sendJsonEvent({{"at", timePointToString(std::chrono::system_clock::now())}}, "bridges_starting", std::to_string(id++));
130 }

References id, replayEvents, sendJsonEvent(), and timePointToString().

Referenced by startBridges().

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

◆ bridgesStopped()

void mqtt::bridge::lib::SSEDistributor::bridgesStopped ( )

Definition at line 140 of file SSEDistributor.cpp.

140 {
141 sendJsonEvent({{"at", timePointToString(std::chrono::system_clock::now())}}, "bridges_stopped", std::to_string(id++));
142 }

References id, sendJsonEvent(), and timePointToString().

Referenced by handleConfig().

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

◆ bridgesStopping()

void mqtt::bridge::lib::SSEDistributor::bridgesStopping ( )

Definition at line 136 of file SSEDistributor.cpp.

136 {
137 sendJsonEvent({{"at", timePointToString(std::chrono::system_clock::now())}}, "bridges_stopping", std::to_string(id++));
138 }

References id, sendJsonEvent(), and timePointToString().

Referenced by closeBridges().

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

◆ bridgeStarted()

void mqtt::bridge::lib::SSEDistributor::bridgeStarted ( const std::string & bridgeName)

Definition at line 154 of file SSEDistributor.cpp.

154 {
156 {{"at", timePointToString(std::chrono::system_clock::now())}, {"name", bridgeName}}, "bridge_started", std::to_string(id++));
157 }

References id, sendJsonEvent(), and timePointToString().

Referenced by mqtt::bridge::lib::Bridge::addMqtt().

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

◆ bridgeStarting()

void mqtt::bridge::lib::SSEDistributor::bridgeStarting ( const std::string & bridgeName)

Definition at line 149 of file SSEDistributor.cpp.

149 {
151 {{"at", timePointToString(std::chrono::system_clock::now())}, {"name", bridgeName}}, "bridge_starting", std::to_string(id++));
152 }

References id, sendJsonEvent(), and timePointToString().

Here is the call graph for this function:

◆ bridgeStopped()

void mqtt::bridge::lib::SSEDistributor::bridgeStopped ( const std::string & bridgeName)

Definition at line 164 of file SSEDistributor.cpp.

164 {
166 {{"at", timePointToString(std::chrono::system_clock::now())}, {"name", bridgeName}}, "bridge_stopped", std::to_string(id++));
167 }

References id, sendJsonEvent(), and timePointToString().

Referenced by mqtt::bridge::lib::Bridge::removeMqtt().

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

◆ bridgeStopping()

void mqtt::bridge::lib::SSEDistributor::bridgeStopping ( const std::string & bridgeName)

Definition at line 159 of file SSEDistributor.cpp.

159 {
161 {{"at", timePointToString(std::chrono::system_clock::now())}, {"name", bridgeName}}, "bridge_stopping", std::to_string(id++));
162 }

References id, sendJsonEvent(), and timePointToString().

Here is the call graph for this function:

◆ brokerConnected()

void mqtt::bridge::lib::SSEDistributor::brokerConnected ( const std::string & bridgeName,
const std::string & instanceName )

Definition at line 181 of file SSEDistributor.cpp.

181 {
182 sendJsonEvent({{"at", timePointToString(std::chrono::system_clock::now())}, {"bridge", bridgeName}, {"instance", instanceName}},
183 "broker_connected",
184 std::to_string(id++));
185 }

References id, sendJsonEvent(), and timePointToString().

Referenced by mqtt::bridge::lib::Bridge::addMqtt().

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

◆ brokerConnecting()

void mqtt::bridge::lib::SSEDistributor::brokerConnecting ( const std::string & bridgeName,
const std::string & instanceName )

Definition at line 175 of file SSEDistributor.cpp.

175 {
176 sendJsonEvent({{"at", timePointToString(std::chrono::system_clock::now())}, {"bridge", bridgeName}, {"instance", instanceName}},
177 "broker_connecting",
178 std::to_string(id++));
179 }

References id, sendJsonEvent(), and timePointToString().

Here is the call graph for this function:

◆ brokerDisabled()

void mqtt::bridge::lib::SSEDistributor::brokerDisabled ( const std::string & bridgeName,
const std::string & instanceName )

Definition at line 169 of file SSEDistributor.cpp.

169 {
170 sendJsonEvent({{"at", timePointToString(std::chrono::system_clock::now())}, {"bridge", bridgeName}, {"instance", instanceName}},
171 "broker_disabled",
172 std::to_string(id++));
173 }

References id, sendJsonEvent(), and timePointToString().

Here is the call graph for this function:

◆ brokerDisconnected()

void mqtt::bridge::lib::SSEDistributor::brokerDisconnected ( const std::string & bridgeName,
const std::string & instanceName )

Definition at line 193 of file SSEDistributor.cpp.

193 {
194 sendJsonEvent({{"at", timePointToString(std::chrono::system_clock::now())}, {"bridge", bridgeName}, {"instance", instanceName}},
195 "broker_disconnected",
196 std::to_string(id++));
197 }

References id, sendJsonEvent(), and timePointToString().

Referenced by mqtt::bridge::lib::Bridge::removeMqtt().

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

◆ brokerDisconnecting()

void mqtt::bridge::lib::SSEDistributor::brokerDisconnecting ( const std::string & bridgeName,
const std::string & instanceName )

Definition at line 187 of file SSEDistributor.cpp.

187 {
188 sendJsonEvent({{"at", timePointToString(std::chrono::system_clock::now())}, {"bridge", bridgeName}, {"instance", instanceName}},
189 "broker_disconnecting",
190 std::to_string(id++));
191 }

References id, sendJsonEvent(), and timePointToString().

Here is the call graph for this function:

◆ durationToString()

std::string mqtt::bridge::lib::SSEDistributor::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 218 of file SSEDistributor.cpp.

219 {
220 using seconds_duration_type = std::chrono::duration<std::chrono::seconds::rep>::rep;
221
222 seconds_duration_type totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(later - bevore).count();
223
224 // Compute days, hours, minutes, and seconds
225 seconds_duration_type days = totalSeconds / 86400; // 86400 seconds in a day
226 seconds_duration_type remainder = totalSeconds % 86400;
227 seconds_duration_type hours = remainder / 3600;
228 remainder = remainder % 3600;
229 seconds_duration_type minutes = remainder / 60;
230 seconds_duration_type seconds = remainder % 60;
231
232 // Format the components into a string using stringstream
233 std::ostringstream oss;
234 if (days > 0) {
235 oss << days << " day" << (days == 1 ? "" : "s") << ", ";
236 }
237 oss << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2)
238 << std::setfill('0') << seconds;
239
240 return oss.str();
241 }

◆ instance()

SSEDistributor & mqtt::bridge::lib::SSEDistributor::instance ( )
static

Definition at line 68 of file SSEDistributor.cpp.

68 {
69 static SSEDistributor sseDistributor;
70
71 return sseDistributor;
72 }

Referenced by mqtt::bridge::lib::Bridge::addMqtt(), closeBridges(), handleConfig(), main(), mqtt::bridge::lib::BridgeStore::mqttConnected(), mqtt::bridge::lib::Bridge::removeMqtt(), and startBridges().

Here is the caller graph for this function:

◆ operator=() [1/2]

SSEDistributor & mqtt::bridge::lib::SSEDistributor::operator= ( const SSEDistributor & )
delete

◆ operator=() [2/2]

SSEDistributor & mqtt::bridge::lib::SSEDistributor::operator= ( SSEDistributor && )
delete

◆ sendEvent() [1/2]

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

Definition at line 87 of file SSEDistributor.cpp.

90 {
91 if (response->isConnected()) {
92 if (!event.empty()) {
93 response->sendFragment("event:" + event);
94 }
95 if (!id.empty()) {
96 response->sendFragment("id:" + id);
97 }
98 response->sendFragment("data:" + data);
99 response->sendFragment();
100 }
101 }

Referenced by addEventReceiver(), sendEvent(), and sendJsonEvent().

Here is the caller graph for this function:

◆ sendEvent() [2/2]

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

Definition at line 110 of file SSEDistributor.cpp.

110 {
111 VLOG(0) << "Server sent event: " << event << "\n" << data;
112
113 for (const auto& eventReceiver : eventReceiverList) {
114 if (const auto& response = eventReceiver.getResponse()) {
115 sendEvent(response, data, event, id);
116 }
117 }
118
119 replayEvents.emplace_back(data, event, id);
120 }

References eventReceiverList, mqtt::bridge::lib::SSEDistributor::EventReceiver::getResponse(), replayEvents, 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::bridge::lib::SSEDistributor::sendJsonEvent ( const nlohmann::json & json,
const std::string & event = "",
const std::string & id = "" )
private

Definition at line 122 of file SSEDistributor.cpp.

122 {
123 sendEvent(json.dump(), event, id);
124 }
nlohmann::json json

References sendEvent().

Referenced by bridgeDisabled(), bridgesStarted(), bridgesStarting(), bridgesStopped(), bridgesStopping(), bridgeStarted(), bridgeStarting(), bridgeStopped(), bridgeStopping(), brokerConnected(), brokerConnecting(), brokerDisabled(), brokerDisconnected(), and brokerDisconnecting().

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

◆ sendJsonEvent() [2/2]

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

Definition at line 103 of file SSEDistributor.cpp.

106 {
107 sendEvent(response, json.dump(), event, id);
108 }

References sendEvent().

Here is the call graph for this function:

◆ timePointToString()

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

Definition at line 203 of file SSEDistributor.cpp.

203 {
204 std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
205 const std::tm* tm_ptr = std::gmtime(&time);
206
207 char buffer[100];
208 std::string onlineSince = "Formatting error";
209
210 // Format: "2025-02-02 14:30:00"
211 if (std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_ptr)) {
212 onlineSince = std::string(buffer) + " UTC";
213 }
214
215 return onlineSince;
216 }

Referenced by bridgeDisabled(), bridgesStarted(), bridgesStartedAt(), bridgesStarting(), bridgesStopped(), bridgesStopping(), bridgeStarted(), bridgeStarting(), bridgeStopped(), bridgeStopping(), brokerConnected(), brokerConnecting(), brokerDisabled(), brokerDisconnected(), and brokerDisconnecting().

Here is the caller graph for this function:

Member Data Documentation

◆ bridgesStartTimePoint

std::chrono::time_point<std::chrono::system_clock> mqtt::bridge::lib::SSEDistributor::bridgesStartTimePoint
private

Definition at line 158 of file SSEDistributor.h.

Referenced by bridgesStartedAt().

◆ eventReceiverList

std::list<EventReceiver> mqtt::bridge::lib::SSEDistributor::eventReceiverList
private

Definition at line 156 of file SSEDistributor.h.

Referenced by addEventReceiver(), and sendEvent().

◆ id

◆ onlineSinceTimePoint

std::chrono::time_point<std::chrono::system_clock> mqtt::bridge::lib::SSEDistributor::onlineSinceTimePoint
private

Definition at line 157 of file SSEDistributor.h.

Referenced by SSEDistributor().

◆ replayEvents

std::list<Event> mqtt::bridge::lib::SSEDistributor::replayEvents
private

Definition at line 161 of file SSEDistributor.h.

Referenced by addEventReceiver(), bridgesStarting(), and sendEvent().


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