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 +++++++++++----------- src/Server/ConnectionManager.h | 54 ++++++------- src/Server/RequestHandlers/CMakeLists.txt | 2 +- .../DaemonCommandRequestHandler.cpp | 4 +- .../RequestHandlers/DaemonFSInfoRequestHandler.cpp | 4 +- .../RequestHandlers/DaemonStatusRequestHandler.cpp | 4 +- .../RequestHandlers/IdentifyRequestHandler.cpp | 5 +- 7 files changed, 85 insertions(+), 76 deletions(-) (limited to 'src/Server') 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; diff --git a/src/Server/ConnectionManager.h b/src/Server/ConnectionManager.h index 7d97edc..710665d 100644 --- a/src/Server/ConnectionManager.h +++ b/src/Server/ConnectionManager.h @@ -20,38 +20,38 @@ #ifndef MAD_SERVER_CONNECTIONMANAGER_H_ #define MAD_SERVER_CONNECTIONMANAGER_H_ -#include -#include -#include -#include - #include #include #include #include -#include +#include +#include +#include +#include + +#include namespace Mad { namespace Net { +class Connection; class Listener; -class ServerConnection; class Packet; } namespace Server { -class ConnectionManager : public Common::Configurable, public Common::Initializable { +class ConnectionManager : public Common::Configurable, public Common::Initializable, boost::noncopyable { private: - class Connection : public Common::Connection { + class ServerConnection : public Common::Connection { public: enum ConnectionType { - DAEMON, CLIENT + UNKNOWN = 0, DAEMON, CLIENT }; private: - Net::ServerConnection *connection; + boost::shared_ptr connection; ConnectionType type; Common::HostInfo *hostInfo; @@ -59,14 +59,13 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa virtual bool send(const Net::Packet &packet); public: - Connection(Net::ServerConnection *connection0); - virtual ~Connection(); + ServerConnection(boost::shared_ptr connection0); bool isConnected() const; virtual bool disconnect(); - virtual void* getCertificate(size_t *size) const; - virtual void* getPeerCertificate(size_t *size) const; + //virtual void* getCertificate(size_t *size) const; + //virtual void* getPeerCertificate(size_t *size) const; ConnectionType getConnectionType() const { return type; @@ -77,10 +76,15 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa } bool isIdentified() const { - return hostInfo; + return (type != UNKNOWN); + } + + void identify() { + type = CLIENT; } void identify(Common::HostInfo *info) { + type = DAEMON; hostInfo = info; } }; @@ -89,17 +93,13 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa std::string x509TrustFile, x509CrlFile, x509CertFile, x509KeyFile; - std::vector listenerAddresses; - std::list listeners; + std::vector listenerAddresses; + std::list > listeners; - std::set connections; + std::set > connections; std::map daemonInfo; - // Prevent shallow copy - ConnectionManager(const ConnectionManager &o); - ConnectionManager& operator=(const ConnectionManager &o); - void updateState(Common::HostInfo *hostInfo, Common::HostInfo::State state); void updateStateFinished(const Common::Request&) { // TODO Error handling (updateStateFinished) @@ -107,8 +107,8 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa ConnectionManager() {} - void newConnectionHandler(Net::ServerConnection *con); - void disconnectHandler(Connection *con); + void handleNewConnection(boost::shared_ptr con); + void handleDisconnect(boost::shared_ptr con); protected: virtual bool handleConfigEntry(const Common::ConfigEntry &entry, bool handled); @@ -122,10 +122,12 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa return &connectionManager; } - Common::Connection* getDaemonConnection(const std::string &name) const throw (Net::Exception&); + boost::shared_ptr getDaemonConnection(const std::string &name) const throw (Net::Exception&); std::string getDaemonName(const Common::Connection *con) const throw (Net::Exception&); void identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Net::Exception&); + void identifyClientConnection(Common::Connection *con) throw (Net::Exception&); + std::vector getDaemonList() const; }; diff --git a/src/Server/RequestHandlers/CMakeLists.txt b/src/Server/RequestHandlers/CMakeLists.txt index 2c6305d..e340a1e 100644 --- a/src/Server/RequestHandlers/CMakeLists.txt +++ b/src/Server/RequestHandlers/CMakeLists.txt @@ -3,7 +3,7 @@ include_directories(${INCLUDES}) add_library(ServerRequestHandlers DaemonCommandRequestHandler.cpp DaemonFSInfoRequestHandler.cpp DaemonListRequestHandler.cpp DaemonStatusRequestHandler.cpp - GSSAPIAuthRequestHandler.cpp IdentifyRequestHandler.cpp + IdentifyRequestHandler.cpp LogRequestHandler.cpp UserInfoRequestHandler.cpp UserListRequestHandler.cpp ) diff --git a/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp b/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp index 17a7e5d..e4b511e 100644 --- a/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp +++ b/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp @@ -46,8 +46,8 @@ void DaemonCommandRequestHandler::handlePacket(const Common::XmlPacket &packet) std::string command = packet["command"]; try { - Common::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); - Common::RequestManager::get()->sendRequest(daemonCon, + boost::shared_ptr daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); + Common::RequestManager::get()->sendRequest(daemonCon.get(), boost::bind(&DaemonCommandRequestHandler::requestFinished, this, _1), command == "reboot"); } catch(Net::Exception &e) { diff --git a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp b/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp index df57a94..11a3f09 100644 --- a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp +++ b/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp @@ -44,8 +44,8 @@ void DaemonFSInfoRequestHandler::handlePacket(const Common::XmlPacket &packet) { // TODO Require authentication try { - Common::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); - Common::RequestManager::get()->sendRequest(daemonCon, + boost::shared_ptr daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); + Common::RequestManager::get()->sendRequest(daemonCon.get(), boost::bind(&DaemonFSInfoRequestHandler::requestFinished, this, _1)); } catch(Net::Exception &e) { diff --git a/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp b/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp index 3d99c57..c1e2fcd 100644 --- a/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp +++ b/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp @@ -46,8 +46,8 @@ void DaemonStatusRequestHandler::handlePacket(const Common::XmlPacket &packet) { std::string daemonName = packet["daemonName"]; try { - Common::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); - Common::RequestManager::get()->sendRequest(daemonCon, + boost::shared_ptr daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); + Common::RequestManager::get()->sendRequest(daemonCon.get(), boost::bind(&DaemonStatusRequestHandler::requestFinished, this, _1)); } catch(Net::Exception &e) { diff --git a/src/Server/RequestHandlers/IdentifyRequestHandler.cpp b/src/Server/RequestHandlers/IdentifyRequestHandler.cpp index f69b3f5..b47b505 100644 --- a/src/Server/RequestHandlers/IdentifyRequestHandler.cpp +++ b/src/Server/RequestHandlers/IdentifyRequestHandler.cpp @@ -42,7 +42,10 @@ void IdentifyRequestHandler::handlePacket(const Common::XmlPacket &packet) { // TODO Require authentication try { - ConnectionManager::get()->identifyDaemonConnection(getConnection(), packet["hostname"]); + if(packet["hostname"].isEmpty()) + ConnectionManager::get()->identifyClientConnection(getConnection()); + else + ConnectionManager::get()->identifyDaemonConnection(getConnection(), packet["hostname"]); Common::XmlPacket ret; ret.setType("OK"); -- cgit v1.2.3