1package eu.siacs.conversations.xmpp.jingle;
2
3import android.os.SystemClock;
4import android.util.Base64;
5import android.util.Log;
6
7import com.google.common.base.Function;
8import com.google.common.base.Objects;
9import com.google.common.base.Preconditions;
10import com.google.common.cache.Cache;
11import com.google.common.cache.CacheBuilder;
12import com.google.common.collect.Collections2;
13import com.google.common.collect.ImmutableSet;
14
15import org.checkerframework.checker.nullness.compatqual.NullableDecl;
16
17import java.lang.ref.WeakReference;
18import java.security.SecureRandom;
19import java.util.Collection;
20import java.util.Collections;
21import java.util.HashMap;
22import java.util.List;
23import java.util.Map;
24import java.util.Set;
25import java.util.concurrent.ConcurrentHashMap;
26import java.util.concurrent.TimeUnit;
27
28import eu.siacs.conversations.Config;
29import eu.siacs.conversations.entities.Account;
30import eu.siacs.conversations.entities.Conversation;
31import eu.siacs.conversations.entities.Conversational;
32import eu.siacs.conversations.entities.Message;
33import eu.siacs.conversations.entities.RtpSessionStatus;
34import eu.siacs.conversations.entities.Transferable;
35import eu.siacs.conversations.services.AbstractConnectionManager;
36import eu.siacs.conversations.services.XmppConnectionService;
37import eu.siacs.conversations.xml.Element;
38import eu.siacs.conversations.xml.Namespace;
39import eu.siacs.conversations.xmpp.OnIqPacketReceived;
40import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
41import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
42import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
43import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
44import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
45import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
46import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
47import eu.siacs.conversations.xmpp.stanzas.IqPacket;
48import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
49import rocks.xmpp.addr.Jid;
50
51public class JingleConnectionManager extends AbstractConnectionManager {
52 private final HashMap<RtpSessionProposal, DeviceDiscoveryState> rtpSessionProposals = new HashMap<>();
53 private final Map<AbstractJingleConnection.Id, AbstractJingleConnection> connections = new ConcurrentHashMap<>();
54
55 private final Cache<PersistableSessionId, JingleRtpConnection.State> endedSessions = CacheBuilder.newBuilder()
56 .expireAfterWrite(30, TimeUnit.MINUTES)
57 .build();
58
59 private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
60
61 public JingleConnectionManager(XmppConnectionService service) {
62 super(service);
63 }
64
65 static String nextRandomId() {
66 final byte[] id = new byte[16];
67 new SecureRandom().nextBytes(id);
68 return Base64.encodeToString(id, Base64.NO_WRAP | Base64.NO_PADDING);
69 }
70
71 public void deliverPacket(final Account account, final JinglePacket packet) {
72 final String sessionId = packet.getSessionId();
73 if (sessionId == null) {
74 respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
75 return;
76 }
77 final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, packet);
78 final AbstractJingleConnection existingJingleConnection = connections.get(id);
79 if (existingJingleConnection != null) {
80 existingJingleConnection.deliverPacket(packet);
81 } else if (packet.getAction() == JinglePacket.Action.SESSION_INITIATE) {
82 final Jid from = packet.getFrom();
83 final Content content = packet.getJingleContent();
84 final String descriptionNamespace = content == null ? null : content.getDescriptionNamespace();
85 final AbstractJingleConnection connection;
86 if (FileTransferDescription.NAMESPACES.contains(descriptionNamespace)) {
87 connection = new JingleFileTransferConnection(this, id, from);
88 } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace) && !usesTor(account)) {
89 final boolean sessionEnded = this.endedSessions.asMap().containsKey(PersistableSessionId.of(id));
90 if (isBusy() || sessionEnded) {
91 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": rejected session with " + id.with + " because busy. sessionEnded=" + sessionEnded);
92 mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
93 final JinglePacket sessionTermination = new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
94 sessionTermination.setTo(id.with);
95 sessionTermination.setReason(Reason.BUSY, null);
96 mXmppConnectionService.sendIqPacket(account, sessionTermination, null);
97 return;
98 }
99 connection = new JingleRtpConnection(this, id, from);
100 } else {
101 respondWithJingleError(account, packet, "unsupported-info", "feature-not-implemented", "cancel");
102 return;
103 }
104 connections.put(id, connection);
105 connection.deliverPacket(packet);
106 } else {
107 Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
108 respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
109 }
110 }
111
112 private boolean usesTor(final Account account) {
113 return account.isOnion() || mXmppConnectionService.useTorToConnect();
114 }
115
116 public boolean isBusy() {
117 for (AbstractJingleConnection connection : this.connections.values()) {
118 if (connection instanceof JingleRtpConnection) {
119 return true;
120 }
121 }
122 synchronized (this.rtpSessionProposals) {
123 return this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED) || this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING);
124 }
125 }
126
127 public void respondWithJingleError(final Account account, final IqPacket original, String jingleCondition, String condition, String conditionType) {
128 final IqPacket response = original.generateResponse(IqPacket.TYPE.ERROR);
129 final Element error = response.addChild("error");
130 error.setAttribute("type", conditionType);
131 error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
132 error.addChild(jingleCondition, "urn:xmpp:jingle:errors:1");
133 account.getXmppConnection().sendIqPacket(response, null);
134 }
135
136 public void deliverMessage(final Account account, final Jid to, final Jid from, final Element message, String serverMsgId, long timestamp) {
137 Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
138 final String sessionId = message.getAttribute("id");
139 if (sessionId == null) {
140 return;
141 }
142 if ("accept".equals(message.getName())) {
143 for (AbstractJingleConnection connection : connections.values()) {
144 if (connection instanceof JingleRtpConnection) {
145 final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
146 final AbstractJingleConnection.Id id = connection.getId();
147 if (id.account == account && id.sessionId.equals(sessionId)) {
148 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
149 return;
150 }
151 }
152 }
153 return;
154 }
155 final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
156 final AbstractJingleConnection.Id id;
157 if (fromSelf) {
158 if (to.isFullJid()) {
159 id = AbstractJingleConnection.Id.of(account, to, sessionId);
160 } else {
161 return;
162 }
163 } else {
164 id = AbstractJingleConnection.Id.of(account, from, sessionId);
165 }
166 final AbstractJingleConnection existingJingleConnection = connections.get(id);
167 if (existingJingleConnection != null) {
168 if (existingJingleConnection instanceof JingleRtpConnection) {
169 ((JingleRtpConnection) existingJingleConnection).deliveryMessage(from, message, serverMsgId, timestamp);
170 } else {
171 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + existingJingleConnection.getClass().getName() + " does not support jingle messages");
172 }
173 return;
174 }
175
176 if (fromSelf) {
177 if ("proceed".equals(message.getName())) {
178 final Conversation c = mXmppConnectionService.findOrCreateConversation(account, id.with, false, false);
179 final Message previousBusy = c.findRtpSession(sessionId, Message.STATUS_RECEIVED);
180 if (previousBusy != null) {
181 previousBusy.setBody(new RtpSessionStatus(true, 0).toString());
182 if (serverMsgId != null) {
183 previousBusy.setServerMsgId(serverMsgId);
184 }
185 previousBusy.setTime(timestamp);
186 mXmppConnectionService.updateMessage(previousBusy, true);
187 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": updated previous busy because call got picked up by another device");
188 return;
189 }
190 }
191 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignore jingle message from self");
192 return;
193 }
194
195 if ("propose".equals(message.getName())) {
196 final Propose propose = Propose.upgrade(message);
197 final List<GenericDescription> descriptions = propose.getDescriptions();
198 final Collection<RtpDescription> rtpDescriptions = Collections2.transform(
199 Collections2.filter(descriptions, d -> d instanceof RtpDescription),
200 input -> (RtpDescription) input
201 );
202 if (rtpDescriptions.size() > 0 && rtpDescriptions.size() == descriptions.size() && !usesTor(account)) {
203 final Collection<Media> media = Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
204 if (media.contains(Media.UNKNOWN)) {
205 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered unknown media in session proposal. " + propose);
206 return;
207 }
208 if (isBusy()) {
209 writeLogMissedIncoming(account, id.with.asBareJid(), id.sessionId, serverMsgId, timestamp);
210 final int activeDevices = account.countPresences();
211 Log.d(Config.LOGTAG, "active devices: " + activeDevices);
212 if (activeDevices == 0) {
213 final MessagePacket reject = mXmppConnectionService.getMessageGenerator().sessionReject(from, sessionId);
214 mXmppConnectionService.sendMessagePacket(account, reject);
215 } else {
216 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring proposal because busy on this device but there are other devices");
217 }
218 } else {
219 final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, from);
220 this.connections.put(id, rtpConnection);
221 rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
222 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
223 }
224 } else {
225 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to react to proposed session with " + rtpDescriptions.size() + " rtp descriptions of " + descriptions.size() + " total descriptions");
226 }
227 } else if ("proceed".equals(message.getName())) {
228 synchronized (rtpSessionProposals) {
229 final RtpSessionProposal proposal = getRtpSessionProposal(account, from.asBareJid(), sessionId);
230 if (proposal != null) {
231 rtpSessionProposals.remove(proposal);
232 final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, account.getJid());
233 rtpConnection.setProposedMedia(proposal.media);
234 this.connections.put(id, rtpConnection);
235 rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
236 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
237 } else {
238 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver proceed");
239 }
240 }
241 } else if ("reject".equals(message.getName())) {
242 final RtpSessionProposal proposal = new RtpSessionProposal(account, from.asBareJid(), sessionId);
243 synchronized (rtpSessionProposals) {
244 if (rtpSessionProposals.remove(proposal) != null) {
245 writeLogMissedOutgoing(account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
246 mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, proposal.with, proposal.sessionId, RtpEndUserState.DECLINED_OR_BUSY);
247 } else {
248 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver reject");
249 }
250 }
251 } else {
252 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": retrieved out of order jingle message");
253 }
254
255 }
256
257 private RtpSessionProposal getRtpSessionProposal(final Account account, Jid from, String sessionId) {
258 for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
259 if (rtpSessionProposal.sessionId.equals(sessionId) && rtpSessionProposal.with.equals(from) && rtpSessionProposal.account.getJid().equals(account.getJid())) {
260 return rtpSessionProposal;
261 }
262 }
263 return null;
264 }
265
266 private void writeLogMissedOutgoing(final Account account, Jid with, final String sessionId, String serverMsgId, long timestamp) {
267 final Conversation conversation = mXmppConnectionService.findOrCreateConversation(
268 account,
269 with.asBareJid(),
270 false,
271 false
272 );
273 final Message message = new Message(
274 conversation,
275 Message.STATUS_SEND,
276 Message.TYPE_RTP_SESSION,
277 sessionId
278 );
279 message.setBody(new RtpSessionStatus(false, 0).toString());
280 message.setServerMsgId(serverMsgId);
281 message.setTime(timestamp);
282 writeMessage(message);
283 }
284
285 private void writeLogMissedIncoming(final Account account, Jid with, final String sessionId, String serverMsgId, long timestamp) {
286 final Conversation conversation = mXmppConnectionService.findOrCreateConversation(
287 account,
288 with.asBareJid(),
289 false,
290 false
291 );
292 final Message message = new Message(
293 conversation,
294 Message.STATUS_RECEIVED,
295 Message.TYPE_RTP_SESSION,
296 sessionId
297 );
298 message.setBody(new RtpSessionStatus(false, 0).toString());
299 message.setServerMsgId(serverMsgId);
300 message.setTime(timestamp);
301 writeMessage(message);
302 }
303
304 private void writeMessage(final Message message) {
305 final Conversational conversational = message.getConversation();
306 if (conversational instanceof Conversation) {
307 ((Conversation) conversational).add(message);
308 mXmppConnectionService.databaseBackend.createMessage(message);
309 mXmppConnectionService.updateConversationUi();
310 } else {
311 throw new IllegalStateException("Somehow the conversation in a message was a stub");
312 }
313 }
314
315 public void startJingleFileTransfer(final Message message) {
316 Preconditions.checkArgument(message.isFileOrImage(), "Message is not of type file or image");
317 final Transferable old = message.getTransferable();
318 if (old != null) {
319 old.cancel();
320 }
321 final Account account = message.getConversation().getAccount();
322 final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message);
323 final JingleFileTransferConnection connection = new JingleFileTransferConnection(this, id, account.getJid());
324 mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
325 this.connections.put(id, connection);
326 connection.init(message);
327 }
328
329 void finishConnection(final AbstractJingleConnection connection) {
330 this.connections.remove(connection.getId());
331 }
332
333 void getPrimaryCandidate(final Account account, final boolean initiator, final OnPrimaryCandidateFound listener) {
334 if (Config.DISABLE_PROXY_LOOKUP) {
335 listener.onPrimaryCandidateFound(false, null);
336 return;
337 }
338 if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
339 final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
340 if (proxy != null) {
341 IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
342 iq.setTo(proxy);
343 iq.query(Namespace.BYTE_STREAMS);
344 account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {
345
346 @Override
347 public void onIqPacketReceived(Account account, IqPacket packet) {
348 final Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS);
349 final String host = streamhost == null ? null : streamhost.getAttribute("host");
350 final String port = streamhost == null ? null : streamhost.getAttribute("port");
351 if (host != null && port != null) {
352 try {
353 JingleCandidate candidate = new JingleCandidate(nextRandomId(), true);
354 candidate.setHost(host);
355 candidate.setPort(Integer.parseInt(port));
356 candidate.setType(JingleCandidate.TYPE_PROXY);
357 candidate.setJid(proxy);
358 candidate.setPriority(655360 + (initiator ? 30 : 0));
359 primaryCandidates.put(account.getJid().asBareJid(), candidate);
360 listener.onPrimaryCandidateFound(true, candidate);
361 } catch (final NumberFormatException e) {
362 listener.onPrimaryCandidateFound(false, null);
363 }
364 } else {
365 listener.onPrimaryCandidateFound(false, null);
366 }
367 }
368 });
369 } else {
370 listener.onPrimaryCandidateFound(false, null);
371 }
372
373 } else {
374 listener.onPrimaryCandidateFound(true,
375 this.primaryCandidates.get(account.getJid().asBareJid()));
376 }
377 }
378
379 public void retractSessionProposal(final Account account, final Jid with) {
380 synchronized (this.rtpSessionProposals) {
381 RtpSessionProposal matchingProposal = null;
382 for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
383 if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
384 matchingProposal = proposal;
385 break;
386 }
387 }
388 if (matchingProposal != null) {
389 this.rtpSessionProposals.remove(matchingProposal);
390 final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionRetract(matchingProposal);
391 writeLogMissedOutgoing(account, matchingProposal.with, matchingProposal.sessionId, null, System.currentTimeMillis());
392 mXmppConnectionService.sendMessagePacket(account, messagePacket);
393
394 }
395 }
396 }
397
398 public void proposeJingleRtpSession(final Account account, final Jid with, final Set<Media> media) {
399 synchronized (this.rtpSessionProposals) {
400 for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry : this.rtpSessionProposals.entrySet()) {
401 RtpSessionProposal proposal = entry.getKey();
402 if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
403 final DeviceDiscoveryState preexistingState = entry.getValue();
404 if (preexistingState != null && preexistingState != DeviceDiscoveryState.FAILED) {
405 mXmppConnectionService.notifyJingleRtpConnectionUpdate(
406 account,
407 with,
408 proposal.sessionId,
409 preexistingState.toEndUserState()
410 );
411 return;
412 }
413 }
414 }
415 if (isBusy()) {
416 throw new IllegalStateException("There is already a running RTP session. This should have been caught by the UI");
417 }
418 final RtpSessionProposal proposal = RtpSessionProposal.of(account, with.asBareJid(), media);
419 this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
420 mXmppConnectionService.notifyJingleRtpConnectionUpdate(
421 account,
422 proposal.with,
423 proposal.sessionId,
424 RtpEndUserState.FINDING_DEVICE
425 );
426 final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
427 Log.d(Config.LOGTAG, messagePacket.toString());
428 mXmppConnectionService.sendMessagePacket(account, messagePacket);
429 }
430 }
431
432 public void deliverIbbPacket(Account account, IqPacket packet) {
433 final String sid;
434 final Element payload;
435 if (packet.hasChild("open", Namespace.IBB)) {
436 payload = packet.findChild("open", Namespace.IBB);
437 sid = payload.getAttribute("sid");
438 } else if (packet.hasChild("data", Namespace.IBB)) {
439 payload = packet.findChild("data", Namespace.IBB);
440 sid = payload.getAttribute("sid");
441 } else if (packet.hasChild("close", Namespace.IBB)) {
442 payload = packet.findChild("close", Namespace.IBB);
443 sid = payload.getAttribute("sid");
444 } else {
445 payload = null;
446 sid = null;
447 }
448 if (sid != null) {
449 for (final AbstractJingleConnection connection : this.connections.values()) {
450 if (connection instanceof JingleFileTransferConnection) {
451 final JingleFileTransferConnection fileTransfer = (JingleFileTransferConnection) connection;
452 final JingleTransport transport = fileTransfer.getTransport();
453 if (transport instanceof JingleInBandTransport) {
454 final JingleInBandTransport inBandTransport = (JingleInBandTransport) transport;
455 if (inBandTransport.matches(account, sid)) {
456 inBandTransport.deliverPayload(packet, payload);
457 }
458 return;
459 }
460 }
461 }
462 }
463 Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
464 account.getXmppConnection().sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
465 }
466
467 public void notifyRebound() {
468 for (final AbstractJingleConnection connection : this.connections.values()) {
469 connection.notifyRebound();
470 }
471 }
472
473 public WeakReference<JingleRtpConnection> findJingleRtpConnection(Account account, Jid with, String sessionId) {
474 final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, Jid.ofEscaped(with), sessionId);
475 final AbstractJingleConnection connection = connections.get(id);
476 if (connection instanceof JingleRtpConnection) {
477 return new WeakReference<>((JingleRtpConnection) connection);
478 }
479 return null;
480 }
481
482 public void updateProposedSessionDiscovered(Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
483 synchronized (this.rtpSessionProposals) {
484 final RtpSessionProposal sessionProposal = getRtpSessionProposal(account, from.asBareJid(), sessionId);
485 final DeviceDiscoveryState currentState = sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
486 if (currentState == null) {
487 Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
488 return;
489 }
490 if (currentState == DeviceDiscoveryState.DISCOVERED) {
491 Log.d(Config.LOGTAG, "session proposal already at discovered. not going to fall back");
492 return;
493 }
494 this.rtpSessionProposals.put(sessionProposal, target);
495 mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, sessionProposal.with, sessionProposal.sessionId, target.toEndUserState());
496 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": flagging session " + sessionId + " as " + target);
497 }
498 }
499
500 public void rejectRtpSession(final String sessionId) {
501 for (final AbstractJingleConnection connection : this.connections.values()) {
502 if (connection.getId().sessionId.equals(sessionId)) {
503 if (connection instanceof JingleRtpConnection) {
504 ((JingleRtpConnection) connection).rejectCall();
505 }
506 }
507 }
508 }
509
510 public void endRtpSession(final String sessionId) {
511 for (final AbstractJingleConnection connection : this.connections.values()) {
512 if (connection.getId().sessionId.equals(sessionId)) {
513 if (connection instanceof JingleRtpConnection) {
514 ((JingleRtpConnection) connection).endCall();
515 }
516 }
517 }
518 }
519
520 public void failProceed(Account account, final Jid with, String sessionId) {
521 final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with, sessionId);
522 final AbstractJingleConnection existingJingleConnection = connections.get(id);
523 if (existingJingleConnection instanceof JingleRtpConnection) {
524 ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed();
525 }
526 }
527
528 void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
529 if (connections.containsValue(connection)) {
530 return;
531 }
532 throw new IllegalStateException("JingleConnection has not been registered with connection manager");
533 }
534
535 public void endSession(AbstractJingleConnection.Id id, final AbstractJingleConnection.State state) {
536 this.endedSessions.put(PersistableSessionId.of(id), state);
537 }
538
539 private static class PersistableSessionId {
540 private final Jid with;
541 private final String sessionId;
542
543 private PersistableSessionId(Jid with, String sessionId) {
544 this.with = with;
545 this.sessionId = sessionId;
546 }
547
548 public static PersistableSessionId of(AbstractJingleConnection.Id id) {
549 return new PersistableSessionId(id.with, id.sessionId);
550 }
551
552 @Override
553 public boolean equals(Object o) {
554 if (this == o) return true;
555 if (o == null || getClass() != o.getClass()) return false;
556 PersistableSessionId that = (PersistableSessionId) o;
557 return Objects.equal(with, that.with) &&
558 Objects.equal(sessionId, that.sessionId);
559 }
560
561 @Override
562 public int hashCode() {
563 return Objects.hashCode(with, sessionId);
564 }
565 }
566
567 public enum DeviceDiscoveryState {
568 SEARCHING, DISCOVERED, FAILED;
569
570 public RtpEndUserState toEndUserState() {
571 switch (this) {
572 case SEARCHING:
573 return RtpEndUserState.FINDING_DEVICE;
574 case DISCOVERED:
575 return RtpEndUserState.RINGING;
576 default:
577 return RtpEndUserState.CONNECTIVITY_ERROR;
578 }
579 }
580 }
581
582 public static class RtpSessionProposal {
583 public final Jid with;
584 public final String sessionId;
585 public final Set<Media> media;
586 private final Account account;
587
588 private RtpSessionProposal(Account account, Jid with, String sessionId) {
589 this(account, with, sessionId, Collections.emptySet());
590 }
591
592 private RtpSessionProposal(Account account, Jid with, String sessionId, Set<Media> media) {
593 this.account = account;
594 this.with = with;
595 this.sessionId = sessionId;
596 this.media = media;
597 }
598
599 public static RtpSessionProposal of(Account account, Jid with, Set<Media> media) {
600 return new RtpSessionProposal(account, with, nextRandomId(), media);
601 }
602
603 @Override
604 public boolean equals(Object o) {
605 if (this == o) return true;
606 if (o == null || getClass() != o.getClass()) return false;
607 RtpSessionProposal proposal = (RtpSessionProposal) o;
608 return Objects.equal(account.getJid(), proposal.account.getJid()) &&
609 Objects.equal(with, proposal.with) &&
610 Objects.equal(sessionId, proposal.sessionId);
611 }
612
613 @Override
614 public int hashCode() {
615 return Objects.hashCode(account.getJid(), with, sessionId);
616 }
617 }
618}