From 7147be63a03f1e133f83bc057be98cf9f2631733 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Sat, 18 Jun 2011 18:24:09 +0200 Subject: Added ConnectionServerFactory. License: This patch is BSD-licensed, see http://www.opensource.org/licenses/bsd-license.php diff --git a/Swiften/Network/BoostConnectionServer.cpp b/Swiften/Network/BoostConnectionServer.cpp index 4c6403c..27a1008 100644 --- a/Swiften/Network/BoostConnectionServer.cpp +++ b/Swiften/Network/BoostConnectionServer.cpp @@ -16,13 +16,22 @@ namespace Swift { BoostConnectionServer::BoostConnectionServer(int port, boost::shared_ptr ioService, EventLoop* eventLoop) : port_(port), ioService_(ioService), eventLoop(eventLoop), acceptor_(NULL) { } +BoostConnectionServer::BoostConnectionServer(const HostAddress &address, int port, boost::shared_ptr ioService, EventLoop* eventLoop) : address_(address), port_(port), ioService_(ioService), eventLoop(eventLoop), acceptor_(NULL) { +} void BoostConnectionServer::start() { try { assert(!acceptor_); - acceptor_ = new boost::asio::ip::tcp::acceptor( + if (address_.isValid()) { + acceptor_ = new boost::asio::ip::tcp::acceptor( + *ioService_, + boost::asio::ip::tcp::endpoint(address_.getRawAddress(), port_)); + } + else { + acceptor_ = new boost::asio::ip::tcp::acceptor( *ioService_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port_)); + } acceptNextConnection(); } catch (const boost::system::system_error& e) { diff --git a/Swiften/Network/BoostConnectionServer.h b/Swiften/Network/BoostConnectionServer.h index a45e598..aba9e3e 100644 --- a/Swiften/Network/BoostConnectionServer.h +++ b/Swiften/Network/BoostConnectionServer.h @@ -29,8 +29,12 @@ namespace Swift { return ref(new BoostConnectionServer(port, ioService, eventLoop)); } - void start(); - void stop(); + static ref create(const HostAddress &address, int port, boost::shared_ptr ioService, EventLoop* eventLoop) { + return ref(new BoostConnectionServer(address, port, ioService, eventLoop)); + } + + virtual void start(); + virtual void stop(); virtual HostAddressPort getAddressPort() const; @@ -38,12 +42,14 @@ namespace Swift { private: BoostConnectionServer(int port, boost::shared_ptr ioService, EventLoop* eventLoop); + BoostConnectionServer(const HostAddress &address, int port, boost::shared_ptr ioService, EventLoop* eventLoop); void stop(boost::optional e); void acceptNextConnection(); void handleAccept(boost::shared_ptr newConnection, const boost::system::error_code& error); private: + HostAddress address_; int port_; boost::shared_ptr ioService_; EventLoop* eventLoop; diff --git a/Swiften/Network/BoostConnectionServerFactory.cpp b/Swiften/Network/BoostConnectionServerFactory.cpp new file mode 100644 index 0000000..04c614e --- /dev/null +++ b/Swiften/Network/BoostConnectionServerFactory.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include +#include + +namespace Swift { + +BoostConnectionServerFactory::BoostConnectionServerFactory(boost::shared_ptr ioService, EventLoop* eventLoop) : ioService(ioService), eventLoop(eventLoop) { +} + +boost::shared_ptr BoostConnectionServerFactory::createConnectionServer(int port) { + return BoostConnectionServer::create(port, ioService, eventLoop); +} + +boost::shared_ptr BoostConnectionServerFactory::createConnectionServer(const Swift::HostAddress &hostAddress, int port) { + return BoostConnectionServer::create(hostAddress, port, ioService, eventLoop); +} + +} diff --git a/Swiften/Network/BoostConnectionServerFactory.h b/Swiften/Network/BoostConnectionServerFactory.h new file mode 100644 index 0000000..9132b5c --- /dev/null +++ b/Swiften/Network/BoostConnectionServerFactory.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include + +#include +#include + +namespace Swift { + class ConnectionServer; + + class BoostConnectionServerFactory : public ConnectionServerFactory { + public: + BoostConnectionServerFactory(boost::shared_ptr, EventLoop* eventLoop); + + virtual boost::shared_ptr createConnectionServer(int port); + + virtual boost::shared_ptr createConnectionServer(const Swift::HostAddress &hostAddress, int port); + + private: + boost::shared_ptr ioService; + EventLoop* eventLoop; + }; +} diff --git a/Swiften/Network/BoostNetworkFactories.cpp b/Swiften/Network/BoostNetworkFactories.cpp index a8e3d0a..6d1cc71 100644 --- a/Swiften/Network/BoostNetworkFactories.cpp +++ b/Swiften/Network/BoostNetworkFactories.cpp @@ -8,6 +8,7 @@ #include "Swiften/Network/BoostTimerFactory.h" #include "Swiften/Network/BoostConnectionFactory.h" #include +#include namespace Swift { @@ -15,12 +16,14 @@ BoostNetworkFactories::BoostNetworkFactories(EventLoop* eventLoop) { timerFactory = new BoostTimerFactory(ioServiceThread.getIOService(), eventLoop); connectionFactory = new BoostConnectionFactory(ioServiceThread.getIOService(), eventLoop); domainNameResolver = new PlatformDomainNameResolver(eventLoop); + connectionServerFactory = new BoostConnectionServerFactory(ioServiceThread.getIOService(), eventLoop); } BoostNetworkFactories::~BoostNetworkFactories() { delete domainNameResolver; delete connectionFactory; delete timerFactory; + delete connectionServerFactory; } } diff --git a/Swiften/Network/BoostNetworkFactories.h b/Swiften/Network/BoostNetworkFactories.h index cb7e359..d9fb566 100644 --- a/Swiften/Network/BoostNetworkFactories.h +++ b/Swiften/Network/BoostNetworkFactories.h @@ -33,10 +33,15 @@ namespace Swift { return domainNameResolver; } + ConnectionServerFactory* getConnectionServerFactory() const { + return connectionServerFactory; + } + private: BoostIOServiceThread ioServiceThread; TimerFactory* timerFactory; ConnectionFactory* connectionFactory; DomainNameResolver* domainNameResolver; + ConnectionServerFactory *connectionServerFactory; }; } diff --git a/Swiften/Network/ConnectionServer.h b/Swiften/Network/ConnectionServer.h index 8129372..b90f73d 100644 --- a/Swiften/Network/ConnectionServer.h +++ b/Swiften/Network/ConnectionServer.h @@ -19,6 +19,10 @@ namespace Swift { virtual HostAddressPort getAddressPort() const = 0; + virtual void start() = 0; + + virtual void stop() = 0; + boost::signal)> onNewConnection; }; } diff --git a/Swiften/Network/ConnectionServerFactory.cpp b/Swiften/Network/ConnectionServerFactory.cpp new file mode 100644 index 0000000..3c59001 --- /dev/null +++ b/Swiften/Network/ConnectionServerFactory.cpp @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include + +namespace Swift { + +ConnectionServerFactory::~ConnectionServerFactory() { +} + +} diff --git a/Swiften/Network/ConnectionServerFactory.h b/Swiften/Network/ConnectionServerFactory.h new file mode 100644 index 0000000..c516007 --- /dev/null +++ b/Swiften/Network/ConnectionServerFactory.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include + +namespace Swift { + class ConnectionServer; + class HostAddress; + + class ConnectionServerFactory { + public: + virtual ~ConnectionServerFactory(); + + virtual boost::shared_ptr createConnectionServer(int port) = 0; + + virtual boost::shared_ptr createConnectionServer(const Swift::HostAddress &hostAddress, int port) = 0; + }; +} diff --git a/Swiften/Network/NetworkFactories.h b/Swiften/Network/NetworkFactories.h index b4400bd..d0d2299 100644 --- a/Swiften/Network/NetworkFactories.h +++ b/Swiften/Network/NetworkFactories.h @@ -10,6 +10,7 @@ namespace Swift { class TimerFactory; class ConnectionFactory; class DomainNameResolver; + class ConnectionServerFactory; /** * An interface collecting network factories. @@ -21,5 +22,6 @@ namespace Swift { virtual TimerFactory* getTimerFactory() const = 0; virtual ConnectionFactory* getConnectionFactory() const = 0; virtual DomainNameResolver* getDomainNameResolver() const = 0; + virtual ConnectionServerFactory* getConnectionServerFactory() const = 0; }; } diff --git a/Swiften/Network/SConscript b/Swiften/Network/SConscript index fa186fa..d5cc185 100644 --- a/Swiften/Network/SConscript +++ b/Swiften/Network/SConscript @@ -9,9 +9,11 @@ sourceList = [ "BoostConnection.cpp", "BoostConnectionFactory.cpp", "BoostConnectionServer.cpp", + "BoostConnectionServerFactory.cpp", "BoostIOServiceThread.cpp", "ConnectionFactory.cpp", "ConnectionServer.cpp", + "ConnectionServerFactory.cpp", "Connector.cpp", "TimerFactory.cpp", "DummyTimerFactory.cpp", -- cgit v0.10.2-6-g49f6 From 7d0e9c3d963ffcb3f7d6d313dfa55466beb742b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sat, 18 Jun 2011 18:26:26 +0200 Subject: Small tweaks to the previous patch. diff --git a/Swiften/Network/BoostNetworkFactories.cpp b/Swiften/Network/BoostNetworkFactories.cpp index 6d1cc71..1d9479a 100644 --- a/Swiften/Network/BoostNetworkFactories.cpp +++ b/Swiften/Network/BoostNetworkFactories.cpp @@ -20,10 +20,10 @@ BoostNetworkFactories::BoostNetworkFactories(EventLoop* eventLoop) { } BoostNetworkFactories::~BoostNetworkFactories() { + delete connectionServerFactory; delete domainNameResolver; delete connectionFactory; delete timerFactory; - delete connectionServerFactory; } } diff --git a/Swiften/Network/BoostNetworkFactories.h b/Swiften/Network/BoostNetworkFactories.h index d9fb566..e4d3128 100644 --- a/Swiften/Network/BoostNetworkFactories.h +++ b/Swiften/Network/BoostNetworkFactories.h @@ -42,6 +42,6 @@ namespace Swift { TimerFactory* timerFactory; ConnectionFactory* connectionFactory; DomainNameResolver* domainNameResolver; - ConnectionServerFactory *connectionServerFactory; + ConnectionServerFactory* connectionServerFactory; }; } diff --git a/Swiften/Network/ConnectionServerFactory.h b/Swiften/Network/ConnectionServerFactory.h index c516007..df5f912 100644 --- a/Swiften/Network/ConnectionServerFactory.h +++ b/Swiften/Network/ConnectionServerFactory.h @@ -18,6 +18,6 @@ namespace Swift { virtual boost::shared_ptr createConnectionServer(int port) = 0; - virtual boost::shared_ptr createConnectionServer(const Swift::HostAddress &hostAddress, int port) = 0; + virtual boost::shared_ptr createConnectionServer(const Swift::HostAddress& hostAddress, int port) = 0; }; } diff --git a/Swiften/Network/HostAddress.cpp b/Swiften/Network/HostAddress.cpp index 7acd407..2ca1414 100644 --- a/Swiften/Network/HostAddress.cpp +++ b/Swiften/Network/HostAddress.cpp @@ -57,4 +57,8 @@ bool HostAddress::isValid() const { return !(address_.is_v4() && address_.to_v4().to_ulong() == 0); } +boost::asio::ip::address HostAddress::getRawAddress() const { + return address_; +} + } diff --git a/Swiften/Network/HostAddress.h b/Swiften/Network/HostAddress.h index 34ccd24..4b05c32 100644 --- a/Swiften/Network/HostAddress.h +++ b/Swiften/Network/HostAddress.h @@ -21,6 +21,7 @@ namespace Swift { HostAddress(const boost::asio::ip::address& address); std::string toString() const; + boost::asio::ip::address getRawAddress() const; bool operator==(const HostAddress& o) const { return address_ == o.address_; -- cgit v0.10.2-6-g49f6