From fcdd58703e3f5a0f6e77fd74e0304038e7cd4d3e Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 6 Jun 2009 17:43:47 +0200 Subject: RequestHandler-Klassen vereinfacht --- src/Client/InformationManager.cpp | 21 +--- src/Client/InformationManager.h | 10 +- src/Client/Requests/DaemonStatusRequest.cpp | 2 +- src/Common/RequestHandlers/CMakeLists.txt | 2 + .../RequestHandlers/FSInfoRequestHandler.cpp | 57 +++-------- src/Common/RequestHandlers/FSInfoRequestHandler.h | 14 +-- .../RequestHandlers/SimpleRequestHandler.cpp | 64 ++++++++++++ src/Common/RequestHandlers/SimpleRequestHandler.h | 49 +++++++++ .../RequestHandlers/SimpleRequestHandlerGroup.cpp | 38 +++++++ .../RequestHandlers/SimpleRequestHandlerGroup.h | 54 ++++++++++ .../RequestHandlers/StatusRequestHandler.cpp | 52 +++------- src/Common/RequestHandlers/StatusRequestHandler.h | 12 ++- .../RequestHandlers/CommandRequestHandler.cpp | 41 ++------ src/Daemon/RequestHandlers/CommandRequestHandler.h | 11 ++- src/Server/ConnectionManager.cpp | 22 +++-- src/Server/ConnectionManager.h | 1 + src/Server/RequestHandlers/CMakeLists.txt | 4 +- .../DaemonCommandRequestHandler.cpp | 87 ---------------- .../RequestHandlers/DaemonCommandRequestHandler.h | 43 -------- .../RequestHandlers/DaemonFSInfoRequestHandler.cpp | 85 ---------------- .../RequestHandlers/DaemonFSInfoRequestHandler.h | 43 -------- .../RequestHandlers/DaemonListRequestHandler.cpp | 32 ++---- .../RequestHandlers/DaemonListRequestHandler.h | 11 ++- .../RequestHandlers/DaemonRequestHandlerGroup.cpp | 109 +++++++++++++++++++++ .../RequestHandlers/DaemonRequestHandlerGroup.h | 61 ++++++++++++ .../RequestHandlers/DaemonStatusRequestHandler.cpp | 85 ---------------- .../RequestHandlers/DaemonStatusRequestHandler.h | 43 -------- .../RequestHandlers/IdentifyRequestHandler.cpp | 42 ++------ .../RequestHandlers/IdentifyRequestHandler.h | 11 ++- src/Server/RequestHandlers/LogRequestHandler.cpp | 22 +---- src/Server/RequestHandlers/LogRequestHandler.h | 11 ++- .../RequestHandlers/UserRequestHandlerGroup.cpp | 46 --------- .../RequestHandlers/UserRequestHandlerGroup.h | 28 +----- 33 files changed, 495 insertions(+), 718 deletions(-) create mode 100644 src/Common/RequestHandlers/SimpleRequestHandler.cpp create mode 100644 src/Common/RequestHandlers/SimpleRequestHandler.h create mode 100644 src/Common/RequestHandlers/SimpleRequestHandlerGroup.cpp create mode 100644 src/Common/RequestHandlers/SimpleRequestHandlerGroup.h delete mode 100644 src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp delete mode 100644 src/Server/RequestHandlers/DaemonCommandRequestHandler.h delete mode 100644 src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp delete mode 100644 src/Server/RequestHandlers/DaemonFSInfoRequestHandler.h create mode 100644 src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp create mode 100644 src/Server/RequestHandlers/DaemonRequestHandlerGroup.h delete mode 100644 src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp delete mode 100644 src/Server/RequestHandlers/DaemonStatusRequestHandler.h 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 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 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 #include #include +#include #include #include @@ -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 packet); + class DaemonStateUpdateRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr 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 -#include "../Logger.h" - -#include +#include "../SystemManager.h" namespace Mad { namespace Common { namespace RequestHandlers { -void FSInfoRequestHandler::handlePacket(boost::shared_ptr 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 packet _UNUSED_PARAMETER_, Common::XmlPacket *ret) { // TODO Require authentication std::vector 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::const_iterator fs = fsInfo.begin(); fs != fsInfo.end(); ++fs) { - ret["filesystems"].addEntry(); - XmlPacket::Entry &entry = ret["filesystems"].back(); + for(std::vector::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 +#include "SimpleRequestHandler.h" namespace Mad { namespace Common { namespace RequestHandlers { -class FSInfoRequestHandler : public RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr packet); +class FSInfoRequestHandler : public SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr 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 + * + * 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 . + */ + +#include "SimpleRequestHandler.h" +#include "../Logger.h" + +#include + +namespace Mad { +namespace Common { +namespace RequestHandlers { + +void SimpleRequestHandler::handlePacket(boost::shared_ptr 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 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_COMMON_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, XmlPacket*> handler; + + protected: + virtual void handlePacket(boost::shared_ptr packet); + + SimpleRequestHandler(const std::string &type0, const boost::function2, 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 + * + * 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 . + */ + +#include "SimpleRequestHandlerGroup.h" +#include "SimpleRequestHandler.h" + +namespace Mad { +namespace Common { +namespace RequestHandlers { + +boost::shared_ptr SimpleRequestHandlerGroup::createRequestHandler(const std::string &type) { + std::map, XmlPacket*> >::iterator handler = handlers.find(type); + + if(handler == handlers.end()) + return boost::shared_ptr(); + else + return boost::shared_ptr(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 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_COMMON_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 types; + std::map, XmlPacket*> > handlers; + + protected: + void registerHandler(const std::string &type, const boost::function2, XmlPacket*> &handler) { + types.insert(type); + handlers.insert(std::make_pair(type, handler)); + } + + SimpleRequestHandlerGroup() {} + + public: + virtual const std::set& getPacketTypes() { + return types; + } + + virtual boost::shared_ptr 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 -#include "../SystemBackend.h" -#include "../Logger.h" - -#include +#include "../SystemManager.h" namespace Mad { namespace Common { namespace RequestHandlers { -void StatusRequestHandler::handlePacket(boost::shared_ptr 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 packet _UNUSED_PARAMETER_, Common::XmlPacket *ret) { // TODO Require authentication unsigned long uptime, idleTime; @@ -50,8 +33,6 @@ void StatusRequestHandler::handlePacket(boost::shared_ptr 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 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 +#include "SimpleRequestHandler.h" namespace Mad { namespace Common { namespace RequestHandlers { -class StatusRequestHandler : public RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr packet); +class StatusRequestHandler : public SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr 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 -#include #include namespace Mad { namespace Daemon { namespace RequestHandlers { -void CommandRequestHandler::handlePacket(boost::shared_ptr 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 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 +#include namespace Mad { namespace Daemon { namespace RequestHandlers { -class CommandRequestHandler : public Common::RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr packet); +class CommandRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr 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 #include #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 #include @@ -185,18 +186,20 @@ void ConnectionManager::handleDisconnect(boost::shared_ptr con void ConnectionManager::doInit() { Common::RequestManager::get()->setServer(true); + daemonRequestHandlerGroup.reset(new RequestHandlers::DaemonRequestHandlerGroup); userRequestHandlerGroup.reset(new RequestHandlers::UserRequestHandlerGroup); //Common::RequestManager::get()->registerPacketType("AuthGSSAPI"); - Common::RequestManager::get()->registerPacketType("DaemonCommand"); - Common::RequestManager::get()->registerPacketType("DaemonFSInfo"); + //Common::RequestManager::get()->registerPacketType("DaemonCommand"); + //Common::RequestManager::get()->registerPacketType("DaemonFSInfo"); Common::RequestManager::get()->registerPacketType("FSInfo"); Common::RequestManager::get()->registerPacketType("GetStatus"); - Common::RequestManager::get()->registerPacketType("GetDaemonStatus"); + //Common::RequestManager::get()->registerPacketType("GetDaemonStatus"); Common::RequestManager::get()->registerPacketType("Identify"); Common::RequestManager::get()->registerPacketType("ListHosts"); Common::RequestManager::get()->registerPacketType("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 daemonInfo; + boost::shared_ptr daemonRequestHandlerGroup; boost::shared_ptr 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 - * - * 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 . - */ - -#include "DaemonCommandRequestHandler.h" -#include "../ConnectionManager.h" -#include "../Requests/CommandRequest.h" - -#include - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -void DaemonCommandRequestHandler::handlePacket(boost::shared_ptr 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 daemonCon = ConnectionManager::get()->getDaemonConnection((*packet)["daemon"]); - - boost::shared_ptr 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 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 - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_SERVER_REQUESTHANDLERS_DAEMONCOMMANDREQUESTHANDLER_H_ -#define MAD_SERVER_REQUESTHANDLERS_DAEMONCOMMANDREQUESTHANDLER_H_ - -#include -#include -#include - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -class DaemonCommandRequestHandler : public Common::RequestHandler { - private: - void requestFinished(boost::shared_ptr packet, Net::Exception error); - - protected: - virtual void handlePacket(boost::shared_ptr packet); -}; - -} -} -} - -#endif /* MAD_SERVER_REQUESTHANDLERS_DAEMONCOMMANDREQUESTHANDLER_H_ */ diff --git a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp b/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp deleted file mode 100644 index c66e3d3..0000000 --- a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * DaemonFSInfoRequestHandler.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "DaemonFSInfoRequestHandler.h" -#include "../ConnectionManager.h" -#include -#include - - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -void DaemonFSInfoRequestHandler::handlePacket(boost::shared_ptr packet) { - if(packet->getType() != "DaemonFSInfo") { - 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 daemonCon = ConnectionManager::get()->getDaemonConnection((*packet)["daemon"]); - - boost::shared_ptr request(new Common::Requests::FSInfoRequest); - request->connectSignalFinished(boost::bind(&DaemonFSInfoRequestHandler::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 DaemonFSInfoRequestHandler::requestFinished(boost::shared_ptr 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/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 - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_SERVER_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ -#define MAD_SERVER_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ - -#include -#include -#include - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -class DaemonFSInfoRequestHandler : public Common::RequestHandler { - private: - void requestFinished(boost::shared_ptr packet, Net::Exception error); - - protected: - virtual void handlePacket(boost::shared_ptr 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 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 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 daemons = ConnectionManager::get()->getDaemonList(); for(std::vector::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 +#include namespace Mad { namespace Server { namespace RequestHandlers { -class DaemonListRequestHandler : public Common::RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr packet); +class DaemonListRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + static void handleRequest(boost::shared_ptr packet, Common::XmlPacket *ret); + + public: + DaemonListRequestHandler() : Common::RequestHandlers::SimpleRequestHandler("ListHosts", &DaemonListRequestHandler::handleRequest) {} }; } diff --git a/src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp new file mode 100644 index 0000000..4ff8c40 --- /dev/null +++ b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp @@ -0,0 +1,109 @@ +/* + * DaemonRequestHandlerGroup.cpp + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "DaemonRequestHandlerGroup.h" +#include "../ConnectionManager.h" +#include "../Requests/CommandRequest.h" + +#include +#include +#include + +namespace Mad { +namespace Server { +namespace RequestHandlers { + +void DaemonRequestHandlerGroup::DaemonRequestHandler::handlePacket(boost::shared_ptr 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 + + try { + boost::shared_ptr daemonCon = ConnectionManager::get()->getDaemonConnection((*packet)["daemon"]); + boost::shared_ptr 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); + + request->connectSignalFinished(boost::bind(&DaemonRequestHandlerGroup::DaemonRequestHandler::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); + signalFinished(); + } +} + +void DaemonRequestHandlerGroup::DaemonRequestHandler::requestFinished(boost::shared_ptr 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(); +} + + +DaemonRequestHandlerGroup::DaemonRequestHandlerGroup() { + types.insert("DaemonCommand"); + types.insert("DaemonFSInfo"); + types.insert("GetDaemonStatus"); +} + +boost::shared_ptr DaemonRequestHandlerGroup::createRequestHandler(const std::string &type) { + if(types.find(type) == types.end()) + return boost::shared_ptr(); + else + return boost::shared_ptr(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 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_SERVER_REQUESTHANDLERS_DAEMONREQUESTHANDLERGROUP_H_ +#define MAD_SERVER_REQUESTHANDLERS_DAEMONREQUESTHANDLERGROUP_H_ + +#include +#include + +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 packet, Net::Exception error); + + protected: + virtual void handlePacket(boost::shared_ptr packet); + + public: + DaemonRequestHandler(const std::string &type0) : type(type0) {} + }; + + std::set types; + + public: + DaemonRequestHandlerGroup(); + + virtual const std::set& getPacketTypes() { + return types; + } + + virtual boost::shared_ptr 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 - * - * 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 . - */ - -#include "DaemonStatusRequestHandler.h" -#include "../ConnectionManager.h" -#include -#include - - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -void DaemonStatusRequestHandler::handlePacket(boost::shared_ptr 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 daemonCon = ConnectionManager::get()->getDaemonConnection((*packet)["daemonName"]); - - boost::shared_ptr 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 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/DaemonStatusRequestHandler.h b/src/Server/RequestHandlers/DaemonStatusRequestHandler.h deleted file mode 100644 index 728be4a..0000000 --- a/src/Server/RequestHandlers/DaemonStatusRequestHandler.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * DaemonStatusRequestHandler.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_SERVER_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ -#define MAD_SERVER_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ - -#include -#include -#include - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -class DaemonStatusRequestHandler : public Common::RequestHandler { - private: - void requestFinished(boost::shared_ptr packet, Net::Exception error); - - protected: - virtual void handlePacket(boost::shared_ptr packet); -}; - -} -} -} - -#endif /* MAD_SERVER_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ */ 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 packet) { - if(packet->getType() != "Identify") { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); +void IdentifyRequestHandler::handleRequest(boost::shared_ptr 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 +#include namespace Mad { namespace Server { namespace RequestHandlers { -class IdentifyRequestHandler : public Common::RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr packet); +class IdentifyRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + void handleRequest(boost::shared_ptr 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 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 packet, Common::XmlPacket *ret) { // TODO Require authentication try { @@ -50,12 +37,7 @@ void LogRequestHandler::handlePacket(boost::shared_ptr 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 +#include namespace Mad { namespace Server { namespace RequestHandlers { -class LogRequestHandler : public Common::RequestHandler { - protected: - virtual void handlePacket(boost::shared_ptr packet); +class LogRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { + private: + void handleRequest(boost::shared_ptr 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 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 packet _UNUSED_PARAMETER_, Common::XmlPacket *ret) { boost::shared_ptr > info = UserManager::get()->getUserList(); @@ -138,19 +105,6 @@ UserRequestHandlerGroup::UserRequestHandlerGroup() { registerHandler("ListGroupUsers", &UserRequestHandlerGroup::handleGroupUserListRequest); } -const std::set& UserRequestHandlerGroup::getPacketTypes() { - return types; -} - -boost::shared_ptr UserRequestHandlerGroup::createRequestHandler(const std::string &type) { - std::map, Common::XmlPacket*> >::iterator handler = handlers.find(type); - - if(handler == handlers.end()) - return boost::shared_ptr(); - else - return boost::shared_ptr(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 +#include 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, Common::XmlPacket*> handler; - - protected: - virtual void handlePacket(boost::shared_ptr packet); - - public: - RequestHandler(const std::string &type0, const boost::function2, Common::XmlPacket*> &handler0) : type(type0), handler(handler0) {} - }; - - - std::set types; - std::map, Common::XmlPacket*> > handlers; - - void registerHandler(const std::string &type, const boost::function2, Common::XmlPacket*> &handler) { - types.insert(type); - handlers.insert(std::make_pair(type, handler)); - } - static void handleUserListRequest(boost::shared_ptr packet, Common::XmlPacket *ret); static void handleUserInfoRequest(boost::shared_ptr packet, Common::XmlPacket *ret); static void handleUserGroupListRequest(boost::shared_ptr packet, Common::XmlPacket *ret); @@ -57,9 +36,6 @@ class UserRequestHandlerGroup : public Common::RequestHandlerGroup { public: UserRequestHandlerGroup(); - - virtual const std::set& getPacketTypes(); - virtual boost::shared_ptr createRequestHandler(const std::string &type); }; } -- cgit v1.2.3