Commit a43edfcf authored by Micha Mueller's avatar Micha Mueller
Browse files

Refactor: move variables which belong together into own structs

parent b2993ca3
......@@ -25,17 +25,19 @@ Configuration::Configuration(const std::string& cfgFilePath) :
//set default values for global variables
_global.daemonize = 0;
_global.restHost = "";
_global.restPort = "8000";
_global.brokerHost = "";
_global.brokerPort = 1883;
_global.mqttPrefix = "";
_global.threads = 1;
_global.tempdir = "./";
_global.cacheInterval = 900000;
//log levels will get inverted...
_global.logLevelFile = boost::log::trivial::fatal;
_global.logLevelCmd = boost::log::trivial::warning;
_global.pluginSettings.mqttPrefix = "";
_global.pluginSettings.tempdir = "./";
_global.pluginSettings.cacheInterval = 900000;
_global.restAPISettings.restHost = "";
_global.restAPISettings.restPort = "8000";
}
Configuration::~Configuration() {
......@@ -67,11 +69,11 @@ bool Configuration::readGlobal() {
//read global variables
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
if (boost::iequals(global.first, "restAddr")) {
_global.restHost = global.second.data();
size_t pos = _global.restHost.find(":");
_global.restAPISettings.restHost = global.second.data();
size_t pos = _global.restAPISettings.restHost.find(":");
if (pos != string::npos) {
_global.restPort = _global.restHost.substr(pos+1);
_global.restHost.erase(pos);
_global.restAPISettings.restPort = _global.restAPISettings.restHost.substr(pos+1);
_global.restAPISettings.restHost.erase(pos);
}
} else if (boost::iequals(global.first, "mqttBroker")) {
_global.brokerHost = global.second.data();
......@@ -81,14 +83,14 @@ bool Configuration::readGlobal() {
_global.brokerHost.erase(pos);
}
} else if (boost::iequals(global.first, "mqttprefix")) {
_global.mqttPrefix = global.second.data();
if (_global.mqttPrefix[_global.mqttPrefix.length()-1] != '/') {
_global.mqttPrefix.append("/");
_global.pluginSettings.mqttPrefix = global.second.data();
if (_global.pluginSettings.mqttPrefix[_global.pluginSettings.mqttPrefix.length()-1] != '/') {
_global.pluginSettings.mqttPrefix.append("/");
}
} else if (boost::iequals(global.first, "tempdir")) {
_global.tempdir = global.second.data();
if (_global.tempdir[_global.tempdir.length()-1] != '/') {
_global.tempdir.append("/");
_global.pluginSettings.tempdir = global.second.data();
if (_global.pluginSettings.tempdir[_global.pluginSettings.tempdir.length()-1] != '/') {
_global.pluginSettings.tempdir.append("/");
}
} else if (boost::iequals(global.first, "threads")) {
_global.threads = stoi(global.second.data());
......@@ -99,8 +101,8 @@ bool Configuration::readGlobal() {
} else if (boost::iequals(global.first, "verbosity")) {
_global.logLevelFile = static_cast<boost::log::trivial::severity_level>(stoi(global.second.data()));
} else if (boost::iequals(global.first, "cacheInterval")) {
_global.cacheInterval = stoul(global.second.data());
_global.cacheInterval *= 1000;
_global.pluginSettings.cacheInterval = stoul(global.second.data());
_global.pluginSettings.cacheInterval *= 1000;
} else {
LOG(warning) << " Value \"" << global.first << "\" not recognized. Omitting";
}
......@@ -191,7 +193,7 @@ bool Configuration::read() {
dynLib.configurator = dynLib.create();
//set prefix to global prefix (may be overwritten)
dynLib.configurator->setGlobalSettings(_global);
dynLib.configurator->setGlobalSettings(_global.pluginSettings);
//read in config
if (!(dynLib.configurator->readConfig(pluginConfig))) {
LOG(error) << "Plugin \"" << dynLib.id << "\" could not read configuration!";
......
......@@ -17,6 +17,23 @@
#include <boost/property_tree/ptree.hpp>
#include <boost/log/trivial.hpp>
typedef struct {
std::string restHost;
std::string restPort;
} restAPISettings_t;
typedef struct {
int daemonize;
int brokerPort;
std::string brokerHost;
uint32_t threads;
boost::log::trivial::severity_level logLevelFile;
boost::log::trivial::severity_level logLevelCmd;
pluginSettings_t pluginSettings;
restAPISettings_t restAPISettings;
} global_t;
//struct of values required for a dynamic library.
typedef struct {
std::string id;
......
......@@ -14,18 +14,10 @@
#include <string>
typedef struct {
int daemonize;
int brokerPort;
std::string restHost;
std::string restPort;
std::string brokerHost;
std::string mqttPrefix;
std::string tempdir;
uint32_t threads;
unsigned int cacheInterval;
boost::log::trivial::severity_level logLevelFile;
boost::log::trivial::severity_level logLevelCmd;
} global_t;
} pluginSettings_t;
/**
* Abstract base class, which defines the interface for the configurators in the shared dynamic libraries.
......@@ -42,9 +34,9 @@ public:
*/
virtual bool readConfig(std::string cfgPath) = 0;
virtual void setGlobalSettings(const global_t& globalSettings) {
_mqttPrefix = globalSettings.mqttPrefix;
_cacheInterval = globalSettings.cacheInterval;
virtual void setGlobalSettings(const pluginSettings_t& pluginSettings) {
_mqttPrefix = pluginSettings.mqttPrefix;
_cacheInterval = pluginSettings.cacheInterval;
}
std::vector<Sensor*>& getSensors() {
......
......@@ -206,8 +206,8 @@ bool HttpsServer::requestHandler::check_authkey(const std::string& authkey) {
return authkey == "qwertz" ? true : false;
}
HttpsServer::HttpsServer(const std::string& host, const std::string& port, pluginVector_t& plugins) :
_host(host), _port(port), _plugins(plugins), _handler(*this) {
HttpsServer::HttpsServer(restAPISettings_t restAPISettings, pluginVector_t& plugins) :
_host(restAPISettings.restHost), _port(restAPISettings.restPort), _plugins(plugins), _handler(*this) {
std::shared_ptr<asio::ssl::context> ctx = std::make_shared<asio::ssl::context>(asio::ssl::context::sslv23);
ctx->set_options(asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv3 | asio::ssl::context::single_dh_use);
......
......@@ -14,9 +14,8 @@
#include <asio.hpp>
#include <asio/ssl.hpp>
#include "Configuration.h"
#include "Logging.h"
#include "Configuration.h"
/*
* Provides REST API services over configurable host and port.
......@@ -24,8 +23,7 @@
class HttpsServer {
public:
HttpsServer(const std::string& host, const std::string& port,
pluginVector_t& plugins);
HttpsServer(restAPISettings_t restAPISettings, pluginVector_t& plugins);
virtual ~HttpsServer();
void run() {
......
......@@ -202,7 +202,7 @@ int main(int argc, char** argv) {
globalSettings.brokerPort = atoi(optarg);
break;
case 'm':
globalSettings.mqttPrefix = optarg;
globalSettings.pluginSettings.mqttPrefix = optarg;
break;
case 't':
globalSettings.threads = stoul(optarg);
......@@ -214,9 +214,9 @@ int main(int argc, char** argv) {
globalSettings.daemonize = 1;
break;
case 'w':
globalSettings.tempdir = optarg;
if (globalSettings.tempdir[globalSettings.tempdir.length()-1] != '/') {
globalSettings.tempdir.append("/");
globalSettings.pluginSettings.tempdir = optarg;
if (globalSettings.pluginSettings.tempdir[globalSettings.pluginSettings.tempdir.length()-1] != '/') {
globalSettings.pluginSettings.tempdir.append("/");
}
break;
case 'h':
......@@ -234,7 +234,7 @@ int main(int argc, char** argv) {
//we now should know where the writable tempdir is
//set up logger to file
auto fileSink = boost::log::add_file_log(
boost::log::keywords::file_name = globalSettings.tempdir + "dcdb_%N.log", // number logfiles ascending
boost::log::keywords::file_name = globalSettings.pluginSettings.tempdir + "dcdb_%N.log", // number logfiles ascending
boost::log::keywords::rotation_size = 10 * 1024 * 1024, //rotate logfile every 10 MiB
//boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0), //Throws bad year-exception for no obvious reason
boost::log::keywords::format = // format: LineID [Timestamp] ThreadID <severity>: Message
......@@ -268,17 +268,19 @@ int main(int argc, char** argv) {
//give some feedback
LOG(info) << "Global Settings:";
LOG(info) << " REST Server: " << globalSettings.restHost << ":" << globalSettings.restPort;
LOG(info) << " Broker: " << globalSettings.brokerHost << ":" << globalSettings.brokerPort;
LOG(info) << " MQTT-prefix: " << globalSettings.mqttPrefix;
LOG(info) << " Threads: " << globalSettings.threads;
if(globalSettings.daemonize) {
LOG(info) << " Daemonize: Enabled";
} else {
LOG(info) << " Daemonize: Disabled";
}
LOG(info) << " Write-Dir: " << globalSettings.tempdir;
LOG(info) << " CacheInterval: " << globalSettings.cacheInterval / 1000 << " [s]";
LOG(info) << " MQTT-prefix: " << globalSettings.pluginSettings.mqttPrefix;
LOG(info) << " Write-Dir: " << globalSettings.pluginSettings.tempdir;
LOG(info) << " CacheInterval: " << globalSettings.pluginSettings.cacheInterval / 1000 << " [s]";
LOG(info) << "RestAPI Settings:";
LOG(info) << " REST Server: " << globalSettings.restAPISettings.restHost << ":" << globalSettings.restAPISettings.restPort;
//Init all sensors
LOG(info) << "Init sensors...";
......@@ -327,8 +329,8 @@ int main(int argc, char** argv) {
}
//MQTTPusher and Https server get their own threads
MQTTPusher mqttPusher(globalSettings.brokerPort, globalSettings.brokerHost, globalSettings.mqttPrefix, plugins);
HttpsServer httpsServer(globalSettings.restHost, globalSettings.restPort, plugins);
MQTTPusher mqttPusher(globalSettings.brokerPort, globalSettings.brokerHost, globalSettings.pluginSettings.mqttPrefix, plugins);
HttpsServer httpsServer(globalSettings.restAPISettings, plugins);
boost::thread mqttThread(bind(&MQTTPusher::push, &mqttPusher));
boost::thread restThread(bind(&HttpsServer::run, &httpsServer));
......
......@@ -36,9 +36,9 @@ namespace DCDB {
bool readConfig(std::string cfgPath);
//Overwrite from Configurator
virtual void setGlobalSettings(const global_t& globalSettings) {
Configurator::setGlobalSettings(globalSettings);
_tempdir = globalSettings.tempdir;
virtual void setGlobalSettings(const pluginSettings_t& pluginSettings) {
Configurator::setGlobalSettings(pluginSettings);
_tempdir = pluginSettings.tempdir;
}
private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment