From 63907817cb057f497f03a28016d408885cbe41ea Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 Mar 2009 00:51:00 +0100 Subject: Alle uebrigen Requests ausser GSSAPIAuthRequest in XmlRequests umgewandelt --- src/Core/ConnectionManager.cpp | 18 ++++----- .../DaemonCommandRequestHandler.cpp | 47 +++++++++++++++------- .../RequestHandlers/DaemonCommandRequestHandler.h | 10 ++--- .../RequestHandlers/DaemonFSInfoRequestHandler.cpp | 45 ++++++++++++++------- .../RequestHandlers/DaemonFSInfoRequestHandler.h | 10 ++--- .../RequestHandlers/IdentifyRequestHandler.cpp | 28 +++++++++---- src/Core/RequestHandlers/IdentifyRequestHandler.h | 6 +-- src/Core/Requests/CommandRequest.cpp | 16 +++++--- src/Core/Requests/CommandRequest.h | 8 ++-- 9 files changed, 119 insertions(+), 69 deletions(-) (limited to 'src/Core') diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 607f80b..6136edb 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -135,15 +135,14 @@ void ConnectionManager::doInit() { Net::Connection::init(); - Common::RequestManager::get()->registerPacketType(Net::Packet::FS_INFO); - Common::RequestManager::get()->registerPacketType(Net::Packet::DAEMON_COMMAND_REBOOT); - Common::RequestManager::get()->registerPacketType(Net::Packet::DAEMON_COMMAND_SHUTDOWN); - Common::RequestManager::get()->registerPacketType(Net::Packet::DAEMON_FS_INFO); Common::RequestManager::get()->registerPacketType(Net::Packet::GSSAPI_AUTH); - Common::RequestManager::get()->registerPacketType(Net::Packet::IDENTIFY); + 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("Identify"); Common::RequestManager::get()->registerPacketType("ListHosts"); Common::RequestManager::get()->registerPacketType("ListUsers"); Common::RequestManager::get()->registerPacketType("Log"); @@ -156,15 +155,14 @@ void ConnectionManager::doDeinit() { for(std::list::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) delete *con; - Common::RequestManager::get()->unregisterPacketType(Net::Packet::FS_INFO); - Common::RequestManager::get()->unregisterPacketType(Net::Packet::DAEMON_COMMAND_REBOOT); - Common::RequestManager::get()->unregisterPacketType(Net::Packet::DAEMON_COMMAND_SHUTDOWN); - Common::RequestManager::get()->unregisterPacketType(Net::Packet::DAEMON_FS_INFO); Common::RequestManager::get()->unregisterPacketType(Net::Packet::GSSAPI_AUTH); - Common::RequestManager::get()->unregisterPacketType(Net::Packet::IDENTIFY); + 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("Identify"); Common::RequestManager::get()->unregisterPacketType("ListHosts"); Common::RequestManager::get()->unregisterPacketType("ListUsers"); Common::RequestManager::get()->unregisterPacketType("Log"); 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 #include -#include 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( - 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( + 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 -#include +#include +#include #include 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 #include -#include -#include 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(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(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 &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 -#include +#include +#include #include 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 &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 +#include #include -#include 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 +#include 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() {} diff --git a/src/Core/Requests/CommandRequest.cpp b/src/Core/Requests/CommandRequest.cpp index 35d4dd2..a34a551 100644 --- a/src/Core/Requests/CommandRequest.cpp +++ b/src/Core/Requests/CommandRequest.cpp @@ -26,20 +26,24 @@ namespace Core { namespace Requests { void CommandRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(reboot ? Net::Packet::COMMAND_REBOOT : Net::Packet::COMMAND_SHUTDOWN, requestId)); + Common::XmlPacket packet; + packet.setType("Command"); + packet.add("command", reboot ? "reboot" : "shutdown"); + + connection->send(packet.encode(requestId)); } -void CommandRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() == Net::Packet::ERROR) { - finishWithError(Net::Packets::ErrorPacket(packet).getException()); +void CommandRequest::handlePacket(Net::Connection*, uint16_t, const Common::XmlPacket &packet) { + if(packet.getType() == "Error") { + finishWithError(Common::Exception(packet["Where"], packet["ErrorCode"], packet["SubCode"], packet["SubSubCode"])); return; } - else if(packet.getType() != Net::Packet::OK) { + else if(packet.getType() != "OK") { finishWithError(Common::Exception(Common::Exception::UNEXPECTED_PACKET)); return; // TODO Logging } - finish(); + finish(packet); } } diff --git a/src/Core/Requests/CommandRequest.h b/src/Core/Requests/CommandRequest.h index 181fa4d..534fcef 100644 --- a/src/Core/Requests/CommandRequest.h +++ b/src/Core/Requests/CommandRequest.h @@ -20,22 +20,22 @@ #ifndef MAD_CORE_REQUESTS_COMMANDREQUEST_H_ #define MAD_CORE_REQUESTS_COMMANDREQUEST_H_ -#include +#include namespace Mad { namespace Core { namespace Requests { -class CommandRequest : public Common::Request<> { +class CommandRequest : public Common::XmlRequest { private: bool reboot; protected: virtual void sendRequest(Net::Connection *connection, uint16_t requestId); - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection*, uint16_t, const Common::XmlPacket &packet); public: - CommandRequest(bool reboot0, slot_type slot) : Common::Request<>(slot), reboot(reboot0) {} + CommandRequest(bool reboot0, slot_type slot) : Common::XmlRequest(slot), reboot(reboot0) {} }; } -- cgit v1.2.3