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

#include <BridgeStore.h>

Collaboration diagram for mqtt::bridge::lib::BridgeStore:

Public Member Functions

 BridgeStore (const BridgeStore &)=delete
 BridgeStore (BridgeStore &&)=delete
BridgeStoreoperator= (const BridgeStore &)=delete
BridgeStoreoperator= (const BridgeStore &&)=delete
bool loadAndValidate (const std::string &fileName)
bool patch (const nlohmann::json &jsonPatch)
void activateStaged ()
BrokergetBroker (const std::string &fullInstanceName)
const std::map< std::string, Broker > & getBrokers () const
const std::map< const std::string, Bridge > & getBridgeMap ()
const nlohmann::json & getBridgesConfigJson ()
void mqttConnected (Broker &broker, mqtt::bridge::lib::Mqtt *mqtt) const
void mqttDisconnected (Broker &broker, mqtt::bridge::lib::Mqtt *mqtt) const

Static Public Member Functions

static BridgeStoreinstance ()

Private Member Functions

 BridgeStore ()=default

Private Attributes

std::map< const std::string, BridgebridgeMap
nlohmann::json bridgesConfigJsonActive
nlohmann::json bridgesConfigJsonStaged
nlohmann::json_schema::json_validator validator
std::string fileName

Detailed Description

Definition at line 60 of file BridgeStore.h.

Constructor & Destructor Documentation

◆ BridgeStore() [1/3]

mqtt::bridge::lib::BridgeStore::BridgeStore ( )
privatedefault

◆ BridgeStore() [2/3]

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

◆ BridgeStore() [3/3]

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

Member Function Documentation

◆ activateStaged()

void mqtt::bridge::lib::BridgeStore::activateStaged ( )

Definition at line 165 of file BridgeStore.cpp.

165 {
166 for (auto& [fullInstanceName, bridge] : bridgeMap) {
167 bridge.clear();
168 }
169
170 bridgeMap.clear();
171
173
174 std::ofstream ofs(fileName, std::ios::binary);
175
176 ofs << bridgesConfigJsonActive.dump(4);
177
178 ofs.close();
179
180 for (const nlohmann::json& bridgeConfigJson : bridgesConfigJsonActive["bridges"]) {
181 bridgeMap.emplace(bridgeConfigJson["name"],
182 Bridge{bridgeConfigJson["name"], bridgeConfigJson["prefix"], bridgeConfigJson["disabled"]});
183
184 for (const nlohmann::json& brokerConfigJson : bridgeConfigJson["brokers"]) {
185 std::list<iot::mqtt::Topic> topics;
186 for (const nlohmann::json& topicJson : brokerConfigJson["topics"]) {
187 if (!topicJson["topic"].get<std::string>().empty()) {
188 topics.emplace_back(topicJson["topic"], // cppcheck-suppress useStlAlgorithm
189 topicJson["qos"]);
190 }
191 }
192
193 const nlohmann::json& mqtt = brokerConfigJson["mqtt"];
194 const nlohmann::json& network = brokerConfigJson["network"];
195
196 const std::string fullInstanceName =
197 bridgeMap.find(bridgeConfigJson["name"])->second.getName() + "+" + network["instance_name"].get<std::string>();
198
199 bridgeMap.find(bridgeConfigJson["name"])
200 ->second.addBroker(fullInstanceName,
201 Broker(bridgeMap.find(bridgeConfigJson["name"])->second,
202 brokerConfigJson["session_store"],
203 fullInstanceName,
204 network["protocol"],
205 network["encryption"],
206 network["transport"],
207 network[network["protocol"]],
208 mqtt["client_id"],
209 mqtt["keep_alive"],
210 mqtt["clean_session"],
211 mqtt["will_topic"],
212 mqtt["will_message"],
213 mqtt["will_qos"],
214 mqtt["will_retain"],
215 mqtt["username"],
216 mqtt["password"],
217 mqtt["loop_prevention"],
218 brokerConfigJson["prefix"],
219 brokerConfigJson["disabled"],
220 topics));
221 }
222 }
223 }
nlohmann::json bridgesConfigJsonActive
Definition BridgeStore.h:90
std::map< const std::string, Bridge > bridgeMap
Definition BridgeStore.h:88
nlohmann::json bridgesConfigJsonStaged
Definition BridgeStore.h:91

