From 0a1df98c0420e9ac097622f2bddcd73a6488ed52 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 20 Nov 2008 00:33:28 +0100 Subject: SingletonPtr hinzugefuegt --- src/Client/InformationManager.cpp | 12 +++--- src/Client/InformationManager.h | 17 ++++---- src/Common/Backends/SystemBackendPosix.cpp | 4 +- src/Common/Initializable.cpp | 12 ++++-- src/Common/Initializable.h | 1 + src/Common/LogManager.cpp | 2 +- src/Common/LogManager.h | 9 +++-- src/Common/Makefile.am | 2 +- src/Common/Makefile.in | 2 +- src/Common/RequestManager.cpp | 4 +- src/Common/RequestManager.h | 21 ++++++---- src/Common/SingletonPtr.h | 63 ++++++++++++++++++++++++++++++ src/Core/ConnectionManager.cpp | 9 ++--- src/Core/ConnectionManager.h | 14 +++---- src/Net/ClientConnection.cpp | 2 +- src/Net/Connection.cpp | 4 +- src/Net/FdManager.h | 2 +- src/Net/Listener.cpp | 2 +- src/Net/ServerConnection.cpp | 2 +- src/mad.cpp | 5 +-- src/madc.cpp | 18 ++++----- 21 files changed, 135 insertions(+), 72 deletions(-) create mode 100644 src/Common/SingletonPtr.h (limited to 'src') diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index 37835c1..a2f67cb 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -28,7 +28,7 @@ namespace Mad { namespace Client { -InformationManager InformationManager::informationManager; +Common::SingletonPtr InformationManager::informationManager; void InformationManager::DaemonStateUpdateRequest::handlePacket(Net::Connection *connection, const Net::Packet &packet) { @@ -44,8 +44,8 @@ void InformationManager::DaemonStateUpdateRequest::handlePacket(Net::Connection Net::Packets::HostStatePacket hostStatePacket(packet); - std::map::iterator host = informationManager.daemons.find(hostStatePacket.getName()); - if(host != informationManager.daemons.end()) + std::map::iterator host = informationManager.get()->daemons.find(hostStatePacket.getName()); + if(host != informationManager.get()->daemons.end()) host->second.setState(hostStatePacket.getState()); else Common::Logger::log(Common::Logger::WARNING, "Received a state update for an unknown host."); @@ -56,13 +56,11 @@ void InformationManager::DaemonStateUpdateRequest::handlePacket(Net::Connection } -void InformationManager::doInit() { - Common::RequestManager::get()->init(); - +InformationManager::InformationManager() : updating(false) { Common::RequestManager::get()->registerPacketType(Net::Packet::DAEMON_STATE_UPDATE); } -void InformationManager::doUninit() { +InformationManager::~InformationManager() { Common::RequestManager::get()->unregisterPacketType(Net::Packet::DAEMON_STATE_UPDATE); } diff --git a/src/Client/InformationManager.h b/src/Client/InformationManager.h index 4acde37..1464e2f 100644 --- a/src/Client/InformationManager.h +++ b/src/Client/InformationManager.h @@ -24,8 +24,8 @@ #include #include -#include #include +#include namespace Mad { @@ -40,7 +40,7 @@ class HostListPacket; namespace Client { -class InformationManager : public Common::Initializable { +class InformationManager { private: class DaemonStateUpdateRequest : public Common::RequestHandler { protected: @@ -50,7 +50,7 @@ class InformationManager : public Common::Initializable { DaemonStateUpdateRequest() {} }; - static InformationManager informationManager; + static Common::SingletonPtr informationManager; std::map daemons; @@ -60,19 +60,16 @@ class InformationManager : public Common::Initializable { InformationManager(const InformationManager &o); InformationManager& operator=(const InformationManager &o); - InformationManager() : updating(false) {} - void daemonListRequestFinished(const Common::Request &request); - protected: - virtual void doInit(); - virtual void doUninit(); - public: static InformationManager* get() { - return &informationManager; + return informationManager.get(); } + InformationManager(); + virtual ~InformationManager(); + void updateDaemonList(Net::Connection *con); bool isUpdating() const { diff --git a/src/Common/Backends/SystemBackendPosix.cpp b/src/Common/Backends/SystemBackendPosix.cpp index 62067c2..bff72db 100644 --- a/src/Common/Backends/SystemBackendPosix.cpp +++ b/src/Common/Backends/SystemBackendPosix.cpp @@ -131,7 +131,7 @@ void SystemBackendPosix::childHandler(int) { while((n = read(handle, buffer, sizeof(buffer))) > 0) output += std::string(buffer, n); - Net::FdManager::getFdManager()->unregisterFd(handle); + Net::FdManager::get()->unregisterFd(handle); close(handle); it2->second(status, output); @@ -266,7 +266,7 @@ bool SystemBackendPosix::execWithOutput(const sigc::slotregisterFd(pipeHandles[0], sigc::bind(sigc::ptr_fun(&SystemBackendPosix::outputHandler), pid), POLLIN); + Net::FdManager::get()->registerFd(pipeHandles[0], sigc::bind(sigc::ptr_fun(&SystemBackendPosix::outputHandler), pid), POLLIN); } dup2(saveStdout, STDOUT_FILENO); // restore old stdout diff --git a/src/Common/Initializable.cpp b/src/Common/Initializable.cpp index 9e7a49e..65893a1 100644 --- a/src/Common/Initializable.cpp +++ b/src/Common/Initializable.cpp @@ -28,12 +28,17 @@ namespace Mad { namespace Common { std::stack Initializable::initializedObjects; - +bool Initializable::logInit = false; void Initializable::init() { if(initialized) return; + if(!logInit) { + logInit = true; + LogManager::get(); + } + if(initializing) { Logger::log(Logger::CRITICAL, "Fatal initialization error: cyclic dependencies."); std::terminate(); @@ -41,9 +46,6 @@ void Initializable::init() { initializing = true; - if(!(LogManager::get()->isInitialized() || LogManager::get()->isInitializing())) - LogManager::get()->init(); - doInit(); Configurable *c = dynamic_cast(this); @@ -68,6 +70,8 @@ void Initializable::deinit() { initializedObjects.pop(); } + + logInit = false; } } diff --git a/src/Common/Initializable.h b/src/Common/Initializable.h index 6f67678..27ab5bf 100644 --- a/src/Common/Initializable.h +++ b/src/Common/Initializable.h @@ -28,6 +28,7 @@ namespace Common { class Initializable { private: static std::stack initializedObjects; + static bool logInit; bool initializing; bool initialized; diff --git a/src/Common/LogManager.cpp b/src/Common/LogManager.cpp index d974d7c..09cdd10 100644 --- a/src/Common/LogManager.cpp +++ b/src/Common/LogManager.cpp @@ -26,7 +26,7 @@ namespace Mad { namespace Common { -LogManager LogManager::logManager; +SingletonPtr LogManager::logManager; bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) { diff --git a/src/Common/LogManager.h b/src/Common/LogManager.h index 22a8c75..7c0f1e0 100644 --- a/src/Common/LogManager.h +++ b/src/Common/LogManager.h @@ -23,6 +23,7 @@ #include "Configurable.h" #include "Logger.h" #include "RemoteLogger.h" +#include "SingletonPtr.h" #include "SharedPtr.h" #include @@ -52,7 +53,7 @@ class LogManager : public Configurable { std::string source; }; - static LogManager logManager; + static SingletonPtr logManager; std::set > loggers; std::set > remoteLoggers; @@ -60,13 +61,13 @@ class LogManager : public Configurable { std::auto_ptr > messageQueue; std::auto_ptr > remoteMessageQueue; - LogManager() : messageQueue(new std::queue()), remoteMessageQueue(new std::queue()) {} - protected: virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled); virtual void configFinished(); public: + LogManager() : messageQueue(new std::queue()), remoteMessageQueue(new std::queue()) {} + void log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message); void log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message, const std::string &source); @@ -87,7 +88,7 @@ class LogManager : public Configurable { } static LogManager *get() { - return &logManager; + return logManager.get(); } }; diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index 20daebb..7375c64 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -7,4 +7,4 @@ libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHa noinst_HEADERS = ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h Initializable.h \ Logger.h LoggerBase.h LogManager.h RemoteLogger.h Request.h RequestBase.h \ - RequestHandler.h RequestManager.h SharedPtr.h SystemBackend.h Tokenizer.h + RequestHandler.h RequestManager.h SharedPtr.h SingletonPtr.h SystemBackend.h Tokenizer.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 3bd9f4a..b011031 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -207,7 +207,7 @@ libcommon_la_SOURCES = ConfigEntry.cpp ConfigManager.cpp Exception.cpp Initializ libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la noinst_HEADERS = ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h Initializable.h \ Logger.h LoggerBase.h LogManager.h RemoteLogger.h Request.h RequestBase.h \ - RequestHandler.h RequestManager.h SharedPtr.h SystemBackend.h Tokenizer.h + RequestHandler.h RequestManager.h SharedPtr.h SingletonPtr.h SystemBackend.h Tokenizer.h all: all-recursive diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index e15ff67..3dfd1cf 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -30,7 +30,7 @@ namespace Mad { namespace Common { -RequestManager RequestManager::requestManager; +SingletonPtr RequestManager::requestManager; RequestManager::RequestMap::~RequestMap() { @@ -149,7 +149,7 @@ void RequestManager::unregisterPacketType(Net::Packet::Type type) { requestHandlerFactories.erase(it); } -RequestManager::RequestManager() : core(false) { +RequestManager::RequestManager() : core(false), requestId(-1) { registerPacketType(Net::Packet::DISCONNECT); } diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h index 723e619..0bc8080 100644 --- a/src/Common/RequestManager.h +++ b/src/Common/RequestManager.h @@ -20,7 +20,7 @@ #ifndef MAD_COMMON_REQUESTMANAGER_H_ #define MAD_COMMON_REQUESTMANAGER_H_ -#include "Initializable.h" +#include "SingletonPtr.h" #include #include @@ -32,7 +32,7 @@ namespace Common { class RequestBase; class RequestHandler; -class RequestManager : public Initializable { +class RequestManager { private: class RequestMap : private std::map { private: @@ -65,7 +65,7 @@ class RequestManager : public Initializable { } }; - static RequestManager requestManager; + static SingletonPtr requestManager; std::map requestMaps; bool core; @@ -81,19 +81,24 @@ class RequestManager : public Initializable { RequestManager(const RequestManager &o); RequestManager& operator=(const RequestManager &o); - RequestManager(); - void receiveHandler(Net::Connection *connection, const Net::Packet &packet); public: static RequestManager* get() { - return &requestManager; + return requestManager.get(); } + RequestManager(); + bool isCore() const {return core;} - void setCore(bool newCore) {core = newCore;} + void setCore(bool newCore) { + core = newCore; - virtual void doInit() {requestId = core ? -2 : -1;} + if(core) + requestId &= ~0x01; + else + requestId |= 0x01; + } void registerConnection(Net::Connection *connection); void unregisterConnection(Net::Connection *connection); diff --git a/src/Common/SingletonPtr.h b/src/Common/SingletonPtr.h new file mode 100644 index 0000000..ea4eda1 --- /dev/null +++ b/src/Common/SingletonPtr.h @@ -0,0 +1,63 @@ +/* + * SingletonPtr.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_SINGLETONPTR_H_ +#define MAD_COMMON_SINGLETONPTR_H_ + +#include "Initializable.h" + +namespace Mad { +namespace Common { + +template +class SingletonPtr : public virtual Initializable { + private: + T *ptr; + + protected: + virtual void doInit() { + if(ptr) + delete ptr; + + ptr = new T(); + + Initializable *in = dynamic_cast(ptr); + if(in) + in->init(); + } + + public: + SingletonPtr() : ptr(0) {} + + virtual ~SingletonPtr() { + delete ptr; + } + + T *get() { + if(!ptr) + init(); + + return ptr; + } +}; + +} +} + +#endif /* MAD_COMMON_SINGLETONPTR_H_ */ diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 7df9e8b..833e2e6 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -42,7 +42,7 @@ namespace Mad { namespace Core { -ConnectionManager ConnectionManager::connectionManager; +Common::SingletonPtr ConnectionManager::connectionManager; void ConnectionManager::updateState(const std::string &name, Common::HostInfo::State state) { @@ -129,9 +129,8 @@ void ConnectionManager::configFinished() { } } -void ConnectionManager::doInit() { +ConnectionManager::ConnectionManager() { Common::RequestManager::get()->setCore(true); - Common::RequestManager::get()->init(); Common::RequestManager::get()->registerPacketType(Net::Packet::FS_INFO); Common::RequestManager::get()->registerPacketType(Net::Packet::STATUS); @@ -147,7 +146,7 @@ void ConnectionManager::doInit() { Net::Connection::init(); } -void ConnectionManager::doDeinit() { +ConnectionManager::~ConnectionManager() { for(std::list::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) delete *con; @@ -183,7 +182,7 @@ void ConnectionManager::handleConnections(std::list& con void ConnectionManager::run() { // TODO Logging - Net::FdManager::getFdManager()->run(); + Net::FdManager::get()->run(); handleConnections(daemonConnections); handleConnections(clientConnections); diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index c17bae5..86c342c 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -27,8 +27,8 @@ #include #include #include -#include #include +#include #include @@ -45,7 +45,7 @@ namespace Core { class ConnectionManager : public Common::Configurable { private: - static ConnectionManager connectionManager; + static Common::SingletonPtr connectionManager; std::string x509TrustFile, x509CrlFile, x509CertFile, x509KeyFile; @@ -62,22 +62,20 @@ class ConnectionManager : public Common::Configurable { ConnectionManager(const ConnectionManager &o); ConnectionManager& operator=(const ConnectionManager &o); - ConnectionManager() {} - void handleConnections(std::list &connections); void updateState(const std::string &name, Common::HostInfo::State state); protected: - virtual void doInit(); - virtual void doDeinit(); - virtual bool handleConfigEntry(const Common::ConfigEntry &entry, bool handled); virtual void configFinished(); public: + ConnectionManager(); + virtual ~ConnectionManager(); + static ConnectionManager* get() { - return &connectionManager; + return connectionManager.get(); } void run(); diff --git a/src/Net/ClientConnection.cpp b/src/Net/ClientConnection.cpp index 25673fd..75a9ad6 100644 --- a/src/Net/ClientConnection.cpp +++ b/src/Net/ClientConnection.cpp @@ -98,7 +98,7 @@ void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Com gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred); gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t)sock); - FdManager::getFdManager()->registerFd(sock, sigc::mem_fun(this, &Connection::sendReceive)); + FdManager::get()->registerFd(sock, sigc::mem_fun(this, &Connection::sendReceive)); handshake(); } diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 5454dcf..7f8d9a9 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -288,7 +288,7 @@ void Connection::doDisconnect() { if(!isConnected()) return; - FdManager::getFdManager()->unregisterFd(sock); + FdManager::get()->unregisterFd(sock); shutdown(sock, SHUT_RDWR); close(sock); @@ -304,7 +304,7 @@ void Connection::updateEvents() const { if(state == HANDSHAKE || state == BYE) events = ((gnutls_record_get_direction(session) == 0) ? POLLIN : POLLOUT); - FdManager::getFdManager()->setFdEvents(sock, events); + FdManager::get()->setFdEvents(sock, events); } } diff --git a/src/Net/FdManager.h b/src/Net/FdManager.h index 1308a79..2e6fc66 100644 --- a/src/Net/FdManager.h +++ b/src/Net/FdManager.h @@ -44,7 +44,7 @@ class FdManager { public: virtual ~FdManager(); - static FdManager *getFdManager() {return &fdManager;} + static FdManager *get() {return &fdManager;} bool registerFd(int fd, const sigc::slot &handler, short events = 0); bool unregisterFd(int fd); diff --git a/src/Net/Listener.cpp b/src/Net/Listener.cpp index ed820d0..c4c5194 100644 --- a/src/Net/Listener.cpp +++ b/src/Net/Listener.cpp @@ -78,7 +78,7 @@ Listener::Listener(const std::string &x905CertFile0, const std::string &x905KeyF throw Common::Exception("listen()", Common::Exception::INTERNAL_ERRNO, errno); } - FdManager::getFdManager()->registerFd(sock, sigc::mem_fun(this, &Listener::acceptHandler), POLLIN); + FdManager::get()->registerFd(sock, sigc::mem_fun(this, &Listener::acceptHandler), POLLIN); } Listener::~Listener() { diff --git a/src/Net/ServerConnection.cpp b/src/Net/ServerConnection.cpp index 05cc861..97e2ae4 100644 --- a/src/Net/ServerConnection.cpp +++ b/src/Net/ServerConnection.cpp @@ -73,7 +73,7 @@ ServerConnection::ServerConnection(int sock0, const IPAddress &address, gnutls_d gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred); gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t)sock); - FdManager::getFdManager()->registerFd(sock, sigc::mem_fun(this, &Connection::sendReceive)); + FdManager::get()->registerFd(sock, sigc::mem_fun(this, &Connection::sendReceive)); handshake(); } diff --git a/src/mad.cpp b/src/mad.cpp index 54b25d0..4a4b405 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -47,7 +47,6 @@ int main() { Common::ConfigManager::get()->finish(); - Common::RequestManager::get()->init(); Common::RequestManager::get()->registerPacketType(Net::Packet::FS_INFO); Common::RequestManager::get()->registerPacketType(Net::Packet::STATUS); Common::RequestManager::get()->registerPacketType(Net::Packet::COMMAND_REBOOT); @@ -62,7 +61,7 @@ int main() { connection->connect(Net::IPAddress("127.0.0.1"), true); while(connection->isConnecting()) - Net::FdManager::getFdManager()->run(); + Net::FdManager::get()->run(); Common::RequestManager::get()->registerConnection(connection); @@ -76,7 +75,7 @@ int main() { Common::RequestManager::get()->sendRequest(connection, std::auto_ptr(new Daemon::Requests::IdentifyRequest("test", sigc::ptr_fun(requestFinished)))); while(connection->isConnected()) - Net::FdManager::getFdManager()->run(); + Net::FdManager::get()->run(); Common::LogManager::get()->unregisterLogger(networkLogger); diff --git a/src/madc.cpp b/src/madc.cpp index 7397972..5dcb6ad 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -54,7 +54,7 @@ static void handleCommand(char *cmd) { if(Client::CommandManager::get()->requestsActive()) { rl_callback_handler_remove(); - Net::FdManager::getFdManager()->setFdEvents(STDIN_FILENO, 0); + Net::FdManager::get()->setFdEvents(STDIN_FILENO, 0); } } @@ -68,7 +68,7 @@ static void activateReadline() { return; rl_callback_handler_install("mad> ", handleCommand); - Net::FdManager::getFdManager()->setFdEvents(STDIN_FILENO, POLLIN); + Net::FdManager::get()->setFdEvents(STDIN_FILENO, POLLIN); } int main(int argc, char *argv[]) { @@ -79,11 +79,10 @@ int main(int argc, char *argv[]) { Net::Connection::init(); - Common::LogManager::get()->init(); + Common::LogManager::get(); Common::ConfigManager::get()->finish(); - Common::RequestManager::get()->init(); Net::ClientConnection *connection = new Net::ClientConnection; try { @@ -92,7 +91,7 @@ int main(int argc, char *argv[]) { std::cerr << "Connecting to " << argv[1] << "..." << std::flush; while(connection->isConnecting()) - Net::FdManager::getFdManager()->run(); + Net::FdManager::get()->run(); std::cerr << " connected." << std::endl; @@ -100,25 +99,24 @@ int main(int argc, char *argv[]) { std::cerr << "Receiving host list..." << std::flush; - Client::InformationManager::get()->init(); Client::InformationManager::get()->updateDaemonList(connection); while(Client::InformationManager::get()->isUpdating()) - Net::FdManager::getFdManager()->run(); + Net::FdManager::get()->run(); std::cerr << " done." << std::endl << std::endl; Client::CommandParser::get()->setConnection(connection); Client::CommandManager::get()->signalFinished().connect(sigc::ptr_fun(activateReadline)); - Net::FdManager::getFdManager()->registerFd(STDIN_FILENO, sigc::ptr_fun(charHandler)); + Net::FdManager::get()->registerFd(STDIN_FILENO, sigc::ptr_fun(charHandler)); activateReadline(); while(connection->isConnected()) - Net::FdManager::getFdManager()->run(); + Net::FdManager::get()->run(); - Net::FdManager::getFdManager()->unregisterFd(STDIN_FILENO); + Net::FdManager::get()->unregisterFd(STDIN_FILENO); Common::RequestManager::get()->unregisterConnection(connection); } -- cgit v1.2.3