summaryrefslogtreecommitdiffstats
blob: 0a1283f42b1c476a5e9a59d646e785e5fc5db6d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (c) 2011 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swiften/Network/ChainedConnector.h>

#include <boost/bind.hpp>
#include <typeinfo>

#include <Swiften/Base/Log.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Network/Connector.h>
#include <Swiften/Network/ConnectionFactory.h>

using namespace Swift;

ChainedConnector::ChainedConnector(
		const std::string& hostname, 
		DomainNameResolver* resolver, 
		const std::vector<ConnectionFactory*>& connectionFactories, 
		TimerFactory* timerFactory) : 
			hostname(hostname), 
			resolver(resolver), 
			connectionFactories(connectionFactories), 
			timerFactory(timerFactory), 
			timeoutMilliseconds(0) {
}

void ChainedConnector::setTimeoutMilliseconds(int milliseconds) {
	timeoutMilliseconds = milliseconds;
}

void ChainedConnector::start() {
	SWIFT_LOG(debug) << "Starting queued connector for " << hostname << std::endl;

	connectionFactoryQueue = std::deque<ConnectionFactory*>(connectionFactories.begin(), connectionFactories.end());
	tryNextConnectionFactory();
}

void ChainedConnector::stop() {
	if (currentConnector) {
		currentConnector->onConnectFinished.disconnect(boost::bind(&ChainedConnector::handleConnectorFinished, this, _1, _2));
		currentConnector->stop();
		currentConnector.reset();
	}
	finish(boost::shared_ptr<Connection>(), boost::shared_ptr<Error>());
}

void ChainedConnector::tryNextConnectionFactory() {
	assert(!currentConnector);
	if (connectionFactoryQueue.empty()) {
		SWIFT_LOG(debug) << "No more connection factories" << std::endl;
		finish(boost::shared_ptr<Connection>(), lastError);
	}
	else {
		ConnectionFactory* connectionFactory = connectionFactoryQueue.front();
		SWIFT_LOG(debug) << "Trying next connection factory: " << typeid(*connectionFactory).name() << std::endl;
		connectionFactoryQueue.pop_front();
		currentConnector = Connector::create(hostname, resolver, connectionFactory, timerFactory);
		currentConnector->setTimeoutMilliseconds(timeoutMilliseconds);
		currentConnector->onConnectFinished.connect(boost::bind(&ChainedConnector::handleConnectorFinished, this, _1, _2));
		currentConnector->start();
	}
}

void ChainedConnector::handleConnectorFinished(boost::shared_ptr<Connection> connection, boost::shared_ptr<Error> error) {
	SWIFT_LOG(debug) << "Connector finished" << std::endl;
	currentConnector->onConnectFinished.disconnect(boost::bind(&ChainedConnector::handleConnectorFinished, this, _1, _2));
	lastError = error;
	currentConnector.reset();
	if (connection) {
		finish(connection, error);
	}
	else {
		tryNextConnectionFactory();
	}
}

void ChainedConnector::finish(boost::shared_ptr<Connection> connection, boost::shared_ptr<Error> error) {
	onConnectFinished(connection, error);
}