diff options
Diffstat (limited to 'src/Daemon/RequestHandlers')
-rw-r--r-- | src/Daemon/RequestHandlers/CommandRequestHandler.cpp | 48 | ||||
-rw-r--r-- | src/Daemon/RequestHandlers/CommandRequestHandler.h | 6 |
2 files changed, 33 insertions, 21 deletions
diff --git a/src/Daemon/RequestHandlers/CommandRequestHandler.cpp b/src/Daemon/RequestHandlers/CommandRequestHandler.cpp index c6750ba..3f4ef17 100644 --- a/src/Daemon/RequestHandlers/CommandRequestHandler.cpp +++ b/src/Daemon/RequestHandlers/CommandRequestHandler.cpp @@ -21,8 +21,8 @@ #include <Common/Exception.h> #include <Common/Logger.h> #include <Common/SystemBackend.h> +#include <Common/XmlPacket.h> #include <Net/Connection.h> -#include <Net/Packets/ErrorPacket.h> #include <sigc++/bind.h> @@ -30,37 +30,49 @@ namespace Mad { namespace Daemon { namespace RequestHandlers { -void CommandRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - // TODO Require authentication +void CommandRequestHandler::handlePacket(Net::Connection *connection, uint16_t requestId, const Common::XmlPacket &packet) { + if(packet.getType() != "Command") { + Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - switch(packet.getType()) { - case Net::Packet::COMMAND_SHUTDOWN: - if(Common::SystemBackend::shutdown(sigc::bind(sigc::mem_fun(this, &CommandRequestHandler::sendReply), connection, packet.getRequestId()))) - return; + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Common::Exception::UNEXPECTED_PACKET); - break; + connection->send(ret.encode(requestId)); - case Net::Packet::COMMAND_REBOOT: - if(Common::SystemBackend::reboot(sigc::bind(sigc::mem_fun(this, &CommandRequestHandler::sendReply), connection, packet.getRequestId()))) - return; + signalFinished().emit(); + return; + } - break; + // TODO Require authentication + // TODO Error handling - default: - 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))); + std::string command = packet["command"]; - signalFinished().emit(); + if(command == "reboot") { + if(Common::SystemBackend::shutdown(sigc::bind(sigc::mem_fun(this, &CommandRequestHandler::sendReply), connection, requestId))) + return; + } + else { + if(Common::SystemBackend::reboot(sigc::bind(sigc::mem_fun(this, &CommandRequestHandler::sendReply), connection, requestId))) return; } - connection->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); + + connection->send(ret.encode(requestId)); signalFinished().emit(); } void CommandRequestHandler::sendReply(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::OK, requestId)); + Common::XmlPacket packet; + packet.setType("OK"); + + connection->send(packet.encode(requestId)); + signalFinished().emit(); } diff --git a/src/Daemon/RequestHandlers/CommandRequestHandler.h b/src/Daemon/RequestHandlers/CommandRequestHandler.h index 5066c1d..86391f2 100644 --- a/src/Daemon/RequestHandlers/CommandRequestHandler.h +++ b/src/Daemon/RequestHandlers/CommandRequestHandler.h @@ -20,19 +20,19 @@ #ifndef MAD_DAEMON_REQUESTHANDLERS_COMMANDREQUESTHANDLER_H_ #define MAD_DAEMON_REQUESTHANDLERS_COMMANDREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> +#include <Common/XmlRequestHandler.h> #include <stdint.h> namespace Mad { namespace Daemon { namespace RequestHandlers { -class CommandRequestHandler : public Common::RequestHandler { +class CommandRequestHandler : public Common::XmlRequestHandler { private: void sendReply(Net::Connection *connection, uint16_t requestId); 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: CommandRequestHandler() {} |