From c9fa963e71258c5adfb71cf1996cd1bcb33df0bb Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 26 Feb 2023 08:54:17 +0100 Subject: Start with copy of whiteboard --- config.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 config.cpp (limited to 'config.cpp') diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..afb5cd2 --- /dev/null +++ b/config.cpp @@ -0,0 +1,83 @@ +#include "config.h" + +#include +#include + +#include +#include + +namespace pt = boost::property_tree; +using namespace std::string_literals; + +namespace { + const std::string default_datapath {"/var/lib/whiteboard"}; + const uint64_t default_maxage{0}; // timeout in seconds; 0 = no timeout + const std::string default_listen {"::1:9000"}; + const int default_threads{1}; + const int default_max_connections{1000}; +} + +Config::Config(const std::string& config_filename): + m_dataPath{default_datapath}, + m_maxage{default_maxage}, + m_listenAddress{"::1"}, + m_listenPort{9000}, + m_threads{default_threads}, + m_max_connections{default_max_connections} +{ + + try { + pt::ptree tree; + + pt::read_xml(config_filename, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); + + m_dataPath = tree.get("config.datapath", default_datapath); + m_maxage = tree.get("config.maxage", default_maxage); + std::string listen {tree.get("config.port", default_listen)}; + auto pos{listen.find_last_of(':')}; + if (pos == std::string::npos) + throw std::runtime_error("Bad port address: "s + listen); + + m_listenAddress = listen.substr(0, pos); + m_listenPort = std::stoi(listen.substr(pos + 1)); + if (m_listenPort < 0 || m_listenPort > 65535) + throw std::runtime_error("Bad listen port: "s + std::to_string(m_listenPort)); + + m_threads = tree.get("config.threads", default_threads); + + m_max_connections = tree.get("config.maxconnections", default_max_connections); + } catch (const std::exception& ex) { + std::cerr << "Error reading config file " << config_filename << ". Using defaults." << std::endl; + } +} + +std::string Config::getDataPath() const +{ + return m_dataPath; +} + +uint64_t Config::getMaxage() const +{ + return m_maxage; +} + +std::string Config::getListenAddress() const +{ + return m_listenAddress; +} + +int Config::getListenPort() const +{ + return m_listenPort; +} + +int Config::getThreads() const +{ + return m_threads; +} + +int Config::getMaxConnections() const +{ + return m_max_connections; +} + -- cgit v1.2.3