From a3a41c71c50f95f95965165eff28d24a1be24453 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 4 Sep 2008 23:52:14 +0200 Subject: RequestManager kann jetzt flexibler auf eingehende Anfragen reagieren --- src/Common/Makefile.am | 2 +- src/Common/Makefile.in | 2 +- src/Common/Request/DisconnectRequest.h | 8 +++--- src/Common/Request/IdentifyRequest.h | 8 +++--- src/Common/Request/Request.h | 23 ++++++--------- src/Common/RequestHandler.h | 52 ++++++++++++++++++++++++++++++++++ src/Common/RequestManager.cpp | 17 ++++++++++- src/Common/RequestManager.h | 48 ++++++++++++++++++++++++++++--- src/Net/Packet.h | 2 +- 9 files changed, 132 insertions(+), 30 deletions(-) create mode 100644 src/Common/RequestHandler.h (limited to 'src') diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index 1958a1c..f04f3db 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -3,4 +3,4 @@ SUBDIRS = Request noinst_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = ConfigManager.cpp RequestManager.cpp -noinst_HEADERS = ConfigManager.h RequestManager.h Util.h +noinst_HEADERS = ConfigManager.h RequestHandler.h RequestManager.h Util.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index da63be0..9e02df1 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -192,7 +192,7 @@ top_srcdir = @top_srcdir@ SUBDIRS = Request noinst_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = ConfigManager.cpp RequestManager.cpp -noinst_HEADERS = ConfigManager.h RequestManager.h Util.h +noinst_HEADERS = ConfigManager.h RequestHandler.h RequestManager.h Util.h all: all-recursive .SUFFIXES: diff --git a/src/Common/Request/DisconnectRequest.h b/src/Common/Request/DisconnectRequest.h index 77509a0..48256a7 100644 --- a/src/Common/Request/DisconnectRequest.h +++ b/src/Common/Request/DisconnectRequest.h @@ -17,8 +17,8 @@ * with this program. If not, see . */ -#ifndef DISCONNECTREQUEST_H_ -#define DISCONNECTREQUEST_H_ +#ifndef MAD_COMMON_REQUEST_DISCONNECTREQUEST_H_ +#define MAD_COMMON_REQUEST_DISCONNECTREQUEST_H_ #include "Request.h" #include "../RequestManager.h" @@ -35,7 +35,7 @@ class DisconnectRequest: public Request { public: static bool send(Net::Connection *connection, RequestManager &requestManager) { - Request *request = new DisconnectRequest(); + DisconnectRequest *request = new DisconnectRequest(); if(requestManager.sendRequest(connection, request)) return true; @@ -73,4 +73,4 @@ class DisconnectRequest: public Request { } } -#endif /* DISCONNECTREQUEST_H_ */ +#endif /* MAD_COMMON_REQUEST_DISCONNECTREQUEST_H_ */ diff --git a/src/Common/Request/IdentifyRequest.h b/src/Common/Request/IdentifyRequest.h index 9df1548..bb98bad 100644 --- a/src/Common/Request/IdentifyRequest.h +++ b/src/Common/Request/IdentifyRequest.h @@ -17,8 +17,8 @@ * with this program. If not, see . */ -#ifndef IDENTIFYREQUEST_H_ -#define IDENTIFYREQUEST_H_ +#ifndef MAD_COMMON_REQUEST_IDENTIFYREQUEST_H_ +#define MAD_COMMON_REQUEST_IDENTIFYREQUEST_H_ #include "Request.h" #include "../RequestManager.h" @@ -40,7 +40,7 @@ class IdentifyRequest: public Request { public: static bool send(Net::Connection *connection, RequestManager &requestManager, const std::string &hostname0) { - Request *request = new IdentifyRequest(hostname0); + IdentifyRequest *request = new IdentifyRequest(hostname0); if(requestManager.sendRequest(connection, request)) return true; @@ -78,4 +78,4 @@ class IdentifyRequest: public Request { } } -#endif /* IDENTIFYREQUEST_H_ */ +#endif /* MAD_COMMON_REQUEST_IDENTIFYREQUEST_H_ */ diff --git a/src/Common/Request/Request.h b/src/Common/Request/Request.h index 211b2cd..e0fcf11 100644 --- a/src/Common/Request/Request.h +++ b/src/Common/Request/Request.h @@ -1,7 +1,7 @@ /* * Request.h * - * Copyright (C) 2008 Matthias Schiffer + * Copyright (C) 2008 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 @@ -20,38 +20,33 @@ #ifndef MAD_COMMON_REQUEST_REQUEST_H_ #define MAD_COMMON_REQUEST_REQUEST_H_ -namespace Mad { +#include "../RequestHandler.h" -namespace Net { -class Connection; -class Packet; -} +namespace Mad { namespace Common { + namespace Request { -class Request { +class Request : public RequestHandler { private: - bool sent, finished; + bool sent; protected: - Request() : sent(false), finished(false) {} + Request() : sent(false) {} void setSent() {sent = true;} - void setFinished() {finished = true;} public: - virtual ~Request() {} - bool isSent() const {return sent;} - bool isFinished() const {return finished;} virtual bool sendRequest(Net::Connection *connection, unsigned short requestId) = 0; - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet) = 0; }; } + } + } #endif /* MAD_COMMON_REQUEST_REQUEST_H_ */ diff --git a/src/Common/RequestHandler.h b/src/Common/RequestHandler.h new file mode 100644 index 0000000..a6995ec --- /dev/null +++ b/src/Common/RequestHandler.h @@ -0,0 +1,52 @@ +/* + * RequestHandler.h + * + * Copyright (C) 2008 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 . + */ + +#ifndef MAD_COMMON_REQUESTHANDLER_H_ +#define MAD_COMMON_REQUESTHANDLER_H_ + +namespace Mad { + +namespace Net { +class Connection; +class Packet; +} + +namespace Common { + +class RequestHandler { + private: + bool finished; + + protected: + RequestHandler() : finished(false) {} + + void setFinished() {finished = true;} + + public: + virtual ~RequestHandler() {} + + bool isFinished() const {return finished;} + + virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet) = 0; +}; + +} +} + +#endif /* MAD_COMMON_REQUESTHANDLER_H_ */ diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index 5f6c3a4..f8da7dd 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -18,6 +18,7 @@ */ #include "RequestManager.h" +#include "Request/Request.h" #include @@ -35,7 +36,7 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack } RequestMap *requestMap = it->second; - Request::Request *request = requestMap->findRequest(packet.getRequestId()); + RequestHandler *request = requestMap->findRequest(packet.getRequestId()); if(request) { request->handlePacket(connection, packet); @@ -46,6 +47,20 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack return; } + std::map::iterator factoryIt = requestHandlerFactories.find(packet.getType()); + if(factoryIt != requestHandlerFactories.end()) { + request = factoryIt->second->createRequestHandler(); + request->handlePacket(connection, packet); + + if(!request->isFinished()) + requestMap->addRequest(packet.getRequestId(), request); + else + delete request; + + return; + } + + switch(packet.getType()) { case Net::Packet::TYPE_DISCONNECT: connection->send(Net::Packet(Net::Packet::TYPE_OK, packet.getRequestId())); diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h index 543fb83..77eca52 100644 --- a/src/Common/RequestManager.h +++ b/src/Common/RequestManager.h @@ -20,16 +20,20 @@ #ifndef MAD_COMMON_REQUESTMANAGER_H_ #define MAD_COMMON_REQUESTMANAGER_H_ -#include "Request/Request.h" +#include "RequestHandler.h" #include #include namespace Mad { namespace Common { +namespace Request { +class Request; +} + class RequestManager { private: - class RequestMap : private std::map { + class RequestMap : private std::map { private: // Prevent shallow copy RequestMap(const RequestMap &o); @@ -43,11 +47,11 @@ class RequestManager { delete it->second; } - bool addRequest(unsigned short id, Request::Request *info) { + bool addRequest(unsigned short id, RequestHandler *info) { return insert(std::make_pair(id, info)).second; } - Request::Request* findRequest(unsigned short id) { + RequestHandler* findRequest(unsigned short id) { iterator it = find(id); if(it == end()) return 0; @@ -67,6 +71,22 @@ class RequestManager { } }; + class RequestHandlerFactory { + protected: + RequestHandlerFactory() {} + + public: + virtual RequestHandler* createRequestHandler() = 0; + virtual ~RequestHandlerFactory() {} + }; + + template class SpecificRequestHandlerFactory : public RequestHandlerFactory { + public: + virtual RequestHandler* createRequestHandler() { + return new T(); + } + }; + // Prevent shallow copy RequestManager(const RequestManager &o); RequestManager& operator=(const RequestManager &o); @@ -74,6 +94,8 @@ class RequestManager { std::map requestMaps; unsigned short requestId; + std::map requestHandlerFactories; + unsigned short getRequestId() { return requestId+=2; } @@ -98,6 +120,21 @@ class RequestManager { requestMaps.erase(it); } + template void registerPacketType(Net::Packet::Type type) { + requestHandlerFactories.insert(std::make_pair(type, new SpecificRequestHandlerFactory())); + } + + void unregisterPacketType(Net::Packet::Type type) { + std::map::iterator it = requestHandlerFactories.find(type); + + if(it == requestHandlerFactories.end()) + return; + + delete it->second; + + requestHandlerFactories.erase(it); + } + bool sendRequest(Net::Connection *connection, Request::Request *request); RequestManager(bool core) : requestId(core ? -2 : -1) {} @@ -105,6 +142,9 @@ class RequestManager { virtual ~RequestManager() { for(std::map::iterator it = requestMaps.begin(); it != requestMaps.end(); ++it) delete it->second; + + for(std::map::iterator it = requestHandlerFactories.begin(); it != requestHandlerFactories.end(); ++it) + delete it->second; } }; diff --git a/src/Net/Packet.h b/src/Net/Packet.h index 2d2fb77..cd734a4 100644 --- a/src/Net/Packet.h +++ b/src/Net/Packet.h @@ -30,7 +30,7 @@ class Packet { public: enum Type { TYPE_OK = 0x0000, TYPE_ERROR = 0x0001, TYPE_DISCONNECT = 0x0002, - TYPE_IDENTIFY_REQ = 0x0010, TYPE_IDENTIFY_REP = 0x0011 + TYPE_CERT_REQ = 0x0010, TYPE_CERT_REP = 0x0011, TYPE_IDENTIFY_REQ = 0x0012, TYPE_IDENTIFY_REP = 0x0013 }; struct Data { -- cgit v1.2.3