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/StatusRequestHandler.cpp | 36 ++++++++++++++---- src/Common/RequestHandlers/StatusRequestHandler.h | 8 ++-- src/Common/RequestManager.cpp | 4 +- src/Common/Requests/StatusRequest.cpp | 11 ++++-- src/Common/Requests/StatusRequest.h | 9 ++--- src/Common/Requests/UserListRequest.cpp | 11 ++++-- src/Common/Requests/UserListRequest.h | 9 ++--- src/Common/XmlPacket.cpp | 2 +- src/Common/XmlPacket.h | 38 +++++++++++++++++-- src/Common/XmlRequest.h | 43 ++++------------------ src/Common/XmlRequestHandler.h | 3 +- 11 files changed, 102 insertions(+), 72 deletions(-) (limited to 'src/Common') diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index 8494fee..86ed970 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -18,20 +18,25 @@ */ #include "StatusRequestHandler.h" +#include "../Exception.h" #include "../SystemBackend.h" #include "../Logger.h" +#include "../XmlPacket.h" #include -#include -#include namespace Mad { namespace Common { namespace RequestHandlers { -void StatusRequestHandler::handlePacket(Net::Connection *con, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::STATUS) { +void StatusRequestHandler::handlePacket(Net::Connection *con, uint16_t requestId, const XmlPacket &packet) { + if(packet.getType() != "GetStatus") { 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); + + con->send(ret.encode(requestId)); signalFinished().emit(); return; @@ -40,7 +45,7 @@ void StatusRequestHandler::handlePacket(Net::Connection *con, const Net::Packet // TODO Require authentication connection = con; - requestId = packet.getRequestId(); + rid = requestId; if(!SystemBackend::getUptimeInfo(sigc::mem_fun(this, &StatusRequestHandler::uptimeHandler))) needUptime = false; @@ -56,8 +61,23 @@ void StatusRequestHandler::send() { if(needUptime || needMemory || needLoad || sent) return; - connection->send(Net::Packets::HostStatusPacket(Net::Packet::OK, requestId, uptime, idleTime, - totalMem, freeMem, totalSwap, freeSwap, currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15)); + XmlPacket packet; + + packet.setType("OK"); + + packet.add("uptime", uptime); + packet.add("idleTime", idleTime); + packet.add("totalMem", totalMem); + packet.add("freeMem", freeMem); + packet.add("totalSwap", totalSwap); + packet.add("freeSwap", freeSwap); + packet.add("currentLoad", currentLoad); + packet.add("nProcesses", nProcesses); + packet.add("loadAvg1", loadAvg1); + packet.add("loadAvg5", loadAvg5); + packet.add("loadAvg15", loadAvg15); + + connection->send(packet.encode(rid)); sent = true; diff --git a/src/Common/RequestHandlers/StatusRequestHandler.h b/src/Common/RequestHandlers/StatusRequestHandler.h index fd907de..0b53db0 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.h +++ b/src/Common/RequestHandlers/StatusRequestHandler.h @@ -20,17 +20,17 @@ #ifndef MAD_COMMON_REQUESTHANDLERS_STATUSREQUESTHANDLER_H_ #define MAD_COMMON_REQUESTHANDLERS_STATUSREQUESTHANDLER_H_ -#include "../RequestHandler.h" +#include "../XmlRequestHandler.h" #include namespace Mad { namespace Common { namespace RequestHandlers { -class StatusRequestHandler : public RequestHandler { +class StatusRequestHandler : public XmlRequestHandler { private: Net::Connection *connection; - uint16_t requestId; + uint16_t rid; bool needUptime, needMemory, needLoad, sent; @@ -60,7 +60,7 @@ class StatusRequestHandler : public RequestHandler { void send(); protected: - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *con, uint16_t requestId, const XmlPacket &packet); public: StatusRequestHandler() : needUptime(true), needMemory(true), needLoad(true), sent(false), diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index 94b3da9..03a2603 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -135,7 +135,7 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack if(xmlRequest) { XmlPacket xmlPacket(packet); - xmlRequest->handlePacket(connection, xmlPacket); + xmlRequest->handlePacket(connection, packet.getRequestId(), xmlPacket); return; } @@ -147,7 +147,7 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack if(factoryIt != xmlRequestHandlerFactories.end()) { xmlRequest = factoryIt->second->createRequestHandler(); xmlRequestMap->addRequest(packet.getRequestId(), xmlRequest); - xmlRequest->handlePacket(connection, xmlPacket); + xmlRequest->handlePacket(connection, packet.getRequestId(), xmlPacket); return; } diff --git a/src/Common/Requests/StatusRequest.cpp b/src/Common/Requests/StatusRequest.cpp index dbaba23..3526e23 100644 --- a/src/Common/Requests/StatusRequest.cpp +++ b/src/Common/Requests/StatusRequest.cpp @@ -25,16 +25,19 @@ namespace Common { namespace Requests { void StatusRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::STATUS, requestId)); + XmlPacket packet; + packet.setType("GetStatus"); + + connection->send(packet.encode(requestId)); } -void StatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { +void StatusRequest::handlePacket(Net::Connection*, uint16_t, const XmlPacket &packet) { + if(packet.getType() != "OK") { finishWithError(Exception(Exception::UNEXPECTED_PACKET)); return; // TODO Logging } - finish(Net::Packets::HostStatusPacket(packet)); + finish(packet); } } diff --git a/src/Common/Requests/StatusRequest.h b/src/Common/Requests/StatusRequest.h index 5712404..e9c8a4c 100644 --- a/src/Common/Requests/StatusRequest.h +++ b/src/Common/Requests/StatusRequest.h @@ -20,20 +20,19 @@ #ifndef MAD_COMMON_REQUESTS_STATUSREQUEST_H_ #define MAD_COMMON_REQUESTS_STATUSREQUEST_H_ -#include "../Request.h" -#include +#include "../XmlRequest.h" namespace Mad { namespace Common { namespace Requests { -class StatusRequest : public Request { +class StatusRequest : 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: - StatusRequest(slot_type slot) : Request(slot) {} + StatusRequest(slot_type slot) : XmlRequest(slot) {} }; } diff --git a/src/Common/Requests/UserListRequest.cpp b/src/Common/Requests/UserListRequest.cpp index cd9c760..2545677 100644 --- a/src/Common/Requests/UserListRequest.cpp +++ b/src/Common/Requests/UserListRequest.cpp @@ -25,16 +25,19 @@ namespace Common { namespace Requests { void UserListRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::USERS_LIST, requestId)); + XmlPacket packet; + packet.setType("ListUsers"); + + connection->send(packet.encode(requestId)); } -void UserListRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { +void UserListRequest::handlePacket(Net::Connection*, uint16_t, const XmlPacket &packet) { + if(packet.getType() != "OK") { finishWithError(Exception(Exception::UNEXPECTED_PACKET)); return; // TODO Logging } - finish(Net::Packets::UserListPacket(packet)); + finish(packet); } } diff --git a/src/Common/Requests/UserListRequest.h b/src/Common/Requests/UserListRequest.h index c62e284..9702a42 100644 --- a/src/Common/Requests/UserListRequest.h +++ b/src/Common/Requests/UserListRequest.h @@ -20,20 +20,19 @@ #ifndef MAD_COMMON_REQUESTS_USERLISTREQUEST_H_ #define MAD_COMMON_REQUESTS_USERLISTREQUEST_H_ -#include "../Request.h" -#include +#include "../XmlRequest.h" namespace Mad { namespace Common { namespace Requests { -class UserListRequest : public Request { +class UserListRequest : 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: - UserListRequest(slot_type slot) : Request(slot) {} + UserListRequest(slot_type slot) : XmlRequest(slot) {} }; } diff --git a/src/Common/XmlPacket.cpp b/src/Common/XmlPacket.cpp index 8da861c..672d092 100644 --- a/src/Common/XmlPacket.cpp +++ b/src/Common/XmlPacket.cpp @@ -222,7 +222,7 @@ void XmlPacket::setType(const std::string &type) { xmlSetProp(packetNode, (xmlChar*)"type", (xmlChar*)type.c_str()); } -Net::Packet XmlPacket::encode(uint16_t requestId) { +Net::Packet XmlPacket::encode(uint16_t requestId) const { xmlChar *buf; int length; diff --git a/src/Common/XmlPacket.h b/src/Common/XmlPacket.h index 007404a..e80a266 100644 --- a/src/Common/XmlPacket.h +++ b/src/Common/XmlPacket.h @@ -122,6 +122,10 @@ class XmlPacket { return 0; } + bool isEmpty() const { + return (getSize() == 0); + } + virtual Entry& operator[](size_t) { return nilEntry; } @@ -130,12 +134,26 @@ class XmlPacket { return nilEntry; } + Entry& back() { + if(isEmpty()) + return nilEntry; + + return operator[](getSize()-1); + } + + const Entry& back() const { + if(isEmpty()) + return nilEntry; + + return operator[](getSize()-1); + } + virtual bool insertEntry(size_t) {return false;} virtual bool addEntry() {return false;} virtual bool removeEntry(size_t) {return false;} template - operator T() { + operator T() const { switch(type) { case INT: return static_cast(value.var_int); @@ -156,7 +174,7 @@ class XmlPacket { } } - operator std::string() { + operator std::string() const { return str; } @@ -258,6 +276,20 @@ class XmlPacket { return add(name, buf.str(), "uint"); } + bool add(const std::string &name, long val) { + std::ostringstream buf; + buf << val; + + return add(name, buf.str(), "int"); + } + + bool add(const std::string &name, unsigned long val) { + std::ostringstream buf; + buf << val; + + return add(name, buf.str(), "uint"); + } + bool add(const std::string &name, long long val) { std::ostringstream buf; buf << val; @@ -428,7 +460,7 @@ class XmlPacket { return entry->remove(name); } - Net::Packet encode(uint16_t requestId); + Net::Packet encode(uint16_t requestId) const; }; } diff --git a/src/Common/XmlRequest.h b/src/Common/XmlRequest.h index 0de0291..667b9a7 100644 --- a/src/Common/XmlRequest.h +++ b/src/Common/XmlRequest.h @@ -20,6 +20,7 @@ #ifndef MAD_COMMON_XMLREQUEST_H_ #define MAD_COMMON_XMLREQUEST_H_ +#include "XmlPacket.h" #include "XmlRequestBase.h" #include "Exception.h" @@ -29,15 +30,15 @@ namespace Mad { namespace Common { -template class XmlRequest : public XmlRequestBase { +class XmlRequest : public XmlRequestBase { private: - sigc::signal&> finished; + sigc::signal finished; - std::auto_ptr res; + std::auto_ptr res; Exception exp; public: - typedef sigc::slot&> slot_type; + typedef sigc::slot slot_type; protected: XmlRequest(slot_type slot) : exp(Exception::NOT_FINISHED) { @@ -45,12 +46,12 @@ template class XmlRequest : public XmlRequestBase { finished.connect(sigc::hide(signalFinished().make_slot())); } - void finish(std::auto_ptr result) {res = result; finished(*this);} - void finish(const T& result) {res.reset(new T(result)); finished(*this);} + void finish(std::auto_ptr result) {res = result; finished(*this);} + void finish(const XmlPacket& result) {res.reset(new XmlPacket(result)); finished(*this);} void finishWithError(const Exception &e) {exp = e; finished(*this);} public: - const T& getResult() const throw(Exception) { + const XmlPacket& getResult() const throw(Exception) { if(res.get()) return *res; @@ -58,34 +59,6 @@ template class XmlRequest : public XmlRequestBase { } }; -template<> class XmlRequest : public XmlRequestBase { - private: - sigc::signal&> finished; - - bool isFinished; - Exception exp; - - public: - typedef sigc::slot&> slot_type; - - protected: - XmlRequest(slot_type slot) : isFinished(false), exp(Exception::NOT_FINISHED) { - finished.connect(slot); - finished.connect(sigc::hide(signalFinished().make_slot())); - } - - void finish() {isFinished = true; finished(*this);} - void finishWithError(const Exception &e) {exp = e; finished(*this);} - - public: - void getResult() const throw(Exception) { - if(isFinished) - return; - - throw exp; - } -}; - } } diff --git a/src/Common/XmlRequestHandler.h b/src/Common/XmlRequestHandler.h index e04e5b9..30438b7 100644 --- a/src/Common/XmlRequestHandler.h +++ b/src/Common/XmlRequestHandler.h @@ -21,6 +21,7 @@ #define MAD_COMMON_XMLREQUESTHANDLER_H_ #include +#include namespace Mad { @@ -46,7 +47,7 @@ class XmlRequestHandler { sigc::signal signalFinished() {return finished;} - virtual void handlePacket(Net::Connection *connection, const XmlPacket &packet) = 0; + virtual void handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet) = 0; public: virtual ~XmlRequestHandler() {} -- cgit v1.2.3