From 082dac7a8cb39ec1b005680680c4f3e1e8ddc256 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 14 Sep 2008 23:16:58 +0200 Subject: Verwende statische Instanzen von ConfigManager, ConnectionManager und RequestManager (Singleton) --- src/Core/ConfigManager.h | 10 ++++- src/Core/ConnectionManager.cpp | 44 +++++++++++++++---- src/Core/ConnectionManager.h | 33 ++++++++++---- src/Core/DaemonInfo.h | 2 +- .../RequestHandlers/DaemonStatusRequestHandler.cpp | 50 ++++++++++++++++++++++ .../RequestHandlers/DaemonStatusRequestHandler.h | 43 +++++++++++++++++++ src/Core/RequestHandlers/Makefile.am | 4 +- src/Core/RequestHandlers/Makefile.in | 7 +-- 8 files changed, 169 insertions(+), 24 deletions(-) create mode 100644 src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp create mode 100644 src/Core/RequestHandlers/DaemonStatusRequestHandler.h (limited to 'src/Core') diff --git a/src/Core/ConfigManager.h b/src/Core/ConfigManager.h index 6470a33..760312a 100644 --- a/src/Core/ConfigManager.h +++ b/src/Core/ConfigManager.h @@ -42,11 +42,19 @@ class ConfigManager : public Common::ConfigManager { std::string x509TrustFile, x509CrlFile, x509CertFile, x509KeyFile; + ConfigManager(); + protected: virtual bool parseLine(const std::vector §ion, const std::string &key, const std::string &value); public: - ConfigManager(); + static void useConfigManager() { + setConfigManager(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;} diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 98288ba..78497e7 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -20,6 +20,7 @@ #include "ConnectionManager.h" #include "ConfigManager.h" #include "RequestHandlers/CoreStatusRequestHandler.h" +#include "RequestHandlers/DaemonStatusRequestHandler.h" #include "RequestHandlers/GSSAPIAuthRequestHandler.h" #include #include @@ -29,6 +30,9 @@ namespace Mad { namespace Core { +std::auto_ptr ConnectionManager::connectionManager; + + void ConnectionManager::refreshPollfds() { pollfds.clear(); pollfdMap.clear(); @@ -53,15 +57,22 @@ void ConnectionManager::refreshPollfds() { } } -ConnectionManager::ConnectionManager(const ConfigManager& configManager) : requestManager(true) { - requestManager.registerPacketType(Net::Packet::CORE_STATUS); - requestManager.registerPacketType(Net::Packet::GSSAPI_AUTH); +ConnectionManager::ConnectionManager() { + Common::RequestManager::init(true); + + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::CORE_STATUS); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_STATUS); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::GSSAPI_AUTH); - const std::vector &listenerAddresses = configManager.getListenerAddresses(); + ConfigManager *configManager = ConfigManager::getConfigManager(); + + Net::Connection::init(); + + const std::vector &listenerAddresses = configManager->getListenerAddresses(); if(listenerAddresses.empty()) { try { - listeners.push_back(new Net::Listener(configManager.getX509CertFile(), configManager.getX509KeyFile())); + listeners.push_back(new Net::Listener(configManager->getX509CertFile(), configManager->getX509KeyFile())); } catch(Net::Exception &e) { // TODO Log error @@ -70,7 +81,7 @@ ConnectionManager::ConnectionManager(const ConfigManager& configManager) : reque 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(configManager->getX509CertFile(), configManager->getX509KeyFile(), *address)); } catch(Net::Exception &e) { // TODO Log error @@ -79,6 +90,13 @@ ConnectionManager::ConnectionManager(const ConfigManager& configManager) : reque } refreshPollfds(); + + const std::vector& daemons = configManager->getDaemonList(); + + 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)); + } } ConnectionManager::~ConnectionManager() { @@ -87,6 +105,8 @@ ConnectionManager::~ConnectionManager() { for(std::list::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) delete *con; + + Net::Connection::deinit(); } void ConnectionManager::handleConnections(std::list& connections) { @@ -100,7 +120,7 @@ void ConnectionManager::handleConnections(std::list& con ++con; } else { - requestManager.unregisterConnection(*con); + Common::RequestManager::getRequestManager()->unregisterConnection(*con); delete *con; connections.erase(con++); } @@ -118,12 +138,20 @@ void ConnectionManager::run() { while((con = (*listener)->getConnection(pollfdMap)) != 0) { (con->isDaemonConnection() ? daemonConnections : clientConnections).push_back(con); - requestManager.registerConnection(con); + Common::RequestManager::getRequestManager()->registerConnection(con); } } refreshPollfds(); } +Net::Connection* ConnectionManager::getDaemonConnection(const std::string &name) const { + std::map::const_iterator daemon = identifiedDaemonConnections.find(name); + if(daemon == identifiedDaemonConnections.end()) + return 0; + + return daemon->second; +} + } } diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index 6161c3f..4ca3d59 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -23,7 +23,10 @@ #include #include #include +#include #include + +#include "DaemonInfo.h" #include namespace Mad { @@ -37,37 +40,49 @@ class Packet; namespace Core { -class ConfigManager; - class ConnectionManager { private: - // Prevent shallow copy - ConnectionManager(const ConnectionManager &o); - ConnectionManager& operator=(const ConnectionManager &o); - - Common::RequestManager requestManager; + static std::auto_ptr connectionManager; std::list listeners; std::list daemonConnections; std::list clientConnections; + std::map daemonInfo; + std::map identifiedDaemonConnections; + std::vector pollfds; std::map pollfdMap; + // Prevent shallow copy + ConnectionManager(const ConnectionManager &o); + ConnectionManager& operator=(const ConnectionManager &o); + + ConnectionManager(); + void refreshPollfds(); - void handleConnections(std::list& connections); + void handleConnections(std::list &connections); public: - ConnectionManager(const ConfigManager& configManager); + static ConnectionManager* getConnectionManager() { + return connectionManager.get(); + } + virtual ~ConnectionManager(); + void init() { + connectionManager = std::auto_ptr(new ConnectionManager()); + } + bool wait(int timeout) { return (poll(pollfds.data(), pollfds.size(), timeout) > 0); } void run(); + + Net::Connection* getDaemonConnection(const std::string &name) const; }; } diff --git a/src/Core/DaemonInfo.h b/src/Core/DaemonInfo.h index b1dd2ff..0377c57 100644 --- a/src/Core/DaemonInfo.h +++ b/src/Core/DaemonInfo.h @@ -1,7 +1,7 @@ /* * DaemonInfo.h * - * Copyright (C) 2008 neoraider + * 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 diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp new file mode 100644 index 0000000..81f066d --- /dev/null +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp @@ -0,0 +1,50 @@ +/* + * DaemonStatusRequestHandler.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 "DaemonStatusRequestHandler.h" +#include "../ConnectionManager.h" +#include + +namespace Mad { +namespace Core { +namespace RequestHandlers { + +bool DaemonStatusRequestHandler::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(isFinished()) + return false; + + if(packet.getType() != Net::Packet::DAEMON_STATUS) + return false; // TODO Logging + + // TODO Require authentication + + std::string daemonName((char*)packet.getData(), packet.getLength()); + + /*Net::Connection *daemonCon = connectionManager->getDaemonConnection(daemonName); + if(!daemonCon) + return false;*/ + + + + return true; +} + +} +} +} diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h new file mode 100644 index 0000000..accc990 --- /dev/null +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h @@ -0,0 +1,43 @@ +/* + * DaemonStatusRequestHandler.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_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ +#define MAD_CORE_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ + +#include + +namespace Mad { +namespace Core { + +class ConnectionManager; + +namespace RequestHandlers { + +class DaemonStatusRequestHandler : public Common::RequestHandler { + public: + DaemonStatusRequestHandler() {} + + virtual bool handlePacket(Net::Connection*, const Net::Packet &packet); +}; + +} +} +} + +#endif /* MAD_CORE_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ */ diff --git a/src/Core/RequestHandlers/Makefile.am b/src/Core/RequestHandlers/Makefile.am index d556af6..3be5350 100644 --- a/src/Core/RequestHandlers/Makefile.am +++ b/src/Core/RequestHandlers/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = CoreStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp +librequesthandlers_la_SOURCES = CoreStatusRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp -noinst_HEADERS = CoreStatusRequestHandler.h GSSAPIAuthRequestHandler.h +noinst_HEADERS = CoreStatusRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h diff --git a/src/Core/RequestHandlers/Makefile.in b/src/Core/RequestHandlers/Makefile.in index 9147203..6661e32 100644 --- a/src/Core/RequestHandlers/Makefile.in +++ b/src/Core/RequestHandlers/Makefile.in @@ -46,7 +46,7 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) librequesthandlers_la_LIBADD = am_librequesthandlers_la_OBJECTS = CoreStatusRequestHandler.lo \ - GSSAPIAuthRequestHandler.lo + DaemonStatusRequestHandler.lo GSSAPIAuthRequestHandler.lo librequesthandlers_la_OBJECTS = $(am_librequesthandlers_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -185,8 +185,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = CoreStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp -noinst_HEADERS = CoreStatusRequestHandler.h GSSAPIAuthRequestHandler.h +librequesthandlers_la_SOURCES = CoreStatusRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp +noinst_HEADERS = CoreStatusRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h all: all-am .SUFFIXES: @@ -239,6 +239,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoreStatusRequestHandler.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonStatusRequestHandler.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GSSAPIAuthRequestHandler.Plo@am__quote@ .cpp.o: -- cgit v1.2.3