From 776377bb21ee1cfe0bcdbc000f7c6fa0be227226 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 20 May 2009 01:08:16 +0200 Subject: Netzwerk-Code auf boost::asio umgestellt --- src/Server/ConnectionManager.cpp | 88 +++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 42 deletions(-) (limited to 'src/Server/ConnectionManager.cpp') diff --git a/src/Server/ConnectionManager.cpp b/src/Server/ConnectionManager.cpp index 6ef918a..bbaf673 100644 --- a/src/Server/ConnectionManager.cpp +++ b/src/Server/ConnectionManager.cpp @@ -33,8 +33,6 @@ #include "RequestHandlers/LogRequestHandler.h" #include "RequestHandlers/UserInfoRequestHandler.h" #include "RequestHandlers/UserListRequestHandler.h" -#include -#include #include #include @@ -46,49 +44,46 @@ namespace Server { ConnectionManager ConnectionManager::connectionManager; -bool ConnectionManager::Connection::send(const Net::Packet &packet) { +bool ConnectionManager::ServerConnection::send(const Net::Packet &packet) { return connection->send(packet); } -ConnectionManager::Connection::Connection(Net::ServerConnection *connection0) -: connection(connection0), type(connection0->isDaemonConnection() ? DAEMON : CLIENT), hostInfo(0) { - connection->signalReceive().connect(boost::bind(&Connection::receive, this, _1)); +ConnectionManager::ServerConnection::ServerConnection(boost::shared_ptr connection0) +: connection(connection0), type(UNKNOWN), hostInfo(0) { + connection->signalReceive().connect(boost::bind(&ServerConnection::receive, this, _1)); } -ConnectionManager::Connection::~Connection() { - delete connection; -} - -bool ConnectionManager::Connection::isConnected() const { +bool ConnectionManager::ServerConnection::isConnected() const { return connection->isConnected(); } -bool ConnectionManager::Connection::disconnect() { +bool ConnectionManager::ServerConnection::disconnect() { connection->disconnect(); return true; } -void* ConnectionManager::Connection::getCertificate(size_t *size) const { +/*void* ConnectionManager::ServerConnection::getCertificate(size_t *size) const { const gnutls_datum_t *cert = connection->getCertificate(); *size = cert->size; return cert->data; } -void* ConnectionManager::Connection::getPeerCertificate(size_t *size) const { +void* ConnectionManager::ServerConnection::getPeerCertificate(size_t *size) const { const gnutls_datum_t *cert = connection->getPeerCertificate(); *size = cert->size; return cert->data; -} +}*/ void ConnectionManager::updateState(Common::HostInfo *hostInfo, Common::HostInfo::State state) { hostInfo->setState(state); - for(std::set::iterator con = connections.begin(); con != connections.end(); ++con) { - if((*con)->getConnectionType() == Connection::CLIENT) - Common::RequestManager::get()->sendRequest(*con, boost::bind(&ConnectionManager::updateStateFinished, this, _1), hostInfo->getName(), state); + + for(std::set >::iterator con = connections.begin(); con != connections.end(); ++con) { + if((*con)->getConnectionType() == ServerConnection::CLIENT) + Common::RequestManager::get()->sendRequest(con->get(), boost::bind(&ConnectionManager::updateStateFinished, this, _1), hostInfo->getName(), state); } } @@ -98,7 +93,7 @@ bool ConnectionManager::handleConfigEntry(const Common::ConfigEntry &entry, bool if(entry[0].getKey().matches("Listen") && entry[1].empty()) { try { - listenerAddresses.push_back(Net::IPAddress(entry[0][0])); + listenerAddresses.push_back(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(entry[0][0]), 6666)); } catch(Net::Exception &e) { // TODO Log error @@ -147,8 +142,8 @@ bool ConnectionManager::handleConfigEntry(const Common::ConfigEntry &entry, bool void ConnectionManager::configFinished() { if(listenerAddresses.empty()) { try { - Net::Listener *listener = new Net::Listener(x509CertFile, x509KeyFile); - listener->signalNewConnection().connect(boost::bind(&ConnectionManager::newConnectionHandler, this, _1)); + boost::shared_ptr listener(new Net::Listener(x509CertFile, x509KeyFile)); + listener->signalNewConnection().connect(boost::bind(&ConnectionManager::handleNewConnection, this, _1)); listeners.push_back(listener); } catch(Net::Exception &e) { @@ -156,10 +151,10 @@ void ConnectionManager::configFinished() { } } else { - for(std::vector::const_iterator address = listenerAddresses.begin(); address != listenerAddresses.end(); ++address) { + for(std::vector::const_iterator address = listenerAddresses.begin(); address != listenerAddresses.end(); ++address) { try { - Net::Listener *listener = new Net::Listener(x509CertFile, x509KeyFile, *address); - listener->signalNewConnection().connect(boost::bind(&ConnectionManager::newConnectionHandler, this, _1)); + boost::shared_ptr listener(new Net::Listener(x509CertFile, x509KeyFile, *address)); + listener->signalNewConnection().connect(boost::bind(&ConnectionManager::handleNewConnection, this, _1)); listeners.push_back(listener); } catch(Net::Exception &e) { @@ -169,28 +164,27 @@ void ConnectionManager::configFinished() { } } -void ConnectionManager::newConnectionHandler(Net::ServerConnection *con) { - Connection *connection = new Connection(con); - con->signalDisconnected().connect(boost::bind(&ConnectionManager::disconnectHandler, this, connection)); +void ConnectionManager::handleNewConnection(boost::shared_ptr con) { + boost::shared_ptr connection(new ServerConnection(con)); + con->signalDisconnected().connect(boost::bind(&ConnectionManager::handleDisconnect, this, connection)); connections.insert(connection); - Common::RequestManager::get()->registerConnection(connection); + Common::RequestManager::get()->registerConnection(connection.get()); } -void ConnectionManager::disconnectHandler(Connection *con) { - if(con->isIdentified()) +void ConnectionManager::handleDisconnect(boost::shared_ptr con) { + if(con->getHostInfo()) updateState(con->getHostInfo(), Common::HostInfo::INACTIVE); connections.erase(con); - Common::RequestManager::get()->unregisterConnection(con); - delete con; + Common::RequestManager::get()->unregisterConnection(con.get()); } void ConnectionManager::doInit() { Common::RequestManager::get()->setServer(true); - Common::RequestManager::get()->registerPacketType("AuthGSSAPI"); + //Common::RequestManager::get()->registerPacketType("AuthGSSAPI"); Common::RequestManager::get()->registerPacketType("DaemonCommand"); Common::RequestManager::get()->registerPacketType("DaemonFSInfo"); Common::RequestManager::get()->registerPacketType("FSInfo"); @@ -204,11 +198,9 @@ void ConnectionManager::doInit() { } void ConnectionManager::doDeinit() { - for(std::set::iterator con = connections.begin(); con != connections.end(); ++con) - delete *con; + connections.clear(); - - Common::RequestManager::get()->unregisterPacketType("AuthGSSAPI"); + //Common::RequestManager::get()->unregisterPacketType("AuthGSSAPI"); Common::RequestManager::get()->unregisterPacketType("DaemonCommand"); Common::RequestManager::get()->unregisterPacketType("DaemonFSInfo"); Common::RequestManager::get()->unregisterPacketType("FSInfo"); @@ -221,7 +213,7 @@ void ConnectionManager::doDeinit() { Common::RequestManager::get()->unregisterPacketType("Log"); } -Common::Connection* ConnectionManager::getDaemonConnection(const std::string &name) const throw (Net::Exception&) { +boost::shared_ptr ConnectionManager::getDaemonConnection(const std::string &name) const throw (Net::Exception&) { const Common::HostInfo *hostInfo; try { @@ -232,7 +224,7 @@ Common::Connection* ConnectionManager::getDaemonConnection(const std::string &na } if(hostInfo->getState() != Common::HostInfo::INACTIVE) { - for(std::set::const_iterator it = connections.begin(); it != connections.end(); ++it) { + for(std::set >::const_iterator it = connections.begin(); it != connections.end(); ++it) { if((*it)->getHostInfo() == hostInfo) { return *it; } @@ -243,7 +235,7 @@ Common::Connection* ConnectionManager::getDaemonConnection(const std::string &na } std::string ConnectionManager::getDaemonName(const Common::Connection *con) const throw (Net::Exception&) { - const Connection *connection = dynamic_cast(con); + const ServerConnection *connection = dynamic_cast(con); if(connection) { if(connection->isIdentified()) { @@ -257,9 +249,9 @@ std::string ConnectionManager::getDaemonName(const Common::Connection *con) cons void ConnectionManager::identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Net::Exception&) { // TODO Logging - Connection *connection = dynamic_cast(con); + ServerConnection *connection = dynamic_cast(con); - if(!connection || (connection->getConnectionType() != Connection::DAEMON)) + if(!connection) throw Net::Exception(Net::Exception::INVALID_ACTION); if(connection->isIdentified()) @@ -284,6 +276,18 @@ void ConnectionManager::identifyDaemonConnection(Common::Connection *con, const Common::Logger::logf("Identified as '%s'.", name.c_str()); } +void ConnectionManager::identifyClientConnection(Common::Connection *con) throw (Net::Exception&) { + ServerConnection *connection = dynamic_cast(con); + + if(!connection) + throw Net::Exception(Net::Exception::INVALID_ACTION); + + if(connection->isIdentified()) + throw Net::Exception(Net::Exception::ALREADY_IDENTIFIED); + + connection->identify(); +} + std::vector ConnectionManager::getDaemonList() const { std::vector ret; -- cgit v1.2.3