diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-05-20 01:08:16 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-05-20 01:08:16 +0200 |
commit | 776377bb21ee1cfe0bcdbc000f7c6fa0be227226 (patch) | |
tree | a60e8bdd92678dece5fbb96e75535eba2b61b7da /src/Server | |
parent | badc0da3b74d99c90b7b28180d08cd6d08830254 (diff) | |
download | mad-776377bb21ee1cfe0bcdbc000f7c6fa0be227226.tar mad-776377bb21ee1cfe0bcdbc000f7c6fa0be227226.zip |
Netzwerk-Code auf boost::asio umgestellt
Diffstat (limited to 'src/Server')
-rw-r--r-- | src/Server/ConnectionManager.cpp | 88 | ||||
-rw-r--r-- | src/Server/ConnectionManager.h | 54 | ||||
-rw-r--r-- | src/Server/RequestHandlers/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp | 4 | ||||
-rw-r--r-- | src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp | 4 | ||||
-rw-r--r-- | src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp | 4 | ||||
-rw-r--r-- | src/Server/RequestHandlers/IdentifyRequestHandler.cpp | 5 |
7 files changed, 85 insertions, 76 deletions
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 <Net/FdManager.h> -#include <Net/ServerConnection.h> #include <Net/Packet.h> #include <Net/Listener.h> @@ -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<Net::Connection> 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<Connection*>::iterator con = connections.begin(); con != connections.end(); ++con) { - if((*con)->getConnectionType() == Connection::CLIENT) - Common::RequestManager::get()->sendRequest<Requests::DaemonStateUpdateRequest>(*con, boost::bind(&ConnectionManager::updateStateFinished, this, _1), hostInfo->getName(), state); + + for(std::set<boost::shared_ptr<ServerConnection> >::iterator con = connections.begin(); con != connections.end(); ++con) { + if((*con)->getConnectionType() == ServerConnection::CLIENT) + Common::RequestManager::get()->sendRequest<Requests::DaemonStateUpdateRequest>(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<Net::Listener> 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<Net::IPAddress>::const_iterator address = listenerAddresses.begin(); address != listenerAddresses.end(); ++address) { + for(std::vector<boost::asio::ip::tcp::endpoint>::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<Net::Listener> 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<Net::Connection> con) { + boost::shared_ptr<ServerConnection> 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<ServerConnection> 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<RequestHandlers::GSSAPIAuthRequestHandler>("AuthGSSAPI"); + //Common::RequestManager::get()->registerPacketType<RequestHandlers::GSSAPIAuthRequestHandler>("AuthGSSAPI"); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>("DaemonCommand"); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonFSInfoRequestHandler>("DaemonFSInfo"); Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::FSInfoRequestHandler>("FSInfo"); @@ -204,11 +198,9 @@ void ConnectionManager::doInit() { } void ConnectionManager::doDeinit() { - for(std::set<Connection*>::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<Common::Connection> 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<Connection*>::const_iterator it = connections.begin(); it != connections.end(); ++it) { + for(std::set<boost::shared_ptr<ServerConnection> >::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<const Connection*>(con); + const ServerConnection *connection = dynamic_cast<const ServerConnection*>(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<Connection*>(con); + ServerConnection *connection = dynamic_cast<ServerConnection*>(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<ServerConnection*>(con); + + if(!connection) + throw Net::Exception(Net::Exception::INVALID_ACTION); + + if(connection->isIdentified()) + throw Net::Exception(Net::Exception::ALREADY_IDENTIFIED); + + connection->identify(); +} + std::vector<Common::HostInfo> ConnectionManager::getDaemonList() const { std::vector<Common::HostInfo> 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 <list> -#include <vector> -#include <map> -#include <set> - #include <Common/Configurable.h> #include <Common/HostInfo.h> #include <Common/Initializable.h> #include <Common/RequestManager.h> -#include <Net/IPAddress.h> +#include <list> +#include <vector> +#include <map> +#include <set> + +#include <boost/asio.hpp> 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<Net::Connection> 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<Net::Connection> 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<Net::IPAddress> listenerAddresses; - std::list<Net::Listener*> listeners; + std::vector<boost::asio::ip::tcp::endpoint> listenerAddresses; + std::list<boost::shared_ptr<Net::Listener> > listeners; - std::set<Connection*> connections; + std::set<boost::shared_ptr<ServerConnection> > connections; std::map<std::string,Common::HostInfo> 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<Net::Connection> con); + void handleDisconnect(boost::shared_ptr<ServerConnection> 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<Common::Connection> 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<Common::HostInfo> 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<Requests::CommandRequest>(daemonCon, + boost::shared_ptr<Common::Connection> daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); + Common::RequestManager::get()->sendRequest<Requests::CommandRequest>(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<Common::Requests::FSInfoRequest>(daemonCon, + boost::shared_ptr<Common::Connection> daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); + Common::RequestManager::get()->sendRequest<Common::Requests::FSInfoRequest>(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<Common::Requests::StatusRequest>(daemonCon, + boost::shared_ptr<Common::Connection> daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); + Common::RequestManager::get()->sendRequest<Common::Requests::StatusRequest>(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"); |