diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-06-06 17:43:47 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-06-06 17:43:47 +0200 |
commit | fcdd58703e3f5a0f6e77fd74e0304038e7cd4d3e (patch) | |
tree | 6956263f5ee93680ba730bb6b522e195000b2543 /src/Common/RequestHandlers | |
parent | 454cfccd51e2b9ad453fe79df04360fcd6eeb742 (diff) | |
download | mad-fcdd58703e3f5a0f6e77fd74e0304038e7cd4d3e.tar mad-fcdd58703e3f5a0f6e77fd74e0304038e7cd4d3e.zip |
RequestHandler-Klassen vereinfacht
Diffstat (limited to 'src/Common/RequestHandlers')
-rw-r--r-- | src/Common/RequestHandlers/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Common/RequestHandlers/FSInfoRequestHandler.cpp | 57 | ||||
-rw-r--r-- | src/Common/RequestHandlers/FSInfoRequestHandler.h | 14 | ||||
-rw-r--r-- | src/Common/RequestHandlers/SimpleRequestHandler.cpp | 64 | ||||
-rw-r--r-- | src/Common/RequestHandlers/SimpleRequestHandler.h | 49 | ||||
-rw-r--r-- | src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp | 38 | ||||
-rw-r--r-- | src/Common/RequestHandlers/SimpleRequestHandlerGroup.h | 54 | ||||
-rw-r--r-- | src/Common/RequestHandlers/StatusRequestHandler.cpp | 52 | ||||
-rw-r--r-- | src/Common/RequestHandlers/StatusRequestHandler.h | 12 |
9 files changed, 249 insertions, 93 deletions
diff --git a/src/Common/RequestHandlers/CMakeLists.txt b/src/Common/RequestHandlers/CMakeLists.txt index 5788ada..e6e34a7 100644 --- a/src/Common/RequestHandlers/CMakeLists.txt +++ b/src/Common/RequestHandlers/CMakeLists.txt @@ -3,6 +3,8 @@ include_directories(${INCLUDES}) add_library(RequestHandlers STATIC DisconnectRequestHandler.cpp DisconnectRequestHandler.h FSInfoRequestHandler.cpp FSInfoRequestHandler.h + SimpleRequestHandler.cpp SimpleRequestHandler.h + SimpleRequestHandlerGroup.cpp SimpleRequestHandlerGroup.h StatusRequestHandler.cpp StatusRequestHandler.h ) target_link_libraries(RequestHandlers Common) diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp index 845258b..7ac4644 100644 --- a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp @@ -18,62 +18,31 @@ */ #include "FSInfoRequestHandler.h" -#include <Net/Exception.h> -#include "../Logger.h" - -#include <boost/bind.hpp> +#include "../SystemManager.h" namespace Mad { namespace Common { namespace RequestHandlers { -void FSInfoRequestHandler::handlePacket(boost::shared_ptr<const XmlPacket> packet) { - if(packet->getType() != "FSInfo") { - Logger::log(Logger::ERROR, "Received an unexpected packet."); - - XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - +void FSInfoRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPacket> packet _UNUSED_PARAMETER_, Common::XmlPacket *ret) { // TODO Require authentication std::vector<SystemManager::FSInfo> fsInfo; + SystemManager::get()->getFSInfo(&fsInfo); - XmlPacket ret; - - try { - SystemManager::get()->getFSInfo(&fsInfo); - - ret.setType("OK"); - ret.addList("filesystems"); + ret->setType("OK"); + ret->addList("filesystems"); - for(std::vector<SystemManager::FSInfo>::const_iterator fs = fsInfo.begin(); fs != fsInfo.end(); ++fs) { - ret["filesystems"].addEntry(); - XmlPacket::Entry &entry = ret["filesystems"].back(); + for(std::vector<SystemManager::FSInfo>::const_iterator fs = fsInfo.begin(); fs != fsInfo.end(); ++fs) { + (*ret)["filesystems"].addEntry(); + XmlPacket::Entry &entry = (*ret)["filesystems"].back(); - entry.add("name", fs->fsName); - entry.add("mountedOn", fs->mountedOn); - entry.add("totalSize", fs->total); - entry.add("usedSize", fs->used); - entry.add("availableSize", fs->available); - } + entry.add("name", fs->fsName); + entry.add("mountedOn", fs->mountedOn); + entry.add("totalSize", fs->total); + entry.add("usedSize", fs->used); + entry.add("availableSize", fs->available); } - catch(Net::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(); } } diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.h b/src/Common/RequestHandlers/FSInfoRequestHandler.h index 6d21787..167e116 100644 --- a/src/Common/RequestHandlers/FSInfoRequestHandler.h +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.h @@ -20,18 +20,18 @@ #ifndef MAD_COMMON_REQUESTHANDLERS_FSINFOREQUESTHANDLER_H_ #define MAD_COMMON_REQUESTHANDLERS_FSINFOREQUESTHANDLER_H_ -#include "../RequestHandler.h" -#include "../SystemManager.h" - -#include <stdint.h> +#include "SimpleRequestHandler.h" namespace Mad { namespace Common { namespace RequestHandlers { -class FSInfoRequestHandler : public RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr<const XmlPacket> packet); +class FSInfoRequestHandler : public SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); + + public: + FSInfoRequestHandler() : SimpleRequestHandler("FSInfo", &FSInfoRequestHandler::handleRequest) {} }; } diff --git a/src/Common/RequestHandlers/SimpleRequestHandler.cpp b/src/Common/RequestHandlers/SimpleRequestHandler.cpp new file mode 100644 index 0000000..0abc484 --- /dev/null +++ b/src/Common/RequestHandlers/SimpleRequestHandler.cpp @@ -0,0 +1,64 @@ +/* + * SimpleRequestHandler.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 "SimpleRequestHandler.h" +#include "../Logger.h" + +#include <Net/Exception.h> + +namespace Mad { +namespace Common { +namespace RequestHandlers { + +void SimpleRequestHandler::handlePacket(boost::shared_ptr<const XmlPacket> packet) { + if(packet->getType() != type) { + Logger::log(Logger::ERROR, "Received an unexpected packet."); + + XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); + + sendPacket(ret); + + signalFinished(); + return; + } + + // TODO Require authentication + + XmlPacket ret; + + try { + handler(packet, &ret); + } + catch(Net::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(); +} + +} +} +} diff --git a/src/Common/RequestHandlers/SimpleRequestHandler.h b/src/Common/RequestHandlers/SimpleRequestHandler.h new file mode 100644 index 0000000..5d388d3 --- /dev/null +++ b/src/Common/RequestHandlers/SimpleRequestHandler.h @@ -0,0 +1,49 @@ +/* + * SimpleRequestHandler.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_REQUESTHANDLERS_SIMPLEREQUESTHANDLER_H_ +#define MAD_COMMON_REQUESTHANDLERS_SIMPLEREQUESTHANDLER_H_ + +#include "../RequestHandler.h" + +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) {} +}; + +} +} +} + +#endif /* MAD_COMMON_REQUESTHANDLERS_SIMPLEREQUESTHANDLER_H_ */ diff --git a/src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp new file mode 100644 index 0000000..9c80e23 --- /dev/null +++ b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp @@ -0,0 +1,38 @@ +/* + * SimpleRequestHandlerGroup.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 "SimpleRequestHandlerGroup.h" +#include "SimpleRequestHandler.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); + + if(handler == handlers.end()) + return boost::shared_ptr<RequestHandler>(); + else + return boost::shared_ptr<SimpleRequestHandler>(new SimpleRequestHandler(type, handler->second)); +} + +} +} +} diff --git a/src/Common/RequestHandlers/SimpleRequestHandlerGroup.h b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.h new file mode 100644 index 0000000..86009f3 --- /dev/null +++ b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.h @@ -0,0 +1,54 @@ +/* + * SimpleRequestHandlerGroup.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_REQUESTHANDLERS_SIMPLEREQUESTHANDLERGROUP_H_ +#define MAD_COMMON_REQUESTHANDLERS_SIMPLEREQUESTHANDLERGROUP_H_ + +#include "../RequestHandlerGroup.h" + +namespace Mad { +namespace Common { +namespace RequestHandlers { + +class SimpleRequestHandlerGroup : public RequestHandlerGroup { + private: + std::set<std::string> types; + std::map<std::string, boost::function2<void, boost::shared_ptr<const XmlPacket>, XmlPacket*> > handlers; + + protected: + void registerHandler(const std::string &type, const boost::function2<void, boost::shared_ptr<const XmlPacket>, XmlPacket*> &handler) { + types.insert(type); + handlers.insert(std::make_pair(type, handler)); + } + + SimpleRequestHandlerGroup() {} + + public: + virtual const std::set<std::string>& getPacketTypes() { + return types; + } + + virtual boost::shared_ptr<RequestHandler> createRequestHandler(const std::string &type); +}; + +} +} +} + +#endif /* MAD_COMMON_REQUESTHANDLERS_SIMPLEREQUESTHANDLERGROUP_H_ */ diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index 989ce7c..25d2f23 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -18,30 +18,13 @@ */ #include "StatusRequestHandler.h" -#include <Net/Exception.h> -#include "../SystemBackend.h" -#include "../Logger.h" - -#include <boost/bind.hpp> +#include "../SystemManager.h" namespace Mad { namespace Common { namespace RequestHandlers { -void StatusRequestHandler::handlePacket(boost::shared_ptr<const XmlPacket> packet) { - if(packet->getType() != "GetStatus") { - Logger::log(Logger::ERROR, "Received an unexpected packet."); - - XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - +void StatusRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPacket> packet _UNUSED_PARAMETER_, Common::XmlPacket *ret) { // TODO Require authentication unsigned long uptime, idleTime; @@ -50,8 +33,6 @@ void StatusRequestHandler::handlePacket(boost::shared_ptr<const XmlPacket> packe float loadAvg1, loadAvg5, loadAvg15; - XmlPacket ret; - try { SystemManager::get()->getUptimeInfo(&uptime, &idleTime); SystemManager::get()->getMemoryInfo(&totalMem, &freeMem, &totalSwap, &freeSwap); @@ -59,22 +40,19 @@ void StatusRequestHandler::handlePacket(boost::shared_ptr<const XmlPacket> packe } catch(Net::Exception e) {} - ret.setType("OK"); - - ret.add("uptime", uptime); - ret.add("idleTime", idleTime); - ret.add("totalMem", totalMem); - ret.add("freeMem", freeMem); - ret.add("totalSwap", totalSwap); - ret.add("freeSwap", freeSwap); - ret.add("currentLoad", currentLoad); - ret.add("nProcesses", nProcesses); - ret.add("loadAvg1", loadAvg1); - ret.add("loadAvg5", loadAvg5); - ret.add("loadAvg15", loadAvg15); - - sendPacket(ret); - signalFinished(); + ret->setType("OK"); + + ret->add("uptime", uptime); + ret->add("idleTime", idleTime); + ret->add("totalMem", totalMem); + ret->add("freeMem", freeMem); + ret->add("totalSwap", totalSwap); + ret->add("freeSwap", freeSwap); + ret->add("currentLoad", currentLoad); + ret->add("nProcesses", nProcesses); + ret->add("loadAvg1", loadAvg1); + ret->add("loadAvg5", loadAvg5); + ret->add("loadAvg15", loadAvg15); } } diff --git a/src/Common/RequestHandlers/StatusRequestHandler.h b/src/Common/RequestHandlers/StatusRequestHandler.h index 656a1a5..5583733 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.h +++ b/src/Common/RequestHandlers/StatusRequestHandler.h @@ -20,16 +20,18 @@ #ifndef MAD_COMMON_REQUESTHANDLERS_STATUSREQUESTHANDLER_H_ #define MAD_COMMON_REQUESTHANDLERS_STATUSREQUESTHANDLER_H_ -#include "../RequestHandler.h" -#include <stdint.h> +#include "SimpleRequestHandler.h" namespace Mad { namespace Common { namespace RequestHandlers { -class StatusRequestHandler : public RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr<const XmlPacket> packet); +class StatusRequestHandler : public SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); + + public: + StatusRequestHandler() : SimpleRequestHandler("GetStatus", &StatusRequestHandler::handleRequest) {} }; } |