2020-06-24 02:49:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019-2020 PixlOne
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-07-02 04:38:40 +00:00
|
|
|
#include <utility>
|
2019-07-17 05:53:51 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "Configuration.h"
|
2020-07-02 04:38:40 +00:00
|
|
|
#include "util/log.h"
|
2019-07-17 05:53:51 +00:00
|
|
|
|
2019-10-05 01:56:17 +00:00
|
|
|
using namespace logid;
|
2019-07-17 05:53:51 +00:00
|
|
|
using namespace libconfig;
|
2020-07-08 07:41:05 +00:00
|
|
|
using namespace std::chrono;
|
2019-07-17 05:53:51 +00:00
|
|
|
|
2020-07-02 04:38:40 +00:00
|
|
|
Configuration::Configuration(const std::string& config_file)
|
2019-07-17 05:53:51 +00:00
|
|
|
{
|
2020-07-02 04:38:40 +00:00
|
|
|
try {
|
|
|
|
_config.readFile(config_file.c_str());
|
|
|
|
} catch(const FileIOException &e) {
|
|
|
|
logPrintf(ERROR, "I/O Error while reading %s: %s", config_file.c_str(),
|
|
|
|
e.what());
|
2019-07-17 05:53:51 +00:00
|
|
|
throw e;
|
2020-07-02 04:38:40 +00:00
|
|
|
} catch(const ParseException &e) {
|
|
|
|
logPrintf(ERROR, "Parse error in %s, line %d: %s", e.getFile(),
|
|
|
|
e.getLine(), e.getError());
|
2019-07-17 05:53:51 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2020-07-04 04:10:44 +00:00
|
|
|
|
2020-07-02 04:38:40 +00:00
|
|
|
const Setting &root = _config.getRoot();
|
2020-07-04 04:10:44 +00:00
|
|
|
|
2020-07-10 07:16:51 +00:00
|
|
|
try {
|
|
|
|
auto& worker_count = root["workers"];
|
|
|
|
if(worker_count.getType() == Setting::TypeInt) {
|
|
|
|
_worker_threads = worker_count;
|
|
|
|
if(_worker_threads < 0)
|
|
|
|
logPrintf(WARN, "Line %d: workers cannot be negative.",
|
|
|
|
worker_count.getSourceLine());
|
|
|
|
} else {
|
|
|
|
logPrintf(WARN, "Line %d: workers must be an integer.",
|
|
|
|
worker_count.getSourceLine());
|
|
|
|
}
|
|
|
|
} catch(const SettingNotFoundException& e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
|
2020-07-08 07:41:05 +00:00
|
|
|
try {
|
|
|
|
auto& timeout = root["io_timeout"];
|
|
|
|
if(timeout.isNumber()) {
|
2020-07-10 07:16:51 +00:00
|
|
|
if(timeout.getType() == Setting::TypeFloat)
|
2020-07-08 07:41:05 +00:00
|
|
|
_io_timeout = duration_cast<milliseconds>(
|
|
|
|
duration<double, std::milli>(timeout));
|
|
|
|
else
|
|
|
|
_io_timeout = milliseconds((int)timeout);
|
|
|
|
} else
|
|
|
|
logPrintf(WARN, "Line %d: io_timeout must be a number.",
|
|
|
|
timeout.getSourceLine());
|
|
|
|
} catch(const SettingNotFoundException& e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
|
2020-07-14 04:12:40 +00:00
|
|
|
try {
|
|
|
|
auto& devices = root["devices"];
|
|
|
|
|
|
|
|
for(int i = 0; i < devices.getLength(); i++) {
|
|
|
|
const Setting& device = devices[i];
|
|
|
|
std::string name;
|
|
|
|
try {
|
|
|
|
if(!device.lookupValue("name", name)) {
|
|
|
|
logPrintf(WARN, "Line %d: 'name' must be a string, skipping"
|
|
|
|
" device.", device["name"].getSourceLine());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch(SettingNotFoundException &e) {
|
|
|
|
logPrintf(WARN, "Line %d: Missing name field, skipping device."
|
|
|
|
, device.getSourceLine());
|
2019-07-17 05:53:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-14 04:12:40 +00:00
|
|
|
_device_paths.insert({name, device.getPath()});
|
2019-07-17 05:53:51 +00:00
|
|
|
}
|
2020-07-14 04:12:40 +00:00
|
|
|
}
|
|
|
|
catch(const SettingNotFoundException &e) {
|
|
|
|
logPrintf(WARN, "No devices listed in config file.");
|
2019-07-17 05:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 04:12:40 +00:00
|
|
|
libconfig::Setting& Configuration::getSetting(const std::string& path)
|
2019-07-17 05:53:51 +00:00
|
|
|
{
|
2020-07-02 04:38:40 +00:00
|
|
|
return _config.lookup(path);
|
2019-07-17 05:53:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 04:12:40 +00:00
|
|
|
std::string Configuration::getDevice(const std::string& name)
|
2019-08-08 22:22:54 +00:00
|
|
|
{
|
2020-07-02 04:38:40 +00:00
|
|
|
auto it = _device_paths.find(name);
|
|
|
|
if(it == _device_paths.end())
|
|
|
|
throw DeviceNotFound(name);
|
|
|
|
else
|
|
|
|
return it->second;
|
2019-08-08 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 04:38:40 +00:00
|
|
|
Configuration::DeviceNotFound::DeviceNotFound(std::string name) :
|
|
|
|
_name (std::move(name))
|
2019-07-17 05:53:51 +00:00
|
|
|
{
|
2019-08-17 23:58:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 06:55:46 +00:00
|
|
|
const char * Configuration::DeviceNotFound::what() const noexcept
|
2019-08-17 23:58:00 +00:00
|
|
|
{
|
2020-07-02 04:38:40 +00:00
|
|
|
return _name.c_str();
|
2020-07-04 18:36:53 +00:00
|
|
|
}
|
2020-07-08 07:41:05 +00:00
|
|
|
|
2020-07-10 07:16:51 +00:00
|
|
|
int Configuration::workerCount() const
|
|
|
|
{
|
|
|
|
return _worker_threads;
|
|
|
|
}
|
|
|
|
|
2020-07-08 07:41:05 +00:00
|
|
|
std::chrono::milliseconds Configuration::ioTimeout() const
|
|
|
|
{
|
|
|
|
return _io_timeout;
|
|
|
|
}
|