summaryrefslogtreecommitdiffstats
blob: a19947a7827bedd63a08c08b272ea7956b3afcb1 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Jingle/JingleSessionImpl.h>

#include <algorithm>

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

#include <Swiften/Base/Log.h>
#include <Swiften/Elements/JingleContentPayload.h>
#include <Swiften/Jingle/JingleContentID.h>
#include <Swiften/Jingle/JingleSessionListener.h>
#include <Swiften/Parser/PayloadParsers/JingleParser.h>
#include <Swiften/Queries/GenericRequest.h>
#include <Swiften/Queries/Request.h>
#include <Swiften/Serializer/PayloadSerializers/JinglePayloadSerializer.h>

namespace Swift {

JingleSessionImpl::JingleSessionImpl(const JID& initiator, const JID& peerJID, const std::string& id, IQRouter* router) : JingleSession(initiator, id), iqRouter(router), peerJID(peerJID) {
	SWIFT_LOG(debug) << "initiator: " << initiator << ", peerJID: " << peerJID << std::endl;
}

void JingleSessionImpl::handleIncomingAction(JinglePayload::ref action) {
	if (action->getAction() == JinglePayload::SessionTerminate) {
		notifyListeners(&JingleSessionListener::handleSessionTerminateReceived, action->getReason());
		return;
	}
	if (action->getAction() == JinglePayload::SessionInfo) {
		notifyListeners(&JingleSessionListener::handleSessionInfoReceived, action);
		return;
	}

	JingleContentPayload::ref content = action->getPayload<JingleContentPayload>();
	if (!content) {
		SWIFT_LOG(debug) << "no content payload!" << std::endl;
		return;
	}
	JingleContentID contentID(content->getName(), content->getCreator());
	JingleDescription::ref description = content->getDescriptions().empty() ? JingleDescription::ref() : content->getDescriptions()[0];
	JingleTransportPayload::ref transport = content->getTransports().empty() ? JingleTransportPayload::ref() : content->getTransports()[0];
	switch(action->getAction()) {
		case JinglePayload::SessionAccept:
			notifyListeners(&JingleSessionListener::handleSessionAcceptReceived, contentID, description, transport);
			return;
		case JinglePayload::TransportAccept:
			notifyListeners(&JingleSessionListener::handleTransportAcceptReceived, contentID, transport);
			return;
		case JinglePayload::TransportInfo:
			notifyListeners(&JingleSessionListener::handleTransportInfoReceived, contentID, transport);
			return;
		case JinglePayload::TransportReject:
			notifyListeners(&JingleSessionListener::handleTransportRejectReceived, contentID, transport);
			return;
		case JinglePayload::TransportReplace:
			notifyListeners(&JingleSessionListener::handleTransportReplaceReceived, contentID, transport);
			return;
		// following unused Jingle actions
		case JinglePayload::ContentAccept:
		case JinglePayload::ContentAdd:
		case JinglePayload::ContentModify:
		case JinglePayload::ContentReject:
		case JinglePayload::ContentRemove:
		case JinglePayload::DescriptionInfo:
		case JinglePayload::SecurityInfo:

		// handled elsewhere
		case JinglePayload::SessionInitiate:
		case JinglePayload::SessionInfo:
		case JinglePayload::SessionTerminate:

		case JinglePayload::UnknownAction:
			return;
	}
	assert(false);
}

void JingleSessionImpl::sendInitiate(const JingleContentID& id, JingleDescription::ref description, JingleTransportPayload::ref transport) {
	JinglePayload::ref payload = boost::make_shared<JinglePayload>(JinglePayload::SessionInitiate, getID());
	payload->setInitiator(getInitiator());
	JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>();
	content->setCreator(id.getCreator());
	content->setName(id.getName());
	content->addDescription(description);
	content->addTransport(transport);
	payload->addPayload(content);

	sendSetRequest(payload);
}

void JingleSessionImpl::sendTerminate(JinglePayload::Reason::Type reason) {
	JinglePayload::ref payload = boost::make_shared<JinglePayload>(JinglePayload::SessionTerminate, getID());
	payload->setReason(JinglePayload::Reason(reason));
	payload->setInitiator(getInitiator());
	sendSetRequest(payload);
}

void JingleSessionImpl::sendInfo(boost::shared_ptr<Payload> info) {
	JinglePayload::ref payload = boost::make_shared<JinglePayload>(JinglePayload::SessionInfo, getID());
	payload->addPayload(info);

	sendSetRequest(payload);
}

void JingleSessionImpl::sendAccept(const JingleContentID& id, JingleDescription::ref description, JingleTransportPayload::ref transPayload) {
	JinglePayload::ref payload = createPayload();
	
	JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>();
	content->setCreator(id.getCreator());
	content->setName(id.getName());
	content->addTransport(transPayload);
	content->addDescription(description);
	payload->setAction(JinglePayload::SessionAccept);
	payload->addPayload(content);
	
	// put into IQ:set and send it away
	sendSetRequest(payload);
}


void JingleSessionImpl::sendTransportAccept(const JingleContentID& id, JingleTransportPayload::ref transPayload) {
	JinglePayload::ref payload = createPayload();

	JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>();
	content->setCreator(id.getCreator());
	content->setName(id.getName());
	content->addTransport(transPayload);
	payload->setAction(JinglePayload::TransportAccept);
	payload->addPayload(content);

	// put into IQ:set and send it away
	sendSetRequest(payload);
}

std::string JingleSessionImpl::sendTransportInfo(const JingleContentID& id, JingleTransportPayload::ref transPayload) {
	JinglePayload::ref payload = createPayload();

	JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>();
	content->setCreator(id.getCreator());
	content->setName(id.getName());
	content->addTransport(transPayload);
	payload->setAction(JinglePayload::TransportInfo);
	payload->addPayload(content);

	return sendSetRequest(payload);
}

void JingleSessionImpl::sendTransportReject(const JingleContentID& id, JingleTransportPayload::ref transPayload) {
	JinglePayload::ref payload = createPayload();

	JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>();
	content->setCreator(id.getCreator());
	content->setName(id.getName());
	content->addTransport(transPayload);
	payload->setAction(JinglePayload::TransportReject);
	payload->addPayload(content);

	sendSetRequest(payload);
}

void JingleSessionImpl::sendTransportReplace(const JingleContentID& id, JingleTransportPayload::ref transPayload) {
	JinglePayload::ref payload = createPayload();

	JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>();
	content->setCreator(id.getCreator());
	content->setName(id.getName());
	content->addTransport(transPayload);
	payload->setAction(JinglePayload::TransportReplace);
	payload->addContent(content);

	sendSetRequest(payload);
}


std::string JingleSessionImpl::sendSetRequest(JinglePayload::ref payload) {
	boost::shared_ptr<GenericRequest<JinglePayload> > request = boost::make_shared<GenericRequest<JinglePayload> >(
			IQ::Set, peerJID, payload, iqRouter);
	pendingRequests.insert(std::make_pair(
		request,
		request->onResponse.connect(boost::bind(&JingleSessionImpl::handleRequestResponse, this, request))));
	return request->send();
}


JinglePayload::ref JingleSessionImpl::createPayload() const {
	JinglePayload::ref payload = boost::make_shared<JinglePayload>();
	payload->setSessionID(getID());
	payload->setInitiator(getInitiator());
	return payload;
}

void JingleSessionImpl::handleRequestResponse(RequestRef request) {
	RequestsMap::iterator i = pendingRequests.find(request);
	assert(i != pendingRequests.end());
	if (i->first->getPayloadGeneric()->getAction() == JinglePayload::TransportInfo) {
		notifyListeners(&JingleSessionListener::handleTransportInfoAcknowledged, i->first->getID());
	}
	i->second.disconnect();
	pendingRequests.erase(i);
}


}