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

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

#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/smart_ptr/intrusive_ptr.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/Serializer/PayloadSerializers/JingleFileTransferDescriptionSerializer.h>
#include <Swiften/Serializer/PayloadSerializers/JingleRTPDescriptionSerializer.h>

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

#include "Swiften/FileTransfer/JingleTransport.h"

namespace Swift {

JingleContentPayloadSerializer::JingleContentPayloadSerializer() {
}

std::string JingleContentPayloadSerializer::serializePayload(boost::shared_ptr<JingleContentPayload> payload) const {
	XMLElement payloadXML("content");
	payloadXML.setAttribute("creator", creatorToString(payload->getCreator()));
	payloadXML.setAttribute("name", payload->getName());
	
	if (!payload->getDescriptions().empty()) {
		// JingleFileTransferDescription
		JingleFileTransferDescription::ref filetransfer;
		JingleRTPDescription::ref rtp;
		
		foreach(JingleDescription::ref desc, payload->getDescriptions()) {
			if ((filetransfer = boost::dynamic_pointer_cast<JingleFileTransferDescription>(desc))) {
				JingleFileTransferDescriptionSerializer ftSerializer;
				payloadXML.addNode(boost::make_shared<XMLRawTextNode>(ftSerializer.serializePayload(filetransfer)));
			} else if ((rtp = boost::dynamic_pointer_cast<JingleRTPDescription>(desc))) {
				JingleRTPDescriptionSerializer rtpSerializer;
				payloadXML.addNode(boost::make_shared<XMLRawTextNode>(rtpSerializer.serializePayload(rtp)));
			}
		}
	}
	
	if (!payload->getTransports().empty()) {	
		// JingleIBBTransportPayload
		JingleIBBTransportPayloadSerializer ibbSerializer;
		JingleIBBTransportPayload::ref ibb;
		
		// JingleS5BTransportPayload
		JingleS5BTransportPayloadSerializer s5bSerializer;
		JingleS5BTransportPayload::ref s5b;

		// JingleRawUDPTransportPayload
		JingleRawUDPTransportPayloadSerializer rawUdpSerializer;
		JingleRawUDPTransportPayload::ref rawUDP;

		foreach(JingleTransportPayload::ref transport, payload->getTransports()) {
			if ((ibb = boost::dynamic_pointer_cast<JingleIBBTransportPayload>(transport))) {
				payloadXML.addNode(boost::make_shared<XMLRawTextNode>(ibbSerializer.serializePayload(ibb)));
			} else if ((s5b = boost::dynamic_pointer_cast<JingleS5BTransportPayload>(transport))) {
				payloadXML.addNode(boost::make_shared<XMLRawTextNode>(s5bSerializer.serializePayload(s5b)));
			} else if ((rawUDP = boost::dynamic_pointer_cast<JingleRawUDPTransportPayload>(transport))) {
				payloadXML.addNode(boost::make_shared<XMLRawTextNode>(rawUdpSerializer.serializePayload(rawUDP)));
			}
		}
	}
	return payloadXML.serialize();
}

std::string JingleContentPayloadSerializer::creatorToString(JingleContentPayload::Creator creator) const {
	switch(creator) {
		case JingleContentPayload::InitiatorCreator:
			return "initiator";
		case JingleContentPayload::ResponderCreator:
			return "responder";
		case JingleContentPayload::UnknownCreator:
			std::cerr << "Serializing unknown creator value." << std::endl;
			return "ERROR ERROR ERROR";
	}
	assert(false);
	return "";
}
}