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

#include <BridgeStore.h>

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

Public Member Functions

bool loadAndValidate (const std::string &fileName)
const BrokergetBroker (const std::string &instanceName)
const std::map< std::string, Broker > & getBrokers ()

Static Public Member Functions

static BridgeStoreinstance ()

Private Member Functions

 BridgeStore ()=default

Private Attributes

std::list< BridgebridgeList
std::map< std::string, Brokerbrokers

Detailed Description

Definition at line 58 of file BridgeStore.h.

Constructor & Destructor Documentation

◆ BridgeStore()

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

Member Function Documentation

◆ getBroker()

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

Definition at line 177 of file BridgeStore.cpp.

177 {
178 return brokers.find(instanceName)->second;
179 }
std::map< std::string, Broker > brokers
Definition BridgeStore.h:72

◆ getBrokers()

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

Definition at line 181 of file BridgeStore.cpp.

181 {
182 return brokers;
183 }

References brokers.

◆ instance()

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

Definition at line 64 of file BridgeStore.cpp.

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

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

Here is the caller graph for this function:

◆ loadAndValidate()

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

Definition at line 70 of file BridgeStore.cpp.

70 {
71 bool success = !brokers.empty();
72
73#include "bridge-schema.json.h" // IWYU pragma: keep
74
75 if (!success) {
76 try {
77 const nlohmann::json bridgeJsonSchema = nlohmann::json::parse(bridgeJsonSchemaString);
78
79 if (!fileName.empty()) {
80 std::ifstream bridgeConfigJsonFile(fileName);
81
82 if (bridgeConfigJsonFile.is_open()) {
83 VLOG(1) << "Bridge config JSON: " << fileName;
84
85 try {
86 nlohmann::json bridgesConfigJson;
87
88 bridgeConfigJsonFile >> bridgesConfigJson;
89
90 try {
91 const nlohmann::json_schema::json_validator validator(bridgeJsonSchema);
92
93 try {
94 const nlohmann::json defaultPatch = validator.validate(bridgesConfigJson);
95
96 try {
97 bridgesConfigJson = bridgesConfigJson.patch(defaultPatch);
98
99 for (const nlohmann::json& bridgeConfigJson : bridgesConfigJson["bridges"]) {
100 Bridge& bridge = bridgeList.emplace_back(
101 bridgeConfigJson["name"], bridgeConfigJson["prefix"], bridgeConfigJson["disabled"]);
102
103 for (const nlohmann::json& brokerConfigJson : bridgeConfigJson["brokers"]) {
104 std::list<iot::mqtt::Topic> topics;
105 for (const nlohmann::json& topicJson : brokerConfigJson["topics"]) {
106 if (!topicJson["topic"].get<std::string>().empty()) {
107 topics.emplace_back(topicJson["topic"], // cppcheck-suppress useStlAlgorithm
108 topicJson["qos"]);
109 }
110 }
111
112 const nlohmann::json& mqtt = brokerConfigJson["mqtt"];
113 const nlohmann::json& network = brokerConfigJson["network"];
114
115 const std::string fullInstanceName =
116 bridge.getName() + "+" + network["instance_name"].get<std::string>();
117
118 brokers.emplace(fullInstanceName,
119 Broker(bridge,
120 brokerConfigJson["session_store"],
121 fullInstanceName,
122 network["protocol"],
123 network["encryption"],
124 network["transport"],
125 network[network["protocol"]],
126 mqtt["client_id"],
127 mqtt["keep_alive"],
128 mqtt["clean_session"],
129 mqtt["will_topic"],
130 mqtt["will_message"],
131 mqtt["will_qos"],
132 mqtt["will_retain"],
133 mqtt["username"],
134 mqtt["password"],
135 mqtt["loop_prevention"],
136 brokerConfigJson["prefix"],
137 brokerConfigJson["disabled"],
138 topics));
139 }
140 }
141
142 success = true;
143 } catch (const std::exception& e) {
144 VLOG(1) << " Patching JSON with default patch failed:\n" << defaultPatch.dump(4);
145 VLOG(1) << " " << e.what();
146 }
147 } catch (const std::exception& e) {
148 VLOG(1) << " Validating JSON failed:\n" << bridgesConfigJson.dump(4);
149 VLOG(1) << " " << e.what();
150 }
151 } catch (const std::exception& e) {
152 VLOG(1) << " Setting root json mapping schema failed:\n" << bridgeJsonSchema.dump(4);
153 VLOG(1) << " " << e.what();
154 }
155 } catch (const std::exception& e) {
156 VLOG(1) << " JSON map file parsing failed:" << e.what() << " at " << bridgeConfigJsonFile.tellg();
157 }
158
159 bridgeConfigJsonFile.close();
160 } else {
161 VLOG(1) << "BridgeJsonConfig: " << fileName << " not found";
162 }
163 } else {
164 // Do not log missing path. In regular use this missing option is captured by the command line interface
165 }
166 } catch (const std::exception& e) {
167 VLOG(1) << "Parsing schema failed: " << e.what();
168 VLOG(1) << bridgeJsonSchemaString;
169 }
170 } else {
171 VLOG(1) << "MappingFile already loaded and validated";
172 }
173
174 return success;
175 }
nlohmann::json_schema::json_validator validator
std::list< Bridge > bridgeList
Definition BridgeStore.h:71

References brokers, nlohmann::json_schema::json_validator::json_validator(), and nlohmann::json_schema::json_validator::validate().

Referenced by main().

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

Member Data Documentation

◆ bridgeList

std::list<Bridge> mqtt::bridge::lib::BridgeStore::bridgeList
private

Definition at line 71 of file BridgeStore.h.

◆ brokers

std::map<std::string, Broker> mqtt::bridge::lib::BridgeStore::brokers
private

Definition at line 72 of file BridgeStore.h.

Referenced by getBrokers(), and loadAndValidate().


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