References bridgeMap, bridgesConfigJsonActive, bridgesConfigJsonStaged, and fileName.

Referenced by loadAndValidate(), and restartBridges().

Here is the caller graph for this function:

◆ getBridgeMap()

const std::map< const std::string, Bridge > & mqtt::bridge::lib::BridgeStore::getBridgeMap ( )

Definition at line 225 of file BridgeStore.cpp.

225 {
226 return bridgeMap;
227 }

References bridgeMap.

◆ getBridgesConfigJson()

const nlohmann::json & mqtt::bridge::lib::BridgeStore::getBridgesConfigJson ( )

Definition at line 229 of file BridgeStore.cpp.

229 {
231 }

References bridgesConfigJsonActive.

◆ getBroker()

Broker * mqtt::bridge::lib::BridgeStore::getBroker ( const std::string & fullInstanceName)

Definition at line 258 of file BridgeStore.cpp.

258 {
259 auto [bridgeName, instanceName] = split_plus(fullInstanceName);
260
261 return bridgeMap.find(bridgeName)->second.getBroker(fullInstanceName);
262 }
static std::pair< std::string, std::string > split_plus(const std::string &s)

References bridgeMap.

Referenced by mqtt::bridge::SocketContextFactory::create(), and mqtt::mqttbridge::websocket::SubProtocolFactory::create().

Here is the caller graph for this function:

◆ getBrokers()

const std::map< std::string, Broker > & mqtt::bridge::lib::BridgeStore::getBrokers ( ) const

◆ instance()

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

Definition at line 65 of file BridgeStore.cpp.

65 {
66 static BridgeStore bridgeConfigLoader;
67
68 return bridgeConfigLoader;
69 }

Referenced by mqtt::bridge::SocketContextFactory::create(), mqtt::mqttbridge::websocket::SubProtocolFactory::create(), main(), mqtt::bridge::lib::Mqtt::onConnack(), mqtt::bridge::lib::Mqtt::onDisconnected(), and restartBridges().

Here is the caller graph for this function:

◆ loadAndValidate()

bool mqtt::bridge::lib::BridgeStore::loadAndValidate ( const std::string & fileName)

Definition at line 71 of file BridgeStore.cpp.

71 {
72 this->fileName = fileName;
73
74 bool success = bridgeMap.empty();
75
76 if (success) {
77 try {
78 const nlohmann::json bridgeJsonSchema = nlohmann::json::parse(bridgeJsonSchemaString);
79 validator.set_root_schema(bridgeJsonSchema);
80
81 if (!fileName.empty()) {
82 std::ifstream bridgeConfigJsonFile(fileName);
83
84 if (bridgeConfigJsonFile.is_open()) {
85 VLOG(1) << "Bridge config JSON: " << fileName;
86
87 try {
88 bridgeConfigJsonFile >> bridgesConfigJsonStaged;
89
90 try {
91 try {
92 const nlohmann::json defaultPatch = validator.validate(bridgesConfigJsonStaged);
93
94 try {
96
98
99 success = true;
100 } catch (const std::exception& e) {
101 VLOG(1) << " Patching JSON with default patch failed:\n" << defaultPatch.dump(4);
102 VLOG(1) << " " << e.what();
103 }
104 } catch (const std::exception& e) {
105 VLOG(1) << " Validating JSON failed:\n" << bridgesConfigJsonActive.dump(4);
106 VLOG(1) << " " << e.what();
107 }
108 } catch (const std::exception& e) {
109 VLOG(1) << " Setting root json mapping schema failed:\n" << bridgeJsonSchema.dump(4);
110 VLOG(1) << " " << e.what();
111 }
112 } catch (const std::exception& e) {
113 VLOG(1) << " JSON map file parsing failed:" << e.what() << " at " << bridgeConfigJsonFile.tellg();
114 }
115
116 bridgeConfigJsonFile.close();
117 } else {
118 VLOG(1) << "BridgeJsonConfig: " << fileName << " not found";
119 }
120 } else {
121 // Do not log missing path. In regular use this missing option is captured by the command line interface
122 }
123 } catch (const std::exception& e) {
124 VLOG(1) << "Parsing schema failed: " << e.what();
125 VLOG(1) << bridgeJsonSchemaString;
126 }
127 } else {
128 VLOG(1) << "MappingFile already loaded and validated";
129 }
130
131 return success;
132 }
nlohmann::json_schema::json_validator validator
Definition BridgeStore.h:92

