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/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 +----- 17 files changed, 209 insertions(+), 554 deletions(-) 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 (limited to 'src/Server/RequestHandlers') 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