From c316a3115bf790fb623f0765dd6b04f781af5b51 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 4 Mar 2009 22:53:02 +0100 Subject: Alte Request-Klassen entfernt --- src/Client/CommandManager.cpp | 2 +- src/Client/CommandManager.h | 2 +- src/Client/CommandParser.h | 1 - src/Common/Makefile.am | 2 +- src/Common/Makefile.in | 2 +- src/Common/Request.h | 92 ---------------------------- src/Common/RequestBase.h | 39 ------------ src/Common/RequestHandler.h | 60 ------------------ src/Common/RequestManager.cpp | 137 ++++-------------------------------------- src/Common/RequestManager.h | 42 ------------- src/Common/XmlPacket.cpp | 2 +- src/Net/Connection.cpp | 4 +- src/Net/Packet.cpp | 28 +-------- src/Net/Packet.h | 24 +------- src/mad.cpp | 1 - 15 files changed, 21 insertions(+), 417 deletions(-) delete mode 100644 src/Common/Request.h delete mode 100644 src/Common/RequestBase.h delete mode 100644 src/Common/RequestHandler.h diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index 59de7c4..bcb3429 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -68,7 +68,7 @@ void CommandManager::printFSInfo(const Common::XmlPacket &packet) { nameString.resize(32, ' '); } else { - nameString += '\n'; + nameString += "\n\t"; nameString.resize(nameString.length() + 32, ' '); } diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index e2e5ff4..a49cda7 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -20,7 +20,7 @@ #ifndef MAD_CLIENT_COMMANDMANAGER_H_ #define MAD_CLIENT_COMMANDMANAGER_H_ -#include +#include namespace Mad { diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index cb38588..2827443 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -22,7 +22,6 @@ #include "CommandManager.h" #include -#include #include #include diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index 5d4fb09..79bc0b1 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -8,5 +8,5 @@ libcommon_la_LIBADD = Requests/librequests.la RequestHandlers/librequesthandler 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 RequestBase.h RequestHandler.h RequestManager.h \ + RemoteLogger.h RequestManager.h \ SystemBackend.h Tokenizer.h UserInfo.h XmlPacket.h XmlRequest.h XmlRequestHandler.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 948343c..1f7bca5 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -245,7 +245,7 @@ libcommon_la_SOURCES = ActionManager.cpp ConfigEntry.cpp ConfigManager.cpp Excep 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 RequestBase.h RequestHandler.h RequestManager.h \ + RemoteLogger.h RequestManager.h \ SystemBackend.h Tokenizer.h UserInfo.h XmlPacket.h XmlRequest.h XmlRequestHandler.h all: all-recursive diff --git a/src/Common/Request.h b/src/Common/Request.h deleted file mode 100644 index 2317d5a..0000000 --- a/src/Common/Request.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Request.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_REQUEST_H_ -#define MAD_COMMON_REQUEST_H_ - -#include "RequestBase.h" -#include "Exception.h" - -#include -#include - -namespace Mad { -namespace Common { - -template class Request : public RequestBase { - private: - sigc::signal&> finished; - - std::auto_ptr res; - Exception exp; - - public: - typedef sigc::slot&> slot_type; - - protected: - Request(slot_type slot) : exp(Exception::NOT_FINISHED) { - finished.connect(slot); - finished.connect(sigc::hide(signalFinished().make_slot())); - } - - void finish(std::auto_ptr result) {res = result; finished(*this);} - void finish(const T& result) {res.reset(new T(result)); finished(*this);} - void finishWithError(const Exception &e) {exp = e; finished(*this);} - - public: - const T& getResult() const throw(Exception) { - if(res.get()) - return *res; - - throw exp; - } -}; - -template<> class Request : public RequestBase { - private: - sigc::signal&> finished; - - bool isFinished; - Exception exp; - - public: - typedef sigc::slot&> slot_type; - - protected: - Request(slot_type slot) : isFinished(false), exp(Exception::NOT_FINISHED) { - finished.connect(slot); - finished.connect(sigc::hide(signalFinished().make_slot())); - } - - void finish() {isFinished = true; finished(*this);} - void finishWithError(const Exception &e) {exp = e; finished(*this);} - - public: - void getResult() const throw(Exception) { - if(isFinished) - return; - - throw exp; - } -}; - -} -} - -#endif /* MAD_COMMON_REQUEST_H_ */ diff --git a/src/Common/RequestBase.h b/src/Common/RequestBase.h deleted file mode 100644 index 535f6d0..0000000 --- a/src/Common/RequestBase.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * RequestBase.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_REQUESTBASE_H_ -#define MAD_COMMON_REQUESTBASE_H_ - -#include "RequestHandler.h" -#include - -namespace Mad { -namespace Common { - -class RequestBase : public RequestHandler { - protected: - virtual void sendRequest(Net::Connection *connection, uint16_t requestId) = 0; - - friend class RequestManager; -}; - -} -} - -#endif /* MAD_COMMON_REQUESTBASE_H_ */ diff --git a/src/Common/RequestHandler.h b/src/Common/RequestHandler.h deleted file mode 100644 index 434f969..0000000 --- a/src/Common/RequestHandler.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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_ - -#include - -namespace Mad { - -namespace Net { -class Connection; -class Packet; -} - -namespace Common { - -class RequestManager; - -class RequestHandler { - private: - sigc::signal finished; - - // Prevent shallow copy - RequestHandler(const RequestHandler &o); - RequestHandler& operator=(const RequestHandler &o); - - protected: - RequestHandler() {} - - sigc::signal signalFinished() {return finished;} - - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet) = 0; - - public: - virtual ~RequestHandler() {} - - friend class RequestManager; -}; - -} -} - -#endif /* MAD_COMMON_REQUESTHANDLER_H_ */ diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index b5acc5c..de4f2ca 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -18,7 +18,6 @@ */ #include "RequestManager.h" -#include "Request.h" #include "XmlRequest.h" #include "XmlPacket.h" #include "RequestHandlers/DisconnectRequestHandler.h" @@ -33,40 +32,6 @@ namespace Common { RequestManager RequestManager::requestManager; -RequestManager::RequestMap::~RequestMap() { - for(iterator it = begin(); it != end(); ++it) - delete it->second; -} - -bool RequestManager::RequestMap::addRequest(uint16_t id, RequestHandler *info) { - if(!insert(std::make_pair(id, info)).second) - return false; - - info->signalFinished().connect(sigc::hide_return(sigc::bind(sigc::mem_fun(this, &RequestManager::RequestMap::deleteRequest), id))); - - return true; -} - -RequestHandler* RequestManager::RequestMap::findRequest(uint16_t id) { - iterator it = find(id); - if(it == end()) - return 0; - - return it->second; -} - -bool RequestManager::RequestMap::deleteRequest(uint16_t id) { - iterator it = find(id); - if(it == end()) - return false; - - delete it->second; - - erase(it); - return true; -} - - RequestManager::XmlRequestMap::~XmlRequestMap() { for(iterator it = begin(); it != end(); ++it) delete it->second; @@ -102,60 +67,29 @@ bool RequestManager::XmlRequestMap::deleteRequest(uint16_t id) { void RequestManager::receiveHandler(Net::Connection *connection, const Net::Packet &packet) { - std::map::iterator it = requestMaps.find(connection); - - if(it == requestMaps.end()) { - // TODO: Error - Logger::log(Logger::ERROR, "Received a packet from an unregistered connection."); - - return; - } - - RequestMap *requestMap = it->second; - RequestHandler *request = requestMap->findRequest(packet.getRequestId()); + XmlPacket xmlPacket(packet); - if(request) { - request->handlePacket(connection, packet); - - return; - } - - std::map::iterator it2 = xmlRequestMaps.find(connection); - if(it2 == xmlRequestMaps.end()) { + std::map::iterator it = xmlRequestMaps.find(connection); + if(it == xmlRequestMaps.end()) { // TODO: Error Logger::log(Logger::ERROR, "Received a packet from an unregistered connection."); return; } - XmlRequestMap *xmlRequestMap = it2->second; + XmlRequestMap *xmlRequestMap = it->second; XmlRequestHandler *xmlRequest = xmlRequestMap->findRequest(packet.getRequestId()); if(xmlRequest) { - XmlPacket xmlPacket(packet); xmlRequest->handlePacket(connection, packet.getRequestId(), xmlPacket); - return; } - if(packet.getType() == Net::Packet::XML) { - XmlPacket xmlPacket(packet); - - std::map::iterator factoryIt = xmlRequestHandlerFactories.find(xmlPacket.getType()); - if(factoryIt != xmlRequestHandlerFactories.end()) { - xmlRequest = factoryIt->second->createRequestHandler(); - xmlRequestMap->addRequest(packet.getRequestId(), xmlRequest); - xmlRequest->handlePacket(connection, packet.getRequestId(), xmlPacket); - - return; - } - } - - std::map::iterator factoryIt = requestHandlerFactories.find(packet.getType()); - if(factoryIt != requestHandlerFactories.end()) { - request = factoryIt->second->createRequestHandler(); - requestMap->addRequest(packet.getRequestId(), request); - request->handlePacket(connection, packet); + std::map::iterator factoryIt = xmlRequestHandlerFactories.find(xmlPacket.getType()); + if(factoryIt != xmlRequestHandlerFactories.end()) { + xmlRequest = factoryIt->second->createRequestHandler(); + xmlRequestMap->addRequest(packet.getRequestId(), xmlRequest); + xmlRequest->handlePacket(connection, packet.getRequestId(), xmlPacket); return; } @@ -169,28 +103,6 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack connection->send(ret.encode(requestId)); } -bool RequestManager::sendRequest(Net::Connection *connection, std::auto_ptr request) { - 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; - } - - RequestMap *requestMap = it->second; - - uint16_t id; - do { - id = getRequestId(); - } while(requestMap->findRequest(id)); - - request->sendRequest(connection, id); - - requestMap->addRequest(id, request.release()); - - return true; -} - bool RequestManager::sendRequest(Net::Connection *connection, std::auto_ptr request) { std::map::iterator it = xmlRequestMaps.find(connection); @@ -214,37 +126,18 @@ bool RequestManager::sendRequest(Net::Connection *connection, std::auto_ptrsignalReceive().connect(sigc::mem_fun(this, &RequestManager::receiveHandler)); } void RequestManager::unregisterConnection(Net::Connection *connection) { - std::map::iterator it = requestMaps.find(connection); + std::map::iterator it = xmlRequestMaps.find(connection); - if(it != requestMaps.end()) { + if(it != xmlRequestMaps.end()) { delete it->second; - requestMaps.erase(it); + xmlRequestMaps.erase(it); } - - std::map::iterator it2 = xmlRequestMaps.find(connection); - - if(it2 != xmlRequestMaps.end()) { - delete it2->second; - xmlRequestMaps.erase(it2); - } -} - -void RequestManager::unregisterPacketType(Net::Packet::Type type) { - std::map::iterator it = requestHandlerFactories.find(type); - - if(it == requestHandlerFactories.end()) - return; - - delete it->second; - - requestHandlerFactories.erase(it); } void RequestManager::unregisterPacketType(const std::string &type) { @@ -265,15 +158,9 @@ RequestManager::RequestManager() : core(false), requestId(-1) { RequestManager::~RequestManager() { unregisterPacketType("Disconnect"); - for(std::map::iterator it = requestMaps.begin(); it != requestMaps.end(); ++it) - delete it->second; - for(std::map::iterator it = xmlRequestMaps.begin(); it != xmlRequestMaps.end(); ++it) delete it->second; - for(std::map::iterator it = requestHandlerFactories.begin(); it != requestHandlerFactories.end(); ++it) - delete it->second; - for(std::map::iterator it = xmlRequestHandlerFactories.begin(); it != xmlRequestHandlerFactories.end(); ++it) delete it->second; } diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h index f4ad208..8a3d151 100644 --- a/src/Common/RequestManager.h +++ b/src/Common/RequestManager.h @@ -28,28 +28,11 @@ namespace Mad { namespace Common { -class RequestBase; -class RequestHandler; class XmlRequest; 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 @@ -65,22 +48,6 @@ class RequestManager { 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() {} @@ -99,12 +66,10 @@ class RequestManager { static RequestManager requestManager; - std::map requestMaps; std::map xmlRequestMaps; bool core; uint16_t requestId; - std::map requestHandlerFactories; std::map xmlRequestHandlerFactories; uint16_t getRequestId() { @@ -137,19 +102,12 @@ class RequestManager { 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(); diff --git a/src/Common/XmlPacket.cpp b/src/Common/XmlPacket.cpp index 87045a8..73dd87d 100644 --- a/src/Common/XmlPacket.cpp +++ b/src/Common/XmlPacket.cpp @@ -294,7 +294,7 @@ Net::Packet XmlPacket::encode(uint16_t requestId) const { xmlDocDumpMemory(doc, &buf, &length); - Net::Packet packet(Net::Packet::XML, requestId, buf, length); + Net::Packet packet(requestId, buf, length); xmlFree(buf); diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 7f8d9a9..9b6b87b 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -124,7 +124,7 @@ void Connection::packetHeaderReceiveHandler(const void *data, unsigned long leng header = *(const Packet::Data*)data; if(header.length == 0) { - signal(this, Packet((Packet::Type)ntohs(header.type), ntohs(header.requestId))); + signal(this, Packet(ntohs(header.requestId))); enterReceiveLoop(); } @@ -144,7 +144,7 @@ void Connection::packetDataReceiveHandler(const void *data, unsigned long length return; } - signal(this, Packet((Packet::Type)ntohs(header.type), ntohs(header.requestId), data, length)); + signal(this, Packet(ntohs(header.requestId), data, length)); enterReceiveLoop(); } diff --git a/src/Net/Packet.cpp b/src/Net/Packet.cpp index 976fd65..a4e1b89 100644 --- a/src/Net/Packet.cpp +++ b/src/Net/Packet.cpp @@ -22,12 +22,10 @@ namespace Mad { namespace Net { -Packet::Packet(Type type, uint16_t requestId, const void *data, uint16_t length) { +Packet::Packet(uint16_t requestId, const void *data, uint16_t length) { rawData = (Data*)std::malloc(sizeof(Data)+length); - rawData->type = htons(type); rawData->requestId = htons(requestId); - rawData->reserved = 0; rawData->length = htons(length); if(length) @@ -46,29 +44,5 @@ Packet& Packet::operator=(const Packet &p) { return *this; } - -uint64_t Packet::htonll(uint64_t val) { - union { - uint32_t u32[2]; - uint64_t u64; - } ret; - - ret.u32[0] = htonl(val >> 32); - ret.u32[1] = htonl(val); - - return ret.u64; -} - -uint64_t Packet::ntohll(uint64_t val) { - union { - uint32_t u32[2]; - uint64_t u64; - } v; - - v.u64 = val; - - return (((uint64_t)ntohl(v.u32[0])) << 32) | ntohl(v.u32[1]); -} - } } diff --git a/src/Net/Packet.h b/src/Net/Packet.h index 34f3428..48804de 100644 --- a/src/Net/Packet.h +++ b/src/Net/Packet.h @@ -30,16 +30,8 @@ namespace Net { class Packet { public: - enum Type { - OK = 0x0000, ERROR = 0x0001, - GSSAPI_AUTH = 0x0010, - XML = 0xFFFF - }; - struct Data { - uint16_t type; uint16_t requestId; - uint16_t reserved; uint16_t length; uint8_t data[0]; }; @@ -47,14 +39,8 @@ class Packet { protected: Data *rawData; - void setLength(uint16_t length) { - rawData->length = htons(length); - - rawData = (Data*)std::realloc(rawData, getRawDataLength()); - } - public: - Packet(Type type, uint16_t requestId, const void *data = 0, uint16_t length = 0); + Packet(uint16_t requestId, const void *data = 0, uint16_t length = 0); Packet(const Packet &p) { rawData = (Data*)std::malloc(p.getRawDataLength()); @@ -67,10 +53,6 @@ class Packet { Packet& operator=(const Packet &p); - Type getType() const { - return (Type)ntohs(rawData->type); - } - uint16_t getRequestId() const { return ntohs(rawData->requestId); } @@ -90,10 +72,6 @@ class Packet { unsigned long getRawDataLength() const { return sizeof(Data) + ntohs(rawData->length); } - - - static uint64_t htonll(uint64_t val); - static uint64_t ntohll(uint64_t val); }; } diff --git a/src/mad.cpp b/src/mad.cpp index 4a9904a..bc218f6 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -24,7 +24,6 @@ #include "Common/LogManager.h" #include "Common/Logger.h" #include "Common/ModuleManager.h" -#include "Common/Request.h" #include "Common/RequestManager.h" #include "Common/RequestHandlers/FSInfoRequestHandler.h" #include "Common/RequestHandlers/StatusRequestHandler.h" -- cgit v1.2.3