From f85b6d5ab264910f272e69ce5997cebec54886ce Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 24 Feb 2009 22:03:34 +0100 Subject: Requests Status, DaemonStatus und UserList in XmlRequests umgewandelt --- .../RequestHandlers/DaemonStatusRequestHandler.cpp | 43 +++++++++++++++------- .../RequestHandlers/DaemonStatusRequestHandler.h | 12 +++--- .../RequestHandlers/UserListRequestHandler.cpp | 40 ++++++++++++++------ src/Core/RequestHandlers/UserListRequestHandler.h | 8 ++-- 4 files changed, 69 insertions(+), 34 deletions(-) (limited to 'src/Core/RequestHandlers') diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp index 80b61ba..a1dfdee 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp @@ -21,18 +21,21 @@ #include "../ConnectionManager.h" #include #include -#include -#include namespace Mad { namespace Core { namespace RequestHandlers { -void DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::DAEMON_STATUS) { +void DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, uint16_t requestId, const Common::XmlPacket &packet) { + if(packet.getType() != "GetDaemonStatus") { 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); + + con->send(ret.encode(requestId)); signalFinished().emit(); return; @@ -41,26 +44,40 @@ void DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, const // TODO Require authentication con = connection; - requestId = packet.getRequestId(); + rid = requestId; - std::string daemonName((char*)packet.getData(), packet.getLength()); + std::string daemonName = packet["daemonName"]; try { Net::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); - Common::RequestManager::get()->sendRequest(daemonCon, std::auto_ptr(new Common::Requests::StatusRequest(sigc::mem_fun(this, &DaemonStatusRequestHandler::requestFinished)))); + Common::RequestManager::get()->sendRequest(daemonCon, std::auto_ptr(new Common::Requests::StatusRequest(sigc::mem_fun(this, &DaemonStatusRequestHandler::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(rid)); } } -void DaemonStatusRequestHandler::requestFinished(const Common::Request &request) { +void DaemonStatusRequestHandler::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(rid)); } 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(rid)); } signalFinished().emit(); diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h index 36fabc0..30b2ae1 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h @@ -20,8 +20,8 @@ #ifndef MAD_CORE_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ #define MAD_CORE_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ -#include -#include +#include +#include #include namespace Mad { @@ -35,15 +35,15 @@ class HostStatusPacket; namespace Core { namespace RequestHandlers { -class DaemonStatusRequestHandler : public Common::RequestHandler { +class DaemonStatusRequestHandler : public Common::XmlRequestHandler { private: Net::Connection *con; - uint16_t requestId; + uint16_t rid; - 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 requestId, const Common::XmlPacket &packet); public: DaemonStatusRequestHandler() {} diff --git a/src/Core/RequestHandlers/UserListRequestHandler.cpp b/src/Core/RequestHandlers/UserListRequestHandler.cpp index 01c2eb3..176fde9 100644 --- a/src/Core/RequestHandlers/UserListRequestHandler.cpp +++ b/src/Core/RequestHandlers/UserListRequestHandler.cpp @@ -19,19 +19,24 @@ #include "UserListRequestHandler.h" #include "../UserBackend.h" +#include #include +#include #include -#include -#include namespace Mad { namespace Core { namespace RequestHandlers { -void UserListRequestHandler::handlePacket(Net::Connection *con, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::USERS_LIST) { +void UserListRequestHandler::handlePacket(Net::Connection *con, uint16_t requestId, const Common::XmlPacket &packet) { + if(packet.getType() != "ListUsers") { 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); + + con->send(ret.encode(requestId)); signalFinished().emit(); return; @@ -40,21 +45,34 @@ void UserListRequestHandler::handlePacket(Net::Connection *con, const Net::Packe // TODO Require authentication connection = con; - requestId = packet.getRequestId(); + rid = requestId; if(!UserBackend::getUserList(sigc::mem_fun(this, &UserListRequestHandler::userListHandler))) { - con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::NOT_IMPLEMENTED))); + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Common::Exception::NOT_IMPLEMENTED); + + con->send(ret.encode(requestId)); signalFinished().emit(); } } void UserListRequestHandler::userListHandler(const std::map &info) { - std::vector userList; + Common::XmlPacket ret; + ret.setType("OK"); + ret.addList("users"); - for(std::map::const_iterator user = info.begin(); user != info.end(); ++user) - userList.push_back(user->second); + for(std::map::const_iterator user = info.begin(); user != info.end(); ++user) { + ret["users"].addEntry(); + Common::XmlPacket::Entry &entry = ret["users"].back(); + + entry.add("uid", user->second.getUid()); + entry.add("gid", user->second.getGid()); + entry.add("username", user->second.getUsername()); + entry.add("fullName", user->second.getFullName()); + } - connection->send(Net::Packets::UserListPacket(Net::Packet::OK, requestId, userList)); + connection->send(ret.encode(rid)); signalFinished().emit(); } diff --git a/src/Core/RequestHandlers/UserListRequestHandler.h b/src/Core/RequestHandlers/UserListRequestHandler.h index 92eddb7..7739809 100644 --- a/src/Core/RequestHandlers/UserListRequestHandler.h +++ b/src/Core/RequestHandlers/UserListRequestHandler.h @@ -20,7 +20,7 @@ #ifndef MAD_CORE_REQUESTHANDLERS_USERLISTREQUESTHANDLER_H_ #define MAD_CORE_REQUESTHANDLERS_USERLISTREQUESTHANDLER_H_ -#include +#include #include #include @@ -30,15 +30,15 @@ namespace Mad { namespace Core { namespace RequestHandlers { -class UserListRequestHandler : public Common::RequestHandler { +class UserListRequestHandler : public Common::XmlRequestHandler { private: Net::Connection *connection; - uint16_t requestId; + uint16_t rid; void userListHandler(const std::map &info); protected: - virtual void handlePacket(Net::Connection *con, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *con, uint16_t requestId, const Common::XmlPacket &packet); public: UserListRequestHandler() {} -- cgit v1.2.3