diff options
Diffstat (limited to 'src/Core/RequestHandlers')
6 files changed, 97 insertions, 49 deletions
diff --git a/src/Core/RequestHandlers/DaemonCommandRequestHandler.cpp b/src/Core/RequestHandlers/DaemonCommandRequestHandler.cpp index 6782b8d..97a1bdc 100644 --- a/src/Core/RequestHandlers/DaemonCommandRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonCommandRequestHandler.cpp @@ -21,16 +21,20 @@ #include "../ConnectionManager.h" #include <Common/Logger.h> #include <Core/Requests/CommandRequest.h> -#include <Net/Packets/ErrorPacket.h> namespace Mad { namespace Core { namespace RequestHandlers { -void DaemonCommandRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::DAEMON_COMMAND_SHUTDOWN && packet.getType() != Net::Packet::DAEMON_COMMAND_REBOOT) { +void DaemonCommandRequestHandler::handlePacket(Net::Connection *connection, uint16_t rid, const Common::XmlPacket &packet) { + if(packet.getType() != "DaemonCommand") { Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Common::Exception::UNEXPECTED_PACKET); + + connection->send(ret.encode(rid)); signalFinished().emit(); return; @@ -39,28 +43,43 @@ void DaemonCommandRequestHandler::handlePacket(Net::Connection *connection, cons // TODO Require authentication con = connection; - requestId = packet.getRequestId(); + requestId = rid; - std::string daemonName((char*)packet.getData(), packet.getLength()); + std::string command = packet["command"]; try { - Net::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); - Common::RequestManager::get()->sendRequest(daemonCon, std::auto_ptr<Common::RequestBase>( - new Requests::CommandRequest(packet.getType() == Net::Packet::DAEMON_COMMAND_REBOOT, sigc::mem_fun(this, &DaemonCommandRequestHandler::requestFinished)) + Net::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); + Common::RequestManager::get()->sendRequest(daemonCon, std::auto_ptr<Common::XmlRequest>( + new Requests::CommandRequest(command == "reboot", sigc::mem_fun(this, &DaemonCommandRequestHandler::requestFinished)) )); } catch(Common::Exception &e) { - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), 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()); + + con->send(ret.encode(requestId)); } } -void DaemonCommandRequestHandler::requestFinished(const Common::Request<> &request) { +void DaemonCommandRequestHandler::requestFinished(const Common::XmlRequest &request) { try { - request.getResult(); - con->send(Net::Packet(Net::Packet::OK, requestId)); + const Common::XmlPacket &packet = request.getResult(); + + con->send(packet.encode(requestId)); } catch(Common::Exception &e) { - con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, requestId, 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()); + + con->send(ret.encode(requestId)); } signalFinished().emit(); diff --git a/src/Core/RequestHandlers/DaemonCommandRequestHandler.h b/src/Core/RequestHandlers/DaemonCommandRequestHandler.h index bb021ed..9570528 100644 --- a/src/Core/RequestHandlers/DaemonCommandRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonCommandRequestHandler.h @@ -20,23 +20,23 @@ #ifndef MAD_CORE_REQUESTHANDLERS_DAEMONCOMMANDREQUESTHANDLER_H_ #define MAD_CORE_REQUESTHANDLERS_DAEMONCOMMANDREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> -#include <Common/Request.h> +#include <Common/XmlRequestHandler.h> +#include <Common/XmlRequest.h> #include <stdint.h> namespace Mad { namespace Core { namespace RequestHandlers { -class DaemonCommandRequestHandler : public Common::RequestHandler { +class DaemonCommandRequestHandler : public Common::XmlRequestHandler { private: Net::Connection *con; uint16_t requestId; - void requestFinished(const Common::Request<> &request); + void requestFinished(const Common::XmlRequest &request); protected: - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, uint16_t rid, const Common::XmlPacket &packet); public: DaemonCommandRequestHandler() {} diff --git a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp index 3d371a9..c0e4b9f 100644 --- a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp @@ -21,18 +21,21 @@ #include "../ConnectionManager.h" #include <Common/Logger.h> #include <Common/Requests/FSInfoRequest.h> -#include <Net/Packets/ErrorPacket.h> -#include <Net/Packets/FSInfoPacket.h> namespace Mad { namespace Core { namespace RequestHandlers { -void DaemonFSInfoRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::DAEMON_FS_INFO) { +void DaemonFSInfoRequestHandler::handlePacket(Net::Connection *connection, uint16_t rid, const Common::XmlPacket &packet) { + if(packet.getType() != "DaemonFSInfo") { Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Common::Exception::UNEXPECTED_PACKET); + + connection->send(ret.encode(rid)); signalFinished().emit(); return; @@ -41,26 +44,38 @@ void DaemonFSInfoRequestHandler::handlePacket(Net::Connection *connection, const // TODO Require authentication con = connection; - requestId = packet.getRequestId(); - - std::string daemonName((char*)packet.getData(), packet.getLength()); + requestId = rid; try { - Net::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); - Common::RequestManager::get()->sendRequest(daemonCon, std::auto_ptr<Common::RequestBase>(new Common::Requests::FSInfoRequest(sigc::mem_fun(this, &DaemonFSInfoRequestHandler::requestFinished)))); + Net::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); + Common::RequestManager::get()->sendRequest(daemonCon, std::auto_ptr<Common::XmlRequest>(new Common::Requests::FSInfoRequest(sigc::mem_fun(this, &DaemonFSInfoRequestHandler::requestFinished)))); } catch(Common::Exception &e) { - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), 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()); + + con->send(ret.encode(requestId)); } } -void DaemonFSInfoRequestHandler::requestFinished(const Common::Request<Net::Packets::FSInfoPacket> &request) { +void DaemonFSInfoRequestHandler::requestFinished(const Common::XmlRequest &request) { try { - const Net::Packet &packet = request.getResult(); - con->send(Net::Packet(Net::Packet::OK, requestId, packet.getData(), packet.getLength())); + const Common::XmlPacket &packet = request.getResult(); + con->send(packet.encode(requestId)); } catch(Common::Exception &e) { - con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, requestId, 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()); + + con->send(ret.encode(requestId)); } signalFinished().emit(); diff --git a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h index 2f6c69c..68b8760 100644 --- a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h @@ -20,8 +20,8 @@ #ifndef MAD_CORE_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ #define MAD_CORE_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> -#include <Common/Request.h> +#include <Common/XmlRequestHandler.h> +#include <Common/XmlRequest.h> #include <stdint.h> namespace Mad { @@ -35,15 +35,15 @@ class FSInfoPacket; namespace Core { namespace RequestHandlers { -class DaemonFSInfoRequestHandler : public Common::RequestHandler { +class DaemonFSInfoRequestHandler : public Common::XmlRequestHandler { private: Net::Connection *con; uint16_t requestId; - void requestFinished(const Common::Request<Net::Packets::FSInfoPacket> &request); + void requestFinished(const Common::XmlRequest &request); protected: - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, uint16_t rid, const Common::XmlPacket &packet); public: DaemonFSInfoRequestHandler() {} diff --git a/src/Core/RequestHandlers/IdentifyRequestHandler.cpp b/src/Core/RequestHandlers/IdentifyRequestHandler.cpp index 9666f7e..4bf921e 100644 --- a/src/Core/RequestHandlers/IdentifyRequestHandler.cpp +++ b/src/Core/RequestHandlers/IdentifyRequestHandler.cpp @@ -20,18 +20,23 @@ #include "IdentifyRequestHandler.h" #include "../ConnectionManager.h" #include <Common/Logger.h> +#include <Common/XmlPacket.h> #include <Net/Connection.h> -#include <Net/Packets/ErrorPacket.h> namespace Mad { namespace Core { namespace RequestHandlers { -void IdentifyRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::IDENTIFY) { +void IdentifyRequestHandler::handlePacket(Net::Connection *connection, uint16_t requestId, const Common::XmlPacket &packet) { + if(packet.getType() != "Identify") { Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Common::Exception::UNEXPECTED_PACKET); + + connection->send(ret.encode(requestId)); signalFinished().emit(); return; @@ -39,12 +44,21 @@ void IdentifyRequestHandler::handlePacket(Net::Connection *connection, const Net // TODO Require authentication try { - ConnectionManager::get()->identifyDaemonConnection(connection, std::string((const char*)packet.getData(), packet.getLength())); + ConnectionManager::get()->identifyDaemonConnection(connection, packet["hostname"]); - connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId())); + Common::XmlPacket ret; + ret.setType("OK"); + connection->send(ret.encode(requestId)); } catch(Common::Exception &e) { - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), 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()); + + connection->send(ret.encode(requestId)); } signalFinished().emit(); diff --git a/src/Core/RequestHandlers/IdentifyRequestHandler.h b/src/Core/RequestHandlers/IdentifyRequestHandler.h index f38fff6..877ebf2 100644 --- a/src/Core/RequestHandlers/IdentifyRequestHandler.h +++ b/src/Core/RequestHandlers/IdentifyRequestHandler.h @@ -20,15 +20,15 @@ #ifndef MAD_CORE_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ #define MAD_CORE_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> +#include <Common/XmlRequestHandler.h> namespace Mad { namespace Core { namespace RequestHandlers { -class IdentifyRequestHandler : public Common::RequestHandler { +class IdentifyRequestHandler : public Common::XmlRequestHandler { protected: - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, uint16_t requestId, const Common::XmlPacket &packet); public: IdentifyRequestHandler() {} |