From 7147be63a03f1e133f83bc057be98cf9f2631733 Mon Sep 17 00:00:00 2001
From: HanzZ <hanzz.k@gmail.com>
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<boost::asio::io_service> ioService, EventLoop* eventLoop) : port_(port), ioService_(ioService), eventLoop(eventLoop), acceptor_(NULL) {
 }
 
+BoostConnectionServer::BoostConnectionServer(const HostAddress &address, int port, boost::shared_ptr<boost::asio::io_service> 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<boost::asio::io_service> 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<boost::asio::io_service> ioService, EventLoop* eventLoop);
+			BoostConnectionServer(const HostAddress &address, int port, boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop);
 
 			void stop(boost::optional<Error> e);
 			void acceptNextConnection();
 			void handleAccept(boost::shared_ptr<BoostConnection> newConnection, const boost::system::error_code& error);
 
 		private:
+			HostAddress address_;
 			int port_;
 			boost::shared_ptr<boost::asio::io_service> 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 <Swiften/Network/BoostConnectionServerFactory.h>
+#include <Swiften/Network/BoostConnectionServer.h>
+
+namespace Swift {
+
+BoostConnectionServerFactory::BoostConnectionServerFactory(boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop) : ioService(ioService), eventLoop(eventLoop) {
+}
+
+boost::shared_ptr<ConnectionServer> BoostConnectionServerFactory::createConnectionServer(int port) {
+	return BoostConnectionServer::create(port, ioService, eventLoop);
+}
+
+boost::shared_ptr<ConnectionServer> 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 <boost/asio/io_service.hpp>
+
+#include <Swiften/Network/ConnectionServerFactory.h>
+#include <Swiften/Network/BoostConnectionServer.h>
+
+namespace Swift {
+	class ConnectionServer;
+
+	class BoostConnectionServerFactory : public ConnectionServerFactory {
+		public:
+			BoostConnectionServerFactory(boost::shared_ptr<boost::asio::io_service>, EventLoop* eventLoop);
+
+			virtual boost::shared_ptr<ConnectionServer> createConnectionServer(int port);
+
+			virtual boost::shared_ptr<ConnectionServer> createConnectionServer(const Swift::HostAddress &hostAddress, int port);
+
+		private:
+			boost::shared_ptr<boost::asio::io_service> 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 <Swiften/Network/PlatformDomainNameResolver.h>
+#include <Swiften/Network/BoostConnectionServerFactory.h>
 
 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<void (boost::shared_ptr<Connection>)> 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 <Swiften/Network/ConnectionServerFactory.h>
+
+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 <boost/shared_ptr.hpp>
+
+namespace Swift {
+	class ConnectionServer;
+	class HostAddress;
+
+	class ConnectionServerFactory {
+		public:
+			virtual ~ConnectionServerFactory();
+
+			virtual boost::shared_ptr<ConnectionServer> createConnectionServer(int port) = 0;
+
+			virtual boost::shared_ptr<ConnectionServer> 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