diff options
Diffstat (limited to 'src')
36 files changed, 314 insertions, 383 deletions
diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index d98b041..59de7c4 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -21,7 +21,6 @@ #include <Common/HostInfo.h> #include <Common/Logger.h> #include <Common/XmlRequest.h> -#include <Net/Packets/FSInfoPacket.h> #include <cmath> #include <iostream> @@ -34,19 +33,19 @@ namespace Client { CommandManager CommandManager::commandManager; -void CommandManager::printFSInfo(const Net::Packets::FSInfoPacket &packet) { +void CommandManager::printFSInfo(const Common::XmlPacket &packet) { const std::string units[] = { "kB", "MB", "GB", "TB", "" }; - const std::vector<Common::SystemBackend::FSInfo>& fsList = packet.getFSInfo(); + for(size_t i = 0; i < packet["filesystems"].getSize(); ++i) { + const Common::XmlPacket::Entry &fs = packet["filesystems"][i]; - for(std::vector<Common::SystemBackend::FSInfo>::const_iterator fs = fsList.begin(); fs != fsList.end(); ++fs) { unsigned usedUnit = 0, totalUnit = 0; - float used = fs->used; - float total = fs->total; - float available = fs->available; + float used = fs["usedSize"]; + float total = fs["totalSize"]; + float available = fs["availableSize"]; while(used >= 1024 && !units[usedUnit+1].empty()) { ++usedUnit; @@ -59,7 +58,11 @@ void CommandManager::printFSInfo(const Net::Packets::FSInfoPacket &packet) { total /= 1024; } - std::string nameString = fs->mountedOn + " (" + fs->fsName + ")"; + + std::string name = fs["name"]; + std::string mountedOn = fs["mountedOn"]; + + std::string nameString = mountedOn + " (" + name + ")"; if(nameString.length() < 32) { nameString.resize(32, ' '); @@ -133,7 +136,7 @@ void CommandManager::printHostStatus(const Common::XmlPacket &packet) { } -void CommandManager::daemonCommandRequestFinished(const Common::Request<> &request) { +void CommandManager::daemonCommandRequestFinished(const Common::XmlRequest &request) { try { request.getResult(); } @@ -144,9 +147,9 @@ void CommandManager::daemonCommandRequestFinished(const Common::Request<> &reque requestFinished(); } -void CommandManager::daemonFSInfoRequestFinished(const Common::Request<Net::Packets::FSInfoPacket> &request) { +void CommandManager::daemonFSInfoRequestFinished(const Common::XmlRequest &request) { try { - const Net::Packets::FSInfoPacket &packet = request.getResult(); + const Common::XmlPacket &packet = request.getResult(); std::cout << "Host file system usage:" << std::endl; printFSInfo(packet); } @@ -170,7 +173,7 @@ void CommandManager::daemonStatusRequestFinished(const Common::XmlRequest &reque requestFinished(); } -void CommandManager::disconnectRequestFinished(const Common::Request<> &request) { +void CommandManager::disconnectRequestFinished(const Common::XmlRequest &request) { try { request.getResult(); disconnect = true; @@ -182,9 +185,9 @@ void CommandManager::disconnectRequestFinished(const Common::Request<> &request) requestFinished(); } -void CommandManager::fsInfoRequestFinished(const Common::Request<Net::Packets::FSInfoPacket> &request) { +void CommandManager::fsInfoRequestFinished(const Common::XmlRequest &request) { try { - const Net::Packets::FSInfoPacket &packet = request.getResult(); + const Common::XmlPacket &packet = request.getResult(); std::cout << "Server file system usage:" << std::endl; printFSInfo(packet); } diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index fb90070..e2e5ff4 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -29,14 +29,6 @@ class XmlPacket; class XmlRequest; } -namespace Net { -namespace Packets { -class FSInfoPacket; -class HostStatusPacket; -class HostListPacket; -} -} - namespace Client { class CommandManager { @@ -57,14 +49,14 @@ class CommandManager { finished(); } - void printFSInfo(const Net::Packets::FSInfoPacket &packet); + void printFSInfo(const Common::XmlPacket &packet); void printHostStatus(const Common::XmlPacket &packet); - void daemonCommandRequestFinished(const Common::Request<> &request); - void daemonFSInfoRequestFinished(const Common::Request<Net::Packets::FSInfoPacket> &request); + void daemonCommandRequestFinished(const Common::XmlRequest &request); + void daemonFSInfoRequestFinished(const Common::XmlRequest &request); void daemonStatusRequestFinished(const Common::XmlRequest &request); - void disconnectRequestFinished(const Common::Request<> &request); - void fsInfoRequestFinished(const Common::Request<Net::Packets::FSInfoPacket> &request); + void disconnectRequestFinished(const Common::XmlRequest &request); + void fsInfoRequestFinished(const Common::XmlRequest &request); void statusRequestFinished(const Common::XmlRequest &request); void userListRequestFinished(const Common::XmlRequest &request); diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 878e413..526f434 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -110,9 +110,9 @@ std::map<std::string, Common::HostInfo> CommandParser::parseHostList(const std:: void CommandParser::fsinfoCommand(const std::vector<std::string> &args) { if(args.size() == 1) - Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::FSInfoRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::fsInfoRequestFinished)))); + Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequest>(new Common::Requests::FSInfoRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::fsInfoRequestFinished)))); else if(args.size() == 2) - Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonFSInfoRequest(args[1], sigc::mem_fun(CommandManager::get(), &CommandManager::daemonFSInfoRequestFinished)))); + Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequest>(new Requests::DaemonFSInfoRequest(args[1], sigc::mem_fun(CommandManager::get(), &CommandManager::daemonFSInfoRequestFinished)))); else { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("fsinfo"); @@ -213,7 +213,7 @@ void CommandParser::rebootCommand(const std::vector<std::string> &args) { std::map<std::string, Common::HostInfo> hosts = parseHostList(std::vector<std::string>(args.begin()+1, args.end()), true); for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) { - Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>( + Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequest>( new Requests::DaemonCommandRequest(host->first, true, sigc::mem_fun(CommandManager::get(), &CommandManager::daemonCommandRequestFinished)) )); @@ -231,7 +231,7 @@ void CommandParser::shutdownCommand(const std::vector<std::string> &args) { std::map<std::string, Common::HostInfo> hosts = parseHostList(std::vector<std::string>(args.begin()+1, args.end()), true); for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) { - Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>( + Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequest>( new Requests::DaemonCommandRequest(host->first, false, sigc::mem_fun(CommandManager::get(), &CommandManager::daemonCommandRequestFinished)) )); @@ -262,7 +262,7 @@ void CommandParser::listUsersCommand(const std::vector<std::string>&) { void CommandParser::exitCommand(const std::vector<std::string>&) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::DisconnectRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::disconnectRequestFinished)))); + Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequest>(new Common::Requests::DisconnectRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::disconnectRequestFinished)))); } bool CommandParser::parse(const std::string &cmd) { diff --git a/src/Client/Requests/DaemonCommandRequest.cpp b/src/Client/Requests/DaemonCommandRequest.cpp index d27c3fb..6e82c60 100644 --- a/src/Client/Requests/DaemonCommandRequest.cpp +++ b/src/Client/Requests/DaemonCommandRequest.cpp @@ -18,28 +18,33 @@ */ #include "DaemonCommandRequest.h" +#include <Common/XmlPacket.h> #include <Net/Connection.h> -#include <Net/Packets/ErrorPacket.h> namespace Mad { namespace Client { namespace Requests { void DaemonCommandRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(reboot ? Net::Packet::DAEMON_COMMAND_REBOOT : Net::Packet::DAEMON_COMMAND_SHUTDOWN, requestId, daemon.c_str(), daemon.length())); + Common::XmlPacket packet; + packet.setType("DaemonCommand"); + packet.add("command", reboot ? "reboot" : "shutdown"); + packet.add("daemon", daemon); + + connection->send(packet.encode(requestId)); } -void DaemonCommandRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() == Net::Packet::ERROR) { - finishWithError(Net::Packets::ErrorPacket(packet).getException()); +void DaemonCommandRequest::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/Client/Requests/DaemonCommandRequest.h b/src/Client/Requests/DaemonCommandRequest.h index 439462f..8575123 100644 --- a/src/Client/Requests/DaemonCommandRequest.h +++ b/src/Client/Requests/DaemonCommandRequest.h @@ -20,24 +20,24 @@ #ifndef MAD_CLIENT_REQUEST_DAEMONCOMMANDREQUEST_H_ #define MAD_CLIENT_REQUEST_DAEMONCOMMANDREQUEST_H_ -#include <Common/Request.h> +#include <Common/XmlRequest.h> #include <string> namespace Mad { namespace Client { namespace Requests { -class DaemonCommandRequest : public Common::Request<> { +class DaemonCommandRequest : public Common::XmlRequest { private: std::string daemon; 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: - DaemonCommandRequest(const std::string &daemon0, bool reboot0, slot_type slot) : Common::Request<>(slot), daemon(daemon0), reboot(reboot0) {} + DaemonCommandRequest(const std::string &daemon0, bool reboot0, slot_type slot) : Common::XmlRequest(slot), daemon(daemon0), reboot(reboot0) {} }; } diff --git a/src/Client/Requests/DaemonFSInfoRequest.cpp b/src/Client/Requests/DaemonFSInfoRequest.cpp index eb26cf4..e179f46 100644 --- a/src/Client/Requests/DaemonFSInfoRequest.cpp +++ b/src/Client/Requests/DaemonFSInfoRequest.cpp @@ -26,20 +26,24 @@ namespace Client { namespace Requests { void DaemonFSInfoRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::DAEMON_FS_INFO, requestId, daemon.c_str(), daemon.length())); + Common::XmlPacket packet; + packet.setType("DaemonFSInfo"); + packet.add("daemon", daemon); + + connection->send(packet.encode(requestId)); } -void DaemonFSInfoRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() == Net::Packet::ERROR) { - finishWithError(Net::Packets::ErrorPacket(packet).getException()); +void DaemonFSInfoRequest::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(Net::Packets::FSInfoPacket(packet)); + finish(packet); } } diff --git a/src/Client/Requests/DaemonFSInfoRequest.h b/src/Client/Requests/DaemonFSInfoRequest.h index d966b8d..48a201f 100644 --- a/src/Client/Requests/DaemonFSInfoRequest.h +++ b/src/Client/Requests/DaemonFSInfoRequest.h @@ -20,8 +20,7 @@ #ifndef MAD_CLIENT_REQUESTS_DAEMONFSINFOREQUEST_H_ #define MAD_CLIENT_REQUESTS_DAEMONFSINFOREQUEST_H_ -#include <Common/Request.h> -#include <Net/Packets/FSInfoPacket.h> +#include <Common/XmlRequest.h> #include <string> @@ -29,16 +28,16 @@ namespace Mad { namespace Client { namespace Requests { -class DaemonFSInfoRequest : public Common::Request<Net::Packets::FSInfoPacket> { +class DaemonFSInfoRequest : public Common::XmlRequest { private: std::string daemon; 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: - DaemonFSInfoRequest(const std::string &daemon0, slot_type slot) : Common::Request<Net::Packets::FSInfoPacket>(slot), daemon(daemon0) {} + DaemonFSInfoRequest(const std::string &daemon0, slot_type slot) : Common::XmlRequest(slot), daemon(daemon0) {} }; } diff --git a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp index 6a5ca99..346b6de 100644 --- a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp +++ b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp @@ -18,24 +18,34 @@ */ #include "DisconnectRequestHandler.h" +#include "../Exception.h" #include "../Logger.h" +#include "../XmlPacket.h" #include <Net/Connection.h> -#include <Net/Packets/ErrorPacket.h> namespace Mad { namespace Common { namespace RequestHandlers { -void DisconnectRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::DISCONNECT) { +void DisconnectRequestHandler::handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet) { + if(packet.getType() != "Disconnect") { Logger::log(Logger::ERROR, "Received an unexpected packet."); - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Exception::UNEXPECTED_PACKET); + + connection->send(ret.encode(requestId)); signalFinished().emit(); return; } - connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId())); + XmlPacket ret; + ret.setType("OK"); + + connection->send(ret.encode(requestId)); + connection->disconnect(); signalFinished().emit(); diff --git a/src/Common/RequestHandlers/DisconnectRequestHandler.h b/src/Common/RequestHandlers/DisconnectRequestHandler.h index f0ca85f..23d07dc 100644 --- a/src/Common/RequestHandlers/DisconnectRequestHandler.h +++ b/src/Common/RequestHandlers/DisconnectRequestHandler.h @@ -20,15 +20,15 @@ #ifndef MAD_COMMON_REQUESTHANDLERS_DISCONNECTREQUESTHANDLER_H_ #define MAD_COMMON_REQUESTHANDLERS_DISCONNECTREQUESTHANDLER_H_ -#include "../RequestHandler.h" +#include "../XmlRequestHandler.h" namespace Mad { namespace Common { namespace RequestHandlers { -class DisconnectRequestHandler : public RequestHandler { +class DisconnectRequestHandler : public XmlRequestHandler { protected: - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet); public: DisconnectRequestHandler() {} diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp index 5d71277..878dd7c 100644 --- a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp @@ -18,19 +18,24 @@ */ #include "FSInfoRequestHandler.h" +#include "../Exception.h" #include "../Logger.h" +#include "../XmlPacket.h" #include <Net/Connection.h> -#include <Net/Packets/ErrorPacket.h> -#include <Net/Packets/FSInfoPacket.h> namespace Mad { namespace Common { namespace RequestHandlers { -void FSInfoRequestHandler::handlePacket(Net::Connection *con, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::FS_INFO) { +void FSInfoRequestHandler::handlePacket(Net::Connection *con, uint16_t rid, const XmlPacket &packet) { + if(packet.getType() != "FSInfo") { Logger::log(Logger::ERROR, "Received an unexpected packet."); - con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Exception(Exception::UNEXPECTED_PACKET))); + + XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Exception::UNEXPECTED_PACKET); + + connection->send(ret.encode(rid)); signalFinished().emit(); return; @@ -39,16 +44,36 @@ void FSInfoRequestHandler::handlePacket(Net::Connection *con, const Net::Packet // TODO Require authentication connection = con; - requestId = packet.getRequestId(); + requestId = rid; if(!SystemBackend::getFSInfo(sigc::mem_fun(this, &FSInfoRequestHandler::fsInfoHandler))) { - con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Exception(Exception::NOT_IMPLEMENTED))); + XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Exception::NOT_IMPLEMENTED); + + connection->send(ret.encode(requestId)); + signalFinished().emit(); } } void FSInfoRequestHandler::fsInfoHandler(const std::vector<SystemBackend::FSInfo> &info) { - connection->send(Net::Packets::FSInfoPacket(Net::Packet::OK, requestId, info)); + XmlPacket ret; + ret.setType("OK"); + ret.addList("filesystems"); + + for(std::vector<SystemBackend::FSInfo>::const_iterator fs = info.begin(); fs != info.end(); ++fs) { + ret["filesystems"].addEntry(); + XmlPacket::Entry &entry = ret["filesystems"].back(); + + entry.add("name", fs->fsName); + entry.add("mountedOn", fs->mountedOn); + entry.add("totalSize", fs->total); + entry.add("usedSize", fs->used); + entry.add("availableSize", fs->available); + } + + connection->send(ret.encode(requestId)); signalFinished().emit(); } diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.h b/src/Common/RequestHandlers/FSInfoRequestHandler.h index 8de89ce..32e7a08 100644 --- a/src/Common/RequestHandlers/FSInfoRequestHandler.h +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.h @@ -20,7 +20,7 @@ #ifndef MAD_COMMON_REQUESTHANDLERS_FSINFOREQUESTHANDLER_H_ #define MAD_COMMON_REQUESTHANDLERS_FSINFOREQUESTHANDLER_H_ -#include "../RequestHandler.h" +#include "../XmlRequestHandler.h" #include "../SystemBackend.h" #include <stdint.h> @@ -29,7 +29,7 @@ namespace Mad { namespace Common { namespace RequestHandlers { -class FSInfoRequestHandler : public RequestHandler { +class FSInfoRequestHandler : public XmlRequestHandler { private: Net::Connection *connection; uint16_t requestId; @@ -37,7 +37,7 @@ class FSInfoRequestHandler : public RequestHandler { void fsInfoHandler(const std::vector<SystemBackend::FSInfo> &info); protected: - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *con, uint16_t rid, const XmlPacket &packet); public: FSInfoRequestHandler() {} diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index 6b1bf8f..afcfa4b 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -256,11 +256,11 @@ void RequestManager::unregisterPacketType(const std::string &type) { } RequestManager::RequestManager() : core(false), requestId(-1) { - registerPacketType<RequestHandlers::DisconnectRequestHandler>(Net::Packet::DISCONNECT); + registerPacketType<RequestHandlers::DisconnectRequestHandler>("Disconnect"); } RequestManager::~RequestManager() { - unregisterPacketType(Net::Packet::DISCONNECT); + unregisterPacketType("Disconnect"); for(std::map<Net::Connection*,RequestMap*>::iterator it = requestMaps.begin(); it != requestMaps.end(); ++it) delete it->second; diff --git a/src/Common/Requests/DisconnectRequest.cpp b/src/Common/Requests/DisconnectRequest.cpp index d8e7ee9..afab8ed 100644 --- a/src/Common/Requests/DisconnectRequest.cpp +++ b/src/Common/Requests/DisconnectRequest.cpp @@ -25,18 +25,21 @@ namespace Common { namespace Requests { void DisconnectRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::DISCONNECT, requestId)); + XmlPacket packet; + packet.setType("Disconnect"); + + connection->send(packet.encode(requestId)); } -void DisconnectRequest::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { +void DisconnectRequest::handlePacket(Net::Connection *connection, uint16_t, const XmlPacket &packet) { + if(packet.getType() != "OK") { finishWithError(Exception(Exception::UNEXPECTED_PACKET)); return; // TODO Logging } connection->disconnect(); - finish(); + finish(packet); } } diff --git a/src/Common/Requests/DisconnectRequest.h b/src/Common/Requests/DisconnectRequest.h index 602505a..4f2cdf5 100644 --- a/src/Common/Requests/DisconnectRequest.h +++ b/src/Common/Requests/DisconnectRequest.h @@ -20,19 +20,19 @@ #ifndef MAD_COMMON_REQUESTS_DISCONNECTREQUEST_H_ #define MAD_COMMON_REQUESTS_DISCONNECTREQUEST_H_ -#include "../Request.h" +#include "../XmlRequest.h" namespace Mad { namespace Common { namespace Requests { -class DisconnectRequest : public Request<> { +class DisconnectRequest : public XmlRequest { 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 *connection, uint16_t, const XmlPacket &packet); public: - DisconnectRequest(slot_type slot) : Request<>(slot) {} + DisconnectRequest(slot_type slot) : XmlRequest(slot) {} }; } diff --git a/src/Common/Requests/FSInfoRequest.cpp b/src/Common/Requests/FSInfoRequest.cpp index c8492ff..7b5574d 100644 --- a/src/Common/Requests/FSInfoRequest.cpp +++ b/src/Common/Requests/FSInfoRequest.cpp @@ -27,16 +27,19 @@ namespace Common { namespace Requests { void FSInfoRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::FS_INFO, requestId)); + XmlPacket packet; + packet.setType("FSInfo"); + + connection->send(packet.encode(requestId)); } -void FSInfoRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { +void FSInfoRequest::handlePacket(Net::Connection*, uint16_t, const Common::XmlPacket &packet) { + if(packet.getType() != "OK") { finishWithError(Exception(Exception::UNEXPECTED_PACKET)); return; // TODO Logging } - finish(Net::Packets::FSInfoPacket(packet)); + finish(packet); } } diff --git a/src/Common/Requests/FSInfoRequest.h b/src/Common/Requests/FSInfoRequest.h index 9ae7673..9a6ba14 100644 --- a/src/Common/Requests/FSInfoRequest.h +++ b/src/Common/Requests/FSInfoRequest.h @@ -20,20 +20,19 @@ #ifndef MAD_COMMON_REQUESTS_FSINFOREQUEST_H_ #define MAD_COMMON_REQUESTS_FSINFOREQUEST_H_ -#include "../Request.h" -#include <Net/Packets/FSInfoPacket.h> +#include "../XmlRequest.h" namespace Mad { namespace Common { namespace Requests { -class FSInfoRequest : public Request<Net::Packets::FSInfoPacket> { +class FSInfoRequest : public XmlRequest { 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: - FSInfoRequest(slot_type slot) : Request<Net::Packets::FSInfoPacket>(slot) {} + FSInfoRequest(slot_type slot) : XmlRequest(slot) {} }; } diff --git a/src/Common/XmlPacket.h b/src/Common/XmlPacket.h index e80a266..e32893a 100644 --- a/src/Common/XmlPacket.h +++ b/src/Common/XmlPacket.h @@ -234,7 +234,6 @@ class XmlPacket { static Element nilElement; - private: bool add(const std::string &name, const std::string &value, const char *type) { if(!entryNode) return false; 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<Common::RequestHandlers::FSInfoRequestHandler>(Net::Packet::FS_INFO); - Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>(Net::Packet::DAEMON_COMMAND_REBOOT); - Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>(Net::Packet::DAEMON_COMMAND_SHUTDOWN); - Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonFSInfoRequestHandler>(Net::Packet::DAEMON_FS_INFO); Common::RequestManager::get()->registerPacketType<RequestHandlers::GSSAPIAuthRequestHandler>(Net::Packet::GSSAPI_AUTH); - Common::RequestManager::get()->registerPacketType<RequestHandlers::IdentifyRequestHandler>(Net::Packet::IDENTIFY); + Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>("DaemonCommand"); + Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonFSInfoRequestHandler>("DaemonFSInfo"); + Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::FSInfoRequestHandler>("FSInfo"); Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::StatusRequestHandler>("GetStatus"); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonStatusRequestHandler>("GetDaemonStatus"); + Common::RequestManager::get()->registerPacketType<RequestHandlers::IdentifyRequestHandler>("Identify"); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonListRequestHandler>("ListHosts"); Common::RequestManager::get()->registerPacketType<RequestHandlers::UserListRequestHandler>("ListUsers"); Common::RequestManager::get()->registerPacketType<RequestHandlers::LogRequestHandler>("Log"); @@ -156,15 +155,14 @@ void ConnectionManager::doDeinit() { for(std::list<Net::ServerConnection*>::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 <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() {} 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 <Common/Request.h> +#include <Common/XmlRequest.h> 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) {} }; } 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() {} diff --git a/src/Daemon/Requests/IdentifyRequest.cpp b/src/Daemon/Requests/IdentifyRequest.cpp index e8b6082..6334f00 100644 --- a/src/Daemon/Requests/IdentifyRequest.cpp +++ b/src/Daemon/Requests/IdentifyRequest.cpp @@ -19,22 +19,27 @@ #include "IdentifyRequest.h" #include <Net/Connection.h> +#include <Common/XmlPacket.h> namespace Mad { namespace Daemon { namespace Requests { void IdentifyRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::IDENTIFY, requestId, hostname.c_str(), hostname.length())); + Common::XmlPacket packet; + packet.setType("Identify"); + packet.add("hostname", hostname); + + connection->send(packet.encode(requestId)); } -void IdentifyRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { +void IdentifyRequest::handlePacket(Net::Connection*, uint16_t, const Common::XmlPacket &packet) { + if(packet.getType() != "OK") { finishWithError(Common::Exception(Common::Exception::UNEXPECTED_PACKET)); return; // TODO Logging } - finish(); + finish(packet); } } diff --git a/src/Daemon/Requests/IdentifyRequest.h b/src/Daemon/Requests/IdentifyRequest.h index c728461..c3ab821 100644 --- a/src/Daemon/Requests/IdentifyRequest.h +++ b/src/Daemon/Requests/IdentifyRequest.h @@ -20,23 +20,23 @@ #ifndef MAD_DAEMON_REQUESTS_IDENTIFYREQUEST_H_ #define MAD_DAEMON_REQUESTS_IDENTIFYREQUEST_H_ -#include <Common/Request.h> +#include <Common/XmlRequest.h> #include <string> namespace Mad { namespace Daemon { namespace Requests { -class IdentifyRequest : public Common::Request<> { +class IdentifyRequest : public Common::XmlRequest { private: std::string hostname; protected: virtual void sendRequest(Net::Connection *connection, uint16_t requestId); - virtual void handlePacket(Net::Connection*, const Net::Packet &packet); + virtual void handlePacket(Net::Connection*, uint16_t, const Common::XmlPacket &packet); public: - IdentifyRequest(const std::string &hostname0, slot_type slot) : Common::Request<>(slot), hostname(hostname0) {} + IdentifyRequest(const std::string &hostname0, slot_type slot) : Common::XmlRequest(slot), hostname(hostname0) {} }; } diff --git a/src/Net/Packet.h b/src/Net/Packet.h index 47d44c7..34f3428 100644 --- a/src/Net/Packet.h +++ b/src/Net/Packet.h @@ -31,13 +31,8 @@ namespace Net { class Packet { public: enum Type { - OK = 0x0000, ERROR = 0x0001, DISCONNECT = 0x0002, - GSSAPI_AUTH = 0x0010, IDENTIFY = 0x0011, - LIST_DAEMONS = 0x0020, - FS_INFO = 0x0032, DAEMON_FS_INFO = 0x0033, - COMMAND_SHUTDOWN = 0x0040, COMMAND_REBOOT = 0x0041, - DAEMON_COMMAND_SHUTDOWN = 0x0050, DAEMON_COMMAND_REBOOT = 0x0051, - DAEMON_STATE_UPDATE = 0x0060, + OK = 0x0000, ERROR = 0x0001, + GSSAPI_AUTH = 0x0010, XML = 0xFFFF }; diff --git a/src/Net/Packets/FSInfoPacket.cpp b/src/Net/Packets/FSInfoPacket.cpp deleted file mode 100644 index c328f8a..0000000 --- a/src/Net/Packets/FSInfoPacket.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * FSInfoPacket.cpp - * - * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include "FSInfoPacket.h" -#include <cstring> -#include <sstream> - -namespace Mad { -namespace Net { -namespace Packets { - -void FSInfoPacket::assemblePacket() { - std::string str; - - for(std::vector<Common::SystemBackend::FSInfo>::iterator fs = fsList.begin(); fs != fsList.end(); ++fs) - str += fs->fsName + "\n" + fs->mountedOn + "\n"; - - setLength(sizeof(uint16_t) + sizeof(FSData)*fsList.size() + str.length()); - - nFS = (uint16_t*)rawData->data; - fsData = (FSData*)(rawData->data + sizeof(uint16_t)); - charData = (char*)(rawData->data + sizeof(uint16_t) + sizeof(FSData)*fsList.size()); - - std::memcpy(charData, str.c_str(), str.length()); - - *nFS = htons(fsList.size()); - - for(size_t i = 0; i < fsList.size(); ++i) { - fsData[i].total = htonll(fsList[i].total); - fsData[i].used = htonll(fsList[i].used); - fsData[i].available = htonll(fsList[i].available); - } -} - -void FSInfoPacket::parsePacket() { - fsList.clear(); - - if(getLength() < sizeof(uint16_t)) - return; - - nFS = (uint16_t*)rawData->data; - fsList.resize(ntohs(*nFS)); - - if(getLength() < sizeof(uint16_t) + sizeof(FSData)*fsList.size()) - setLength(sizeof(uint16_t) + sizeof(FSData)*fsList.size()); - - nFS = (uint16_t*)rawData->data; - fsData = (FSData*)(rawData->data + sizeof(uint16_t)); - charData = (char*)(rawData->data + sizeof(uint16_t) + sizeof(FSData)*fsList.size()); - - std::istringstream stream(std::string(charData, getLength() - (sizeof(uint16_t)+sizeof(FSData)*fsList.size()))); - - for(size_t i = 0; i < fsList.size(); ++i) { - fsList[i].total = ntohll(fsData[i].total); - fsList[i].used = ntohll(fsData[i].used); - fsList[i].available = ntohll(fsData[i].available); - - if(!stream.eof()) { - std::string str; - - std::getline(stream, str); - fsList[i].fsName = str; - std::getline(stream, str); - fsList[i].mountedOn = str; - } - } -} - -} -} -} diff --git a/src/Net/Packets/FSInfoPacket.h b/src/Net/Packets/FSInfoPacket.h deleted file mode 100644 index 4e973aa..0000000 --- a/src/Net/Packets/FSInfoPacket.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * FSInfoPacket.h - * - * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef MAD_NET_PACKETS_FSINFOPACKET_H_ -#define MAD_NET_PACKETS_FSINFOPACKET_H_ - -#include "../Packet.h" -#include <Common/SystemBackend.h> - -#include <vector> - -namespace Mad { -namespace Net { -namespace Packets { - -class FSInfoPacket : public Packet { - protected: - struct FSData { - uint64_t total; - uint64_t used; - uint64_t available; - }; - - uint16_t *nFS; - FSData *fsData; - char *charData; - - std::vector<Common::SystemBackend::FSInfo> fsList; - - void assemblePacket(); - void parsePacket(); - - public: - FSInfoPacket(Type type, uint16_t requestId, const std::vector<Common::SystemBackend::FSInfo> &fs) : Packet(type, requestId), fsList(fs) { - assemblePacket(); - } - - FSInfoPacket(const Packet &p) : Packet(p) { - parsePacket(); - } - - FSInfoPacket& operator=(const Packet &p) { - Packet::operator=(p); - parsePacket(); - - return *this; - } - - FSInfoPacket& operator=(const FSInfoPacket &p) { - Packet::operator=(p); - parsePacket(); - - return *this; - } - - const std::vector<Common::SystemBackend::FSInfo>& getFSInfo() const { - return fsList; - } -}; - -} -} -} - -#endif /* MAD_NET_PACKETS_FSINFOPACKET_H_ */ diff --git a/src/Net/Packets/Makefile.am b/src/Net/Packets/Makefile.am index 99030d4..8ae6b8b 100644 --- a/src/Net/Packets/Makefile.am +++ b/src/Net/Packets/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = ErrorPacket.cpp FSInfoPacket.cpp +libpackets_la_SOURCES = ErrorPacket.cpp -noinst_HEADERS = ErrorPacket.h FSInfoPacket.h +noinst_HEADERS = ErrorPacket.h diff --git a/src/Net/Packets/Makefile.in b/src/Net/Packets/Makefile.in index 551550a..cced0ce 100644 --- a/src/Net/Packets/Makefile.in +++ b/src/Net/Packets/Makefile.in @@ -50,7 +50,7 @@ CONFIG_HEADER = $(top_builddir)/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libpackets_la_LIBADD = -am_libpackets_la_OBJECTS = ErrorPacket.lo FSInfoPacket.lo +am_libpackets_la_OBJECTS = ErrorPacket.lo libpackets_la_OBJECTS = $(am_libpackets_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/config/depcomp @@ -216,8 +216,8 @@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = ErrorPacket.cpp FSInfoPacket.cpp -noinst_HEADERS = ErrorPacket.h FSInfoPacket.h +libpackets_la_SOURCES = ErrorPacket.cpp +noinst_HEADERS = ErrorPacket.h all: all-am .SUFFIXES: @@ -270,7 +270,6 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ErrorPacket.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FSInfoPacket.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/mad.cpp b/src/mad.cpp index 40d9ead..4a9904a 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -37,7 +37,7 @@ using namespace Mad; -static void requestFinished(const Common::Request<>&) { +static void requestFinished(const Common::XmlRequest&) { Common::Logger::log("Identified."); } @@ -50,10 +50,8 @@ int main() { Common::ConfigManager::get()->finish(); - Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::FSInfoRequestHandler>(Net::Packet::FS_INFO); - Common::RequestManager::get()->registerPacketType<Daemon::RequestHandlers::CommandRequestHandler>(Net::Packet::COMMAND_REBOOT); - Common::RequestManager::get()->registerPacketType<Daemon::RequestHandlers::CommandRequestHandler>(Net::Packet::COMMAND_SHUTDOWN); - + Common::RequestManager::get()->registerPacketType<Daemon::RequestHandlers::CommandRequestHandler>("Command"); + Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::FSInfoRequestHandler>("FSInfo"); Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::StatusRequestHandler>("GetStatus"); Net::ClientConnection *connection = new Net::ClientConnection; @@ -72,8 +70,8 @@ int main() { //char hostname[256]; //gethostname(hostname, sizeof(hostname)); - //Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Daemon::Requests::IdentifyRequest(hostname, sigc::ptr_fun(requestFinished)))); - Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Daemon::Requests::IdentifyRequest("test", sigc::ptr_fun(requestFinished)))); + //Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequest>(new Daemon::Requests::IdentifyRequest(hostname, sigc::ptr_fun(requestFinished)))); + Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequest>(new Daemon::Requests::IdentifyRequest("test", sigc::ptr_fun(requestFinished)))); while(connection->isConnected()) Net::FdManager::get()->run(); @@ -88,10 +86,8 @@ int main() { delete connection; - Common::RequestManager::get()->unregisterPacketType(Net::Packet::FS_INFO); - Common::RequestManager::get()->unregisterPacketType(Net::Packet::COMMAND_REBOOT); - Common::RequestManager::get()->unregisterPacketType(Net::Packet::COMMAND_SHUTDOWN); - + Common::RequestManager::get()->unregisterPacketType("Command"); + Common::RequestManager::get()->unregisterPacketType("FSInfo"); Common::RequestManager::get()->unregisterPacketType("GetStatus"); Common::Initializable::deinit(); |