From 7601149f476c1aa3dae6cd55027a36b62efabf12 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 1 Jul 2008 03:24:58 +0200 Subject: RequestManager hinzugef?gt --- src/Core/ConnectionManager.cpp | 63 +++++++++++++----------------------------- src/Core/ConnectionManager.h | 19 +++++++------ 2 files changed, 29 insertions(+), 53 deletions(-) (limited to 'src/Core') diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 5ff5e29..020c8a7 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -23,58 +23,33 @@ #include #include -#include - namespace Mad { namespace Core { void ConnectionManager::refreshPollfds() { pollfds.clear(); pollfdMap.clear(); - + for(std::list::iterator listener = listeners.begin(); listener != listeners.end(); ++listener) { std::vector fds = (*listener)->getPollfds(); - + for(std::vector::iterator fd = fds.begin(); fd != fds.end(); ++fd) { pollfds.push_back(*fd); pollfdMap.insert(std::make_pair(fd->fd, &pollfds.back().revents)); } } - + for(std::list::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) { pollfds.push_back((*con)->getPollfd()); pollfdMap.insert(std::make_pair(pollfds.back().fd, &pollfds.back().revents)); } - + for(std::list::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) { pollfds.push_back((*con)->getPollfd()); pollfdMap.insert(std::make_pair(pollfds.back().fd, &pollfds.back().revents)); } } -void ConnectionManager::receiveHandler(Net::Connection *connection, const Net::Packet &packet) { - switch(packet.getType()) { - case Net::Packet::TYPE_UNKNOWN: - break; - case Net::Packet::TYPE_DEBUG: - std::cout << "Received debug packet." << std::endl; - std::cout << " Request ID: 0x" << std::hex << std::uppercase << packet.getRequestId() << std::dec << std::endl; - break; - case Net::Packet::TYPE_PING: - connection->send(Net::Packet(Net::Packet::TYPE_PONG, packet.getRequestId(), packet.getData(), packet.getLength())); - break; - case Net::Packet::TYPE_PONG: - // TODO: Pong! - break; - case Net::Packet::TYPE_DISCONNECT_REQ: - connection->send(Net::Packet(Net::Packet::TYPE_DISCONNECT_REP, packet.getRequestId())); - connection->disconnect(); - break; - case Net::Packet::TYPE_DISCONNECT_REP: - connection->disconnect(); - } -} - ConnectionManager::ConnectionManager() { try { // TODO: Get listener addresses from config @@ -83,72 +58,72 @@ ConnectionManager::ConnectionManager() { catch(Net::Exception &e) { // TODO: Log error } - + refreshPollfds(); } ConnectionManager::~ConnectionManager() { for(std::list::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) delete *con; - + for(std::list::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) delete *con; } void ConnectionManager::run() { // TODO: Logging - + for(std::list::iterator con = daemonConnections.begin(); con != daemonConnections.end();) { if((*con)->isConnected()) { std::map::iterator events = pollfdMap.find((*con)->getSocket()); - + if(events != pollfdMap.end()) (*con)->sendReceive(*events->second); else (*con)->sendReceive(); - + ++con; } else { - std::cout << "A daemon connection was dropped." << std::endl; + requestManager.unregisterConnection(*con); delete *con; daemonConnections.erase(con++); } } - + for(std::list::iterator con = clientConnections.begin(); con != clientConnections.end();) { if((*con)->isConnected()) { std::map::iterator events = pollfdMap.find((*con)->getSocket()); - + if(events != pollfdMap.end()) (*con)->sendReceive(*events->second); else (*con)->sendReceive(); - + ++con; } else { - std::cout << "A client connection was dropped." << std::endl; + requestManager.unregisterConnection(*con); delete *con; clientConnections.erase(con++); } } - + for(std::list::iterator listener = listeners.begin(); listener != listeners.end(); ++listener) { Net::ServerConnection *con; - + while((con = (*listener)->getConnection(pollfdMap)) != 0) { if(con->isDaemonConnection()) { daemonConnections.push_back(con); - con->signalReceive().connect(sigc::mem_fun(this, &ConnectionManager::receiveHandler)); + requestManager.registerConnection(con); } else { clientConnections.push_back(con); - con->signalReceive().connect(sigc::mem_fun(this, &ConnectionManager::receiveHandler)); + requestManager.registerConnection(con); } } } - + refreshPollfds(); } diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index 608ec27..c7fe669 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace Mad { @@ -41,27 +42,27 @@ class ConnectionManager { // Prevent shallow copy ConnectionManager(const ConnectionManager &o); ConnectionManager& operator=(const ConnectionManager &o); - + + Common::RequestManager requestManager; + std::list listeners; - + std::list daemonConnections; std::list clientConnections; - + std::vector pollfds; std::map pollfdMap; - + void refreshPollfds(); - - void receiveHandler(Net::Connection *connection, const Net::Packet &packet); - + public: ConnectionManager(); virtual ~ConnectionManager(); - + bool wait(int timeout) { return (poll(pollfds.data(), pollfds.size(), timeout) > 0); } - + void run(); }; -- cgit v1.2.3