/* * 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 #include #include namespace Mad { namespace Common { class RequestBase; class RequestHandler; class XmlRequestBase; class XmlRequestHandler; class RequestManager { 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 XmlRequestMap : private std::map { private: // Prevent shallow copy XmlRequestMap(const XmlRequestMap &o); XmlRequestMap& operator=(const XmlRequestMap &o); public: XmlRequestMap() {} ~XmlRequestMap(); bool addRequest(uint16_t id, XmlRequestHandler *info); XmlRequestHandler* 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(); } }; class XmlRequestHandlerFactory { protected: XmlRequestHandlerFactory() {} public: virtual XmlRequestHandler* createRequestHandler() = 0; virtual ~XmlRequestHandlerFactory() {} }; template class SpecificXmlRequestHandlerFactory : public XmlRequestHandlerFactory { public: virtual XmlRequestHandler* createRequestHandler() { return new T(); } }; static RequestManager requestManager; std::map requestMaps; std::map xmlRequestMaps; bool core; uint16_t requestId; std::map requestHandlerFactories; std::map xmlRequestHandlerFactories; uint16_t getRequestId() { return requestId+=2; } // Prevent shallow copy RequestManager(const RequestManager &o); RequestManager& operator=(const RequestManager &o); void receiveHandler(Net::Connection *connection, const Net::Packet &packet); RequestManager(); public: static RequestManager* get() { return &requestManager; } bool isCore() const {return core;} void setCore(bool newCore) { core = newCore; if(core) requestId &= ~0x01; else requestId |= 0x01; } 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); template void registerPacketType(const std::string &type) { xmlRequestHandlerFactories.insert(std::make_pair(type, new SpecificXmlRequestHandlerFactory())); } void unregisterPacketType(const std::string &type); bool sendRequest(Net::Connection *connection, std::auto_ptr request); bool sendRequest(Net::Connection *connection, std::auto_ptr request); virtual ~RequestManager(); }; } } #endif /* MAD_COMMON_REQUESTMANAGER_H_ */