/* * ConnectionManager.cpp * * 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 . */ #include "ConnectionManager.h" #include "ConfigManager.h" #include "RequestHandler/CertificateRequestHandler.h" #include #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)); } } ConnectionManager::ConnectionManager(const ConfigManager& configManager) : requestManager(true) { requestManager.registerPacketType(Net::Packet::TYPE_CERT_REQ); const std::vector &listenerAddresses = configManager.getListenerAddresses(); if(listenerAddresses.empty()) { try { listeners.push_back(new Net::Listener(configManager.getX509CertFile(), configManager.getX509KeyFile())); } catch(Net::Exception &e) { // TODO Log error } } else { for(std::vector::const_iterator address = listenerAddresses.begin(); address != listenerAddresses.end(); ++address) { try { listeners.push_back(new Net::Listener(configManager.getX509CertFile(), configManager.getX509KeyFile(), *address)); } 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::handleConnections(std::list& connections) { for(std::list::iterator con = connections.begin(); con != connections.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 { requestManager.unregisterConnection(*con); delete *con; connections.erase(con++); } } } void ConnectionManager::run() { // TODO Logging handleConnections(daemonConnections); handleConnections(clientConnections); for(std::list::iterator listener = listeners.begin(); listener != listeners.end(); ++listener) { Net::ServerConnection *con; while((con = (*listener)->getConnection(pollfdMap)) != 0) { (con->isDaemonConnection() ? daemonConnections : clientConnections).push_back(con); requestManager.registerConnection(con); } } refreshPollfds(); } } }