summaryrefslogtreecommitdiffstats
path: root/src/Common
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common')
-rw-r--r--src/Common/Application.cpp38
-rw-r--r--src/Common/Application.h59
-rw-r--r--src/Common/CMakeLists.txt2
-rw-r--r--src/Common/ClientConnection.cpp2
-rw-r--r--src/Common/ClientConnection.h2
-rw-r--r--src/Common/Connection.h2
-rw-r--r--src/Common/Module.h37
-rw-r--r--src/Common/ModuleManager.cpp48
-rw-r--r--src/Common/ModuleManager.h31
-rw-r--r--src/Common/Request.h2
-rw-r--r--src/Common/RequestHandler.cpp6
-rw-r--r--src/Common/RequestHandler.h11
-rw-r--r--src/Common/RequestHandlerGroup.h2
-rw-r--r--src/Common/RequestHandlers/DisconnectRequestHandler.cpp3
-rw-r--r--src/Common/RequestHandlers/DisconnectRequestHandler.h3
-rw-r--r--src/Common/RequestHandlers/FSInfoRequestHandler.cpp2
-rw-r--r--src/Common/RequestHandlers/FSInfoRequestHandler.h4
-rw-r--r--src/Common/RequestHandlers/SimpleRequestHandler.cpp3
-rw-r--r--src/Common/RequestHandlers/SimpleRequestHandler.h8
-rw-r--r--src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp41
-rw-r--r--src/Common/RequestHandlers/SimpleRequestHandlerGroup.h19
-rw-r--r--src/Common/RequestHandlers/StatusRequestHandler.cpp6
-rw-r--r--src/Common/RequestHandlers/StatusRequestHandler.h4
-rw-r--r--src/Common/RequestManager.cpp22
-rw-r--r--src/Common/RequestManager.h34
-rw-r--r--src/Common/Requests/DisconnectRequest.h3
-rw-r--r--src/Common/Requests/FSInfoRequest.h2
-rw-r--r--src/Common/Requests/GroupListRequest.h2
-rw-r--r--src/Common/Requests/GroupUserListRequest.h2
-rw-r--r--src/Common/Requests/IdentifyRequest.h2
-rw-r--r--src/Common/Requests/SimpleRequest.h2
-rw-r--r--src/Common/Requests/StatusRequest.h2
-rw-r--r--src/Common/Requests/UserGroupListRequest.h2
-rw-r--r--src/Common/Requests/UserInfoRequest.h3
-rw-r--r--src/Common/Requests/UserListRequest.h2
-rw-r--r--src/Common/SystemManager.cpp3
-rw-r--r--src/Common/SystemManager.h12
37 files changed, 293 insertions, 135 deletions
diff --git a/src/Common/Application.cpp b/src/Common/Application.cpp
new file mode 100644
index 0000000..950e61b
--- /dev/null
+++ b/src/Common/Application.cpp
@@ -0,0 +1,38 @@
+/*
+ * Application.cpp
+ *
+ * Copyright (C) 2009 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 "Application.h"
+#include "ModuleManager.h"
+#include "RequestManager.h"
+#include "SystemManager.h"
+
+namespace Mad {
+namespace Common {
+
+Application::Application(bool server) : moduleManager(new ModuleManager(this)), requestManager(new RequestManager(this, server)),
+systemManager(new SystemManager) {}
+
+Application::~Application() {
+ delete systemManager;
+ delete requestManager;
+ delete moduleManager;
+}
+
+}
+}
diff --git a/src/Common/Application.h b/src/Common/Application.h
new file mode 100644
index 0000000..332e4c7
--- /dev/null
+++ b/src/Common/Application.h
@@ -0,0 +1,59 @@
+/*
+ * Application.h
+ *
+ * Copyright (C) 2009 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_APPLICATION_H_
+#define MAD_COMMON_APPLICATION_H_
+
+#include <Core/Application.h>
+
+namespace Mad {
+namespace Common {
+
+class ModuleManager;
+class RequestManager;
+class SystemManager;
+
+class Application : public Core::Application {
+ private:
+ ModuleManager *moduleManager;
+ RequestManager *requestManager;
+ SystemManager *systemManager;
+
+ protected:
+ Application(bool server);
+ virtual ~Application();
+
+ public:
+ ModuleManager* getModuleManager() const {
+ return moduleManager;
+ }
+
+ RequestManager* getRequestManager() const {
+ return requestManager;
+ }
+
+ SystemManager* getSystemManager() const {
+ return systemManager;
+ }
+};
+
+}
+}
+
+#endif /* MAD_COMMON_APPLICATION_H_ */
diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt
index 078df71..4a4eb8e 100644
--- a/src/Common/CMakeLists.txt
+++ b/src/Common/CMakeLists.txt
@@ -6,11 +6,13 @@ include_directories(${INCLUDES})
link_directories(${LTDL_LIBRARY_DIR})
add_library(Common
+ Application.cpp Application.h
Base64Encoder.cpp Base64Encoder.h
ClientConnection.cpp ClientConnection.h
Connection.cpp Connection.h
GroupInfo.h
HostInfo.h
+ Module.h
ModuleManager.cpp ModuleManager.h
Request.cpp Request.h
RequestHandler.cpp RequestHandler.h
diff --git a/src/Common/ClientConnection.cpp b/src/Common/ClientConnection.cpp
index ba558f4..4438c8f 100644
--- a/src/Common/ClientConnection.cpp
+++ b/src/Common/ClientConnection.cpp
@@ -24,7 +24,7 @@
namespace Mad {
namespace Common {
-ClientConnection::ClientConnection() : connection(new Net::ClientConnection) {
+ClientConnection::ClientConnection(Core::Application *application) : Connection(application), connection(new Net::ClientConnection(application)) {
connection->connectSignalReceive(boost::bind(&ClientConnection::receive, this, _1));
}
diff --git a/src/Common/ClientConnection.h b/src/Common/ClientConnection.h
index 305df44..ab0dbb8 100644
--- a/src/Common/ClientConnection.h
+++ b/src/Common/ClientConnection.h
@@ -41,7 +41,7 @@ class ClientConnection : public Connection {
virtual bool send(const Net::Packet &packet);
public:
- ClientConnection();
+ ClientConnection(Core::Application *application);
virtual ~ClientConnection() {}
void connect(const boost::asio::ip::tcp::endpoint &address) throw(Core::Exception);
diff --git a/src/Common/Connection.h b/src/Common/Connection.h
index 6700574..9680452 100644
--- a/src/Common/Connection.h
+++ b/src/Common/Connection.h
@@ -42,7 +42,7 @@ class Connection : private boost::noncopyable {
Core::Signals::Signal2<boost::shared_ptr<const XmlPacket>, uint16_t> signalReceive;
protected:
- Connection() : authenticated(0) {}
+ Connection(Core::Application *application) : authenticated(0), signalReceive(application) {}
void receive(boost::shared_ptr<Net::Packet> packet);
diff --git a/src/Common/Module.h b/src/Common/Module.h
new file mode 100644
index 0000000..877649c
--- /dev/null
+++ b/src/Common/Module.h
@@ -0,0 +1,37 @@
+/*
+ * Module.h
+ *
+ * Copyright (C) 2009 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_MODULE_H_
+#define MAD_COMMON_MODULE_H_
+
+namespace Mad {
+namespace Common {
+
+class Module {
+ protected:
+ Module() {}
+
+ public:
+ virtual ~Module() {}
+};
+
+}
+}
+
+#endif /* MAD_COMMON_MODULE_H_ */
diff --git a/src/Common/ModuleManager.cpp b/src/Common/ModuleManager.cpp
index bec3618..738532f 100644
--- a/src/Common/ModuleManager.cpp
+++ b/src/Common/ModuleManager.cpp
@@ -21,10 +21,10 @@
#include <config.h>
#include "ModuleManager.h"
+#include "Application.h"
#include <Core/ConfigEntry.h>
-#include <Core/Logger.h>
-
+#include <Core/ConfigManager.h>
//extern const lt_dlsymlist lt_preloaded_symbols[];
@@ -32,24 +32,25 @@
namespace Mad {
namespace Common {
-ModuleManager ModuleManager::moduleManager;
-
-
-int ModuleManager::preopenCallback(lt_dlhandle handle) {
+/*int ModuleManager::preopenCallback(lt_dlhandle handle) {
moduleManager.modules.insert(std::make_pair(lt_dlgetinfo(handle)->name, std::make_pair(handle, false)));
return 0;
-}
+}*/
-void ModuleManager::doInit() {
+ModuleManager::ModuleManager(Application *application0) : application(application0) {
lt_dlinit();
//lt_dlpreload_default(lt_preloaded_symbols);
- lt_dlpreload(0);
- lt_dlpreload_open("@PROGRAM@", &ModuleManager::preopenCallback);
+ //lt_dlpreload(0);
+ //lt_dlpreload_open("@PROGRAM@", &ModuleManager::preopenCallback);
+
+ application->getConfigManager()->registerConfigurable(this);
}
-void ModuleManager::doDeinit() {
+ModuleManager::~ModuleManager() {
+ application->getConfigManager()->unregisterConfigurable(this);
+
while(!moduleOrder.empty()) {
unloadModule(moduleOrder.top());
moduleOrder.pop();
@@ -64,7 +65,7 @@ bool ModuleManager::handleConfigEntry(const Core::ConfigEntry &entry, bool handl
if(entry[0].getKey().matches("LoadModule")) {
if(!loadModule(entry[0][0].c_str()))
- Core::Logger::logf(Core::Logger::ERROR, "Can't load module '%s'.", entry[0][0].c_str());
+ application->logf(Core::LoggerBase::ERROR, "Can't load module '%s'.", entry[0][0].c_str());
return true;
}
@@ -73,7 +74,7 @@ bool ModuleManager::handleConfigEntry(const Core::ConfigEntry &entry, bool handl
}
lt_dlhandle ModuleManager::loadModule(const std::string &name) {
- std::map<std::string, std::pair<lt_dlhandle, bool> >::iterator mod = modules.find(name);
+ std::map<std::string, std::pair<lt_dlhandle, Module*> >::iterator mod = modules.find(name);
if(mod == modules.end()) {
lt_dlhandle handle = lt_dlopen((name + MODULE_SUFFIX).c_str());
@@ -81,28 +82,25 @@ lt_dlhandle ModuleManager::loadModule(const std::string &name) {
if(!handle)
return 0;
- mod = modules.insert(std::make_pair(lt_dlgetinfo(handle)->name, std::make_pair(handle, false))).first;
+ mod = modules.insert(std::make_pair(lt_dlgetinfo(handle)->name, std::make_pair(handle, (Module*)0))).first;
}
if(!mod->second.second) {
- void (*initFun)() = (void(*)())lt_dlsym(mod->second.first, (name + "_init").c_str());
+ ModuleLoadFunc loader = (ModuleLoadFunc)lt_dlsym(mod->second.first, (name + "_create").c_str());
+
+ if(!loader)
+ return 0;
- if(initFun)
- (*initFun)();
+ mod->second.second = loader(application);
- mod->second.second = true;
- moduleOrder.push(name);
+ if(mod->second.second)
+ moduleOrder.push(name);
}
return mod->second.first;
}
void ModuleManager::unloadModule(const std::string &name) {
- void (*deinitFun)();
- deinitFun = (void(*)())lt_dlsym(modules[name].first, (name + "_deinit").c_str());
-
- if(deinitFun)
- (*deinitFun)();
-
+ delete modules[name].second;
lt_dlclose(modules[name].first);
modules.erase(name);
diff --git a/src/Common/ModuleManager.h b/src/Common/ModuleManager.h
index de5535f..eb8c800 100644
--- a/src/Common/ModuleManager.h
+++ b/src/Common/ModuleManager.h
@@ -20,8 +20,10 @@
#ifndef MAD_COMMON_MODULEMANAGER_H_
#define MAD_COMMON_MODULEMANAGER_H_
+#include "Module.h"
#include <Core/Configurable.h>
-#include <Core/Initializable.h>
+
+#include <boost/noncopyable.hpp>
#include <map>
#include <stack>
@@ -32,32 +34,31 @@
namespace Mad {
namespace Common {
-class ModuleManager : public Core::Configurable, public Core::Initializable {
+class Application;
+
+class ModuleManager : public Core::Configurable, private boost::noncopyable {
private:
- static ModuleManager moduleManager;
+ friend class Application;
+
+ typedef Module* (*ModuleLoadFunc)(Application*);
+
+ Application *application;
- std::map<std::string, std::pair<lt_dlhandle, bool> > modules;
+ std::map<std::string, std::pair<lt_dlhandle, Module*> > modules;
std::stack<std::string> moduleOrder;
- static int preopenCallback(lt_dlhandle handle);
+ //static int preopenCallback(lt_dlhandle handle);
+
+ ModuleManager(Application *application0);
+ ~ModuleManager();
void unloadModule(const std::string &name);
protected:
- virtual void doInit();
- virtual void doDeinit();
-
virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled);
public:
lt_dlhandle loadModule(const std::string &name);
-
- static ModuleManager* get() {
- if(!moduleManager.isInitialized())
- moduleManager.init();
-
- return &moduleManager;
- }
};
}
diff --git a/src/Common/Request.h b/src/Common/Request.h
index ac59e45..d1b5851 100644
--- a/src/Common/Request.h
+++ b/src/Common/Request.h
@@ -47,7 +47,7 @@ class Request : public RequestHandler {
Core::Signals::Signal2<boost::shared_ptr<const XmlPacket>, Core::Exception> finished;
protected:
- Request() : isFinished(false) {}
+ Request(Application *application) : RequestHandler(application), isFinished(false), finished(application) {}
void signalFinished(boost::shared_ptr<const XmlPacket> pkt, Core::Exception exp) {
{
diff --git a/src/Common/RequestHandler.cpp b/src/Common/RequestHandler.cpp
index 218155d..91ddce6 100644
--- a/src/Common/RequestHandler.cpp
+++ b/src/Common/RequestHandler.cpp
@@ -23,12 +23,14 @@
namespace Mad {
namespace Common {
+RequestHandler::RequestHandler(Application *application0) : application(application0), finished(application) {}
+
Connection* RequestHandler::getConnection() const {
- return RequestManager::get()->requestMap.getRequestInfo(this).first;
+ return getRequestManager()->requestMap.getRequestInfo(this).first;
}
void RequestHandler::sendPacket(const XmlPacket &packet) {
- std::pair<Connection*, boost::uint16_t> requestInfo = RequestManager::get()->requestMap.getRequestInfo(this);
+ std::pair<Connection*, boost::uint16_t> requestInfo = getRequestManager()->requestMap.getRequestInfo(this);
if(!requestInfo.first)
return;
diff --git a/src/Common/RequestHandler.h b/src/Common/RequestHandler.h
index 3deb8c0..096ff45 100644
--- a/src/Common/RequestHandler.h
+++ b/src/Common/RequestHandler.h
@@ -20,31 +20,32 @@
#ifndef MAD_COMMON_REQUESTHANDLER_H_
#define MAD_COMMON_REQUESTHANDLER_H_
+#include "Application.h"
#include "Connection.h"
#include "XmlPacket.h"
#include <Core/Signals.h>
#include <boost/cstdint.hpp>
-#include <boost/thread/locks.hpp>
-#include <boost/thread/mutex.hpp>
namespace Mad {
namespace Common {
class Connection;
-class RequestManager;
class RequestHandler : private boost::noncopyable {
private:
friend class RequestManager;
- boost::mutex mutex;
+ Application *application;
Core::Signals::Signal0 finished;
protected:
- RequestHandler() {}
+ Application* getApplication() const {return application;}
+ RequestManager* getRequestManager() const {return application->getRequestManager();}
+
+ RequestHandler(Application *application0);
void signalFinished() {
finished.emit();
diff --git a/src/Common/RequestHandlerGroup.h b/src/Common/RequestHandlerGroup.h
index c9f57eb..135111a 100644
--- a/src/Common/RequestHandlerGroup.h
+++ b/src/Common/RequestHandlerGroup.h
@@ -35,7 +35,7 @@ class RequestHandlerGroup {
public:
virtual const std::set<std::string>& getPacketTypes() = 0;
- virtual boost::shared_ptr<RequestHandler> createRequestHandler(const std::string &type) = 0;
+ virtual boost::shared_ptr<RequestHandler> createRequestHandler(Application *application, const std::string &type) = 0;
virtual ~RequestHandlerGroup() {}
};
diff --git a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp
index 6c89ab2..11256b1 100644
--- a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp
+++ b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp
@@ -19,7 +19,6 @@
#include "DisconnectRequestHandler.h"
#include <Core/Exception.h>
-#include <Core/Logger.h>
namespace Mad {
namespace Common {
@@ -27,7 +26,7 @@ namespace RequestHandlers {
void DisconnectRequestHandler::handlePacket(boost::shared_ptr<const XmlPacket> packet) {
if(packet->getType() != "Disconnect") {
- Core::Logger::log(Core::Logger::ERROR, "Received an unexpected packet.");
+ getApplication()->log(Core::LoggerBase::ERROR, "Received an unexpected packet.");
XmlPacket ret;
ret.setType("Error");
diff --git a/src/Common/RequestHandlers/DisconnectRequestHandler.h b/src/Common/RequestHandlers/DisconnectRequestHandler.h
index 963f603..d94bc73 100644
--- a/src/Common/RequestHandlers/DisconnectRequestHandler.h
+++ b/src/Common/RequestHandlers/DisconnectRequestHandler.h
@@ -29,6 +29,9 @@ namespace RequestHandlers {
class DisconnectRequestHandler : public RequestHandler {
protected:
virtual void handlePacket(boost::shared_ptr<const XmlPacket> packet);
+
+ public:
+ DisconnectRequestHandler(Application *application) : RequestHandler(application) {}
};
}
diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp
index 7ac4644..c036f7b 100644
--- a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp
+++ b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp
@@ -28,7 +28,7 @@ void FSInfoRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPack
// TODO Require authentication
std::vector<SystemManager::FSInfo> fsInfo;
- SystemManager::get()->getFSInfo(&fsInfo);
+ getApplication()->getSystemManager()->getFSInfo(&fsInfo);
ret->setType("OK");
ret->addList("filesystems");
diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.h b/src/Common/RequestHandlers/FSInfoRequestHandler.h
index 167e116..27a1af3 100644
--- a/src/Common/RequestHandlers/FSInfoRequestHandler.h
+++ b/src/Common/RequestHandlers/FSInfoRequestHandler.h
@@ -28,10 +28,10 @@ namespace RequestHandlers {
class FSInfoRequestHandler : public SimpleRequestHandler {
private:
- static void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret);
+ void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret);
public:
- FSInfoRequestHandler() : SimpleRequestHandler("FSInfo", &FSInfoRequestHandler::handleRequest) {}
+ FSInfoRequestHandler(Application *application) : SimpleRequestHandler(application, "FSInfo", boost::bind(&FSInfoRequestHandler::handleRequest, this, _1, _2)) {}
};
}
diff --git a/src/Common/RequestHandlers/SimpleRequestHandler.cpp b/src/Common/RequestHandlers/SimpleRequestHandler.cpp
index b6de0f4..b6a22bf 100644
--- a/src/Common/RequestHandlers/SimpleRequestHandler.cpp
+++ b/src/Common/RequestHandlers/SimpleRequestHandler.cpp
@@ -19,7 +19,6 @@
#include "SimpleRequestHandler.h"
-#include <Core/Logger.h>
#include <Core/Exception.h>
namespace Mad {
@@ -28,7 +27,7 @@ namespace RequestHandlers {
void SimpleRequestHandler::handlePacket(boost::shared_ptr<const XmlPacket> packet) {
if(packet->getType() != type) {
- Core::Logger::log(Core::Logger::ERROR, "Received an unexpected packet.");
+ getApplication()->log(Core::LoggerBase::ERROR, "Received an unexpected packet.");
XmlPacket ret;
ret.setType("Error");
diff --git a/src/Common/RequestHandlers/SimpleRequestHandler.h b/src/Common/RequestHandlers/SimpleRequestHandler.h
index 5d388d3..0c295b5 100644
--- a/src/Common/RequestHandlers/SimpleRequestHandler.h
+++ b/src/Common/RequestHandlers/SimpleRequestHandler.h
@@ -26,20 +26,16 @@ namespace Mad {
namespace Common {
namespace RequestHandlers {
-class SimpleRequestHandlerGroup;
-
class SimpleRequestHandler : public RequestHandler {
private:
- friend class SimpleRequestHandlerGroup;
-
std::string type;
boost::function2<void, boost::shared_ptr<const XmlPacket>, XmlPacket*> handler;
protected:
virtual void handlePacket(boost::shared_ptr<const XmlPacket> packet);
- SimpleRequestHandler(const std::string &type0, const boost::function2<void, boost::shared_ptr<const XmlPacket>, XmlPacket*> &handler0)
- : type(type0), handler(handler0) {}
+ SimpleRequestHandler(Application *application, const std::string &type0, const boost::function2<void, boost::shared_ptr<const XmlPacket>, XmlPacket*> &handler0)
+ : RequestHandler(application), type(type0), handler(handler0) {}
};
}
diff --git a/src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp
index 9c80e23..845021d 100644
--- a/src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp
+++ b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp
@@ -20,17 +20,52 @@
#include "SimpleRequestHandlerGroup.h"
#include "SimpleRequestHandler.h"
+#include <Core/Exception.h>
+
namespace Mad {
namespace Common {
namespace RequestHandlers {
-boost::shared_ptr<RequestHandler> SimpleRequestHandlerGroup::createRequestHandler(const std::string &type) {
- std::map<std::string, boost::function2<void, boost::shared_ptr<const XmlPacket>, XmlPacket*> >::iterator handler = handlers.find(type);
+void SimpleRequestHandlerGroup::GroupRequestHandler::handlePacket(boost::shared_ptr<const XmlPacket> packet) {
+ if(packet->getType() != type) {
+ getApplication()->log(Core::LoggerBase::ERROR, "Received an unexpected packet.");
+
+ XmlPacket ret;
+ ret.setType("Error");
+ ret.add("ErrorCode", Core::Exception::UNEXPECTED_PACKET);
+
+ sendPacket(ret);
+
+ signalFinished();
+ return;
+ }
+
+ // TODO Require authentication
+
+ XmlPacket ret;
+
+ try {
+ handler(packet, &ret, getConnection());
+ }
+ catch(Core::Exception e) {
+ ret.setType("Error");
+ ret.add("ErrorCode", e.getErrorCode());
+ ret.add("SubCode", e.getSubCode());
+ ret.add("SubSubCode", e.getSubSubCode());
+ ret.add("Where", e.getWhere());
+ }
+
+ sendPacket(ret);
+ signalFinished();
+}
+
+boost::shared_ptr<RequestHandler> SimpleRequestHandlerGroup::createRequestHandler(Application *application, const std::string &type) {
+ std::map<std::string, boost::function3<void, boost::shared_ptr<const XmlPacket>, XmlPacket*, Connection*> >::iterator handler = handlers.find(type);
if(handler == handlers.end())
return boost::shared_ptr<RequestHandler>();
else
- return boost::shared_ptr<SimpleRequestHandler>(new SimpleRequestHandler(type, handler->second));
+ return boost::shared_ptr<GroupRequestHandler>(new GroupRequestHandler(application, type, handler->second));
}
}
diff --git a/src/Common/RequestHandlers/SimpleRequestHandlerGroup.h b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.h
index 86009f3..32e46ce 100644
--- a/src/Common/RequestHandlers/SimpleRequestHandlerGroup.h
+++ b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.h
@@ -28,11 +28,24 @@ namespace RequestHandlers {
class SimpleRequestHandlerGroup : public RequestHandlerGroup {
private:
+ class GroupRequestHandler : public RequestHandler {
+ private:
+ std::string type;
+ boost::function3<void, boost::shared_ptr<const XmlPacket>, XmlPacket*, Connection*> handler;
+
+ protected:
+ virtual void handlePacket(boost::shared_ptr<const XmlPacket> packet);
+
+ public:
+ GroupRequestHandler(Application *application, const std::string &type0, const boost::function3<void, boost::shared_ptr<const XmlPacket>, XmlPacket*, Connection*> &handler0)
+ : RequestHandler(application), type(type0), handler(handler0) {}
+ };
+
std::set<std::string> types;
- std::map<std::string, boost::function2<void, boost::shared_ptr<const XmlPacket>, XmlPacket*> > handlers;
+ std::map<std::string, boost::function3<void, boost::shared_ptr<const XmlPacket>, XmlPacket*, Connection*> > handlers;
protected:
- void registerHandler(const std::string &type, const boost::function2<void, boost::shared_ptr<const XmlPacket>, XmlPacket*> &handler) {
+ void registerHandler(const std::string &type, const boost::function3<void, boost::shared_ptr<const XmlPacket>, XmlPacket*, Connection*> &handler) {
types.insert(type);
handlers.insert(std::make_pair(type, handler));
}
@@ -44,7 +57,7 @@ class SimpleRequestHandlerGroup : public RequestHandlerGroup {
return types;
}
- virtual boost::shared_ptr<RequestHandler> createRequestHandler(const std::string &type);
+ virtual boost::shared_ptr<RequestHandler> createRequestHandler(Application *application, const std::string &type);
};
}
diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp
index 7abcd7f..50dbe11 100644
--- a/src/Common/RequestHandlers/StatusRequestHandler.cpp
+++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp
@@ -32,7 +32,7 @@ void StatusRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPack
try {
unsigned long uptime, idleTime;
- SystemManager::get()->getUptimeInfo(&uptime, &idleTime);
+ getApplication()->getSystemManager()->getUptimeInfo(&uptime, &idleTime);
ret->add("uptime", uptime);
ret->add("idleTime", idleTime);
@@ -42,7 +42,7 @@ void StatusRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPack
try {
unsigned long totalMem, freeMem, totalSwap, freeSwap;
- SystemManager::get()->getMemoryInfo(&totalMem, &freeMem, &totalSwap, &freeSwap);
+ getApplication()->getSystemManager()->getMemoryInfo(&totalMem, &freeMem, &totalSwap, &freeSwap);
ret->add("totalMem", totalMem);
ret->add("freeMem", freeMem);
@@ -55,7 +55,7 @@ void StatusRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPack
unsigned long currentLoad, nProcesses;
float loadAvg1, loadAvg5, loadAvg15;
- SystemManager::get()->getLoadInfo(&currentLoad, &nProcesses, &loadAvg1, &loadAvg5, &loadAvg15);
+ getApplication()->getSystemManager()->getLoadInfo(&currentLoad, &nProcesses, &loadAvg1, &loadAvg5, &loadAvg15);
ret->add("currentLoad", currentLoad);
ret->add("nProcesses", nProcesses);
diff --git a/src/Common/RequestHandlers/StatusRequestHandler.h b/src/Common/RequestHandlers/StatusRequestHandler.h
index 5583733..24158c9 100644
--- a/src/Common/RequestHandlers/StatusRequestHandler.h
+++ b/src/Common/RequestHandlers/StatusRequestHandler.h
@@ -28,10 +28,10 @@ namespace RequestHandlers {
class StatusRequestHandler : public SimpleRequestHandler {
private:
- static void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret);
+ void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret);
public:
- StatusRequestHandler() : SimpleRequestHandler("GetStatus", &StatusRequestHandler::handleRequest) {}
+ StatusRequestHandler(Application *application) : SimpleRequestHandler(application, "GetStatus", boost::bind(&StatusRequestHandler::handleRequest, this, _1, _2)) {}
};
}
diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp
index a8b817f..c8b27a2 100644
--- a/src/Common/RequestManager.cpp
+++ b/src/Common/RequestManager.cpp
@@ -19,16 +19,12 @@
#include "RequestManager.h"
#include "RequestHandlers/DisconnectRequestHandler.h"
-#include <Core/Logger.h>
#include <boost/bind.hpp>
namespace Mad {
namespace Common {
-RequestManager RequestManager::requestManager;
-
-
void RequestManager::RequestMap::unregisterConnection(Connection *connection) {
std::map<Connection*, IdMap>::iterator idMap = connectionMap.find(connection);
if(idMap == connectionMap.end())
@@ -96,21 +92,21 @@ void RequestManager::receiveHandler(Connection *connection, boost::shared_ptr<co
boost::shared_ptr<RequestHandler> request = requestMap.findRequest(connection, requestId);
if(request) {
lock.unlock();
- Core::ThreadManager::get()->pushWork(boost::bind(&RequestHandler::handlePacket, request, packet));
+ application->getThreadManager()->pushWork(boost::bind(&RequestHandler::handlePacket, request, packet));
return;
}
if(!requestMap.isConnectionRegistered(connection)) {
// TODO: Error
- Core::Logger::log(Core::Logger::ERROR, "Received a packet from an unregistered connection.");
+ application->log(Core::LoggerBase::ERROR, "Received a packet from an unregistered connection.");
return;
}
std::map<std::string, boost::shared_ptr<RequestHandlerGroup> >::iterator rgh = requestHandlerGroups.find(packet->getType());
if(rgh != requestHandlerGroups.end()) {
- request = rgh->second->createRequestHandler(packet->getType());
+ request = rgh->second->createRequestHandler(application, packet->getType());
if(request) {
{
@@ -122,7 +118,7 @@ void RequestManager::receiveHandler(Connection *connection, boost::shared_ptr<co
}
lock.unlock();
- Core::ThreadManager::get()->pushWork(boost::bind(&RequestHandler::handlePacket, request, packet));
+ application->getThreadManager()->pushWork(boost::bind(&RequestHandler::handlePacket, request, packet));
return;
}
@@ -130,7 +126,7 @@ void RequestManager::receiveHandler(Connection *connection, boost::shared_ptr<co
lock.unlock();
- Core::Logger::logf(Core::Logger::ERROR, "Received an unexpected packet with type '%s'.", packet->getType().c_str());
+ application->logf(Core::LoggerBase::ERROR, "Received an unexpected packet with type '%s'.", packet->getType().c_str());
XmlPacket ret;
ret.setType("Error");
@@ -143,20 +139,20 @@ bool RequestManager::sendRequest(Connection *connection, boost::shared_ptr<Reque
boost::unique_lock<boost::shared_mutex> lock(mutex);
if(!requestMap.isConnectionRegistered(connection)) {
- Core::Logger::log(Core::Logger::CRITICAL, "Trying to send a request over an unregistered connecion.");
+ application->log(Core::LoggerBase::CRITICAL, "Trying to send a request over an unregistered connecion.");
return false;
}
uint16_t requestId = _getUnusedRequestId(connection);
if(request->isFinished || !requestMap.addRequest(connection, requestId, request)) {
- Core::Logger::log(Core::Logger::CRITICAL, "Trying resend a request.");
+ application->log(Core::LoggerBase::CRITICAL, "Trying resend a request.");
return false;
}
request->connectSignalFinished(boost::bind(&RequestManager::handleRequestFinished, this, connection, requestId));
lock.unlock();
- Core::ThreadManager::get()->pushWork(boost::bind(&Request::sendRequest, request));
+ application->getThreadManager()->pushWork(boost::bind(&Request::sendRequest, request));
return true;
}
@@ -176,7 +172,7 @@ void RequestManager::registerConnection(Connection *connection) {
connection->connectSignalReceive(boost::bind(&RequestManager::receiveHandler, this, connection, _1, _2));
}
-RequestManager::RequestManager() : server(false), lastRequestId(-1) {
+RequestManager::RequestManager(Application *application0, bool server) : application(application0), lastRequestId(server ? 0 : -1) {
registerPacketType<RequestHandlers::DisconnectRequestHandler>("Disconnect");
}
diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h
index 2dd892e..fb67748 100644
--- a/src/Common/RequestManager.h
+++ b/src/Common/RequestManager.h
@@ -33,8 +33,11 @@
namespace Mad {
namespace Common {
-class RequestManager : boost::noncopyable {
+class Application;
+
+class RequestManager : private boost::noncopyable {
private:
+ friend class Application;
friend class RequestHandler;
class RequestMap : private boost::noncopyable {
@@ -78,18 +81,17 @@ class RequestManager : boost::noncopyable {
return types;
}
- virtual boost::shared_ptr<RequestHandler> createRequestHandler(const std::string &type _UNUSED_PARAMETER_) {
- return boost::shared_ptr<RequestHandler>(new T());
+ virtual boost::shared_ptr<RequestHandler> createRequestHandler(Application *application, const std::string &type _UNUSED_PARAMETER_) {
+ return boost::shared_ptr<RequestHandler>(new T(application));
}
};
- static RequestManager requestManager;
+ Application *application;
boost::shared_mutex mutex;
RequestMap requestMap;
- bool server;
boost::uint16_t lastRequestId;
std::map<std::string, boost::shared_ptr<RequestHandlerGroup> > requestHandlerGroups;
@@ -109,29 +111,9 @@ class RequestManager : boost::noncopyable {
bool send(Request *request);
- RequestManager();
+ RequestManager(Application *application0, bool server);
public:
- static RequestManager* get() {
- return &requestManager;
- }
-
- bool isServer() {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
- return server;
- }
-
- void setServer(bool newServer) {
- boost::lock_guard<boost::shared_mutex> lock(mutex);
-
- server = newServer;
-
- if(server)
- lastRequestId &= ~0x01;
- else
- lastRequestId |= 0x01;
- }
-
void registerConnection(Connection *connection);
void unregisterConnection(Connection *connection) {
diff --git a/src/Common/Requests/DisconnectRequest.h b/src/Common/Requests/DisconnectRequest.h
index 6da0ac9..f8150bd 100644
--- a/src/Common/Requests/DisconnectRequest.h
+++ b/src/Common/Requests/DisconnectRequest.h
@@ -30,6 +30,9 @@ class DisconnectRequest : public Request {
protected:
virtual void sendRequest();
virtual void handlePacket(boost::shared_ptr<const XmlPacket> packet);
+
+ public:
+ DisconnectRequest(Application *application) : Request(application) {}
};
}
diff --git a/src/Common/Requests/FSInfoRequest.h b/src/Common/Requests/FSInfoRequest.h
index 250889a..2bd9eeb 100644
--- a/src/Common/Requests/FSInfoRequest.h
+++ b/src/Common/Requests/FSInfoRequest.h
@@ -28,7 +28,7 @@ namespace Requests {
class FSInfoRequest : public SimpleRequest {
public:
- FSInfoRequest() : SimpleRequest("FSInfo") {}
+ FSInfoRequest(Application *application) : SimpleRequest(application, "FSInfo") {}
};
}
diff --git a/src/Common/Requests/GroupListRequest.h b/src/Common/Requests/GroupListRequest.h
index b1e4115..f8760ca 100644
--- a/src/Common/Requests/GroupListRequest.h
+++ b/src/Common/Requests/GroupListRequest.h
@@ -28,7 +28,7 @@ namespace Requests {
class GroupListRequest : public SimpleRequest {
public:
- GroupListRequest() : SimpleRequest("ListGroups") {}
+ GroupListRequest(Application *application) : SimpleRequest(application, "ListGroups") {}
};
}
diff --git a/src/Common/Requests/GroupUserListRequest.h b/src/Common/Requests/GroupUserListRequest.h
index bb735f1..f95cd3b 100644
--- a/src/Common/Requests/GroupUserListRequest.h
+++ b/src/Common/Requests/GroupUserListRequest.h
@@ -34,7 +34,7 @@ class GroupUserListRequest : public Request {
virtual void sendRequest();
public:
- GroupUserListRequest(unsigned long gid0) : gid(gid0) {}
+ GroupUserListRequest(Application *application, unsigned long gid0) : Request(application), gid(gid0) {}
};
}
diff --git a/src/Common/Requests/IdentifyRequest.h b/src/Common/Requests/IdentifyRequest.h
index 3084ce6..2ca9d8e 100644
--- a/src/Common/Requests/IdentifyRequest.h
+++ b/src/Common/Requests/IdentifyRequest.h
@@ -36,7 +36,7 @@ class IdentifyRequest : public Common::Request {
virtual void sendRequest();
public:
- IdentifyRequest(const std::string &hostname0 = std::string()) : hostname(hostname0) {}
+ IdentifyRequest(Application *application, const std::string &hostname0 = std::string()) : Request(application), hostname(hostname0) {}
};
}
diff --git a/src/Common/Requests/SimpleRequest.h b/src/Common/Requests/SimpleRequest.h
index d381ecf..649f34c 100644
--- a/src/Common/Requests/SimpleRequest.h
+++ b/src/Common/Requests/SimpleRequest.h
@@ -34,7 +34,7 @@ class SimpleRequest : public Request {
virtual void sendRequest();
- SimpleRequest(const std::string &type0) : type(type0) {}
+ SimpleRequest(Application *application, const std::string &type0) : Request(application), type(type0) {}
};
}
diff --git a/src/Common/Requests/StatusRequest.h b/src/Common/Requests/StatusRequest.h
index b9b9f02..c30e0d5 100644
--- a/src/Common/Requests/StatusRequest.h
+++ b/src/Common/Requests/StatusRequest.h
@@ -28,7 +28,7 @@ namespace Requests {
class StatusRequest : public SimpleRequest {
public:
- StatusRequest() : SimpleRequest("GetStatus") {}
+ StatusRequest(Application *application) : SimpleRequest(application, "GetStatus") {}
};
}
diff --git a/src/Common/Requests/UserGroupListRequest.h b/src/Common/Requests/UserGroupListRequest.h
index 82db411..4cd27b6 100644
--- a/src/Common/Requests/UserGroupListRequest.h
+++ b/src/Common/Requests/UserGroupListRequest.h
@@ -34,7 +34,7 @@ class UserGroupListRequest : public Request {
virtual void sendRequest();
public:
- UserGroupListRequest(unsigned long uid0) : uid(uid0) {}
+ UserGroupListRequest(Application *application, unsigned long uid0) : Request(application), uid(uid0) {}
};
}
diff --git a/src/Common/Requests/UserInfoRequest.h b/src/Common/Requests/UserInfoRequest.h
index c9ac89b..7b4b2fc 100644
--- a/src/Common/Requests/UserInfoRequest.h
+++ b/src/Common/Requests/UserInfoRequest.h
@@ -34,8 +34,7 @@ class UserInfoRequest : public Request {
virtual void sendRequest();
public:
- UserInfoRequest(unsigned long uid0)
- : uid(uid0) {}
+ UserInfoRequest(Application *application, unsigned long uid0) : Request(application), uid(uid0) {}
};
}
diff --git a/src/Common/Requests/UserListRequest.h b/src/Common/Requests/UserListRequest.h
index 9b69564..9196166 100644
--- a/src/Common/Requests/UserListRequest.h
+++ b/src/Common/Requests/UserListRequest.h
@@ -28,7 +28,7 @@ namespace Requests {
class UserListRequest : public SimpleRequest {
public:
- UserListRequest() : SimpleRequest("ListUsers") {}
+ UserListRequest(Application *application) : SimpleRequest(application, "ListUsers") {}
};
}
diff --git a/src/Common/SystemManager.cpp b/src/Common/SystemManager.cpp
index fac50d5..deb361a 100644
--- a/src/Common/SystemManager.cpp
+++ b/src/Common/SystemManager.cpp
@@ -23,9 +23,6 @@
namespace Mad {
namespace Common {
-SystemManager SystemManager::systemManager;
-
-
bool SystemManager::Compare::operator() (boost::shared_ptr<SystemBackend> b1, boost::shared_ptr<SystemBackend> b2) {
if(b1->getPriority() == b2->getPriority())
return (b1.get() > b2.get());
diff --git a/src/Common/SystemManager.h b/src/Common/SystemManager.h
index 6870489..e8567c5 100644
--- a/src/Common/SystemManager.h
+++ b/src/Common/SystemManager.h
@@ -24,6 +24,7 @@
#include <string>
#include <vector>
+#include <boost/noncopyable.hpp>
#include <boost/smart_ptr.hpp>
#include <Core/Exception.h>
@@ -31,16 +32,17 @@
namespace Mad {
namespace Common {
+class Application;
class SystemBackend;
-class SystemManager {
+class SystemManager : private boost::noncopyable {
private:
+ friend class Application;
+
struct Compare {
bool operator() (boost::shared_ptr<SystemBackend> b1, boost::shared_ptr<SystemBackend> b2);
};
- static SystemManager systemManager;
-
std::set<boost::shared_ptr<SystemBackend>, Compare> backends;
SystemManager() {}
@@ -70,10 +72,6 @@ class SystemManager {
void shutdown() throw(Core::Exception);
void reboot() throw(Core::Exception);
-
- static SystemManager *get() {
- return &systemManager;
- }
};
}