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

#include <Swiften/Serializer/PayloadSerializers/JingleS5BTransportPayloadSerializer.h>

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

#include <Swiften/Base/foreach.h>
#include <Swiften/Serializer/XML/XMLNode.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
#include <Swiften/Base/Log.h>

namespace Swift {

JingleS5BTransportPayloadSerializer::JingleS5BTransportPayloadSerializer() {
}

std::string JingleS5BTransportPayloadSerializer::serializePayload(boost::shared_ptr<JingleS5BTransportPayload> payload) const {
	XMLElement payloadXML("transport", "urn:xmpp:jingle:transports:s5b:1");
	payloadXML.setAttribute("sid", payload->getSessionID());
	payloadXML.setAttribute("mode", modeToString(payload->getMode()));
	
	foreach(JingleS5BTransportPayload::Candidate candidate, payload->getCandidates()) {
		boost::shared_ptr<XMLElement> candidateXML = boost::make_shared<XMLElement>("candidate");
		candidateXML->setAttribute("cid", candidate.cid);
		candidateXML->setAttribute("host", candidate.hostPort.getAddress().toString());
		candidateXML->setAttribute("jid", candidate.jid.toString());
		candidateXML->setAttribute("port", boost::lexical_cast<std::string>(candidate.hostPort.getPort()));
		candidateXML->setAttribute("priority", boost::lexical_cast<std::string>(candidate.priority));
		candidateXML->setAttribute("type", typeToString(candidate.type));
		payloadXML.addNode(candidateXML);
	}
	
	if (payload->hasCandidateError()) {
		payloadXML.addNode(boost::make_shared<XMLElement>("candidate-error"));
	}
	if (payload->hasProxyError()) {
		payloadXML.addNode(boost::make_shared<XMLElement>("proxy-error"));
	}
	
	if (!payload->getActivated().empty()) {
		boost::shared_ptr<XMLElement> activatedXML = boost::make_shared<XMLElement>("activated");
		activatedXML->setAttribute("cid", payload->getActivated());
		payloadXML.addNode(activatedXML);
	}
	if (!payload->getCandidateUsed().empty()) {
		boost::shared_ptr<XMLElement> candusedXML = boost::make_shared<XMLElement>("candidate-used");
		candusedXML->setAttribute("cid", payload->getCandidateUsed());
		payloadXML.addNode(candusedXML);
	}
	
	return payloadXML.serialize();
}

std::string JingleS5BTransportPayloadSerializer::modeToString(JingleS5BTransportPayload::Mode mode) const {
	switch(mode) {
		case JingleS5BTransportPayload::TCPMode:
			return "tcp";
		case JingleS5BTransportPayload::UDPMode:
			return "udp";
	}
	assert(false);
	return "";
}

std::string JingleS5BTransportPayloadSerializer::typeToString(JingleS5BTransportPayload::Candidate::Type type) const {
	switch(type) {
		case JingleS5BTransportPayload::Candidate::AssistedType:
			return "assisted";
		case JingleS5BTransportPayload::Candidate::DirectType:
			return "direct";
		case JingleS5BTransportPayload::Candidate::ProxyType:
			return "proxy";
		case JingleS5BTransportPayload::Candidate::TunnelType:
			return "tunnel";
	}
	assert(false);
	return "";
}

}