diff options
Diffstat (limited to 'src/Server/ConnectionManager.cpp')
-rw-r--r-- | src/Server/ConnectionManager.cpp | 76 |
1 files changed, 33 insertions, 43 deletions
diff --git a/src/Server/ConnectionManager.cpp b/src/Server/ConnectionManager.cpp index 9235700..99f5680 100644 --- a/src/Server/ConnectionManager.cpp +++ b/src/Server/ConnectionManager.cpp @@ -41,17 +41,19 @@ #include <unistd.h> #include <algorithm> +#include <sigc++/bind.h> + namespace Mad { namespace Server { ConnectionManager ConnectionManager::connectionManager; -void ConnectionManager::Connection::send(const Net::Packet &packet) { - connection->send(packet); +bool ConnectionManager::Connection::send(const Net::Packet &packet) { + return connection->send(packet); } -ConnectionManager::Connection::Connection(Net::ServerConnection *connection0, ConnectionType type0) -: connection(connection0), type(type0), hostInfo(0) { +ConnectionManager::Connection::Connection(Net::ServerConnection *connection0) +: connection(connection0), type(connection0->isDaemonConnection() ? DAEMON : CLIENT), hostInfo(0) { connection->signalReceive().connect(sigc::mem_fun(this, &Connection::receive)); } @@ -86,7 +88,7 @@ void* ConnectionManager::Connection::getPeerCertificate(size_t *size) const { void ConnectionManager::updateState(Common::HostInfo *hostInfo, Common::HostInfo::State state) { hostInfo->setState(state); - for(std::list<Connection*>::iterator con = connections.begin(); con != connections.end(); ++con) { + for(std::set<Connection*>::iterator con = connections.begin(); con != connections.end(); ++con) { if((*con)->getConnectionType() == Connection::CLIENT) Common::RequestManager::get()->sendRequest<Requests::DaemonStateUpdateRequest>(*con, Common::Request::slot_type(), hostInfo->getName(), state); } @@ -147,7 +149,9 @@ bool ConnectionManager::handleConfigEntry(const Common::ConfigEntry &entry, bool void ConnectionManager::configFinished() { if(listenerAddresses.empty()) { try { - listeners.push_back(new Net::Listener(x509CertFile, x509KeyFile)); + Net::Listener *listener = new Net::Listener(x509CertFile, x509KeyFile); + listener->signalNewConnection().connect(sigc::mem_fun(this, &ConnectionManager::newConnectionHandler)); + listeners.push_back(listener); } catch(Net::Exception &e) { // TODO Log error @@ -156,7 +160,9 @@ void ConnectionManager::configFinished() { else { for(std::vector<Net::IPAddress>::const_iterator address = listenerAddresses.begin(); address != listenerAddresses.end(); ++address) { try { - listeners.push_back(new Net::Listener(x509CertFile, x509KeyFile, *address)); + Net::Listener *listener = new Net::Listener(x509CertFile, x509KeyFile, *address); + listener->signalNewConnection().connect(sigc::mem_fun(this, &ConnectionManager::newConnectionHandler)); + listeners.push_back(listener); } catch(Net::Exception &e) { // TODO Log error @@ -165,11 +171,27 @@ void ConnectionManager::configFinished() { } } +void ConnectionManager::newConnectionHandler(Net::ServerConnection *con) { + Connection *connection = new Connection(con); + con->signalDisconnected().connect(sigc::bind(sigc::mem_fun(this, &ConnectionManager::disconnectHandler), connection)); + connections.insert(connection); + + Common::RequestManager::get()->registerConnection(connection); +} + +void ConnectionManager::disconnectHandler(Connection *con) { + if(con->isIdentified()) + updateState(con->getHostInfo(), Common::HostInfo::INACTIVE); + + connections.erase(con); + + Common::RequestManager::get()->unregisterConnection(con); + delete con; +} + void ConnectionManager::doInit() { Common::RequestManager::get()->setServer(true); - Net::Connection::init(); - Common::RequestManager::get()->registerPacketType<RequestHandlers::GSSAPIAuthRequestHandler>("AuthGSSAPI"); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>("DaemonCommand"); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonFSInfoRequestHandler>("DaemonFSInfo"); @@ -184,7 +206,7 @@ void ConnectionManager::doInit() { } void ConnectionManager::doDeinit() { - for(std::list<Connection*>::iterator con = connections.begin(); con != connections.end(); ++con) + for(std::set<Connection*>::iterator con = connections.begin(); con != connections.end(); ++con) delete *con; @@ -199,38 +221,6 @@ void ConnectionManager::doDeinit() { Common::RequestManager::get()->unregisterPacketType("GetUserInfo"); Common::RequestManager::get()->unregisterPacketType("ListUsers"); Common::RequestManager::get()->unregisterPacketType("Log"); - - Net::Connection::deinit(); -} - -void ConnectionManager::run() { - // TODO Logging - - Net::FdManager::get()->run(); - - for(std::list<Connection*>::iterator con = connections.begin(); con != connections.end();) { - if(!(*con)->isConnected()) { - if((*con)->isIdentified()) - updateState((*con)->getHostInfo(), Common::HostInfo::INACTIVE); - - Common::RequestManager::get()->unregisterConnection(*con); - delete *con; - connections.erase(con++); - } - else - ++con; - } - - for(std::list<Net::Listener*>::iterator listener = listeners.begin(); listener != listeners.end(); ++listener) { - Net::ServerConnection *con; - - while((con = (*listener)->getConnection()) != 0) { - Connection *connection = new Connection(con, - con->isDaemonConnection() ? Connection::DAEMON : Connection::CLIENT); - connections.push_back(connection); - Common::RequestManager::get()->registerConnection(connection); - } - } } Common::Connection* ConnectionManager::getDaemonConnection(const std::string &name) const throw (Net::Exception&) { @@ -244,7 +234,7 @@ Common::Connection* ConnectionManager::getDaemonConnection(const std::string &na } if(hostInfo->getState() != Common::HostInfo::INACTIVE) { - for(std::list<Connection*>::const_iterator it = connections.begin(); it != connections.end(); ++it) { + for(std::set<Connection*>::const_iterator it = connections.begin(); it != connections.end(); ++it) { if((*it)->getHostInfo() == hostInfo) { return *it; } |