summaryrefslogtreecommitdiffstats
blob: 988be7bb1673189021b3aa8c600f2c08cf682b7e (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
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "DefaultLocalJingleTransportCandidateGenerator.h"

#include <vector>

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

#include <Swiften/Base/foreach.h>
#include <Swiften/Base/Log.h>
#include <Swiften/Elements/JingleIBBTransportPayload.h>
#include <Swiften/Elements/JingleS5BTransportPayload.h>
#include <Swiften/FileTransfer/ConnectivityManager.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamRegistry.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamProxy.h>

namespace Swift {

DefaultLocalJingleTransportCandidateGenerator::DefaultLocalJingleTransportCandidateGenerator(ConnectivityManager* connectivityManager, SOCKS5BytestreamRegistry* s5bRegistry, SOCKS5BytestreamProxy* s5bProxy, JID& ownJID) : connectivityManager(connectivityManager), s5bRegistry(s5bRegistry), s5bProxy(s5bProxy), ownJID(ownJID) {
}

DefaultLocalJingleTransportCandidateGenerator::~DefaultLocalJingleTransportCandidateGenerator() {
}

void DefaultLocalJingleTransportCandidateGenerator::start(JingleTransportPayload::ref transportPayload) {
	if (boost::dynamic_pointer_cast<JingleIBBTransportPayload>(transportPayload)) {
		JingleTransportPayload::ref payL = boost::make_shared<JingleTransportPayload>();
		payL->setSessionID(transportPayload->getSessionID());
		onLocalTransportCandidatesGenerated(payL);
	}
	if (boost::dynamic_pointer_cast<JingleS5BTransportPayload>(transportPayload)) {
		JingleS5BTransportPayload::ref payL = boost::make_shared<JingleS5BTransportPayload>();
		payL->setSessionID(transportPayload->getSessionID());
		payL->setMode(JingleS5BTransportPayload::TCPMode);

		const unsigned long localPreference = 0;

		// get direct candidates
		std::vector<HostAddressPort> directCandidates = connectivityManager->getHostAddressPorts();
		foreach(HostAddressPort addressPort, directCandidates) {
			JingleS5BTransportPayload::Candidate candidate;
			candidate.type = JingleS5BTransportPayload::Candidate::DirectType;
			candidate.jid = ownJID;
			candidate.hostPort = addressPort;
			candidate.priority = 65536 * 126 + localPreference;
			candidate.cid = idGenerator.generateID();
			payL->addCandidate(candidate);
		}

		// get assissted candidates
		std::vector<HostAddressPort> assisstedCandidates = connectivityManager->getAssistedHostAddressPorts();
		foreach(HostAddressPort addressPort, assisstedCandidates) {
			JingleS5BTransportPayload::Candidate candidate;
			candidate.type = JingleS5BTransportPayload::Candidate::AssistedType;
			candidate.jid = ownJID;
			candidate.hostPort = addressPort;
			candidate.priority = 65536 * 120 + localPreference;
			candidate.cid = idGenerator.generateID();
			payL->addCandidate(candidate);
		}

		// get proxy candidates
		std::vector<S5BProxyRequest::ref> proxyCandidates = s5bProxy->getS5BProxies();
		foreach(S5BProxyRequest::ref proxy, proxyCandidates) {
			if (proxy->getStreamHost()) { // FIXME: Added this test, because there were cases where this wasn't initialized. Investigate this. (Remko)
				JingleS5BTransportPayload::Candidate candidate;
				candidate.type = JingleS5BTransportPayload::Candidate::ProxyType;
				candidate.jid = (*proxy->getStreamHost()).jid;
				candidate.hostPort = (*proxy->getStreamHost()).addressPort;
				candidate.priority = 65536 * 10 + localPreference;
				candidate.cid = idGenerator.generateID();
				payL->addCandidate(candidate);
			}
		}

		onLocalTransportCandidatesGenerated(payL);
	}
	
}

void DefaultLocalJingleTransportCandidateGenerator::stop() {
}

bool DefaultLocalJingleTransportCandidateGenerator::isActualCandidate(JingleTransportPayload::ref transportPayload) {
	if (!transportPayload.get()) return false;
	return false;
}

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

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

}