/* * RequestManager.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_REQUESTMANAGER_H_ #define MAD_COMMON_REQUESTMANAGER_H_ #include "Initializable.h" #include #include #include namespace Mad { namespace Common { class RequestBase; class RequestHandler; class RequestManager : public Initializable { private: class RequestMap : private std::map { private: // Prevent shallow copy RequestMap(const RequestMap &o); RequestMap& operator=(const RequestMap &o); public: RequestMap() {} ~RequestMap(); bool addRequest(uint16_t id, RequestHandler *info); RequestHandler* findRequest(uint16_t id); bool deleteRequest(uint16_t id); }; class RequestHandlerFactory { protected: RequestHandlerFactory() {} public: virtual RequestHandler* createRequestHandler() = 0; virtual ~RequestHandlerFactory() {} }; template class SpecificRequestHandlerFactory : public RequestHandlerFactory { public: virtual RequestHandler* createRequestHandler() { return new T(); } }; static RequestManager requestManager; std::map requestMaps; bool core; uint16_t requestId; std::map requestHandlerFactories; uint16_t getRequestId() { return requestId+=2; } // Prevent shallow copy RequestManager(const RequestManager &o); RequestManager& operator=(const RequestManager &o); RequestManager(); void receiveHandler(Net::Connection *connection, const Net::Packet &packet); public: static RequestManager* get() { return &requestManager; } bool isCore() const {return core;} void setCore(bool newCore) {core = newCore;} virtual void doInit() {requestId = core ? -2 : -1;} void registerConnection(Net::Connection *connection); void unregisterConnection(Net::Connection *connection); template void registerPacketType(Net::Packet::Type type) { requestHandlerFactories.insert(std::make_pair(type, new SpecificRequestHandlerFactory())); } void unregisterPacketType(Net::Packet::Type type); bool sendRequest(Net::Connection *connection, std::auto_ptr request); virtual ~RequestManager(); }; } } #endif /* MAD_COMMON_REQUESTMANAGER_H_ */