diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-10-20 22:47:07 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-10-20 22:47:07 +0200 |
commit | e3d0c98e5a867518800bc79c18f7b13755c056e0 (patch) | |
tree | 874fff633e497040a28b72ffc7cbec208b2ac3ee /src | |
parent | 3944988f51769b0ffd8e58c05566c82416bf983d (diff) | |
download | mad-e3d0c98e5a867518800bc79c18f7b13755c056e0.tar mad-e3d0c98e5a867518800bc79c18f7b13755c056e0.zip |
Einige Verbesserungen am ConfigManager
Diffstat (limited to 'src')
-rw-r--r-- | src/Common/ConfigManager.cpp | 10 | ||||
-rw-r--r-- | src/Common/ConfigManager.h | 7 | ||||
-rw-r--r-- | src/Common/Configurable.cpp | 35 | ||||
-rw-r--r-- | src/Common/Configurable.h | 8 | ||||
-rw-r--r-- | src/Common/Logger.cpp | 17 | ||||
-rw-r--r-- | src/Common/Logger.h | 17 | ||||
-rw-r--r-- | src/Common/Makefile.am | 2 | ||||
-rw-r--r-- | src/Common/Makefile.in | 8 | ||||
-rw-r--r-- | src/Core/ConnectionManager.cpp | 9 | ||||
-rw-r--r-- | src/Core/ConnectionManager.h | 2 | ||||
-rw-r--r-- | src/mad-core.conf | 3 | ||||
-rw-r--r-- | src/mad-core.cpp | 2 |
12 files changed, 103 insertions, 17 deletions
diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp index 20a6110..a7270e2 100644 --- a/src/Common/ConfigManager.cpp +++ b/src/Common/ConfigManager.cpp @@ -31,11 +31,19 @@ namespace Common { ConfigManager ConfigManager::configManager; +bool ConfigManager::Compare::operator() (const Configurable *c1, const Configurable *c2) { + if(c1->getPriority() != c2->getPriority()) + return c1->getPriority() > c2->getPriority(); + else + return c1 < c2; +} + + void ConfigManager::handleConfigEntry(const std::vector<std::vector<std::string> > &entry) { bool handled = false; for(std::set<Configurable*>::iterator c = configurables.begin(); c != configurables.end(); ++c) { - if((*c)->handleConfigEntry(entry)) + if((*c)->handleConfigEntry(entry, handled)) handled = true; } diff --git a/src/Common/ConfigManager.h b/src/Common/ConfigManager.h index ccf5d13..b447fc4 100644 --- a/src/Common/ConfigManager.h +++ b/src/Common/ConfigManager.h @@ -31,9 +31,14 @@ class Configurable; class ConfigManager { private: + struct Compare { + bool operator() (const Configurable *c1, const Configurable *c2); + }; + static ConfigManager configManager; - std::set<Configurable*> configurables; + std::set<Configurable*, Compare> configurables; + bool finished; ConfigManager() : finished(false) {} diff --git a/src/Common/Configurable.cpp b/src/Common/Configurable.cpp new file mode 100644 index 0000000..4d689ed --- /dev/null +++ b/src/Common/Configurable.cpp @@ -0,0 +1,35 @@ +/* + * Configurable.cpp + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * 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/>. + */ + +#include "Configurable.h" +#include "ConfigManager.h" + +namespace Mad { +namespace Common { + +Configurable::Configurable() { + ConfigManager::getConfigManager()->registerConfigurable(this); +} + +Configurable::~Configurable() { + ConfigManager::getConfigManager()->unregisterConfigurable(this); +} + +} +} diff --git a/src/Common/Configurable.h b/src/Common/Configurable.h index 0c471c7..8c2c083 100644 --- a/src/Common/Configurable.h +++ b/src/Common/Configurable.h @@ -29,10 +29,16 @@ namespace Common { class ConfigManager; class Configurable { + public: + Configurable(); + virtual ~Configurable(); + + virtual int getPriority() const {return 0;} + protected: friend class ConfigManager; - virtual bool handleConfigEntry(const std::vector<std::vector<std::string> >&) {return false;} + virtual bool handleConfigEntry(const std::vector<std::vector<std::string> >&, bool) {return false;} virtual void configFinished() {} }; diff --git a/src/Common/Logger.cpp b/src/Common/Logger.cpp index a7d3bd1..f99d31f 100644 --- a/src/Common/Logger.cpp +++ b/src/Common/Logger.cpp @@ -18,15 +18,30 @@ */ #include "Logger.h" +#include "ConfigManager.h" +#include "Util.h" + #include <cstdlib> -#include <ctime> namespace Mad { namespace Common { +std::auto_ptr<Logger::ConfigHelper> Logger::configHelper; std::list<Logger*> Logger::loggers; +bool Logger::ConfigHelper::handleConfigEntry(const std::vector<std::vector<std::string> > &entry, bool handled) { + if(handled) + return false; + + if(entry.size() == 1 && entry.front().size() == 2 && Util::tolower(entry.front().front()) == "logger") { + logf(WARNING, "Invalid logger '%s'.", entry.back().back().c_str()); + return true; + } + + return false; +} + void Logger::logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap) { int size = 100; char *buf = (char*)std::malloc(size); diff --git a/src/Common/Logger.h b/src/Common/Logger.h index 1fff654..d8f105a 100644 --- a/src/Common/Logger.h +++ b/src/Common/Logger.h @@ -21,17 +21,26 @@ #define MAD_COMMON_LOGGER_H_ #include "LoggerBase.h" +#include "Configurable.h" + #include <algorithm> -#include <list> -#include <string> #include <cstdarg> #include <ctime> +#include <list> +#include <memory> +#include <string> namespace Mad { namespace Common { class Logger : public LoggerBase { private: + class ConfigHelper : private Configurable { + protected: + virtual bool handleConfigEntry(const std::vector<std::vector<std::string> > &entry, bool handled); + }; + + static std::auto_ptr<ConfigHelper> configHelper; static std::list<Logger*> loggers; static void logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap); @@ -40,6 +49,10 @@ class Logger : public LoggerBase { virtual void logMessage(MessageCategory category, MessageLevel level, time_t messageTimestamp, const std::string &message) = 0; public: + static void initConfigHelper() { + configHelper.reset(new ConfigHelper()); + } + static void log(MessageCategory category, MessageLevel level, const std::string &message); static void log(MessageCategory category, const std::string &message) { log(category, DEFAULT, message); diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index d104cac..95b3d50 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -1,7 +1,7 @@ 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_SOURCES = Configurable.cpp 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 = Configurable.h LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h \ diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 6bcffb7..8b9300c 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -49,8 +49,9 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libcommon_la_DEPENDENCIES = Backends/libbackends.la \ Requests/librequests.la RequestHandlers/librequesthandlers.la -am_libcommon_la_OBJECTS = RemoteLogger.lo Logger.lo ConfigManager.lo \ - Exception.lo RequestManager.lo SystemBackend.lo Util.lo +am_libcommon_la_OBJECTS = Configurable.lo RemoteLogger.lo Logger.lo \ + ConfigManager.lo Exception.lo RequestManager.lo \ + SystemBackend.lo Util.lo libcommon_la_OBJECTS = $(am_libcommon_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -200,7 +201,7 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ 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_SOURCES = Configurable.cpp 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 = 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 @@ -257,6 +258,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConfigManager.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Configurable.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Exception.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Logger.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RemoteLogger.Plo@am__quote@ diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index c135d06..b99296e 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -55,7 +55,10 @@ void ConnectionManager::updateState(const std::string &name, Common::HostInfo::S } } -bool ConnectionManager::handleConfigEntry(const std::vector<std::vector<std::string> > &entry) { +bool ConnectionManager::handleConfigEntry(const std::vector<std::vector<std::string> > &entry, bool handled) { + if(handled) + return false; + if(Common::Util::tolower(entry.front().front()) == "listen" && entry.size() == 1) { if(entry.front().size() == 2) { try { @@ -154,14 +157,10 @@ ConnectionManager::ConnectionManager() { Common::RequestManager::getRequestManager()->registerPacketType<RequestHandlers::IdentifyRequestHandler>(Net::Packet::IDENTIFY); Common::RequestManager::getRequestManager()->registerPacketType<RequestHandlers::LogRequestHandler>(Net::Packet::LOG); - Common::ConfigManager::getConfigManager()->registerConfigurable(this); - Net::Connection::init(); } ConnectionManager::~ConnectionManager() { - Common::ConfigManager::getConfigManager()->unregisterConfigurable(this); - for(std::list<Net::ServerConnection*>::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) delete *con; diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index a069478..d5bb718 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -67,7 +67,7 @@ class ConnectionManager : private Common::Configurable { void updateState(const std::string &name, Common::HostInfo::State state); protected: - virtual bool handleConfigEntry(const std::vector<std::vector<std::string> > &entry); + virtual bool handleConfigEntry(const std::vector<std::vector<std::string> > &entry, bool handled); virtual void configFinished(); public: diff --git a/src/mad-core.conf b/src/mad-core.conf index b35b346..b57ab97 100644 --- a/src/mad-core.conf +++ b/src/mad-core.conf @@ -1,4 +1,5 @@ -#ConfigMethod Mysql +Logger Console + Listen * diff --git a/src/mad-core.cpp b/src/mad-core.cpp index ec9d316..971ea9f 100644 --- a/src/mad-core.cpp +++ b/src/mad-core.cpp @@ -38,6 +38,8 @@ int main() { Common::Backends::ConsoleLogger logger; Common::Logger::registerLogger(&logger); + Common::Logger::initConfigHelper(); + Core::ConnectionManager::init(); Common::ConfigManager::getConfigManager()->loadFile("mad-core.conf"); |