From 8f098fc3070f791302ec1f497588fab6ed409980 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 16 Mar 2009 19:13:42 +0100 Subject: Request- und RequestHandler-Interfaces vereinfacht --- src/Common/Makefile.am | 10 +- src/Common/Makefile.in | 15 +-- src/Common/Request.cpp | 6 +- src/Common/Request.h | 8 +- src/Common/RequestHandler.cpp | 31 ++++++ src/Common/RequestHandler.h | 20 +++- .../RequestHandlers/DisconnectRequestHandler.cpp | 10 +- .../RequestHandlers/DisconnectRequestHandler.h | 5 +- .../RequestHandlers/FSInfoRequestHandler.cpp | 13 +-- src/Common/RequestHandlers/FSInfoRequestHandler.h | 8 +- .../RequestHandlers/StatusRequestHandler.cpp | 11 +- src/Common/RequestHandlers/StatusRequestHandler.h | 9 +- src/Common/RequestManager.cpp | 32 +++--- src/Common/RequestManager.h | 117 ++++++++++++++++++--- src/Common/Requests/DisconnectRequest.cpp | 14 ++- src/Common/Requests/DisconnectRequest.h | 7 +- src/Common/Requests/FSInfoRequest.h | 3 +- src/Common/Requests/GSSAPIAuthRequest.cpp | 15 ++- src/Common/Requests/GSSAPIAuthRequest.h | 8 +- src/Common/Requests/SimpleRequest.cpp | 6 +- src/Common/Requests/SimpleRequest.h | 5 +- src/Common/Requests/StatusRequest.h | 3 +- src/Common/Requests/UserListRequest.h | 3 +- 23 files changed, 243 insertions(+), 116 deletions(-) create mode 100644 src/Common/RequestHandler.cpp (limited to 'src/Common') diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index f0a0ad7..9f31371 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -2,11 +2,11 @@ SUBDIRS = Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = ActionManager.cpp ConfigEntry.cpp ConfigManager.cpp Exception.cpp Initializable.cpp \ - Logger.cpp LogManager.cpp ModuleManager.cpp Request.cpp RequestManager.cpp \ - SystemBackend.cpp Tokenizer.cpp XmlPacket.cpp + Logger.cpp LogManager.cpp ModuleManager.cpp Request.cpp RequestHandler.cpp \ + RequestManager.cpp SystemBackend.cpp Tokenizer.cpp XmlPacket.cpp libcommon_la_LIBADD = Requests/librequests.la RequestHandlers/librequesthandlers.la ../../lib/libgnu.la -noinst_HEADERS = ActionManager.h ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h \ - Initializable.h Logger.h LoggerBase.h LogManager.h ModuleManager.h \ - RemoteLogger.h Request.h RequestHandler.h RequestManager.h \ +noinst_HEADERS = ActionManager.h ConfigEntry.h ConfigManager.h Configurable.h \ + Exception.h HostInfo.h Initializable.h Logger.h LoggerBase.h LogManager.h \ + ModuleManager.h RemoteLogger.h Request.h RequestHandler.h RequestManager.h \ SystemBackend.h Tokenizer.h UserInfo.h XmlPacket.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 9841291..6f2595f 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -55,8 +55,8 @@ libcommon_la_DEPENDENCIES = Requests/librequests.la \ RequestHandlers/librequesthandlers.la ../../lib/libgnu.la am_libcommon_la_OBJECTS = ActionManager.lo ConfigEntry.lo \ ConfigManager.lo Exception.lo Initializable.lo Logger.lo \ - LogManager.lo ModuleManager.lo Request.lo RequestManager.lo \ - SystemBackend.lo Tokenizer.lo XmlPacket.lo + LogManager.lo ModuleManager.lo Request.lo RequestHandler.lo \ + RequestManager.lo SystemBackend.lo Tokenizer.lo XmlPacket.lo libcommon_la_OBJECTS = $(am_libcommon_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/config/depcomp @@ -239,13 +239,13 @@ top_srcdir = @top_srcdir@ SUBDIRS = Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = ActionManager.cpp ConfigEntry.cpp ConfigManager.cpp Exception.cpp Initializable.cpp \ - Logger.cpp LogManager.cpp ModuleManager.cpp Request.cpp RequestManager.cpp \ - SystemBackend.cpp Tokenizer.cpp XmlPacket.cpp + Logger.cpp LogManager.cpp ModuleManager.cpp Request.cpp RequestHandler.cpp \ + RequestManager.cpp SystemBackend.cpp Tokenizer.cpp XmlPacket.cpp libcommon_la_LIBADD = Requests/librequests.la RequestHandlers/librequesthandlers.la ../../lib/libgnu.la -noinst_HEADERS = ActionManager.h ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h \ - Initializable.h Logger.h LoggerBase.h LogManager.h ModuleManager.h \ - RemoteLogger.h Request.h RequestHandler.h RequestManager.h \ +noinst_HEADERS = ActionManager.h ConfigEntry.h ConfigManager.h Configurable.h \ + Exception.h HostInfo.h Initializable.h Logger.h LoggerBase.h LogManager.h \ + ModuleManager.h RemoteLogger.h Request.h RequestHandler.h RequestManager.h \ SystemBackend.h Tokenizer.h UserInfo.h XmlPacket.h all: all-recursive @@ -308,6 +308,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Logger.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ModuleManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Request.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestHandler.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SystemBackend.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Tokenizer.Plo@am__quote@ diff --git a/src/Common/Request.cpp b/src/Common/Request.cpp index 0b8715e..bc37708 100644 --- a/src/Common/Request.cpp +++ b/src/Common/Request.cpp @@ -17,19 +17,17 @@ * with this program. If not, see . */ -#include - #include "Request.h" namespace Mad { namespace Common { -void Request::handlePacket(Net::Connection *connection _UNUSED_PARAMETER_, uint16_t requestId _UNUSED_PARAMETER_, const XmlPacket &packet) { +void Request::handlePacket(const XmlPacket &packet) { if(packet.getType() == "Error") { finishWithError(Common::Exception(packet["Where"], packet["ErrorCode"], packet["SubCode"], packet["SubSubCode"])); return; } - if(packet.getType() != "OK") { + else if(packet.getType() != "OK") { finishWithError(Exception(Exception::UNEXPECTED_PACKET)); return; // TODO Logging } diff --git a/src/Common/Request.h b/src/Common/Request.h index ef84194..2c6c8d4 100644 --- a/src/Common/Request.h +++ b/src/Common/Request.h @@ -20,7 +20,6 @@ #ifndef MAD_COMMON_XMLREQUEST_H_ #define MAD_COMMON_XMLREQUEST_H_ -#include "XmlPacket.h" #include "RequestHandler.h" #include "Exception.h" @@ -43,7 +42,8 @@ class Request : public RequestHandler { typedef sigc::slot slot_type; protected: - Request(slot_type slot) : exp(Exception::NOT_FINISHED) { + Request(Net::Connection *connection, uint16_t requestId, slot_type slot) + : RequestHandler(connection, requestId), exp(Exception::NOT_FINISHED) { finished.connect(slot); finished.connect(sigc::hide(signalFinished().make_slot())); } @@ -52,8 +52,8 @@ class Request : public RequestHandler { void finish(const XmlPacket& result) {res.reset(new XmlPacket(result)); finished(*this);} void finishWithError(const Exception &e) {exp = e; finished(*this);} - virtual void sendRequest(Net::Connection *connection, uint16_t requestId) = 0; - virtual void handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet); + virtual void sendRequest() = 0; + virtual void handlePacket(const XmlPacket &packet); public: const XmlPacket& getResult() const throw(Exception) { diff --git a/src/Common/RequestHandler.cpp b/src/Common/RequestHandler.cpp new file mode 100644 index 0000000..3e0eabc --- /dev/null +++ b/src/Common/RequestHandler.cpp @@ -0,0 +1,31 @@ +/* + * RequestHandler.cpp + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#include "RequestHandler.h" +#include + +namespace Mad { +namespace Common { + +bool RequestHandler::sendPacket(const XmlPacket &packet) { + return connection->send(packet.encode(requestId)); +} + +} +} diff --git a/src/Common/RequestHandler.h b/src/Common/RequestHandler.h index b909e81..f36aae5 100644 --- a/src/Common/RequestHandler.h +++ b/src/Common/RequestHandler.h @@ -20,6 +20,8 @@ #ifndef MAD_COMMON_XMLREQUESTHANDLER_H_ #define MAD_COMMON_XMLREQUESTHANDLER_H_ +#include "XmlPacket.h" + #include #include @@ -32,22 +34,34 @@ class Connection; namespace Common { class RequestManager; -class XmlPacket; class RequestHandler { private: sigc::signal finished; + Net::Connection *connection; + uint16_t requestId; + // Prevent shallow copy RequestHandler(const RequestHandler &o); RequestHandler& operator=(const RequestHandler &o); protected: - RequestHandler() {} + RequestHandler(Net::Connection *connection0, uint16_t requestId0) : connection(connection0), requestId(requestId0) {} sigc::signal signalFinished() {return finished;} - virtual void handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet) = 0; + Net::Connection* getConnection() const { + return connection; + } + + uint16_t getRequestId() const { + return requestId; + } + + bool sendPacket(const XmlPacket &packet); + + virtual void handlePacket(const XmlPacket &packet) = 0; public: virtual ~RequestHandler() {} diff --git a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp index 346b6de..031853a 100644 --- a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp +++ b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp @@ -20,14 +20,13 @@ #include "DisconnectRequestHandler.h" #include "../Exception.h" #include "../Logger.h" -#include "../XmlPacket.h" #include namespace Mad { namespace Common { namespace RequestHandlers { -void DisconnectRequestHandler::handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet) { +void DisconnectRequestHandler::handlePacket(const XmlPacket &packet) { if(packet.getType() != "Disconnect") { Logger::log(Logger::ERROR, "Received an unexpected packet."); @@ -35,7 +34,7 @@ void DisconnectRequestHandler::handlePacket(Net::Connection *connection, uint16_ ret.setType("Error"); ret.add("ErrorCode", Exception::UNEXPECTED_PACKET); - connection->send(ret.encode(requestId)); + sendPacket(ret); signalFinished().emit(); return; @@ -44,9 +43,10 @@ void DisconnectRequestHandler::handlePacket(Net::Connection *connection, uint16_ XmlPacket ret; ret.setType("OK"); - connection->send(ret.encode(requestId)); + sendPacket(ret); - connection->disconnect(); + + getConnection()->disconnect(); signalFinished().emit(); } diff --git a/src/Common/RequestHandlers/DisconnectRequestHandler.h b/src/Common/RequestHandlers/DisconnectRequestHandler.h index 7544fd8..b90c96e 100644 --- a/src/Common/RequestHandlers/DisconnectRequestHandler.h +++ b/src/Common/RequestHandlers/DisconnectRequestHandler.h @@ -28,10 +28,11 @@ namespace RequestHandlers { class DisconnectRequestHandler : public RequestHandler { protected: - virtual void handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet); + virtual void handlePacket(const XmlPacket &packet); public: - DisconnectRequestHandler() {} + DisconnectRequestHandler(Net::Connection *connection, uint16_t requestId) + : RequestHandler(connection, requestId) {} }; } diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp index 878dd7c..aaa4d9e 100644 --- a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp @@ -20,14 +20,12 @@ #include "FSInfoRequestHandler.h" #include "../Exception.h" #include "../Logger.h" -#include "../XmlPacket.h" -#include namespace Mad { namespace Common { namespace RequestHandlers { -void FSInfoRequestHandler::handlePacket(Net::Connection *con, uint16_t rid, const XmlPacket &packet) { +void FSInfoRequestHandler::handlePacket(const XmlPacket &packet) { if(packet.getType() != "FSInfo") { Logger::log(Logger::ERROR, "Received an unexpected packet."); @@ -35,7 +33,7 @@ void FSInfoRequestHandler::handlePacket(Net::Connection *con, uint16_t rid, cons ret.setType("Error"); ret.add("ErrorCode", Exception::UNEXPECTED_PACKET); - connection->send(ret.encode(rid)); + sendPacket(ret); signalFinished().emit(); return; @@ -43,15 +41,12 @@ void FSInfoRequestHandler::handlePacket(Net::Connection *con, uint16_t rid, cons // TODO Require authentication - connection = con; - requestId = rid; - if(!SystemBackend::getFSInfo(sigc::mem_fun(this, &FSInfoRequestHandler::fsInfoHandler))) { XmlPacket ret; ret.setType("Error"); ret.add("ErrorCode", Exception::NOT_IMPLEMENTED); - connection->send(ret.encode(requestId)); + sendPacket(ret); signalFinished().emit(); } @@ -73,7 +68,7 @@ void FSInfoRequestHandler::fsInfoHandler(const std::vectoravailable); } - connection->send(ret.encode(requestId)); + sendPacket(ret); signalFinished().emit(); } diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.h b/src/Common/RequestHandlers/FSInfoRequestHandler.h index a9ee120..6fcf524 100644 --- a/src/Common/RequestHandlers/FSInfoRequestHandler.h +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.h @@ -31,16 +31,14 @@ namespace RequestHandlers { class FSInfoRequestHandler : public RequestHandler { private: - Net::Connection *connection; - uint16_t requestId; - void fsInfoHandler(const std::vector &info); protected: - virtual void handlePacket(Net::Connection *con, uint16_t rid, const XmlPacket &packet); + virtual void handlePacket(const XmlPacket &packet); public: - FSInfoRequestHandler() {} + FSInfoRequestHandler(Net::Connection *connection, uint16_t requestId) + : RequestHandler(connection, requestId) {} }; } diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index 86ed970..e03c345 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -21,14 +21,12 @@ #include "../Exception.h" #include "../SystemBackend.h" #include "../Logger.h" -#include "../XmlPacket.h" -#include namespace Mad { namespace Common { namespace RequestHandlers { -void StatusRequestHandler::handlePacket(Net::Connection *con, uint16_t requestId, const XmlPacket &packet) { +void StatusRequestHandler::handlePacket(const XmlPacket &packet) { if(packet.getType() != "GetStatus") { Logger::log(Logger::ERROR, "Received an unexpected packet."); @@ -36,7 +34,7 @@ void StatusRequestHandler::handlePacket(Net::Connection *con, uint16_t requestId ret.setType("Error"); ret.add("ErrorCode", Exception::UNEXPECTED_PACKET); - con->send(ret.encode(requestId)); + sendPacket(ret); signalFinished().emit(); return; @@ -44,9 +42,6 @@ void StatusRequestHandler::handlePacket(Net::Connection *con, uint16_t requestId // TODO Require authentication - connection = con; - rid = requestId; - if(!SystemBackend::getUptimeInfo(sigc::mem_fun(this, &StatusRequestHandler::uptimeHandler))) needUptime = false; if(!SystemBackend::getMemoryInfo(sigc::mem_fun(this, &StatusRequestHandler::memoryHandler))) @@ -77,7 +72,7 @@ void StatusRequestHandler::send() { packet.add("loadAvg5", loadAvg5); packet.add("loadAvg15", loadAvg15); - connection->send(packet.encode(rid)); + sendPacket(packet); sent = true; diff --git a/src/Common/RequestHandlers/StatusRequestHandler.h b/src/Common/RequestHandlers/StatusRequestHandler.h index ce7ae6d..ddaaae4 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.h +++ b/src/Common/RequestHandlers/StatusRequestHandler.h @@ -29,9 +29,6 @@ namespace RequestHandlers { class StatusRequestHandler : public RequestHandler { private: - Net::Connection *connection; - uint16_t rid; - bool needUptime, needMemory, needLoad, sent; unsigned long uptime, idleTime; @@ -60,10 +57,12 @@ class StatusRequestHandler : public RequestHandler { void send(); protected: - virtual void handlePacket(Net::Connection *con, uint16_t requestId, const XmlPacket &packet); + virtual void handlePacket(const XmlPacket &packet); public: - StatusRequestHandler() : needUptime(true), needMemory(true), needLoad(true), sent(false), + StatusRequestHandler(Net::Connection *connection, uint16_t requestId) + : RequestHandler(connection, requestId), + needUptime(true), needMemory(true), needLoad(true), sent(false), uptime(0), idleTime(0), totalMem(0), freeMem(0), totalSwap(0), freeSwap(0), currentLoad(0), nProcesses(0), loadAvg1(0), loadAvg5(0), loadAvg15(0) {} }; diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index ac1909b..9d50865 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -18,11 +18,11 @@ */ #include "RequestManager.h" -#include "Request.h" -#include "XmlPacket.h" #include "RequestHandlers/DisconnectRequestHandler.h" #include "Logger.h" +#include + #include #include @@ -81,48 +81,44 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack RequestHandler *request = requestMap->findRequest(packet.getRequestId()); if(request) { - request->handlePacket(connection, packet.getRequestId(), xmlPacket); + request->handlePacket(xmlPacket); return; } std::map::iterator factoryIt = requestHandlerFactories.find(xmlPacket.getType()); if(factoryIt != requestHandlerFactories.end()) { - request = factoryIt->second->createRequestHandler(); + request = factoryIt->second->createRequestHandler(connection, packet.getRequestId()); + requestMap->addRequest(packet.getRequestId(), request); - request->handlePacket(connection, packet.getRequestId(), xmlPacket); + request->handlePacket(xmlPacket); return; } - Logger::log(Logger::ERROR, "Received an unexpected packet."); + Logger::logf(Logger::ERROR, "Received an unexpected packet with type '%s'.", xmlPacket.getType().c_str()); XmlPacket ret; ret.setType("Error"); ret.add("ErrorCode", Exception::UNEXPECTED_PACKET); - connection->send(ret.encode(requestId)); + connection->send(ret.encode(packet.getRequestId())); } -bool RequestManager::sendRequest(Net::Connection *connection, std::auto_ptr request) { +RequestManager::RequestMap* RequestManager::getUnusedRequestId(Net::Connection *connection, uint16_t *requestId) { std::map::iterator it = requestMaps.find(connection); if(it == requestMaps.end()) { Logger::log(Logger::CRITICAL, "Trying to send a request over an unregistered connecion."); - return false; + return 0; } RequestMap *requestMap = it->second; - uint16_t id; do { - id = getRequestId(); - } while(requestMap->findRequest(id)); + *requestId = getRequestId(); + } while(requestMap->findRequest(*requestId)); - request->sendRequest(connection, id); - - requestMap->addRequest(id, request.release()); - - return true; + return requestMap; } void RequestManager::registerConnection(Net::Connection *connection) { @@ -151,7 +147,7 @@ void RequestManager::unregisterPacketType(const std::string &type) { requestHandlerFactories.erase(it); } -RequestManager::RequestManager() : core(false), requestId(-1) { +RequestManager::RequestManager() : core(false), lastRequestId(-1) { registerPacketType("Disconnect"); } diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h index ee4f4d1..c38435b 100644 --- a/src/Common/RequestManager.h +++ b/src/Common/RequestManager.h @@ -20,16 +20,22 @@ #ifndef MAD_COMMON_REQUESTMANAGER_H_ #define MAD_COMMON_REQUESTMANAGER_H_ -#include +#include "Request.h" #include #include +#include + +#include namespace Mad { -namespace Common { -class Request; -class RequestHandler; +namespace Net { +class Connection; +class Packet; +} + +namespace Common { class RequestManager { private: @@ -53,14 +59,14 @@ class RequestManager { RequestHandlerFactory() {} public: - virtual RequestHandler* createRequestHandler() = 0; + virtual RequestHandler* createRequestHandler(Net::Connection *connection, int16_t requestId) = 0; virtual ~RequestHandlerFactory() {} }; template class SpecificRequestHandlerFactory : public RequestHandlerFactory { public: - virtual RequestHandler* createRequestHandler() { - return new T(); + virtual RequestHandler* createRequestHandler(Net::Connection *connection, int16_t requestId) { + return new T(connection, requestId); } }; @@ -68,12 +74,12 @@ class RequestManager { std::map requestMaps; bool core; - uint16_t requestId; + uint16_t lastRequestId; std::map requestHandlerFactories; uint16_t getRequestId() { - return requestId+=2; + return lastRequestId+=2; } // Prevent shallow copy @@ -82,6 +88,10 @@ class RequestManager { void receiveHandler(Net::Connection *connection, const Net::Packet &packet); + RequestMap* getUnusedRequestId(Net::Connection *connection, uint16_t *requestId); + + bool send(Request *request); + RequestManager(); public: @@ -94,9 +104,9 @@ class RequestManager { core = newCore; if(core) - requestId &= ~0x01; + lastRequestId &= ~0x01; else - requestId |= 0x01; + lastRequestId |= 0x01; } void registerConnection(Net::Connection *connection); @@ -108,7 +118,90 @@ class RequestManager { void unregisterPacketType(const std::string &type); - bool sendRequest(Net::Connection *connection, std::auto_ptr request); + template + bool sendRequest(Net::Connection *connection, Request::slot_type slot) { + RequestMap *requestMap; + uint16_t requestId; + + requestMap = getUnusedRequestId(connection, &requestId); + + if(!requestMap) + return false; + + Request *request = new T(connection, requestId, slot); + request->sendRequest(); + requestMap->addRequest(requestId, request); + + return true; + } + + template + bool sendRequest(Net::Connection *connection, Request::slot_type slot, T1 t1) { + RequestMap *requestMap; + uint16_t requestId; + + requestMap = getUnusedRequestId(connection, &requestId); + + if(!requestMap) + return false; + + Request *request = new T(connection, requestId, slot, t1); + request->sendRequest(); + requestMap->addRequest(requestId, request); + + return true; + } + + template + bool sendRequest(Net::Connection *connection, Request::slot_type slot, T1 t1, T2 t2) { + RequestMap *requestMap; + uint16_t requestId; + + requestMap = getUnusedRequestId(connection, &requestId); + + if(!requestMap) + return false; + + Request *request = new T(connection, requestId, slot, t1, t2); + request->sendRequest(); + requestMap->addRequest(requestId, request); + + return true; + } + + template + bool sendRequest(Net::Connection *connection, Request::slot_type slot, T1 t1, T2 t2, T3 t3) { + RequestMap *requestMap; + uint16_t requestId; + + requestMap = getUnusedRequestId(connection, &requestId); + + if(!requestMap) + return false; + + Request *request = new T(connection, requestId, slot, t1, t2, t3); + request->sendRequest(); + requestMap->addRequest(requestId, request); + + return true; + } + + template + bool sendRequest(Net::Connection *connection, Request::slot_type slot, T1 t1, T2 t2, T3 t3, T4 t4) { + RequestMap *requestMap; + uint16_t requestId; + + requestMap = getUnusedRequestId(connection, &requestId); + + if(!requestMap) + return false; + + Request *request = new T(connection, requestId, slot, t1, t2, t3, t4); + request->sendRequest(); + requestMap->addRequest(requestId, request); + + return true; + } virtual ~RequestManager(); }; diff --git a/src/Common/Requests/DisconnectRequest.cpp b/src/Common/Requests/DisconnectRequest.cpp index afab8ed..ec8719d 100644 --- a/src/Common/Requests/DisconnectRequest.cpp +++ b/src/Common/Requests/DisconnectRequest.cpp @@ -24,20 +24,24 @@ namespace Mad { namespace Common { namespace Requests { -void DisconnectRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { +void DisconnectRequest::sendRequest() { XmlPacket packet; packet.setType("Disconnect"); - connection->send(packet.encode(requestId)); + sendPacket(packet); } -void DisconnectRequest::handlePacket(Net::Connection *connection, uint16_t, const XmlPacket &packet) { - if(packet.getType() != "OK") { +void DisconnectRequest::handlePacket(const XmlPacket &packet) { + if(packet.getType() == "Error") { + finishWithError(Common::Exception(packet["Where"], packet["ErrorCode"], packet["SubCode"], packet["SubSubCode"])); + return; + } + else if(packet.getType() != "OK") { finishWithError(Exception(Exception::UNEXPECTED_PACKET)); return; // TODO Logging } - connection->disconnect(); + getConnection()->disconnect(); finish(packet); } diff --git a/src/Common/Requests/DisconnectRequest.h b/src/Common/Requests/DisconnectRequest.h index af7cca5..22ff16b 100644 --- a/src/Common/Requests/DisconnectRequest.h +++ b/src/Common/Requests/DisconnectRequest.h @@ -28,11 +28,12 @@ namespace Requests { class DisconnectRequest : public Request { protected: - virtual void sendRequest(Net::Connection *connection, uint16_t requestId); - virtual void handlePacket(Net::Connection *connection, uint16_t, const XmlPacket &packet); + virtual void sendRequest(); + virtual void handlePacket(const XmlPacket &packet); public: - DisconnectRequest(slot_type slot) : Request(slot) {} + DisconnectRequest(Net::Connection *connection, uint16_t requestId, slot_type slot) + : Request(connection, requestId, slot) {} }; } diff --git a/src/Common/Requests/FSInfoRequest.h b/src/Common/Requests/FSInfoRequest.h index 27a38ed..285f662 100644 --- a/src/Common/Requests/FSInfoRequest.h +++ b/src/Common/Requests/FSInfoRequest.h @@ -28,7 +28,8 @@ namespace Requests { class FSInfoRequest : public SimpleRequest { public: - FSInfoRequest(slot_type slot) : SimpleRequest("FSInfo", slot) {} + FSInfoRequest(Net::Connection *connection, uint16_t requestId, slot_type slot) + : SimpleRequest(connection, requestId, slot, "FSInfo") {} }; } diff --git a/src/Common/Requests/GSSAPIAuthRequest.cpp b/src/Common/Requests/GSSAPIAuthRequest.cpp index 7f4617a..07e516f 100644 --- a/src/Common/Requests/GSSAPIAuthRequest.cpp +++ b/src/Common/Requests/GSSAPIAuthRequest.cpp @@ -19,7 +19,6 @@ #include "GSSAPIAuthRequest.h" #include "../Logger.h" -#include "../XmlPacket.h" #include #include @@ -37,7 +36,7 @@ GSSAPIAuthRequest::~GSSAPIAuthRequest() { gss_release_name(&minStat, &gssServiceName); } -void GSSAPIAuthRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { +void GSSAPIAuthRequest::sendRequest() { OM_uint32 majStat, minStat; gss_buffer_desc buffer; @@ -70,7 +69,7 @@ void GSSAPIAuthRequest::sendRequest(Net::Connection *connection, uint16_t reques ret.setType("AuthGSSAPI"); ret.addBinary("authToken", buffer.value, buffer.length); - if(!connection->send(ret.encode(requestId))) { + if(!sendPacket(ret)) { gss_release_buffer(&minStat, &buffer); return; } @@ -78,7 +77,7 @@ void GSSAPIAuthRequest::sendRequest(Net::Connection *connection, uint16_t reques gss_release_buffer(&minStat, &buffer); } -void GSSAPIAuthRequest::handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet) { +void GSSAPIAuthRequest::handlePacket(const XmlPacket &packet) { if(packet.getType() != "AuthGSSAPI") { finishWithError(Exception(Exception::UNEXPECTED_PACKET)); return; // TODO Logging @@ -114,7 +113,7 @@ void GSSAPIAuthRequest::handlePacket(Net::Connection *connection, uint16_t reque ret.setType("AuthGSSAPI"); ret.addBinary("authToken", sendBuffer.value, sendBuffer.length); - if(!connection->send(ret.encode(requestId))) { + if(!sendPacket(ret)) { gss_release_buffer(&minStat, &sendBuffer); return; } @@ -128,7 +127,7 @@ void GSSAPIAuthRequest::handlePacket(Net::Connection *connection, uint16_t reque recvBuffer.value = std::malloc(recvBuffer.length); std::memcpy(recvBuffer.value, pkgData, recvBuffer.length); - const gnutls_datum_t *cert = connection->getPeerCertificate(); + const gnutls_datum_t *cert = getConnection()->getPeerCertificate(); sendBuffer.length = cert->size; sendBuffer.value = cert->data; @@ -140,7 +139,7 @@ void GSSAPIAuthRequest::handlePacket(Net::Connection *connection, uint16_t reque if(majStat != GSS_S_COMPLETE) return; - connection->setAuthenticated(); + getConnection()->setAuthenticated(); Logger::log(Logger::VERBOSE, "Authentication complete."); majStat = gss_delete_sec_context(&minStat, &gssContext, &sendBuffer); @@ -154,7 +153,7 @@ void GSSAPIAuthRequest::handlePacket(Net::Connection *connection, uint16_t reque ret.setType("AuthGSSAPI"); ret.addBinary("authToken", sendBuffer.value, sendBuffer.length); - if(!connection->send(ret.encode(requestId))) { + if(!sendPacket(ret)) { gss_release_buffer(&minStat, &sendBuffer); return; } diff --git a/src/Common/Requests/GSSAPIAuthRequest.h b/src/Common/Requests/GSSAPIAuthRequest.h index ca6195b..104af0c 100644 --- a/src/Common/Requests/GSSAPIAuthRequest.h +++ b/src/Common/Requests/GSSAPIAuthRequest.h @@ -38,12 +38,12 @@ class GSSAPIAuthRequest : public Request { bool gssContinue; - virtual void sendRequest(Net::Connection *connection, uint16_t requestId); - virtual void handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet); + virtual void sendRequest(); + virtual void handlePacket(const XmlPacket &packet); public: - GSSAPIAuthRequest(const std::string &serviceName0, slot_type slot) - : Request(slot), serviceName(serviceName0), gssServiceName(GSS_C_NO_NAME), gssContext(GSS_C_NO_CONTEXT), gssContinue(true) {} + GSSAPIAuthRequest(Net::Connection *connection, uint16_t requestId, slot_type slot, const std::string &serviceName0) + : Request(connection, requestId, slot), serviceName(serviceName0), gssServiceName(GSS_C_NO_NAME), gssContext(GSS_C_NO_CONTEXT), gssContinue(true) {} virtual ~GSSAPIAuthRequest(); }; diff --git a/src/Common/Requests/SimpleRequest.cpp b/src/Common/Requests/SimpleRequest.cpp index 018ac39..97426ae 100644 --- a/src/Common/Requests/SimpleRequest.cpp +++ b/src/Common/Requests/SimpleRequest.cpp @@ -19,17 +19,15 @@ #include "SimpleRequest.h" -#include - namespace Mad { namespace Common { namespace Requests { -void SimpleRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { +void SimpleRequest::sendRequest() { XmlPacket packet; packet.setType(type); - connection->send(packet.encode(requestId)); + sendPacket(packet); } } diff --git a/src/Common/Requests/SimpleRequest.h b/src/Common/Requests/SimpleRequest.h index 0426910..ca95eca 100644 --- a/src/Common/Requests/SimpleRequest.h +++ b/src/Common/Requests/SimpleRequest.h @@ -32,9 +32,10 @@ class SimpleRequest : public Request { protected: const std::string type; - virtual void sendRequest(Net::Connection *connection, uint16_t requestId); + virtual void sendRequest(); - SimpleRequest(const std::string &type0, slot_type slot) : Request(slot), type(type0) {} + SimpleRequest(Net::Connection *connection, uint16_t requestId, slot_type slot, const std::string &type0) + : Request(connection, requestId, slot), type(type0) {} }; } diff --git a/src/Common/Requests/StatusRequest.h b/src/Common/Requests/StatusRequest.h index d557358..ad89ec4 100644 --- a/src/Common/Requests/StatusRequest.h +++ b/src/Common/Requests/StatusRequest.h @@ -28,7 +28,8 @@ namespace Requests { class StatusRequest : public SimpleRequest { public: - StatusRequest(slot_type slot) : SimpleRequest("GetStatus", slot) {} + StatusRequest(Net::Connection *connection, uint16_t requestId, slot_type slot) + : SimpleRequest(connection, requestId, slot, "GetStatus") {} }; } diff --git a/src/Common/Requests/UserListRequest.h b/src/Common/Requests/UserListRequest.h index 918925c..69b0f05 100644 --- a/src/Common/Requests/UserListRequest.h +++ b/src/Common/Requests/UserListRequest.h @@ -28,7 +28,8 @@ namespace Requests { class UserListRequest : public SimpleRequest { public: - UserListRequest(slot_type slot) : SimpleRequest("ListUsers", slot) {} + UserListRequest(Net::Connection *connection, uint16_t requestId, slot_type slot) + : SimpleRequest(connection, requestId, slot, "ListUsers") {} }; } -- cgit v1.2.3