diff options
Diffstat (limited to 'src/Server/RequestHandlers')
16 files changed, 133 insertions, 478 deletions
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/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 <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_DAEMONSTATUSREQUESTHANDLER_H_ -#define MAD_SERVER_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ - -#include <Common/RequestHandler.h> -#include <Common/Request.h> -#include <stdint.h> - -namespace Mad { -namespace Server { -namespace RequestHandlers { - -class DaemonStatusRequestHandler : 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_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<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); }; } |