From 96b6f07a32cb02ae5d907bacd81f62fc25fdc278 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 19 Oct 2008 20:35:52 +0200 Subject: Neuen ConfigManager angefangen & alten Code daran angepasst --- src/Common/ConfigManager.cpp | 52 +++++++++++++------- src/Common/ConfigManager.h | 32 +++++++------ src/Common/Configurable.h | 42 ++++++++++++++++ src/Common/Makefile.am | 3 +- src/Common/Makefile.in | 4 +- src/Core/ConfigManager.cpp | 87 --------------------------------- src/Core/ConfigManager.h | 73 ---------------------------- src/Core/ConnectionManager.cpp | 106 ++++++++++++++++++++++++++++++++--------- src/Core/ConnectionManager.h | 10 +++- src/Core/Makefile.am | 4 +- src/Core/Makefile.in | 7 ++- src/mad-core.conf | 2 +- src/mad-core.cpp | 5 +- 13 files changed, 201 insertions(+), 226 deletions(-) create mode 100644 src/Common/Configurable.h delete mode 100644 src/Core/ConfigManager.cpp delete mode 100644 src/Core/ConfigManager.h (limited to 'src') diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp index 6afeeef..fbd73ea 100644 --- a/src/Common/ConfigManager.cpp +++ b/src/Common/ConfigManager.cpp @@ -18,9 +18,9 @@ */ #include "ConfigManager.h" +#include "Configurable.h" +#include "Logger.h" #include "Util.h" -#include "Backends/SystemBackendProc.h" -#include "Backends/SystemBackendPosix.h" #include #include @@ -28,13 +28,26 @@ namespace Mad { namespace Common { -std::auto_ptr ConfigManager::configManager; +ConfigManager ConfigManager::configManager; -bool ConfigManager::loadFile(const std::string &filename) { +void ConfigManager::handleConfigEntry(const std::vector &entry, const std::vector > §ion) { + bool handled = false; + + for(std::set::iterator c = configurables.begin(); c != configurables.end(); ++c) { + if((*c)->handleConfigEntry(entry, section)) + handled = true; + } + + if(!handled) + Logger::logf(Logger::WARNING, "Unknown config option '%s'.", entry.front().c_str()); +} + +bool ConfigManager::loadFile(const std::string &filename, bool finish) { std::ifstream file(filename.c_str()); - std::vector section; - std::string line, key; + std::vector > section; + std::vector entry; + std::string line; if(!file.good()) return false; @@ -48,7 +61,7 @@ bool ConfigManager::loadFile(const std::string &filename) { size_t pos = line.find_first_of('#'); if(pos != std::string::npos) - line[pos] = 0; + line = line.substr(0, pos); line = Util::trim(line); @@ -70,19 +83,24 @@ bool ConfigManager::loadFile(const std::string &filename) { if(!line.empty()) { pos = line.find_first_of(" \t"); + entry.clear(); + if(pos == std::string::npos) { - key = line; - parseLine(section, key); + entry.push_back(line); + + handleConfigEntry(entry, section); } else { - key = line.substr(0, pos); - parseLine(section, key, Util::trim(line.substr(pos))); + entry.push_back(line.substr(0, pos)); + entry.push_back(Util::trim(line.substr(pos))); + + handleConfigEntry(entry, section); } } switch(bracket) { case '{': - section.push_back(key); + section.push_back(entry); break; case '}': section.pop_back(); @@ -93,12 +111,12 @@ bool ConfigManager::loadFile(const std::string &filename) { // TODO Depth check - return true; -} + if(finish) { + for(std::set::iterator c = configurables.begin(); c != configurables.end(); ++c) + (*c)->configFinished(); + } -void ConfigManager::initBackends() { - Backends::SystemBackendProc::registerBackend(); - Backends::SystemBackendPosix::registerBackend(); + return true; } } diff --git a/src/Common/ConfigManager.h b/src/Common/ConfigManager.h index 8bb4c87..7fa781e 100644 --- a/src/Common/ConfigManager.h +++ b/src/Common/ConfigManager.h @@ -20,37 +20,41 @@ #ifndef MAD_COMMON_CONFIGMANAGER_H_ #define MAD_COMMON_CONFIGMANAGER_H_ +#include #include #include -#include namespace Mad { namespace Common { +class Configurable; + class ConfigManager { private: - static std::auto_ptr configManager; + static ConfigManager configManager; - protected: - ConfigManager() { - initBackends(); - } + std::set configurables; + bool finished; - static void setConfigManager(std::auto_ptr configManager0) { - configManager = configManager0; - } + ConfigManager() : finished(false) {} + + void handleConfigEntry(const std::vector&, const std::vector >&); - virtual bool parseLine(const std::vector §ion, const std::string &key, const std::string &value = std::string()) = 0; + public: + bool loadFile(const std::string &filename, bool finish = true); - bool loadFile(const std::string &filename); + void registerConfigurable(Configurable *c) { + configurables.insert(c); + } - void initBackends(); + void unregisterConfigurable(Configurable *c) { + configurables.erase(c); + } - public: virtual ~ConfigManager() {} static ConfigManager *getConfigManager() { - return configManager.get(); + return &configManager; } }; diff --git a/src/Common/Configurable.h b/src/Common/Configurable.h new file mode 100644 index 0000000..350b444 --- /dev/null +++ b/src/Common/Configurable.h @@ -0,0 +1,42 @@ +/* + * Configurable.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_COMMON_CONFIGURABLE_H_ +#define MAD_COMMON_CONFIGURABLE_H_ + +#include +#include + +namespace Mad { +namespace Common { + +class ConfigManager; + +class Configurable { + protected: + friend class ConfigManager; + + virtual bool handleConfigEntry(const std::vector&, const std::vector >&) {return false;} + virtual void configFinished() {} +}; + +} +} + +#endif /* MAD_COMMON_CONFIGURABLE_H_ */ diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index be2f08e..d104cac 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -4,4 +4,5 @@ noinst_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = RemoteLogger.cpp Logger.cpp ConfigManager.cpp Exception.cpp RequestManager.cpp SystemBackend.cpp Util.cpp libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Util.h +noinst_HEADERS = Configurable.h LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h \ + RequestHandler.h RequestManager.h SystemBackend.h Util.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 40cb554..6bcffb7 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -202,7 +202,9 @@ SUBDIRS = Backends Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = RemoteLogger.cpp Logger.cpp ConfigManager.cpp Exception.cpp RequestManager.cpp SystemBackend.cpp Util.cpp libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Util.h +noinst_HEADERS = Configurable.h LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h \ + RequestHandler.h RequestManager.h SystemBackend.h Util.h + all: all-recursive .SUFFIXES: diff --git a/src/Core/ConfigManager.cpp b/src/Core/ConfigManager.cpp deleted file mode 100644 index 9361493..0000000 --- a/src/Core/ConfigManager.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * ConfigManager.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * 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 . - */ - -#include "ConfigManager.h" -#include - -namespace Mad { -namespace Core { - -bool ConfigManager::parseLine(const std::vector §ion, const std::string &key, const std::string &value) { - if(section.empty()) { - if(Common::Util::tolower(key) == "configmethod") { - if(Common::Util::tolower(value) == "mysql") - methods |= (uint16_t)MYSQL; - else - return false; - } - else if(Common::Util::tolower(key) == "daemon") { - daemons.push_back(Common::HostInfo(value)); - } - else if(Common::Util::tolower(key) == "listen") { - try { - listeners.push_back(Net::IPAddress(value)); - } - catch(Common::Exception &e) { - // TODO Logging - } - } - else if(Common::Util::tolower(key) == "x509trustfile") { - x509TrustFile = value; - } - else if(Common::Util::tolower(key) == "x509crlfile") { - x509CrlFile = value; - } - else if(Common::Util::tolower(key) == "x509certfile") { - x509CertFile = value; - } - else if(Common::Util::tolower(key) == "x509keyfile") { - x509KeyFile = value; - } - else { - // TODO Logging - - return false; - } - - return true; - } - else if(section.size() == 1 && Common::Util::tolower(section[0]) == "daemon") { - if(Common::Util::tolower(key) == "ip") - daemons.back().setIP(value); - else { - // TODO Logging - - return false; - } - - return true; - } - - // TODO Logging - - return false; -} - -ConfigManager::ConfigManager() { - loadFile("mad-core.conf"); -} - -} -} diff --git a/src/Core/ConfigManager.h b/src/Core/ConfigManager.h deleted file mode 100644 index 7178887..0000000 --- a/src/Core/ConfigManager.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * ConfigManager.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * 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 . - */ - -#ifndef MAD_CORE_CONFIGMANAGER_H_ -#define MAD_CORE_CONFIGMANAGER_H_ - -#include -#include -#include - -#include -#include - -namespace Mad { -namespace Core { - -class ConfigManager : public Common::ConfigManager { - private: - enum Methods { - MYSQL = (1 << 0) - }; - - uint16_t methods; - - std::vector listeners; - std::vector daemons; - - std::string x509TrustFile, x509CrlFile, x509CertFile, x509KeyFile; - - ConfigManager(); - - protected: - virtual bool parseLine(const std::vector §ion, const std::string &key, const std::string &value); - - public: - static void useConfigManager() { - setConfigManager(std::auto_ptr(new ConfigManager())); - } - - static ConfigManager *getConfigManager() { - return dynamic_cast(Common::ConfigManager::getConfigManager()); - } - - const std::vector& getListenerAddresses() const {return listeners;} - const std::vector& getDaemonList() const {return daemons;} - - const std::string& getX509TrustFile() const {return x509TrustFile;} - const std::string& getX509CrlFile() const {return x509CrlFile;} - const std::string& getX509CertFile() const {return x509CertFile;} - const std::string& getX509KeyFile() const {return x509KeyFile;} -}; - -} - -} - -#endif /* MAD_CORE_CONFIGMANAGER_H_ */ diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index c0edd61..5eed936 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -18,10 +18,11 @@ */ #include "ConnectionManager.h" -#include "ConfigManager.h" +#include #include #include #include +#include #include "Requests/DaemonStateUpdateRequest.h" #include "RequestHandlers/DaemonCommandRequestHandler.h" #include "RequestHandlers/DaemonFSInfoRequestHandler.h" @@ -54,29 +55,74 @@ void ConnectionManager::updateState(const std::string &name, Common::HostInfo::S } } -ConnectionManager::ConnectionManager() { - Common::RequestManager::init(true); +bool ConnectionManager::handleConfigEntry(const std::vector &entry, const std::vector > §ion) { + if(section.empty()) { + if(Common::Util::tolower(entry.front()) == "listen") { + if(entry.size() == 2) { + try { + listenerAddresses.push_back(Net::IPAddress(entry.back())); + } + catch(Common::Exception &e) { + // TODO Log error + } - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::FS_INFO); - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::STATUS); - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_COMMAND_REBOOT); - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_COMMAND_SHUTDOWN); - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_FS_INFO); - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::LIST_DAEMONS); - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_STATUS); - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::GSSAPI_AUTH); - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::IDENTIFY); - Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::LOG); + return true; + } + } + else if(Common::Util::tolower(entry.front()) == "x509trustfile") { + if(entry.size() == 2) { + x509TrustFile = entry.back(); - ConfigManager *configManager = ConfigManager::getConfigManager(); + return true; + } + } + else if(Common::Util::tolower(entry.front()) == "x509crlfile") { + if(entry.size() == 2) { + x509CrlFile = entry.back(); - Net::Connection::init(); + return true; + } + } + else if(Common::Util::tolower(entry.front()) == "x509certfile") { + if(entry.size() == 2) { + x509CertFile = entry.back(); + + return true; + } + } + else if(Common::Util::tolower(entry.front()) == "x509keyfile") { + if(entry.size() == 2) { + x509KeyFile = entry.back(); + + return true; + } + } + else if(Common::Util::tolower(entry.front()) == "daemon") { + if(entry.size() == 2) { + daemonInfo.insert(std::make_pair(entry.back(), Common::HostInfo(entry.back()))); + identifiedDaemonConnections.insert(std::make_pair(entry.back(), 0)); - const std::vector &listenerAddresses = configManager->getListenerAddresses(); + return true; + } + } + } + else if(Common::Util::tolower(section.front().front()) == "daemon" && section.front().size() == 2 && section.size() == 1) { + if(Common::Util::tolower(entry.front()) == "ipaddress") { + if(entry.size() == 2) { + daemonInfo[section.front().back()].setIP(entry.back()); + + return true; + } + } + } + return false; +} + +void ConnectionManager::configFinished() { if(listenerAddresses.empty()) { try { - listeners.push_back(new Net::Listener(configManager->getX509CertFile(), configManager->getX509KeyFile())); + listeners.push_back(new Net::Listener(x509CertFile, x509KeyFile)); } catch(Common::Exception &e) { // TODO Log error @@ -85,23 +131,37 @@ ConnectionManager::ConnectionManager() { else { for(std::vector::const_iterator address = listenerAddresses.begin(); address != listenerAddresses.end(); ++address) { try { - listeners.push_back(new Net::Listener(configManager->getX509CertFile(), configManager->getX509KeyFile(), *address)); + listeners.push_back(new Net::Listener(x509CertFile, x509KeyFile, *address)); } catch(Common::Exception &e) { // TODO Log error } } } +} - const std::vector& daemons = configManager->getDaemonList(); +ConnectionManager::ConnectionManager() { + Common::RequestManager::init(true); - for(std::vector::const_iterator daemon = daemons.begin(); daemon != daemons.end(); ++daemon) { - daemonInfo.insert(std::make_pair(daemon->getName(), *daemon)); - identifiedDaemonConnections.insert(std::make_pair(daemon->getName(), 0)); - } + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::FS_INFO); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::STATUS); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_COMMAND_REBOOT); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_COMMAND_SHUTDOWN); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_FS_INFO); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::LIST_DAEMONS); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_STATUS); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::GSSAPI_AUTH); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::IDENTIFY); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::LOG); + + Common::ConfigManager::getConfigManager()->registerConfigurable(this); + + Net::Connection::init(); } ConnectionManager::~ConnectionManager() { + Common::ConfigManager::getConfigManager()->unregisterConfigurable(this); + for(std::list::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) delete *con; diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index 6bbd66a..ad5a57e 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -40,10 +41,13 @@ class Packet; namespace Core { -class ConnectionManager { +class ConnectionManager : private Common::Configurable { private: static std::auto_ptr connectionManager; + std::string x509TrustFile, x509CrlFile, x509CertFile, x509KeyFile; + + std::vector listenerAddresses; std::list listeners; std::list daemonConnections; @@ -62,6 +66,10 @@ class ConnectionManager { void updateState(const std::string &name, Common::HostInfo::State state); + protected: + virtual bool handleConfigEntry(const std::vector &entry, const std::vector > §ion); + virtual void configFinished(); + public: static ConnectionManager* getConnectionManager() { return connectionManager.get(); diff --git a/src/Core/Makefile.am b/src/Core/Makefile.am index b72f766..fa7bb9e 100644 --- a/src/Core/Makefile.am +++ b/src/Core/Makefile.am @@ -2,7 +2,7 @@ SUBDIRS = Requests RequestHandlers noinst_LTLIBRARIES = libcore.la -libcore_la_SOURCES = ConfigManager.cpp ConnectionManager.cpp +libcore_la_SOURCES = ConnectionManager.cpp libcore_la_LIBADD = Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = ConfigManager.h ConnectionManager.h +noinst_HEADERS = ConnectionManager.h diff --git a/src/Core/Makefile.in b/src/Core/Makefile.in index f770c09..a190ccd 100644 --- a/src/Core/Makefile.in +++ b/src/Core/Makefile.in @@ -49,7 +49,7 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libcore_la_DEPENDENCIES = Requests/librequests.la \ RequestHandlers/librequesthandlers.la -am_libcore_la_OBJECTS = ConfigManager.lo ConnectionManager.lo +am_libcore_la_OBJECTS = ConnectionManager.lo libcore_la_OBJECTS = $(am_libcore_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -199,9 +199,9 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ SUBDIRS = Requests RequestHandlers noinst_LTLIBRARIES = libcore.la -libcore_la_SOURCES = ConfigManager.cpp ConnectionManager.cpp +libcore_la_SOURCES = ConnectionManager.cpp libcore_la_LIBADD = Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = ConfigManager.h ConnectionManager.h +noinst_HEADERS = ConnectionManager.h all: all-recursive .SUFFIXES: @@ -253,7 +253,6 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConfigManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConnectionManager.Plo@am__quote@ .cpp.o: diff --git a/src/mad-core.conf b/src/mad-core.conf index 8094a97..b35b346 100644 --- a/src/mad-core.conf +++ b/src/mad-core.conf @@ -1,4 +1,4 @@ -ConfigMethod Mysql +#ConfigMethod Mysql Listen * diff --git a/src/mad-core.cpp b/src/mad-core.cpp index d2daf4c..ec9d316 100644 --- a/src/mad-core.cpp +++ b/src/mad-core.cpp @@ -17,11 +17,11 @@ * with this program. If not, see . */ +#include "Common/ConfigManager.h" #include "Common/Logger.h" #include "Common/Backends/ConsoleLogger.h" #include "Net/Connection.h" #include "Core/ConnectionManager.h" -#include "Core/ConfigManager.h" #include @@ -38,9 +38,10 @@ int main() { Common::Backends::ConsoleLogger logger; Common::Logger::registerLogger(&logger); - Core::ConfigManager::useConfigManager(); Core::ConnectionManager::init(); + Common::ConfigManager::getConfigManager()->loadFile("mad-core.conf"); + while(true) Core::ConnectionManager::getConnectionManager()->run(); -- cgit v1.2.3