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/Common | |
parent | 8d629ca48e9b5530416127e2e52c945fe1f9ee52 (diff) | |
download | mad-f85b6d5ab264910f272e69ce5997cebec54886ce.tar mad-f85b6d5ab264910f272e69ce5997cebec54886ce.zip |
Requests Status, DaemonStatus und UserList in XmlRequests umgewandelt
Diffstat (limited to 'src/Common')
-rw-r--r-- | src/Common/RequestHandlers/StatusRequestHandler.cpp | 36 | ||||
-rw-r--r-- | src/Common/RequestHandlers/StatusRequestHandler.h | 8 | ||||
-rw-r--r-- | src/Common/RequestManager.cpp | 4 | ||||
-rw-r--r-- | src/Common/Requests/StatusRequest.cpp | 11 | ||||
-rw-r--r-- | src/Common/Requests/StatusRequest.h | 9 | ||||
-rw-r--r-- | src/Common/Requests/UserListRequest.cpp | 11 | ||||
-rw-r--r-- | src/Common/Requests/UserListRequest.h | 9 | ||||
-rw-r--r-- | src/Common/XmlPacket.cpp | 2 | ||||
-rw-r--r-- | src/Common/XmlPacket.h | 38 | ||||
-rw-r--r-- | src/Common/XmlRequest.h | 43 | ||||
-rw-r--r-- | src/Common/XmlRequestHandler.h | 3 |
11 files changed, 102 insertions, 72 deletions
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 <Net/Connection.h> -#include <Net/Packets/ErrorPacket.h> -#include <Net/Packets/HostStatusPacket.h> 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 <stdint.h> 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 <Net/Packets/HostStatusPacket.h> +#include "../XmlRequest.h" namespace Mad { namespace Common { namespace Requests { -class StatusRequest : public Request<Net::Packets::HostStatusPacket> { +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<Net::Packets::HostStatusPacket>(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 <Net/Packets/UserListPacket.h> +#include "../XmlRequest.h" namespace Mad { namespace Common { namespace Requests { -class UserListRequest : public Request<Net::Packets::UserListPacket> { +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<Net::Packets::UserListPacket>(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<typename T> - operator T() { + operator T() const { switch(type) { case INT: return static_cast<T>(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<typename T = void> class XmlRequest : public XmlRequestBase { +class XmlRequest : public XmlRequestBase { private: - sigc::signal<void,const XmlRequest<T>&> finished; + sigc::signal<void,const XmlRequest&> finished; - std::auto_ptr<T> res; + std::auto_ptr<XmlPacket> res; Exception exp; public: - typedef sigc::slot<void,const XmlRequest<T>&> slot_type; + typedef sigc::slot<void,const XmlRequest&> slot_type; protected: XmlRequest(slot_type slot) : exp(Exception::NOT_FINISHED) { @@ -45,12 +46,12 @@ template<typename T = void> class XmlRequest : public XmlRequestBase { finished.connect(sigc::hide(signalFinished().make_slot())); } - void finish(std::auto_ptr<T> result) {res = result; finished(*this);} - void finish(const T& result) {res.reset(new T(result)); finished(*this);} + void finish(std::auto_ptr<XmlPacket> 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<typename T = void> class XmlRequest : public XmlRequestBase { } }; -template<> class XmlRequest<void> : public XmlRequestBase { - private: - sigc::signal<void,const XmlRequest<void>&> finished; - - bool isFinished; - Exception exp; - - public: - typedef sigc::slot<void,const XmlRequest<void>&> 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 <sigc++/signal.h> +#include <stdint.h> namespace Mad { @@ -46,7 +47,7 @@ class XmlRequestHandler { sigc::signal<void> 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() {} |