From 4ec2390efec7cfdd411c7bb14e9f77ec9ee5c005 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Mon, 8 Apr 2013 19:25:21 +0200
Subject: Network refactoring.

Provide sync connection server start method.
Provide NetworkEnvironment through NetworkFactories.

Change-Id: Iea04349255af8aa6326968f17653e8e371a08bdd

diff --git a/Swiften/Network/BoostConnectionServer.cpp b/Swiften/Network/BoostConnectionServer.cpp
index e015eaa..c90b554 100644
--- a/Swiften/Network/BoostConnectionServer.cpp
+++ b/Swiften/Network/BoostConnectionServer.cpp
@@ -10,6 +10,7 @@
 #include <boost/system/system_error.hpp>
 #include <boost/asio/placeholders.hpp>
 #include <boost/numeric/conversion/cast.hpp>
+#include <boost/optional.hpp>
 
 #include <Swiften/EventLoop/EventLoop.h>
 
@@ -22,6 +23,13 @@ BoostConnectionServer::BoostConnectionServer(const HostAddress &address, int por
 }
 
 void BoostConnectionServer::start() {
+	boost::optional<Error> error = tryStart();
+	if (error) {
+		eventLoop->postEvent(boost::bind(boost::ref(onStopped), *error), shared_from_this());
+	}
+}
+
+boost::optional<BoostConnectionServer::Error> BoostConnectionServer::tryStart() {
 	try {
 		assert(!acceptor_);
 		if (address_.isValid()) {
@@ -38,12 +46,13 @@ void BoostConnectionServer::start() {
 	}
 	catch (const boost::system::system_error& e) {
 		if (e.code() == boost::asio::error::address_in_use) {
-			eventLoop->postEvent(boost::bind(boost::ref(onStopped), Conflict), shared_from_this());
+			return Conflict;
 		}
 		else {
-			eventLoop->postEvent(boost::bind(boost::ref(onStopped), UnknownError), shared_from_this());
+			return UnknownError;
 		}
 	}
+	return boost::optional<Error>();
 }
 
 
diff --git a/Swiften/Network/BoostConnectionServer.h b/Swiften/Network/BoostConnectionServer.h
index 66af2a4..3ad0450 100644
--- a/Swiften/Network/BoostConnectionServer.h
+++ b/Swiften/Network/BoostConnectionServer.h
@@ -16,17 +16,13 @@
 #include <Swiften/Network/BoostConnection.h>
 #include <Swiften/Network/ConnectionServer.h>
 #include <Swiften/EventLoop/EventOwner.h>
+#include <boost/optional/optional_fwd.hpp>
 
 namespace Swift {
 	class SWIFTEN_API BoostConnectionServer : public ConnectionServer, public EventOwner, public boost::enable_shared_from_this<BoostConnectionServer> {
 		public:
 			typedef boost::shared_ptr<BoostConnectionServer> ref;
 
-			enum Error {
-				Conflict,
-				UnknownError
-			};
-
 			static ref create(int port, boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop) {
 				return ref(new BoostConnectionServer(port, ioService, eventLoop));
 			}
@@ -35,6 +31,7 @@ namespace Swift {
 				return ref(new BoostConnectionServer(address, port, ioService, eventLoop));
 			}
 
+			virtual boost::optional<Error> tryStart(); // FIXME: This should become the new start
 			virtual void start();
 			virtual void stop();
 
diff --git a/Swiften/Network/BoostNetworkFactories.cpp b/Swiften/Network/BoostNetworkFactories.cpp
index 41646c6..1265682 100644
--- a/Swiften/Network/BoostNetworkFactories.cpp
+++ b/Swiften/Network/BoostNetworkFactories.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2012 Remko Tronçon
  * Licensed under the GNU General Public License v3.
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
@@ -12,6 +12,7 @@
 #include <Swiften/Network/PlatformNATTraversalWorker.h>
 #include <Swiften/Parser/PlatformXMLParserFactory.h>
 #include <Swiften/Network/NullNATTraverser.h>
+#include <Swiften/Network/PlatformNetworkEnvironment.h>
 #include <Swiften/TLS/PlatformTLSFactories.h>
 #include <Swiften/Network/PlatformProxyProvider.h>
 #include <Swiften/IDN/PlatformIDNConverter.h>
@@ -28,6 +29,7 @@ BoostNetworkFactories::BoostNetworkFactories(EventLoop* eventLoop) : eventLoop(e
 #else
 	natTraverser = new NullNATTraverser(eventLoop);
 #endif
+	networkEnvironment = new PlatformNetworkEnvironment();
 	xmlParserFactory = new PlatformXMLParserFactory();
 	tlsFactories = new PlatformTLSFactories();
 	proxyProvider = new PlatformProxyProvider();
@@ -41,6 +43,7 @@ BoostNetworkFactories::~BoostNetworkFactories() {
 	delete proxyProvider;
 	delete tlsFactories;
 	delete xmlParserFactory;
+	delete networkEnvironment;
 	delete natTraverser;
 	delete connectionServerFactory;
 	delete connectionFactory;
diff --git a/Swiften/Network/BoostNetworkFactories.h b/Swiften/Network/BoostNetworkFactories.h
index 11797c6..8bf3576 100644
--- a/Swiften/Network/BoostNetworkFactories.h
+++ b/Swiften/Network/BoostNetworkFactories.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2012 Remko Tronçon
  * Licensed under the GNU General Public License v3.
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
@@ -41,6 +41,10 @@ namespace Swift {
 				return connectionServerFactory;
 			}
 
+			NetworkEnvironment* getNetworkEnvironment() const SWIFTEN_OVERRIDE {
+				return networkEnvironment;
+			}
+
 			NATTraverser* getNATTraverser() const SWIFTEN_OVERRIDE {
 				return natTraverser;
 			}
@@ -70,6 +74,7 @@ namespace Swift {
 			DomainNameResolver* domainNameResolver;
 			ConnectionServerFactory* connectionServerFactory;
 			NATTraverser* natTraverser;
+			NetworkEnvironment* networkEnvironment;
 			XMLParserFactory* xmlParserFactory;
 			PlatformTLSFactories* tlsFactories;
 			ProxyProvider* proxyProvider;
diff --git a/Swiften/Network/ConnectionServer.h b/Swiften/Network/ConnectionServer.h
index e644d90..2e09348 100644
--- a/Swiften/Network/ConnectionServer.h
+++ b/Swiften/Network/ConnectionServer.h
@@ -12,14 +12,22 @@
 #include <Swiften/Base/boost_bsignals.h>
 #include <Swiften/Network/Connection.h>
 #include <Swiften/Network/HostAddressPort.h>
+#include <boost/optional/optional_fwd.hpp>
 
 namespace Swift {
 	class SWIFTEN_API ConnectionServer {
 		public:
+			enum Error {
+				Conflict,
+				UnknownError
+			};
+
 			virtual ~ConnectionServer();
 
 			virtual HostAddressPort getAddressPort() const = 0;
 
+			virtual boost::optional<Error> tryStart() = 0; // FIXME: This should become the new start
+
 			virtual void start() = 0;
 
 			virtual void stop() = 0;
diff --git a/Swiften/Network/NetworkFactories.h b/Swiften/Network/NetworkFactories.h
index f24f493..228d92d 100644
--- a/Swiften/Network/NetworkFactories.h
+++ b/Swiften/Network/NetworkFactories.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2012 Remko Tronçon
  * Licensed under the GNU General Public License v3.
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
@@ -18,6 +18,7 @@ namespace Swift {
 	class ProxyProvider;
 	class EventLoop;
 	class IDNConverter;
+	class NetworkEnvironment;
 
 	/**
 	 * An interface collecting network factories.
@@ -31,6 +32,7 @@ namespace Swift {
 			virtual DomainNameResolver* getDomainNameResolver() const = 0;
 			virtual ConnectionServerFactory* getConnectionServerFactory() const = 0;
 			virtual NATTraverser* getNATTraverser() const = 0;
+			virtual NetworkEnvironment* getNetworkEnvironment() const = 0;
 			virtual XMLParserFactory* getXMLParserFactory() const = 0;
 			virtual TLSContextFactory* getTLSContextFactory() const = 0;
 			virtual ProxyProvider* getProxyProvider() const = 0;
-- 
cgit v0.10.2-6-g49f6