From 4f26d4f044f909a73a1ce3183e21419e18a0dd43 Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Fri, 19 Jun 2015 12:46:56 +0200
Subject: Wait for assisted candidate detection before emitting candidates

Due to missing signal connection and a copy'n'paste bug, Swift did not
wait for port forwarding/public IP detection before emitting the list
of local candidates. This is fixed now.

The signal is automatically disconnected when the file transfer
is finished and s5bServerPortForwardingUser_ is freed.

Test-Information:

Send a file between two Swift instances and verified log output.

Change-Id: I6530a7ac1cbf6941061bd99aa3f3b0624ebc984c

diff --git a/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp b/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp
index 6a059bf..483fdb3 100644
--- a/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp
+++ b/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp
@@ -14,17 +14,17 @@
 
 #include <vector>
 
-#include <boost/shared_ptr.hpp>
 #include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
 #include <boost/smart_ptr/make_shared.hpp>
 
-#include <Swiften/Base/foreach.h>
 #include <Swiften/Base/Log.h>
+#include <Swiften/Base/foreach.h>
 #include <Swiften/Elements/JingleS5BTransportPayload.h>
 #include <Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.h>
 #include <Swiften/FileTransfer/SOCKS5BytestreamServerManager.h>
-#include <Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.h>
 #include <Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.h>
+#include <Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.h>
 
 static const unsigned int LOCAL_PREFERENCE = 0;
 
@@ -73,6 +73,9 @@ void LocalJingleTransportCandidateGenerator::start() {
 }
 
 void LocalJingleTransportCandidateGenerator::stop() {
+	if (s5bServerResourceUser_) {
+		s5bServerResourceUser_->onSuccessfulInitialized.disconnect(boost::bind(&LocalJingleTransportCandidateGenerator::handleS5BServerInitialized, this, _1));
+	}
 	s5bServerResourceUser_.reset();
 }
 
@@ -82,6 +85,7 @@ void LocalJingleTransportCandidateGenerator::handleS5BServerInitialized(bool suc
 		if (options_.isAssistedAllowed()) {
 			// try to setup port forwarding
 			s5bServerPortForwardingUser_ = s5bServerManager->aquirePortForwardingUser();
+			s5bServerPortForwardingUser_->onSetup.connect(boost::bind(&LocalJingleTransportCandidateGenerator::handlePortForwardingSetup, this, _1));
 			if (s5bServerPortForwardingUser_->isForwardingSetup()) {
 				handlePortForwardingSetup(true);
 			}
@@ -89,13 +93,19 @@ void LocalJingleTransportCandidateGenerator::handleS5BServerInitialized(bool suc
 	}
 	else {
 		SWIFT_LOG(warning) << "Unable to start SOCKS5 server" << std::endl;
+		if (s5bServerResourceUser_) {
+			s5bServerResourceUser_->onSuccessfulInitialized.disconnect(boost::bind(&LocalJingleTransportCandidateGenerator::handleS5BServerInitialized, this, _1));
+		}
 		s5bServerResourceUser_.reset();
 		handlePortForwardingSetup(false);
 	}
 	checkS5BCandidatesReady();
 }
 
-void LocalJingleTransportCandidateGenerator::handlePortForwardingSetup(bool success) {
+void LocalJingleTransportCandidateGenerator::handlePortForwardingSetup(bool /* success */) {
+	if (s5bServerPortForwardingUser_) {
+		s5bServerPortForwardingUser_->onSetup.disconnect(boost::bind(&LocalJingleTransportCandidateGenerator::handlePortForwardingSetup, this, _1));
+	}
 	triedForwarding_ = true;
 	checkS5BCandidatesReady();
 }
@@ -109,7 +119,7 @@ void LocalJingleTransportCandidateGenerator::handleDiscoveredProxiesChanged() {
 void LocalJingleTransportCandidateGenerator::checkS5BCandidatesReady() {
 	if ((!options_.isDirectAllowed()  || (options_.isDirectAllowed()  && triedServerInit_)) &&
 		(!options_.isProxiedAllowed() || (options_.isProxiedAllowed() && triedProxyDiscovery_)) &&
-		(!options_.isDirectAllowed()  || (options_.isDirectAllowed()  && triedServerInit_))) {
+		(!options_.isAssistedAllowed()  || (options_.isAssistedAllowed()  && triedForwarding_))) {
 		emitOnLocalTransportCandidatesGenerated();
 	}
 }
-- 
cgit v0.10.2-6-g49f6