summaryrefslogtreecommitdiffstats
path: root/src/Common
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common')
-rw-r--r--src/Common/Backends/SystemBackendPosix.cpp4
-rw-r--r--src/Common/Initializable.cpp12
-rw-r--r--src/Common/Initializable.h1
-rw-r--r--src/Common/LogManager.cpp2
-rw-r--r--src/Common/LogManager.h9
-rw-r--r--src/Common/Makefile.am2
-rw-r--r--src/Common/Makefile.in2
-rw-r--r--src/Common/RequestManager.cpp4
-rw-r--r--src/Common/RequestManager.h21
-rw-r--r--src/Common/SingletonPtr.h63
10 files changed, 97 insertions, 23 deletions
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::slot<void, int, const std::s
processesWOHandles.insert(std::make_pair(pid, pipeHandles[0]));
processesWOOutput.insert(std::make_pair(pid, std::string()));
- Net::FdManager::getFdManager()->registerFd(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*> 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<Configurable*>(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<Initializable*> 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::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 <memory>
@@ -52,7 +53,7 @@ class LogManager : public Configurable {
std::string source;
};
- static LogManager logManager;
+ static SingletonPtr<LogManager> logManager;
std::set<SharedPtr<Logger> > loggers;
std::set<SharedPtr<RemoteLogger> > remoteLoggers;
@@ -60,13 +61,13 @@ class LogManager : public Configurable {
std::auto_ptr<std::queue<Message> > messageQueue;
std::auto_ptr<std::queue<RemoteMessage> > remoteMessageQueue;
- LogManager() : messageQueue(new std::queue<Message>()), remoteMessageQueue(new std::queue<RemoteMessage>()) {}
-
protected:
virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled);
virtual void configFinished();
public:
+ LogManager() : messageQueue(new std::queue<Message>()), remoteMessageQueue(new std::queue<RemoteMessage>()) {}
+
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;
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<RequestHandlers::DisconnectRequestHandler>(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 <Net/Connection.h>
#include <map>
@@ -32,7 +32,7 @@ namespace Common {
class RequestBase;
class RequestHandler;
-class RequestManager : public Initializable {
+class RequestManager {
private:
class RequestMap : private std::map<uint16_t,RequestHandler*> {
private:
@@ -65,7 +65,7 @@ class RequestManager : public Initializable {
}
};
- static RequestManager requestManager;
+ static SingletonPtr<RequestManager> requestManager;
std::map<Net::Connection*,RequestMap*> 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 <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/>.
+ */
+
+#ifndef MAD_COMMON_SINGLETONPTR_H_
+#define MAD_COMMON_SINGLETONPTR_H_
+
+#include "Initializable.h"
+
+namespace Mad {
+namespace Common {
+
+template <typename T>
+class SingletonPtr : public virtual Initializable {
+ private:
+ T *ptr;
+
+ protected:
+ virtual void doInit() {
+ if(ptr)
+ delete ptr;
+
+ ptr = new T();
+
+ Initializable *in = dynamic_cast<Initializable*>(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_ */