summaryrefslogtreecommitdiffstats
blob: 32b4df82cbc24babb3f9b6cefdc847afd11bd21d (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "DefaultRemoteJingleTransportCandidateSelector.h"

#include <boost/smart_ptr/make_shared.hpp>
#include <boost/bind.hpp>

#include <Swiften/Base/Log.h>
#include <Swiften/Base/boost_bsignals.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Elements/JingleS5BTransportPayload.h>
#include <Swiften/Network/ConnectionFactory.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamRegistry.h>

namespace Swift {

DefaultRemoteJingleTransportCandidateSelector::DefaultRemoteJingleTransportCandidateSelector(ConnectionFactory* connectionFactory, TimerFactory* timerFactory) : connectionFactory(connectionFactory), timerFactory(timerFactory) {
}

DefaultRemoteJingleTransportCandidateSelector::~DefaultRemoteJingleTransportCandidateSelector() {
}

void DefaultRemoteJingleTransportCandidateSelector::addRemoteTransportCandidates(JingleTransportPayload::ref transportPayload) {
	JingleS5BTransportPayload::ref s5bPayload;
	transportSID = transportPayload->getSessionID();
	if ((s5bPayload = boost::dynamic_pointer_cast<JingleS5BTransportPayload>(transportPayload))) {
		foreach(JingleS5BTransportPayload::Candidate c,  s5bPayload->getCandidates()) {
			candidates.push(c);
		}
	}
}

void DefaultRemoteJingleTransportCandidateSelector::selectCandidate() {
	tryNextCandidate(true);
}

void DefaultRemoteJingleTransportCandidateSelector::tryNextCandidate(bool error) {
	if (error) {
		if (s5bSession) {
			SWIFT_LOG(debug) << "failed to connect" << std::endl;
		}
		if (candidates.empty()) {
			// failed to connect to any of the candidates
			// issue an error
			SWIFT_LOG(debug) << "out of candidates )=" << std::endl;
			JingleS5BTransportPayload::ref failed = boost::make_shared<JingleS5BTransportPayload>();
			failed->setCandidateError(true);
			failed->setSessionID(transportSID);
			onRemoteTransportCandidateSelectFinished(failed);
		} else {
			lastCandidate = candidates.top();
			// only try direct or assisted for now
			if (lastCandidate.type == JingleS5BTransportPayload::Candidate::DirectType ||
				lastCandidate.type == JingleS5BTransportPayload::Candidate::AssistedType || lastCandidate.type == JingleS5BTransportPayload::Candidate::ProxyType ) {
				// create connection
				connection = connectionFactory->createConnection();
				s5bSession = boost::make_shared<SOCKS5BytestreamClientSession>(connection, lastCandidate.hostPort, SOCKS5BytestreamRegistry::getHostname(transportSID, requester, target), timerFactory);

				// bind onReady to this method
				s5bSession->onSessionReady.connect(boost::bind(&DefaultRemoteJingleTransportCandidateSelector::tryNextCandidate, this, _1));

				std::string candidateType;
				if (lastCandidate.type == JingleS5BTransportPayload::Candidate::DirectType) {
					candidateType = "direct";
				} else if (lastCandidate.type == JingleS5BTransportPayload::Candidate::AssistedType) {
					candidateType = "assisted";
				} else if (lastCandidate.type == JingleS5BTransportPayload::Candidate::ProxyType) {
					candidateType = "proxy";
				}

				// initiate connect
				SWIFT_LOG(debug) << "try to connect to candidate of type " << candidateType << " : " << lastCandidate.hostPort.toString() << std::endl;
				s5bSession->start();

				// that's it. we're gonna be called back
				candidates.pop();
			} else {
				s5bSession.reset();
				candidates.pop();
				tryNextCandidate(true);
			}
		}
	} else {
		// we have a working connection, hooray
		JingleS5BTransportPayload::ref success = boost::make_shared<JingleS5BTransportPayload>();
		success->setCandidateUsed(lastCandidate.cid);
		success->setSessionID(transportSID);
		onRemoteTransportCandidateSelectFinished(success);
	}
}

void DefaultRemoteJingleTransportCandidateSelector::setMinimumPriority(int priority) {
	SWIFT_LOG(debug) << "priority: " << priority << std::endl;
}

void DefaultRemoteJingleTransportCandidateSelector::setRequesterTargtet(const JID& requester, const JID& target) {
	this->requester = requester;
	this->target = target;
}

SOCKS5BytestreamClientSession::ref DefaultRemoteJingleTransportCandidateSelector::getS5BSession() {
	return s5bSession;
}

bool DefaultRemoteJingleTransportCandidateSelector::isActualCandidate(JingleTransportPayload::ref /* transportPayload */) {
	return false;
}

int DefaultRemoteJingleTransportCandidateSelector::getPriority(JingleTransportPayload::ref /* transportPayload */) {
	return 0;
}

JingleTransport::ref DefaultRemoteJingleTransportCandidateSelector::selectTransport(JingleTransportPayload::ref /* transportPayload */) {
	return JingleTransport::ref();
}

}