diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-02-24 22:03:34 +0100 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-02-24 22:03:34 +0100 |
commit | f85b6d5ab264910f272e69ce5997cebec54886ce (patch) | |
tree | 06b801d5b11c2d8a2dfae16b698ebaf0b74f429b /src/Core/RequestHandlers | |
parent | 8d629ca48e9b5530416127e2e52c945fe1f9ee52 (diff) | |
download | mad-f85b6d5ab264910f272e69ce5997cebec54886ce.tar mad-f85b6d5ab264910f272e69ce5997cebec54886ce.zip |
Requests Status, DaemonStatus und UserList in XmlRequests umgewandelt
Diffstat (limited to 'src/Core/RequestHandlers')
4 files changed, 69 insertions, 34 deletions
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 <Common/Logger.h> #include <Common/Requests/StatusRequest.h> -#include <Net/Packets/ErrorPacket.h> -#include <Net/Packets/HostStatusPacket.h> 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<Common::RequestBase>(new Common::Requests::StatusRequest(sigc::mem_fun(this, &DaemonStatusRequestHandler::requestFinished)))); + Common::RequestManager::get()->sendRequest(daemonCon, std::auto_ptr<Common::XmlRequestBase>(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<Net::Packets::HostStatusPacket> &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 <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 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<Net::Packets::HostStatusPacket> &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 <Common/Exception.h> #include <Common/Logger.h> +#include <Common/XmlPacket.h> #include <Net/Connection.h> -#include <Net/Packets/ErrorPacket.h> -#include <Net/Packets/UserListPacket.h> 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<unsigned long, Common::UserInfo> &info) { - std::vector<Common::UserInfo> userList; + Common::XmlPacket ret; + ret.setType("OK"); + ret.addList("users"); - for(std::map<unsigned long, Common::UserInfo>::const_iterator user = info.begin(); user != info.end(); ++user) - userList.push_back(user->second); + for(std::map<unsigned long, Common::UserInfo>::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 <Common/RequestHandler.h> +#include <Common/XmlRequestHandler.h> #include <Common/UserInfo.h> #include <map> @@ -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<unsigned long, Common::UserInfo> &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() {} |