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 | |
parent | 454cfccd51e2b9ad453fe79df04360fcd6eeb742 (diff) | |
download | mad-fcdd58703e3f5a0f6e77fd74e0304038e7cd4d3e.tar mad-fcdd58703e3f5a0f6e77fd74e0304038e7cd4d3e.zip |
RequestHandler-Klassen vereinfacht
31 files changed, 393 insertions, 616 deletions
diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index 03a71c6..6bcdc8c 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -29,20 +29,7 @@ namespace Client { InformationManager InformationManager::informationManager; -void InformationManager::DaemonStateUpdateRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { - if(packet->getType() != "UpdateHostState") { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - +void InformationManager::DaemonStateUpdateRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret) { // TODO Require authentication { @@ -55,11 +42,7 @@ void InformationManager::DaemonStateUpdateRequestHandler::handlePacket(boost::sh Common::Logger::log(Common::Logger::WARNING, "Received a state update for an unknown host."); } - Common::XmlPacket ret; - ret.setType("OK"); - sendPacket(ret); - - signalFinished(); + ret->setType("OK"); } diff --git a/src/Client/InformationManager.h b/src/Client/InformationManager.h index a7d2354..78b1f88 100644 --- a/src/Client/InformationManager.h +++ b/src/Client/InformationManager.h @@ -26,6 +26,7 @@ #include <Common/HostInfo.h> #include <Common/Initializable.h> #include <Common/Request.h> +#include <Common/RequestHandlers/SimpleRequestHandler.h> #include <boost/thread/condition_variable.hpp> #include <boost/thread/locks.hpp> @@ -36,9 +37,12 @@ namespace Client { class InformationManager : public Common::Initializable, private boost::noncopyable { private: - class DaemonStateUpdateRequestHandler : public Common::RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); + class DaemonStateUpdateRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); + + public: + DaemonStateUpdateRequestHandler() : Common::RequestHandlers::SimpleRequestHandler("UpdateHostState", &DaemonStateUpdateRequestHandler::handleRequest) {} }; static InformationManager informationManager; diff --git a/src/Client/Requests/DaemonStatusRequest.cpp b/src/Client/Requests/DaemonStatusRequest.cpp index 9f0a9a0..ff1cda3 100644 --- a/src/Client/Requests/DaemonStatusRequest.cpp +++ b/src/Client/Requests/DaemonStatusRequest.cpp @@ -26,7 +26,7 @@ namespace Requests { void DaemonStatusRequest::sendRequest() { Common::XmlPacket packet; packet.setType("GetDaemonStatus"); - packet.add("daemonName", daemon); + packet.add("daemon", daemon); sendPacket(packet); } 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/Server/RequestHandlers/DaemonStatusRequestHandler.h b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp index 728be4a..9c80e23 100644 --- a/src/Server/RequestHandlers/DaemonStatusRequestHandler.h +++ b/src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp @@ -1,7 +1,7 @@ /* - * DaemonStatusRequestHandler.h + * SimpleRequestHandlerGroup.cpp * - * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * 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 @@ -17,27 +17,22 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef MAD_SERVER_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ -#define MAD_SERVER_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ - -#include <Common/RequestHandler.h> -#include <Common/Request.h> -#include <stdint.h> +#include "SimpleRequestHandlerGroup.h" +#include "SimpleRequestHandler.h" namespace Mad { -namespace Server { +namespace Common { namespace RequestHandlers { -class DaemonStatusRequestHandler : public Common::RequestHandler { - private: - void requestFinished(boost::shared_ptr<const Common::XmlPacket> packet, Net::Exception error); +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); - protected: - virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); -}; + if(handler == handlers.end()) + return boost::shared_ptr<RequestHandler>(); + else + return boost::shared_ptr<SimpleRequestHandler>(new SimpleRequestHandler(type, handler->second)); +} } } } - -#endif /* MAD_SERVER_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ */ 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) {} }; } diff --git a/src/Daemon/RequestHandlers/CommandRequestHandler.cpp b/src/Daemon/RequestHandlers/CommandRequestHandler.cpp index 208d6e0..d00c415 100644 --- a/src/Daemon/RequestHandlers/CommandRequestHandler.cpp +++ b/src/Daemon/RequestHandlers/CommandRequestHandler.cpp @@ -18,53 +18,24 @@ */ #include "CommandRequestHandler.h" -#include <Net/Exception.h> -#include <Common/Logger.h> #include <Common/SystemManager.h> namespace Mad { namespace Daemon { namespace RequestHandlers { -void CommandRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { - if(packet->getType() != "Command") { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - +void CommandRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret) { // TODO Require authentication // TODO Error handling std::string command = (*packet)["command"]; - Common::XmlPacket ret; - - try { - if(command == "reboot") - Common::SystemManager::get()->shutdown(); - else - Common::SystemManager::get()->reboot(); - - ret.setType("OK"); - } - 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()); - } + if(command == "reboot") + Common::SystemManager::get()->reboot(); + else + Common::SystemManager::get()->shutdown(); - sendPacket(ret); - signalFinished(); + ret->setType("OK"); } } diff --git a/src/Daemon/RequestHandlers/CommandRequestHandler.h b/src/Daemon/RequestHandlers/CommandRequestHandler.h index be560f7..22c7a4b 100644 --- a/src/Daemon/RequestHandlers/CommandRequestHandler.h +++ b/src/Daemon/RequestHandlers/CommandRequestHandler.h @@ -20,15 +20,18 @@ #ifndef MAD_DAEMON_REQUESTHANDLERS_COMMANDREQUESTHANDLER_H_ #define MAD_DAEMON_REQUESTHANDLERS_COMMANDREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> +#include <Common/RequestHandlers/SimpleRequestHandler.h> namespace Mad { namespace Daemon { namespace RequestHandlers { -class CommandRequestHandler : public Common::RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); +class CommandRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); + + public: + CommandRequestHandler() : Common::RequestHandlers::SimpleRequestHandler("Command", &CommandRequestHandler::handleRequest) {} }; } diff --git a/src/Server/ConnectionManager.cpp b/src/Server/ConnectionManager.cpp index 0a60ff4..f568f74 100644 --- a/src/Server/ConnectionManager.cpp +++ b/src/Server/ConnectionManager.cpp @@ -24,13 +24,14 @@ #include <Common/RequestHandlers/FSInfoRequestHandler.h> #include <Common/RequestHandlers/StatusRequestHandler.h> #include "Requests/DaemonStateUpdateRequest.h" -#include "RequestHandlers/DaemonCommandRequestHandler.h" -#include "RequestHandlers/DaemonFSInfoRequestHandler.h" +//#include "RequestHandlers/DaemonCommandRequestHandler.h" +//#include "RequestHandlers/DaemonFSInfoRequestHandler.h" #include "RequestHandlers/DaemonListRequestHandler.h" -#include "RequestHandlers/DaemonStatusRequestHandler.h" +//#include "RequestHandlers/DaemonStatusRequestHandler.h" //#include "RequestHandlers/GSSAPIAuthRequestHandler.h" #include "RequestHandlers/IdentifyRequestHandler.h" #include "RequestHandlers/LogRequestHandler.h" +#include "RequestHandlers/DaemonRequestHandlerGroup.h" #include "RequestHandlers/UserRequestHandlerGroup.h" #include <Net/Packet.h> #include <Net/Listener.h> @@ -185,18 +186,20 @@ void ConnectionManager::handleDisconnect(boost::shared_ptr<ServerConnection> con void ConnectionManager::doInit() { Common::RequestManager::get()->setServer(true); + daemonRequestHandlerGroup.reset(new RequestHandlers::DaemonRequestHandlerGroup); userRequestHandlerGroup.reset(new RequestHandlers::UserRequestHandlerGroup); //Common::RequestManager::get()->registerPacketType<RequestHandlers::GSSAPIAuthRequestHandler>("AuthGSSAPI"); - Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>("DaemonCommand"); - Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonFSInfoRequestHandler>("DaemonFSInfo"); + //Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>("DaemonCommand"); + //Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonFSInfoRequestHandler>("DaemonFSInfo"); Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::FSInfoRequestHandler>("FSInfo"); Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::StatusRequestHandler>("GetStatus"); - Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonStatusRequestHandler>("GetDaemonStatus"); + //Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonStatusRequestHandler>("GetDaemonStatus"); Common::RequestManager::get()->registerPacketType<RequestHandlers::IdentifyRequestHandler>("Identify"); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonListRequestHandler>("ListHosts"); Common::RequestManager::get()->registerPacketType<RequestHandlers::LogRequestHandler>("Log"); + Common::RequestManager::get()->registerRequestHandlerGroup(daemonRequestHandlerGroup); Common::RequestManager::get()->registerRequestHandlerGroup(userRequestHandlerGroup); } @@ -204,13 +207,14 @@ void ConnectionManager::doDeinit() { connections.clear(); Common::RequestManager::get()->unregisterRequestHandlerGroup(userRequestHandlerGroup); + Common::RequestManager::get()->unregisterRequestHandlerGroup(daemonRequestHandlerGroup); //Common::RequestManager::get()->unregisterPacketType("AuthGSSAPI"); - Common::RequestManager::get()->unregisterPacketType("DaemonCommand"); - Common::RequestManager::get()->unregisterPacketType("DaemonFSInfo"); + //Common::RequestManager::get()->unregisterPacketType("DaemonCommand"); + //Common::RequestManager::get()->unregisterPacketType("DaemonFSInfo"); Common::RequestManager::get()->unregisterPacketType("FSInfo"); Common::RequestManager::get()->unregisterPacketType("GetStatus"); - Common::RequestManager::get()->unregisterPacketType("GetDaemonStatus"); + //Common::RequestManager::get()->unregisterPacketType("GetDaemonStatus"); Common::RequestManager::get()->unregisterPacketType("Identify"); Common::RequestManager::get()->unregisterPacketType("ListHosts"); Common::RequestManager::get()->unregisterPacketType("Log"); diff --git a/src/Server/ConnectionManager.h b/src/Server/ConnectionManager.h index 0c4e76d..6573883 100644 --- a/src/Server/ConnectionManager.h +++ b/src/Server/ConnectionManager.h @@ -100,6 +100,7 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa std::map<std::string,Common::HostInfo> daemonInfo; + boost::shared_ptr<Common::RequestHandlerGroup> daemonRequestHandlerGroup; boost::shared_ptr<Common::RequestHandlerGroup> userRequestHandlerGroup; void updateState(Common::HostInfo *hostInfo, Common::HostInfo::State state); diff --git a/src/Server/RequestHandlers/CMakeLists.txt b/src/Server/RequestHandlers/CMakeLists.txt index b57b9fe..9b9b9ea 100644 --- a/src/Server/RequestHandlers/CMakeLists.txt +++ b/src/Server/RequestHandlers/CMakeLists.txt @@ -1,10 +1,8 @@ include_directories(${INCLUDES}) add_library(ServerRequestHandlers STATIC - DaemonCommandRequestHandler.cpp DaemonCommandRequestHandler.h - DaemonFSInfoRequestHandler.cpp DaemonFSInfoRequestHandler.h DaemonListRequestHandler.cpp DaemonListRequestHandler.h - DaemonStatusRequestHandler.cpp DaemonStatusRequestHandler.h + DaemonRequestHandlerGroup.cpp DaemonRequestHandlerGroup.h IdentifyRequestHandler.cpp IdentifyRequestHandler.h LogRequestHandler.cpp LogRequestHandler.h UserRequestHandlerGroup.cpp UserRequestHandlerGroup.h diff --git a/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp b/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp deleted file mode 100644 index 65592a1..0000000 --- a/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * DaemonCommandRequestHandler.cpp - * - * 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/>. - */ - -#include "DaemonCommandRequestHandler.h" -#include "../ConnectionManager.h" -#include "../Requests/CommandRequest.h" - -#include <Common/Logger.h> - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -void DaemonCommandRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { - if(packet->getType() != "DaemonCommand") { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - - // TODO Require authentication - - std::string command = (*packet)["command"]; - - try { - boost::shared_ptr<Common::Connection> daemonCon = ConnectionManager::get()->getDaemonConnection((*packet)["daemon"]); - - boost::shared_ptr<Requests::CommandRequest> request(new Requests::CommandRequest(command == "reboot")); - request->connectSignalFinished(boost::bind(&DaemonCommandRequestHandler::requestFinished, this, _1, _2)); - Common::RequestManager::get()->sendRequest(daemonCon.get(), request); - } - catch(Net::Exception &e) { - Common::XmlPacket ret; - 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); - } -} - -void DaemonCommandRequestHandler::requestFinished(boost::shared_ptr<const Common::XmlPacket> packet, Net::Exception error) { - if(error) { - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", error.getErrorCode()); - ret.add("SubCode", error.getSubCode()); - ret.add("SubSubCode", error.getSubSubCode()); - ret.add("Where", error.getWhere()); - - sendPacket(ret); - } - else { - sendPacket(*packet); - } - - signalFinished(); -} - -} -} -} diff --git a/src/Server/RequestHandlers/DaemonCommandRequestHandler.h b/src/Server/RequestHandlers/DaemonCommandRequestHandler.h deleted file mode 100644 index 4595372..0000000 --- a/src/Server/RequestHandlers/DaemonCommandRequestHandler.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * DaemonCommandRequestHandler.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_SERVER_REQUESTHANDLERS_DAEMONCOMMANDREQUESTHANDLER_H_ -#define MAD_SERVER_REQUESTHANDLERS_DAEMONCOMMANDREQUESTHANDLER_H_ - -#include <Common/RequestHandler.h> -#include <Common/Request.h> -#include <stdint.h> - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -class DaemonCommandRequestHandler : public Common::RequestHandler { - private: - void requestFinished(boost::shared_ptr<const Common::XmlPacket> packet, Net::Exception error); - - protected: - virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); -}; - -} -} -} - -#endif /* MAD_SERVER_REQUESTHANDLERS_DAEMONCOMMANDREQUESTHANDLER_H_ */ diff --git a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.h b/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.h deleted file mode 100644 index baf60f7..0000000 --- a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * DaemonFSInfoRequestHandler.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_SERVER_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ -#define MAD_SERVER_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ - -#include <Common/RequestHandler.h> -#include <Common/Request.h> -#include <stdint.h> - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -class DaemonFSInfoRequestHandler : public Common::RequestHandler { - private: - void requestFinished(boost::shared_ptr<const Common::XmlPacket> packet, Net::Exception error); - - protected: - virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); -}; - -} -} -} - -#endif /* MAD_SERVER_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ */ diff --git a/src/Server/RequestHandlers/DaemonListRequestHandler.cpp b/src/Server/RequestHandlers/DaemonListRequestHandler.cpp index 63d6bb3..6c17ce1 100644 --- a/src/Server/RequestHandlers/DaemonListRequestHandler.cpp +++ b/src/Server/RequestHandlers/DaemonListRequestHandler.cpp @@ -25,39 +25,21 @@ namespace Mad { namespace Server { namespace RequestHandlers { -void DaemonListRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { - if(packet->getType() != "ListHosts") { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - +void DaemonListRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPacket> packet _UNUSED_PARAMETER_, Common::XmlPacket *ret) { // TODO Require authentication - Common::XmlPacket ret; - ret.setType("OK"); - ret.addList("hosts"); + ret->setType("OK"); + ret->addList("hosts"); std::vector<Common::HostInfo> daemons = ConnectionManager::get()->getDaemonList(); for(std::vector<Common::HostInfo>::iterator daemon = daemons.begin(); daemon != daemons.end(); ++daemon) { - ret["hosts"].addEntry(); + (*ret)["hosts"].addEntry(); - ret["hosts"].back().add("name", daemon->getName()); - ret["hosts"].back().add("address", daemon->getIP()); - ret["hosts"].back().add("state", daemon->getState()); + (*ret)["hosts"].back().add("name", daemon->getName()); + (*ret)["hosts"].back().add("address", daemon->getIP()); + (*ret)["hosts"].back().add("state", daemon->getState()); } - - sendPacket(ret); - - signalFinished(); } } diff --git a/src/Server/RequestHandlers/DaemonListRequestHandler.h b/src/Server/RequestHandlers/DaemonListRequestHandler.h index adbdabb..45eebc8 100644 --- a/src/Server/RequestHandlers/DaemonListRequestHandler.h +++ b/src/Server/RequestHandlers/DaemonListRequestHandler.h @@ -20,15 +20,18 @@ #ifndef MAD_SERVER_REQUESTHANDLERS_DAEMONLISTREQUESTHANDLER_H_ #define MAD_SERVER_REQUESTHANDLERS_DAEMONLISTREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> +#include <Common/RequestHandlers/SimpleRequestHandler.h> namespace Mad { namespace Server { namespace RequestHandlers { -class DaemonListRequestHandler : public Common::RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); +class DaemonListRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); + + public: + DaemonListRequestHandler() : Common::RequestHandlers::SimpleRequestHandler("ListHosts", &DaemonListRequestHandler::handleRequest) {} }; } diff --git a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp index c66e3d3..4ff8c40 100644 --- a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp +++ b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp @@ -1,7 +1,7 @@ /* - * DaemonFSInfoRequestHandler.cpp + * DaemonRequestHandlerGroup.cpp * - * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * 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 @@ -17,18 +17,20 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "DaemonFSInfoRequestHandler.h" +#include "DaemonRequestHandlerGroup.h" #include "../ConnectionManager.h" +#include "../Requests/CommandRequest.h" + #include <Common/Logger.h> #include <Common/Requests/FSInfoRequest.h> - +#include <Common/Requests/StatusRequest.h> namespace Mad { namespace Server { namespace RequestHandlers { -void DaemonFSInfoRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { - if(packet->getType() != "DaemonFSInfo") { +void DaemonRequestHandlerGroup::DaemonRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { + if(packet->getType() != type) { Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); Common::XmlPacket ret; @@ -45,9 +47,16 @@ void DaemonFSInfoRequestHandler::handlePacket(boost::shared_ptr<const Common::Xm try { boost::shared_ptr<Common::Connection> daemonCon = ConnectionManager::get()->getDaemonConnection((*packet)["daemon"]); + boost::shared_ptr<Common::Request> request; + + if(type == "DaemonCommand") + request.reset(new Requests::CommandRequest((std::string&)((*packet)["command"]) == "reboot")); + else if(type == "DaemonFSInfo") + request.reset(new Common::Requests::FSInfoRequest); + else // type == "GetDaemonStatus" + request.reset(new Common::Requests::StatusRequest); - boost::shared_ptr<Common::Requests::FSInfoRequest> request(new Common::Requests::FSInfoRequest); - request->connectSignalFinished(boost::bind(&DaemonFSInfoRequestHandler::requestFinished, this, _1, _2)); + request->connectSignalFinished(boost::bind(&DaemonRequestHandlerGroup::DaemonRequestHandler::requestFinished, this, _1, _2)); Common::RequestManager::get()->sendRequest(daemonCon.get(), request); } catch(Net::Exception &e) { @@ -59,10 +68,11 @@ void DaemonFSInfoRequestHandler::handlePacket(boost::shared_ptr<const Common::Xm ret.add("Where", e.getWhere()); sendPacket(ret); + signalFinished(); } } -void DaemonFSInfoRequestHandler::requestFinished(boost::shared_ptr<const Common::XmlPacket> packet, Net::Exception error) { +void DaemonRequestHandlerGroup::DaemonRequestHandler::requestFinished(boost::shared_ptr<const Common::XmlPacket> packet, Net::Exception error) { if(error) { Common::XmlPacket ret; ret.setType("Error"); @@ -80,6 +90,20 @@ void DaemonFSInfoRequestHandler::requestFinished(boost::shared_ptr<const Common: signalFinished(); } + +DaemonRequestHandlerGroup::DaemonRequestHandlerGroup() { + types.insert("DaemonCommand"); + types.insert("DaemonFSInfo"); + types.insert("GetDaemonStatus"); +} + +boost::shared_ptr<Common::RequestHandler> DaemonRequestHandlerGroup::createRequestHandler(const std::string &type) { + if(types.find(type) == types.end()) + return boost::shared_ptr<Common::RequestHandler>(); + else + return boost::shared_ptr<DaemonRequestHandler>(new DaemonRequestHandler(type)); +} + } } } diff --git a/src/Server/RequestHandlers/DaemonRequestHandlerGroup.h b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.h new file mode 100644 index 0000000..7c0b127 --- /dev/null +++ b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.h @@ -0,0 +1,61 @@ +/* + * DaemonRequestHandlerGroup.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_SERVER_REQUESTHANDLERS_DAEMONREQUESTHANDLERGROUP_H_ +#define MAD_SERVER_REQUESTHANDLERS_DAEMONREQUESTHANDLERGROUP_H_ + +#include <Common/RequestHandlerGroup.h> +#include <Net/Exception.h> + +namespace Mad { +namespace Server { +namespace RequestHandlers { + +class DaemonRequestHandlerGroup : public Common::RequestHandlerGroup { + private: + class DaemonRequestHandler : public Common::RequestHandler { + private: + std::string type; + + void requestFinished(boost::shared_ptr<const Common::XmlPacket> packet, Net::Exception error); + + protected: + virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); + + public: + DaemonRequestHandler(const std::string &type0) : type(type0) {} + }; + + std::set<std::string> types; + + public: + DaemonRequestHandlerGroup(); + + virtual const std::set<std::string>& getPacketTypes() { + return types; + } + + virtual boost::shared_ptr<Common::RequestHandler> createRequestHandler(const std::string &type); +}; + +} +} +} + +#endif /* MAD_SERVER_REQUESTHANDLERS_DAEMONREQUESTHANDLERGROUP_H_ */ diff --git a/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp b/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp deleted file mode 100644 index 114260d..0000000 --- a/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * DaemonStatusRequestHandler.cpp - * - * 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/>. - */ - -#include "DaemonStatusRequestHandler.h" -#include "../ConnectionManager.h" -#include <Common/Logger.h> -#include <Common/Requests/StatusRequest.h> - - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -void DaemonStatusRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { - if(packet->getType() != "GetDaemonStatus") { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - - // TODO Require authentication - - try { - boost::shared_ptr<Common::Connection> daemonCon = ConnectionManager::get()->getDaemonConnection((*packet)["daemonName"]); - - boost::shared_ptr<Common::Requests::StatusRequest> request(new Common::Requests::StatusRequest); - request->connectSignalFinished(boost::bind(&DaemonStatusRequestHandler::requestFinished, this, _1, _2)); - Common::RequestManager::get()->sendRequest(daemonCon.get(), request); - } - catch(Net::Exception &e) { - Common::XmlPacket ret; - 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); - } -} - -void DaemonStatusRequestHandler::requestFinished(boost::shared_ptr<const Common::XmlPacket> packet, Net::Exception error) { - if(error) { - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", error.getErrorCode()); - ret.add("SubCode", error.getSubCode()); - ret.add("SubSubCode", error.getSubSubCode()); - ret.add("Where", error.getWhere()); - - sendPacket(ret); - } - else { - sendPacket(*packet); - } - - signalFinished(); -} - -} -} -} diff --git a/src/Server/RequestHandlers/IdentifyRequestHandler.cpp b/src/Server/RequestHandlers/IdentifyRequestHandler.cpp index 1b94a52..abee878 100644 --- a/src/Server/RequestHandlers/IdentifyRequestHandler.cpp +++ b/src/Server/RequestHandlers/IdentifyRequestHandler.cpp @@ -26,43 +26,13 @@ namespace Mad { namespace Server { namespace RequestHandlers { -void IdentifyRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { - if(packet->getType() != "Identify") { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); +void IdentifyRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret) { + if((*packet)["hostname"].isEmpty()) + ConnectionManager::get()->identifyClientConnection(getConnection()); + else + ConnectionManager::get()->identifyDaemonConnection(getConnection(), (*packet)["hostname"]); - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - - // TODO Require authentication - try { - if((*packet)["hostname"].isEmpty()) - ConnectionManager::get()->identifyClientConnection(getConnection()); - else - ConnectionManager::get()->identifyDaemonConnection(getConnection(), (*packet)["hostname"]); - - Common::XmlPacket ret; - ret.setType("OK"); - sendPacket(ret); - } - catch(Net::Exception &e) { - Common::XmlPacket ret; - 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(); + ret->setType("OK"); } } diff --git a/src/Server/RequestHandlers/IdentifyRequestHandler.h b/src/Server/RequestHandlers/IdentifyRequestHandler.h index 9b2e6a9..d9954a3 100644 --- a/src/Server/RequestHandlers/IdentifyRequestHandler.h +++ b/src/Server/RequestHandlers/IdentifyRequestHandler.h @@ -20,15 +20,18 @@ #ifndef MAD_SERVER_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ #define MAD_SERVER_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> +#include <Common/RequestHandlers/SimpleRequestHandler.h> namespace Mad { namespace Server { namespace RequestHandlers { -class IdentifyRequestHandler : public Common::RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); +class IdentifyRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); + + public: + IdentifyRequestHandler() : Common::RequestHandlers::SimpleRequestHandler("Identify", boost::bind(&IdentifyRequestHandler::handleRequest, this, _1, _2)) {} }; } diff --git a/src/Server/RequestHandlers/LogRequestHandler.cpp b/src/Server/RequestHandlers/LogRequestHandler.cpp index 0a615c5..1e3f5b4 100644 --- a/src/Server/RequestHandlers/LogRequestHandler.cpp +++ b/src/Server/RequestHandlers/LogRequestHandler.cpp @@ -26,20 +26,7 @@ namespace Mad { namespace Server { namespace RequestHandlers { -void LogRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { - if(packet->getType() != "Log") { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - +void LogRequestHandler::handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret) { // TODO Require authentication try { @@ -50,12 +37,7 @@ void LogRequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> Common::Logger::logf(Common::Logger::ERROR, "Can't determine daemon name: %s", e.strerror().c_str()); } - Common::XmlPacket ret; - ret.setType("OK"); - - sendPacket(ret); - - signalFinished(); + ret->setType("OK"); } } diff --git a/src/Server/RequestHandlers/LogRequestHandler.h b/src/Server/RequestHandlers/LogRequestHandler.h index 211b976..cf1ce34 100644 --- a/src/Server/RequestHandlers/LogRequestHandler.h +++ b/src/Server/RequestHandlers/LogRequestHandler.h @@ -20,15 +20,18 @@ #ifndef MAD_SERVER_REQUESTHANDLERS_LOGREQUESTHANDLER_H_ #define MAD_SERVER_REQUESTHANDLERS_LOGREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> +#include <Common/RequestHandlers/SimpleRequestHandler.h> namespace Mad { namespace Server { namespace RequestHandlers { -class LogRequestHandler : public Common::RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); +class LogRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + void handleRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); + + public: + LogRequestHandler() : Common::RequestHandlers::SimpleRequestHandler("Log", boost::bind(&LogRequestHandler::handleRequest, this, _1, _2)) {} }; } diff --git a/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp b/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp index 1b6dd3d..1ff8883 100644 --- a/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp +++ b/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp @@ -26,39 +26,6 @@ namespace Mad { namespace Server { namespace RequestHandlers { -void UserRequestHandlerGroup::RequestHandler::handlePacket(boost::shared_ptr<const Common::XmlPacket> packet) { - if(packet->getType() != type) { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); - - sendPacket(ret); - - signalFinished(); - return; - } - - // TODO Require authentication - - Common::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(); -} - void UserRequestHandlerGroup::handleUserListRequest(boost::shared_ptr<const Common::XmlPacket> packet _UNUSED_PARAMETER_, Common::XmlPacket *ret) { boost::shared_ptr<std::map<unsigned long, Common::UserInfo> > info = UserManager::get()->getUserList(); @@ -138,19 +105,6 @@ UserRequestHandlerGroup::UserRequestHandlerGroup() { registerHandler("ListGroupUsers", &UserRequestHandlerGroup::handleGroupUserListRequest); } -const std::set<std::string>& UserRequestHandlerGroup::getPacketTypes() { - return types; -} - -boost::shared_ptr<Common::RequestHandler> UserRequestHandlerGroup::createRequestHandler(const std::string &type) { - std::map<std::string, boost::function2<void, boost::shared_ptr<const Common::XmlPacket>, Common::XmlPacket*> >::iterator handler = handlers.find(type); - - if(handler == handlers.end()) - return boost::shared_ptr<Common::RequestHandler>(); - else - return boost::shared_ptr<RequestHandler>(new RequestHandler(type, handler->second)); -} - } } } diff --git a/src/Server/RequestHandlers/UserRequestHandlerGroup.h b/src/Server/RequestHandlers/UserRequestHandlerGroup.h index c24d502..52965dd 100644 --- a/src/Server/RequestHandlers/UserRequestHandlerGroup.h +++ b/src/Server/RequestHandlers/UserRequestHandlerGroup.h @@ -20,35 +20,14 @@ #ifndef MAD_SERVER_REQUESTHANDLERS_USERREQUESTHANDLERGROUP_H_ #define MAD_SERVER_REQUESTHANDLERS_USERREQUESTHANDLERGROUP_H_ -#include <Common/RequestHandlerGroup.h> +#include <Common/RequestHandlers/SimpleRequestHandlerGroup.h> namespace Mad { namespace Server { namespace RequestHandlers { -class UserRequestHandlerGroup : public Common::RequestHandlerGroup { +class UserRequestHandlerGroup : public Common::RequestHandlers::SimpleRequestHandlerGroup { private: - class RequestHandler : public Common::RequestHandler { - private: - std::string type; - boost::function2<void, boost::shared_ptr<const Common::XmlPacket>, Common::XmlPacket*> handler; - - protected: - virtual void handlePacket(boost::shared_ptr<const Common::XmlPacket> packet); - - public: - RequestHandler(const std::string &type0, const boost::function2<void, boost::shared_ptr<const Common::XmlPacket>, Common::XmlPacket*> &handler0) : type(type0), handler(handler0) {} - }; - - - std::set<std::string> types; - std::map<std::string, boost::function2<void, boost::shared_ptr<const Common::XmlPacket>, Common::XmlPacket*> > handlers; - - void registerHandler(const std::string &type, const boost::function2<void, boost::shared_ptr<const Common::XmlPacket>, Common::XmlPacket*> &handler) { - types.insert(type); - handlers.insert(std::make_pair(type, handler)); - } - static void handleUserListRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); static void handleUserInfoRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); static void handleUserGroupListRequest(boost::shared_ptr<const Common::XmlPacket> packet, Common::XmlPacket *ret); @@ -57,9 +36,6 @@ class UserRequestHandlerGroup : public Common::RequestHandlerGroup { public: UserRequestHandlerGroup(); - - virtual const std::set<std::string>& getPacketTypes(); - virtual boost::shared_ptr<Common::RequestHandler> createRequestHandler(const std::string &type); }; } |