MQTTSuite
Loading...
Searching...
No Matches
mqtt::lib::ConfigApplication Class Reference

#include <ConfigApplication.h>

Inheritance diagram for mqtt::lib::ConfigApplication:
Collaboration diagram for mqtt::lib::ConfigApplication:

Public Member Functions

template<typename ConcretConfigApplicationT>
 ConfigApplication (utils::SubCommand *parent, ConcretConfigApplicationT *concretConfigApplication)
 ~ConfigApplication () override
ConfigApplicationsetSessionStore (const std::string &sessionStore)
std::string getSessionStore () const
const std::shared_ptr< MqttMappergetMqttMapper () const
bool setMappingFile (const std::string &mapFilename)
std::string getMappingFilename () const
bool setMapping (const std::string &mapping) const
std::string getMapping (int indent=2) const
bool persistMapping () const
template<typename ConcretConfigApplication>
 ConfigApplication (utils::SubCommand *parent, ConcretConfigApplication *concretConfigApplication)

Protected Attributes

std::shared_ptr< MqttMappermqttMapper
CLI::Option * mappingFileOpt
CLI::Option * sessionStoreOpt

Private Member Functions

bool loadMapping (const std::string mapFilename)

Private Attributes

std::string mapFilename

Detailed Description

Definition at line 61 of file ConfigApplication.h.

Constructor & Destructor Documentation

◆ ConfigApplication() [1/2]

template<typename ConcretConfigApplicationT>
mqtt::lib::ConfigApplication::ConfigApplication ( utils::SubCommand * parent,
ConcretConfigApplicationT * concretConfigApplication )

Referenced by mqtt::lib::ConfigMqttBroker::ConfigMqttBroker(), and mqtt::lib::ConfigMqttIntegrator::ConfigMqttIntegrator().

Here is the caller graph for this function:

◆ ~ConfigApplication()

mqtt::lib::ConfigApplication::~ConfigApplication ( )
overridedefault

◆ ConfigApplication() [2/2]

template<typename ConcretConfigApplication>
mqtt::lib::ConfigApplication::ConfigApplication ( utils::SubCommand * parent,
ConcretConfigApplication * concretConfigApplication )

Definition at line 63 of file ConfigApplication.cpp.

64 : utils::SubCommand(parent, concretConfigApplication, "Applications")
65 , mqttMapper(std::make_shared<MqttMapper>())
66 , mappingFileOpt( //
67 addOptionFunction( //
68 "--mqtt-mapping-file",
69 [this](const std::string& mappFilename) {
70 try {
71 loadMapping(mappFilename);
72 } catch (std::runtime_error& e) {
73 this->mapFilename.clear();
74
75 throw CLI::ValidationError(getName(),
76 std::string("Activating mapping description in '" + mappFilename +
77 "' failed\n"
78 "What: " +
79 e.what()));
80 }
81 },
82 "MQTT mapping file (json format) for integration",
83 "filename",
84 !CLI::ExistingDirectory))
85 , sessionStoreOpt( //
86 addOption( //
87 "--mqtt-session-store",
88 "Path to file for the persistent session store",
89 "filename",
90 !CLI::ExistingDirectory)) {
91 }
bool loadMapping(const std::string mapFilename)
std::shared_ptr< MqttMapper > mqttMapper

References loadMapping(), mapFilename, mappingFileOpt, and mqttMapper.

Here is the call graph for this function:

Member Function Documentation

◆ getMapping()

std::string mqtt::lib::ConfigApplication::getMapping ( int indent = 2) const

Definition at line 123 of file ConfigApplication.cpp.

123 {
124 return mqttMapper->getMapping().dump(indent);
125 }

References mqtt::lib::MqttMapper::getMapping(), and mqttMapper.

Referenced by persistMapping().

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

◆ getMappingFilename()

std::string mqtt::lib::ConfigApplication::getMappingFilename ( ) const

Definition at line 115 of file ConfigApplication.cpp.

115 {
116 return mapFilename;
117 }

References mapFilename.

Referenced by mqtt::lib::admin::makeMappingAdminRouter().

Here is the caller graph for this function:

◆ getMqttMapper()

const std::shared_ptr< MqttMapper > mqtt::lib::ConfigApplication::getMqttMapper ( ) const

Definition at line 105 of file ConfigApplication.cpp.

105 {
106 return mqttMapper;
107 }

References mqttMapper.

Referenced by mqtt::mqttbroker::SocketContextFactory::create(), mqtt::mqttbroker::websocket::SubProtocolFactory::create(), mqtt::mqttintegrator::SocketContextFactory::create(), and mqtt::lib::admin::makeMappingAdminRouter().

