diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-04 23:52:14 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-04 23:52:14 +0200 |
commit | a3a41c71c50f95f95965165eff28d24a1be24453 (patch) | |
tree | ce0c0bbfdf793b055a28ac722659cb629f39ac10 /src/Common | |
parent | 38ddaa7aa1f2093848ad760027096ee8d0b105e2 (diff) | |
download | mad-a3a41c71c50f95f95965165eff28d24a1be24453.tar mad-a3a41c71c50f95f95965165eff28d24a1be24453.zip |
RequestManager kann jetzt flexibler auf eingehende Anfragen reagieren
Diffstat (limited to 'src/Common')
-rw-r--r-- | src/Common/Makefile.am | 2 | ||||
-rw-r--r-- | src/Common/Makefile.in | 2 | ||||
-rw-r--r-- | src/Common/Request/DisconnectRequest.h | 8 | ||||
-rw-r--r-- | src/Common/Request/IdentifyRequest.h | 8 | ||||
-rw-r--r-- | src/Common/Request/Request.h | 23 | ||||
-rw-r--r-- | src/Common/RequestHandler.h | 52 | ||||
-rw-r--r-- | src/Common/RequestManager.cpp | 17 | ||||
-rw-r--r-- | src/Common/RequestManager.h | 48 |
8 files changed, 131 insertions, 29 deletions
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 <http://www.gnu.org/licenses/>. */ -#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 <http://www.gnu.org/licenses/>. */ -#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 <matthias@gamezock.de> + * 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 <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_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 <iostream> @@ -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<Net::Packet::Type,RequestHandlerFactory*>::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 <Net/Connection.h> #include <map> namespace Mad { namespace Common { +namespace Request { +class Request; +} + class RequestManager { private: - class RequestMap : private std::map<unsigned short,Request::Request*> { + class RequestMap : private std::map<unsigned short,RequestHandler*> { 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 T> 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<Net::Connection*,RequestMap*> requestMaps; unsigned short requestId; + std::map<Net::Packet::Type,RequestHandlerFactory*> requestHandlerFactories; + unsigned short getRequestId() { return requestId+=2; } @@ -98,6 +120,21 @@ class RequestManager { requestMaps.erase(it); } + template <class T> void registerPacketType(Net::Packet::Type type) { + requestHandlerFactories.insert(std::make_pair(type, new SpecificRequestHandlerFactory<T>())); + } + + void unregisterPacketType(Net::Packet::Type type) { + std::map<Net::Packet::Type,RequestHandlerFactory*>::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<Net::Connection*,RequestMap*>::iterator it = requestMaps.begin(); it != requestMaps.end(); ++it) delete it->second; + + for(std::map<Net::Packet::Type,RequestHandlerFactory*>::iterator it = requestHandlerFactories.begin(); it != requestHandlerFactories.end(); ++it) + delete it->second; } }; |