References activateStaged(), bridgeMap, bridgesConfigJsonActive, bridgesConfigJsonStaged, fileName, nlohmann::json_schema::json_validator::set_root_schema(), nlohmann::json_schema::json_validator::validate(), and validator.

Referenced by main().

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

◆ mqttConnected()

void mqtt::bridge::lib::BridgeStore::mqttConnected ( Broker & broker,
mqtt::bridge::lib::Mqtt * mqtt ) const

Definition at line 233 of file BridgeStore.cpp.

233 {
234 broker.getBridge().addMqtt(mqtt);
235
236 bool allConnected = true;
237
238 for (const auto& [bridgeName, bridge] : bridgeMap) {
239 allConnected &= bridge.getAllConnected1();
240 }
241
242 if (allConnected) {
244 }
245 }
static SSEDistributor & instance()

References mqtt::bridge::lib::Bridge::addMqtt(), mqtt::bridge::lib::SSEDistributor::bridgesStarted(), mqtt::bridge::lib::Broker::getBridge(), and mqtt::bridge::lib::SSEDistributor::instance().

Referenced by mqtt::bridge::lib::Mqtt::onConnack().

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

◆ mqttDisconnected()

void mqtt::bridge::lib::BridgeStore::mqttDisconnected ( Broker & broker,
mqtt::bridge::lib::Mqtt * mqtt ) const

Definition at line 247 of file BridgeStore.cpp.

247 {
248 broker.getBridge().removeMqtt(mqtt);
249 }

References mqtt::bridge::lib::Broker::getBridge(), and mqtt::bridge::lib::Bridge::removeMqtt().

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

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ patch()

bool mqtt::bridge::lib::BridgeStore::patch ( const nlohmann::json & jsonPatch)

Definition at line 134 of file BridgeStore.cpp.

134 {
135 bool success = false;
136
137 try {
139
140 try {
141 const nlohmann::json defaultPatch = validator.validate(bridgesConfigJsonStaged);
142
143 try {
145
146 success = true;
147 } catch (const std::exception& e) {
148 VLOG(1) << " Patching JSON with default patch failed:\n" << defaultPatch.dump(4);
149 VLOG(1) << " " << e.what();
150 }
151 } catch (const std::exception& e) {
152 VLOG(1) << " Validating JSON failed:\n" << bridgesConfigJsonActive.dump(4);
153 VLOG(1) << " " << e.what();
154 }
155 } catch (const std::exception& e) {
156 VLOG(1) << " Default Patch:\n" << bridgesConfigJsonActive.dump(4);
157
158 VLOG(1) << " Patching JSON with update failed:\n" << jsonPatch.dump(4);
159 VLOG(1) << " " << e.what();
160 }
161
162 return success;
163 }

References bridgesConfigJsonActive, bridgesConfigJsonStaged, nlohmann::json_schema::json_validator::validate(), and validator.

Here is the call graph for this function:

Member Data Documentation

◆ bridgeMap

std::map<const std::string, Bridge> mqtt::bridge::lib::BridgeStore::bridgeMap
private

Definition at line 88 of file BridgeStore.h.

Referenced by activateStaged(), getBridgeMap(), getBroker(), and loadAndValidate().

◆ bridgesConfigJsonActive

nlohmann::json mqtt::bridge::lib::BridgeStore::bridgesConfigJsonActive
private

Definition at line 90 of file BridgeStore.h.

Referenced by activateStaged(), getBridgesConfigJson(), loadAndValidate(), and patch().

◆ bridgesConfigJsonStaged

nlohmann::json mqtt::bridge::lib::BridgeStore::bridgesConfigJsonStaged
private

Definition at line 91 of file BridgeStore.h.

Referenced by activateStaged(), loadAndValidate(), and patch().

◆ fileName

std::string mqtt::bridge::lib::BridgeStore::fileName
private

Definition at line 94 of file BridgeStore.h.

Referenced by activateStaged(), and loadAndValidate().

◆ validator

nlohmann::json_schema::json_validator mqtt::bridge::lib::BridgeStore::validator
private

Definition at line 92 of file BridgeStore.h.

Referenced by loadAndValidate(), and patch().


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