Here is the caller graph for this function:

◆ getSessionStore()

std::string mqtt::lib::ConfigApplication::getSessionStore ( ) const

Definition at line 101 of file ConfigApplication.cpp.

101 {
102 return sessionStoreOpt->as<std::string>();
103 }

References sessionStoreOpt.

Referenced by mqtt::mqttbroker::websocket::SubProtocolFactory::create(), and mqtt::mqttintegrator::SocketContextFactory::create().

Here is the caller graph for this function:

◆ loadMapping()

bool mqtt::lib::ConfigApplication::loadMapping ( const std::string mapFilename)
private

Definition at line 150 of file ConfigApplication.cpp.

150 {
151 VLOG(1) << "Mapping file: " << mapFilename;
152
153 this->mapFilename = mapFilename;
154
155 bool mustReconnect = true;
156
157 if (!mapFilename.empty()) {
158 std::ifstream mapFile(mapFilename);
159
160 if (mapFile.is_open()) {
161 try {
162 mustReconnect = setMapping({std::istreambuf_iterator<char>(mapFile), std::istreambuf_iterator<char>()});
163
164 mapFile.close();
165
166 VLOG(1) << "Load mapping file success";
167 } catch (const std::exception& e) {
168 mapFile.close();
169
170 throw std::runtime_error("Loading mapping description from '" + mapFilename +
171 "' failed\n"
172 "What: " +
173 e.what());
174 }
175
176 } else {
177 VLOG(1) << "Mapping file '" + mapFilename +
178 "' not found. Please provide a valid mapping file with the option --mqtt-mapping-file";
179 }
180 }
181
182 return mustReconnect;
183 }
bool setMapping(const std::string &mapping) const

References mapFilename, and setMapping().

Referenced by ConfigApplication(), and setMappingFile().

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

◆ persistMapping()

bool mqtt::lib::ConfigApplication::persistMapping ( ) const

Definition at line 127 of file ConfigApplication.cpp.

127 {
128 bool success = false;
129
130 std::ofstream mapFile(mapFilename, std::ios::trunc);
131 if (mapFile.is_open()) {
132 try {
133 mapFile << getMapping();
134
135 success = true;
136
137 VLOG(1) << "Write mapping file seccess";
138 } catch (const std::exception& e) {
139 VLOG(1) << "Write mapping file failed: " << e.what();
140 }
141
142 mapFile.close();
143 } else {
144 VLOG(1) << "Cannot open mapping file for writing: " << mapFilename;
145 }
146
147 return success;
148 }
std::string getMapping(int indent=2) const

References getMapping(), and mapFilename.

Referenced by mqtt::lib::admin::makeMappingAdminRouter().

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

◆ setMapping()

bool mqtt::lib::ConfigApplication::setMapping ( const std::string & mapping) const

Definition at line 119 of file ConfigApplication.cpp.

119 {
120 return mqttMapper->setMapping(nlohmann::json::parse(mapping));
121 }

References mqttMapper, and mqtt::lib::MqttMapper::setMapping().

Referenced by loadMapping().

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

◆ setMappingFile()

bool mqtt::lib::ConfigApplication::setMappingFile ( const std::string & mapFilename)

Definition at line 109 of file ConfigApplication.cpp.

109 {
110 setDefaultValue(mappingFileOpt, mapFilename);
111
112 return loadMapping(mapFilename);
113 }

References loadMapping(), and mappingFileOpt.

Here is the call graph for this function:

◆ setSessionStore()

ConfigApplication & mqtt::lib::ConfigApplication::setSessionStore ( const std::string & sessionStore)

Definition at line 95 of file ConfigApplication.cpp.

95 {
96 setDefaultValue(sessionStoreOpt, sessionStore);
97
98 return *this;
99 }

References sessionStoreOpt.

Member Data Documentation

◆ mapFilename

std::string mqtt::lib::ConfigApplication::mapFilename
private

◆ mappingFileOpt

CLI::Option* mqtt::lib::ConfigApplication::mappingFileOpt
protected

Definition at line 84 of file ConfigApplication.h.

Referenced by ConfigApplication(), and setMappingFile().

◆ mqttMapper

std::shared_ptr<MqttMapper> mqtt::lib::ConfigApplication::mqttMapper
protected

Definition at line 82 of file ConfigApplication.h.

Referenced by ConfigApplication(), getMapping(), getMqttMapper(), and setMapping().

◆ sessionStoreOpt

CLI::Option* mqtt::lib::ConfigApplication::sessionStoreOpt
protected

Definition at line 85 of file ConfigApplication.h.

Referenced by getSessionStore(), and setSessionStore().


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