JingleConnectionManager.java

   1package eu.siacs.conversations.xmpp.jingle;
   2
   3import android.util.Base64;
   4import android.util.Log;
   5
   6import com.google.common.base.Objects;
   7import com.google.common.base.Optional;
   8import com.google.common.base.Preconditions;
   9import com.google.common.cache.Cache;
  10import com.google.common.cache.CacheBuilder;
  11import com.google.common.collect.Collections2;
  12import com.google.common.collect.ComparisonChain;
  13import com.google.common.collect.ImmutableSet;
  14
  15import java.lang.ref.WeakReference;
  16import java.security.SecureRandom;
  17import java.util.Collection;
  18import java.util.Collections;
  19import java.util.HashMap;
  20import java.util.List;
  21import java.util.Map;
  22import java.util.Set;
  23import java.util.concurrent.ConcurrentHashMap;
  24import java.util.concurrent.Executors;
  25import java.util.concurrent.ScheduledExecutorService;
  26import java.util.concurrent.ScheduledFuture;
  27import java.util.concurrent.TimeUnit;
  28
  29import eu.siacs.conversations.Config;
  30import eu.siacs.conversations.entities.Account;
  31import eu.siacs.conversations.entities.Contact;
  32import eu.siacs.conversations.entities.Conversation;
  33import eu.siacs.conversations.entities.Conversational;
  34import eu.siacs.conversations.entities.Message;
  35import eu.siacs.conversations.entities.RtpSessionStatus;
  36import eu.siacs.conversations.entities.Transferable;
  37import eu.siacs.conversations.services.AbstractConnectionManager;
  38import eu.siacs.conversations.services.XmppConnectionService;
  39import eu.siacs.conversations.xml.Element;
  40import eu.siacs.conversations.xml.Namespace;
  41import eu.siacs.conversations.xmpp.Jid;
  42import eu.siacs.conversations.xmpp.OnIqPacketReceived;
  43import eu.siacs.conversations.xmpp.XmppConnection;
  44import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
  45import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
  46import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
  47import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
  48import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
  49import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
  50import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
  51import eu.siacs.conversations.xmpp.stanzas.IqPacket;
  52import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
  53
  54public class JingleConnectionManager extends AbstractConnectionManager {
  55    static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE =
  56            Executors.newSingleThreadScheduledExecutor();
  57    final ToneManager toneManager;
  58    private final HashMap<RtpSessionProposal, DeviceDiscoveryState> rtpSessionProposals =
  59            new HashMap<>();
  60    private final ConcurrentHashMap<AbstractJingleConnection.Id, AbstractJingleConnection>
  61            connections = new ConcurrentHashMap<>();
  62
  63    private final Cache<PersistableSessionId, TerminatedRtpSession> terminatedSessions =
  64            CacheBuilder.newBuilder().expireAfterWrite(24, TimeUnit.HOURS).build();
  65
  66    private final HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
  67
  68    public JingleConnectionManager(XmppConnectionService service) {
  69        super(service);
  70        this.toneManager = new ToneManager(service);
  71    }
  72
  73    static String nextRandomId() {
  74        final byte[] id = new byte[16];
  75        new SecureRandom().nextBytes(id);
  76        return Base64.encodeToString(id, Base64.NO_WRAP | Base64.NO_PADDING);
  77    }
  78
  79    public void deliverPacket(final Account account, final JinglePacket packet) {
  80        final String sessionId = packet.getSessionId();
  81        if (sessionId == null) {
  82            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
  83            return;
  84        }
  85        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, packet);
  86        final AbstractJingleConnection existingJingleConnection = connections.get(id);
  87        if (existingJingleConnection != null) {
  88            existingJingleConnection.deliverPacket(packet);
  89        } else if (packet.getAction() == JinglePacket.Action.SESSION_INITIATE) {
  90            final Jid from = packet.getFrom();
  91            final Content content = packet.getJingleContent();
  92            final String descriptionNamespace =
  93                    content == null ? null : content.getDescriptionNamespace();
  94            final AbstractJingleConnection connection;
  95            if (FileTransferDescription.NAMESPACES.contains(descriptionNamespace)) {
  96                connection = new JingleFileTransferConnection(this, id, from);
  97            } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace)
  98                    && isUsingClearNet(account)) {
  99                final boolean sessionEnded =
 100                        this.terminatedSessions.asMap().containsKey(PersistableSessionId.of(id));
 101                final boolean stranger =
 102                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 103                if (isBusy() || sessionEnded || stranger) {
 104                    Log.d(
 105                            Config.LOGTAG,
 106                            id.account.getJid().asBareJid()
 107                                    + ": rejected session with "
 108                                    + id.with
 109                                    + " because busy. sessionEnded="
 110                                    + sessionEnded
 111                                    + ", stranger="
 112                                    + stranger);
 113                    mXmppConnectionService.sendIqPacket(
 114                            account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
 115                    final JinglePacket sessionTermination =
 116                            new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
 117                    sessionTermination.setTo(id.with);
 118                    sessionTermination.setReason(Reason.BUSY, null);
 119                    mXmppConnectionService.sendIqPacket(account, sessionTermination, null);
 120                    return;
 121                }
 122                connection = new JingleRtpConnection(this, id, from);
 123            } else {
 124                respondWithJingleError(
 125                        account, packet, "unsupported-info", "feature-not-implemented", "cancel");
 126                return;
 127            }
 128            connections.put(id, connection);
 129            mXmppConnectionService.updateConversationUi();
 130            connection.deliverPacket(packet);
 131        } else {
 132            Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
 133            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
 134        }
 135    }
 136
 137    private boolean isUsingClearNet(final Account account) {
 138        return !account.isOnion() && !mXmppConnectionService.useTorToConnect();
 139    }
 140
 141    public boolean isBusy() {
 142        if (mXmppConnectionService.isPhoneInCall()) {
 143            return true;
 144        }
 145        for (AbstractJingleConnection connection : this.connections.values()) {
 146            if (connection instanceof JingleRtpConnection) {
 147                if (((JingleRtpConnection) connection).isTerminated()) {
 148                    continue;
 149                }
 150                return true;
 151            }
 152        }
 153        synchronized (this.rtpSessionProposals) {
 154            return this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED)
 155                    || this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING)
 156                    || this.rtpSessionProposals.containsValue(
 157                            DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED);
 158        }
 159    }
 160
 161    public void notifyPhoneCallStarted() {
 162        for (AbstractJingleConnection connection : connections.values()) {
 163            if (connection instanceof JingleRtpConnection) {
 164                final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
 165                if (rtpConnection.isTerminated()) {
 166                    continue;
 167                }
 168                rtpConnection.notifyPhoneCall();
 169            }
 170        }
 171    }
 172
 173    private Optional<RtpSessionProposal> findMatchingSessionProposal(
 174            final Account account, final Jid with, final Set<Media> media) {
 175        synchronized (this.rtpSessionProposals) {
 176            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 177                    this.rtpSessionProposals.entrySet()) {
 178                final RtpSessionProposal proposal = entry.getKey();
 179                final DeviceDiscoveryState state = entry.getValue();
 180                final boolean openProposal =
 181                        state == DeviceDiscoveryState.DISCOVERED
 182                                || state == DeviceDiscoveryState.SEARCHING
 183                                || state == DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED;
 184                if (openProposal
 185                        && proposal.account == account
 186                        && proposal.with.equals(with.asBareJid())
 187                        && proposal.media.equals(media)) {
 188                    return Optional.of(proposal);
 189                }
 190            }
 191        }
 192        return Optional.absent();
 193    }
 194
 195    private boolean hasMatchingRtpSession(
 196            final Account account, final Jid with, final Set<Media> media) {
 197        for (AbstractJingleConnection connection : this.connections.values()) {
 198            if (connection instanceof JingleRtpConnection) {
 199                final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
 200                if (rtpConnection.isTerminated()) {
 201                    continue;
 202                }
 203                if (rtpConnection.getId().account == account
 204                        && rtpConnection.getId().with.asBareJid().equals(with.asBareJid())
 205                        && rtpConnection.getMedia().equals(media)) {
 206                    return true;
 207                }
 208            }
 209        }
 210        return false;
 211    }
 212
 213    private boolean isWithStrangerAndStrangerNotificationsAreOff(final Account account, Jid with) {
 214        final boolean notifyForStrangers =
 215                mXmppConnectionService.getNotificationService().notificationsFromStrangers();
 216        if (notifyForStrangers) {
 217            return false;
 218        }
 219        final Contact contact = account.getRoster().getContact(with);
 220        return !contact.showInContactList();
 221    }
 222
 223    ScheduledFuture<?> schedule(
 224            final Runnable runnable, final long delay, final TimeUnit timeUnit) {
 225        return SCHEDULED_EXECUTOR_SERVICE.schedule(runnable, delay, timeUnit);
 226    }
 227
 228    void respondWithJingleError(
 229            final Account account,
 230            final IqPacket original,
 231            String jingleCondition,
 232            String condition,
 233            String conditionType) {
 234        final IqPacket response = original.generateResponse(IqPacket.TYPE.ERROR);
 235        final Element error = response.addChild("error");
 236        error.setAttribute("type", conditionType);
 237        error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
 238        error.addChild(jingleCondition, Namespace.JINGLE_ERRORS);
 239        account.getXmppConnection().sendIqPacket(response, null);
 240    }
 241
 242    public void deliverMessage(
 243            final Account account,
 244            final Jid to,
 245            final Jid from,
 246            final Element message,
 247            String remoteMsgId,
 248            String serverMsgId,
 249            long timestamp) {
 250        Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
 251        final String sessionId = message.getAttribute("id");
 252        if (sessionId == null) {
 253            return;
 254        }
 255        if ("accept".equals(message.getName())) {
 256            for (AbstractJingleConnection connection : connections.values()) {
 257                if (connection instanceof JingleRtpConnection) {
 258                    final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
 259                    final AbstractJingleConnection.Id id = connection.getId();
 260                    if (id.account == account && id.sessionId.equals(sessionId)) {
 261                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 262                        return;
 263                    }
 264                }
 265            }
 266            return;
 267        }
 268        final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
 269        // XEP version 0.6.0 sends proceed, reject, ringing to bare jid
 270        final boolean addressedDirectly = to != null && to.equals(account.getJid());
 271        final AbstractJingleConnection.Id id;
 272        if (fromSelf) {
 273            if (to != null && to.isFullJid()) {
 274                id = AbstractJingleConnection.Id.of(account, to, sessionId);
 275            } else {
 276                return;
 277            }
 278        } else {
 279            id = AbstractJingleConnection.Id.of(account, from, sessionId);
 280        }
 281        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 282        if (existingJingleConnection != null) {
 283            if (existingJingleConnection instanceof JingleRtpConnection) {
 284                ((JingleRtpConnection) existingJingleConnection)
 285                        .deliveryMessage(from, message, serverMsgId, timestamp);
 286            } else {
 287                Log.d(
 288                        Config.LOGTAG,
 289                        account.getJid().asBareJid()
 290                                + ": "
 291                                + existingJingleConnection.getClass().getName()
 292                                + " does not support jingle messages");
 293            }
 294            return;
 295        }
 296
 297        if (fromSelf) {
 298            if ("proceed".equals(message.getName())) {
 299                final Conversation c =
 300                        mXmppConnectionService.findOrCreateConversation(
 301                                account, id.with, false, false);
 302                final Message previousBusy = c.findRtpSession(sessionId, Message.STATUS_RECEIVED);
 303                if (previousBusy != null) {
 304                    previousBusy.setBody(new RtpSessionStatus(true, 0).toString());
 305                    if (serverMsgId != null) {
 306                        previousBusy.setServerMsgId(serverMsgId);
 307                    }
 308                    previousBusy.setTime(timestamp);
 309                    mXmppConnectionService.updateMessage(previousBusy, true);
 310                    Log.d(
 311                            Config.LOGTAG,
 312                            id.account.getJid().asBareJid()
 313                                    + ": updated previous busy because call got picked up by another device");
 314                    return;
 315                }
 316            }
 317            // TODO handle reject for cases where we don’t have carbon copies (normally reject is to
 318            // be sent to own bare jid as well)
 319            Log.d(
 320                    Config.LOGTAG,
 321                    account.getJid().asBareJid() + ": ignore jingle message from self");
 322            return;
 323        }
 324
 325        if ("propose".equals(message.getName())) {
 326            final Propose propose = Propose.upgrade(message);
 327            final List<GenericDescription> descriptions = propose.getDescriptions();
 328            final Collection<RtpDescription> rtpDescriptions =
 329                    Collections2.transform(
 330                            Collections2.filter(descriptions, d -> d instanceof RtpDescription),
 331                            input -> (RtpDescription) input);
 332            if (rtpDescriptions.size() > 0
 333                    && rtpDescriptions.size() == descriptions.size()
 334                    && isUsingClearNet(account)) {
 335                final Collection<Media> media =
 336                        Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
 337                if (media.contains(Media.UNKNOWN)) {
 338                    Log.d(
 339                            Config.LOGTAG,
 340                            account.getJid().asBareJid()
 341                                    + ": encountered unknown media in session proposal. "
 342                                    + propose);
 343                    return;
 344                }
 345                final Optional<RtpSessionProposal> matchingSessionProposal =
 346                        findMatchingSessionProposal(account, id.with, ImmutableSet.copyOf(media));
 347                if (matchingSessionProposal.isPresent()) {
 348                    final String ourSessionId = matchingSessionProposal.get().sessionId;
 349                    final String theirSessionId = id.sessionId;
 350                    if (ComparisonChain.start()
 351                                    .compare(ourSessionId, theirSessionId)
 352                                    .compare(
 353                                            account.getJid().toEscapedString(),
 354                                            id.with.toEscapedString())
 355                                    .result()
 356                            > 0) {
 357                        Log.d(
 358                                Config.LOGTAG,
 359                                account.getJid().asBareJid()
 360                                        + ": our session lost tie break. automatically accepting their session. winning Session="
 361                                        + theirSessionId);
 362                        // TODO a retract for this reason should probably include some indication of
 363                        // tie break
 364                        retractSessionProposal(matchingSessionProposal.get());
 365                        final JingleRtpConnection rtpConnection =
 366                                new JingleRtpConnection(this, id, from);
 367                        this.connections.put(id, rtpConnection);
 368                        rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 369                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 370                        // TODO actually do the automatic accept?!
 371                    } else {
 372                        Log.d(
 373                                Config.LOGTAG,
 374                                account.getJid().asBareJid()
 375                                        + ": our session won tie break. waiting for other party to accept. winningSession="
 376                                        + ourSessionId);
 377                        // TODO reject their session with <tie-break/>?
 378                    }
 379                    return;
 380                }
 381                final boolean stranger =
 382                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 383                if (isBusy() || stranger) {
 384                    writeLogMissedIncoming(
 385                            account, id.with.asBareJid(), id.sessionId, serverMsgId, timestamp);
 386                    if (stranger) {
 387                        Log.d(
 388                                Config.LOGTAG,
 389                                id.account.getJid().asBareJid()
 390                                        + ": ignoring call proposal from stranger "
 391                                        + id.with);
 392                        return;
 393                    }
 394                    final int activeDevices = account.activeDevicesWithRtpCapability();
 395                    Log.d(Config.LOGTAG, "active devices with rtp capability: " + activeDevices);
 396                    if (activeDevices == 0) {
 397                        final MessagePacket reject =
 398                                mXmppConnectionService
 399                                        .getMessageGenerator()
 400                                        .sessionReject(from, sessionId);
 401                        mXmppConnectionService.sendMessagePacket(account, reject);
 402                    } else {
 403                        Log.d(
 404                                Config.LOGTAG,
 405                                id.account.getJid().asBareJid()
 406                                        + ": ignoring proposal because busy on this device but there are other devices");
 407                    }
 408                } else {
 409                    final JingleRtpConnection rtpConnection =
 410                            new JingleRtpConnection(this, id, from);
 411                    this.connections.put(id, rtpConnection);
 412                    rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 413                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 414                }
 415            } else {
 416                Log.d(
 417                        Config.LOGTAG,
 418                        account.getJid().asBareJid()
 419                                + ": unable to react to proposed session with "
 420                                + rtpDescriptions.size()
 421                                + " rtp descriptions of "
 422                                + descriptions.size()
 423                                + " total descriptions");
 424            }
 425        } else if (addressedDirectly && "proceed".equals(message.getName())) {
 426            synchronized (rtpSessionProposals) {
 427                final RtpSessionProposal proposal =
 428                        getRtpSessionProposal(account, from.asBareJid(), sessionId);
 429                if (proposal != null) {
 430                    rtpSessionProposals.remove(proposal);
 431                    final JingleRtpConnection rtpConnection =
 432                            new JingleRtpConnection(this, id, account.getJid());
 433                    rtpConnection.setProposedMedia(proposal.media);
 434                    this.connections.put(id, rtpConnection);
 435                    rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
 436                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 437                } else {
 438                    Log.d(
 439                            Config.LOGTAG,
 440                            account.getJid().asBareJid()
 441                                    + ": no rtp session proposal found for "
 442                                    + from
 443                                    + " to deliver proceed");
 444                    if (remoteMsgId == null) {
 445                        return;
 446                    }
 447                    final MessagePacket errorMessage = new MessagePacket();
 448                    errorMessage.setTo(from);
 449                    errorMessage.setId(remoteMsgId);
 450                    errorMessage.setType(MessagePacket.TYPE_ERROR);
 451                    final Element error = errorMessage.addChild("error");
 452                    error.setAttribute("code", "404");
 453                    error.setAttribute("type", "cancel");
 454                    error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
 455                    mXmppConnectionService.sendMessagePacket(account, errorMessage);
 456                }
 457            }
 458        } else if (addressedDirectly && "reject".equals(message.getName())) {
 459            final RtpSessionProposal proposal =
 460                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 461            synchronized (rtpSessionProposals) {
 462                if (proposal != null && rtpSessionProposals.remove(proposal) != null) {
 463                    writeLogMissedOutgoing(
 464                            account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
 465                    toneManager.transition(RtpEndUserState.DECLINED_OR_BUSY, proposal.media);
 466                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 467                            account,
 468                            proposal.with,
 469                            proposal.sessionId,
 470                            RtpEndUserState.DECLINED_OR_BUSY);
 471                } else {
 472                    Log.d(
 473                            Config.LOGTAG,
 474                            account.getJid().asBareJid()
 475                                    + ": no rtp session proposal found for "
 476                                    + from
 477                                    + " to deliver reject");
 478                }
 479            }
 480        } else if (addressedDirectly && "ringing".equals(message.getName())) {
 481            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + from + " started ringing");
 482            updateProposedSessionDiscovered(
 483                    account, from, sessionId, DeviceDiscoveryState.DISCOVERED);
 484        } else {
 485            Log.d(
 486                    Config.LOGTAG,
 487                    account.getJid().asBareJid()
 488                            + ": retrieved out of order jingle message from "
 489                            + from
 490                            + message
 491                            + ", addressedDirectly="
 492                            + addressedDirectly);
 493        }
 494    }
 495
 496    private RtpSessionProposal getRtpSessionProposal(
 497            final Account account, Jid from, String sessionId) {
 498        for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
 499            if (rtpSessionProposal.sessionId.equals(sessionId)
 500                    && rtpSessionProposal.with.equals(from)
 501                    && rtpSessionProposal.account.getJid().equals(account.getJid())) {
 502                return rtpSessionProposal;
 503            }
 504        }
 505        return null;
 506    }
 507
 508    private void writeLogMissedOutgoing(
 509            final Account account,
 510            Jid with,
 511            final String sessionId,
 512            String serverMsgId,
 513            long timestamp) {
 514        final Conversation conversation =
 515                mXmppConnectionService.findOrCreateConversation(
 516                        account, with.asBareJid(), false, false);
 517        final Message message =
 518                new Message(conversation, Message.STATUS_SEND, Message.TYPE_RTP_SESSION, sessionId);
 519        message.setBody(new RtpSessionStatus(false, 0).toString());
 520        message.setServerMsgId(serverMsgId);
 521        message.setTime(timestamp);
 522        writeMessage(message);
 523    }
 524
 525    private void writeLogMissedIncoming(
 526            final Account account,
 527            Jid with,
 528            final String sessionId,
 529            String serverMsgId,
 530            long timestamp) {
 531        final Conversation conversation =
 532                mXmppConnectionService.findOrCreateConversation(
 533                        account, with.asBareJid(), false, false);
 534        final Message message =
 535                new Message(
 536                        conversation, Message.STATUS_RECEIVED, Message.TYPE_RTP_SESSION, sessionId);
 537        message.setBody(new RtpSessionStatus(false, 0).toString());
 538        message.setServerMsgId(serverMsgId);
 539        message.setTime(timestamp);
 540        writeMessage(message);
 541    }
 542
 543    private void writeMessage(final Message message) {
 544        final Conversational conversational = message.getConversation();
 545        if (conversational instanceof Conversation) {
 546            ((Conversation) conversational).add(message);
 547            mXmppConnectionService.databaseBackend.createMessage(message);
 548            mXmppConnectionService.updateConversationUi();
 549        } else {
 550            throw new IllegalStateException("Somehow the conversation in a message was a stub");
 551        }
 552    }
 553
 554    public void startJingleFileTransfer(final Message message) {
 555        Preconditions.checkArgument(
 556                message.isFileOrImage(), "Message is not of type file or image");
 557        final Transferable old = message.getTransferable();
 558        if (old != null) {
 559            old.cancel();
 560        }
 561        final Account account = message.getConversation().getAccount();
 562        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message);
 563        final JingleFileTransferConnection connection =
 564                new JingleFileTransferConnection(this, id, account.getJid());
 565        mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
 566        this.connections.put(id, connection);
 567        connection.init(message);
 568    }
 569
 570    public Optional<OngoingRtpSession> getOngoingRtpConnection(final Contact contact) {
 571        for (final Map.Entry<AbstractJingleConnection.Id, AbstractJingleConnection> entry :
 572                this.connections.entrySet()) {
 573            if (entry.getValue() instanceof JingleRtpConnection) {
 574                final AbstractJingleConnection.Id id = entry.getKey();
 575                if (id.account == contact.getAccount()
 576                        && id.with.asBareJid().equals(contact.getJid().asBareJid())) {
 577                    return Optional.of(id);
 578                }
 579            }
 580        }
 581        synchronized (this.rtpSessionProposals) {
 582            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 583                    this.rtpSessionProposals.entrySet()) {
 584                final RtpSessionProposal proposal = entry.getKey();
 585                if (proposal.account == contact.getAccount()
 586                        && contact.getJid().asBareJid().equals(proposal.with)) {
 587                    final DeviceDiscoveryState preexistingState = entry.getValue();
 588                    if (preexistingState != null
 589                            && preexistingState != DeviceDiscoveryState.FAILED) {
 590                        return Optional.of(proposal);
 591                    }
 592                }
 593            }
 594        }
 595        return Optional.absent();
 596    }
 597
 598    void finishConnection(final AbstractJingleConnection connection) {
 599        this.connections.remove(connection.getId());
 600    }
 601
 602    void finishConnectionOrThrow(final AbstractJingleConnection connection) {
 603        final AbstractJingleConnection.Id id = connection.getId();
 604        if (this.connections.remove(id) == null) {
 605            throw new IllegalStateException(
 606                    String.format("Unable to finish connection with id=%s", id.toString()));
 607        }
 608    }
 609
 610    public boolean fireJingleRtpConnectionStateUpdates() {
 611        boolean firedUpdates = false;
 612        for (final AbstractJingleConnection connection : this.connections.values()) {
 613            if (connection instanceof JingleRtpConnection) {
 614                final JingleRtpConnection jingleRtpConnection = (JingleRtpConnection) connection;
 615                if (jingleRtpConnection.isTerminated()) {
 616                    continue;
 617                }
 618                jingleRtpConnection.fireStateUpdate();
 619                firedUpdates = true;
 620            }
 621        }
 622        return firedUpdates;
 623    }
 624
 625    void getPrimaryCandidate(
 626            final Account account,
 627            final boolean initiator,
 628            final OnPrimaryCandidateFound listener) {
 629        if (Config.DISABLE_PROXY_LOOKUP) {
 630            listener.onPrimaryCandidateFound(false, null);
 631            return;
 632        }
 633        if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
 634            final Jid proxy =
 635                    account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
 636            if (proxy != null) {
 637                IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
 638                iq.setTo(proxy);
 639                iq.query(Namespace.BYTE_STREAMS);
 640                account.getXmppConnection()
 641                        .sendIqPacket(
 642                                iq,
 643                                new OnIqPacketReceived() {
 644
 645                                    @Override
 646                                    public void onIqPacketReceived(
 647                                            Account account, IqPacket packet) {
 648                                        final Element streamhost =
 649                                                packet.query()
 650                                                        .findChild(
 651                                                                "streamhost",
 652                                                                Namespace.BYTE_STREAMS);
 653                                        final String host =
 654                                                streamhost == null
 655                                                        ? null
 656                                                        : streamhost.getAttribute("host");
 657                                        final String port =
 658                                                streamhost == null
 659                                                        ? null
 660                                                        : streamhost.getAttribute("port");
 661                                        if (host != null && port != null) {
 662                                            try {
 663                                                JingleCandidate candidate =
 664                                                        new JingleCandidate(nextRandomId(), true);
 665                                                candidate.setHost(host);
 666                                                candidate.setPort(Integer.parseInt(port));
 667                                                candidate.setType(JingleCandidate.TYPE_PROXY);
 668                                                candidate.setJid(proxy);
 669                                                candidate.setPriority(
 670                                                        655360 + (initiator ? 30 : 0));
 671                                                primaryCandidates.put(
 672                                                        account.getJid().asBareJid(), candidate);
 673                                                listener.onPrimaryCandidateFound(true, candidate);
 674                                            } catch (final NumberFormatException e) {
 675                                                listener.onPrimaryCandidateFound(false, null);
 676                                            }
 677                                        } else {
 678                                            listener.onPrimaryCandidateFound(false, null);
 679                                        }
 680                                    }
 681                                });
 682            } else {
 683                listener.onPrimaryCandidateFound(false, null);
 684            }
 685
 686        } else {
 687            listener.onPrimaryCandidateFound(
 688                    true, this.primaryCandidates.get(account.getJid().asBareJid()));
 689        }
 690    }
 691
 692    public void retractSessionProposal(final Account account, final Jid with) {
 693        synchronized (this.rtpSessionProposals) {
 694            RtpSessionProposal matchingProposal = null;
 695            for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
 696                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 697                    matchingProposal = proposal;
 698                    break;
 699                }
 700            }
 701            if (matchingProposal != null) {
 702                retractSessionProposal(matchingProposal);
 703            }
 704        }
 705    }
 706
 707    private void retractSessionProposal(RtpSessionProposal rtpSessionProposal) {
 708        final Account account = rtpSessionProposal.account;
 709        toneManager.transition(RtpEndUserState.ENDED, rtpSessionProposal.media);
 710        Log.d(
 711                Config.LOGTAG,
 712                account.getJid().asBareJid()
 713                        + ": retracting rtp session proposal with "
 714                        + rtpSessionProposal.with);
 715        this.rtpSessionProposals.remove(rtpSessionProposal);
 716        final MessagePacket messagePacket =
 717                mXmppConnectionService.getMessageGenerator().sessionRetract(rtpSessionProposal);
 718        writeLogMissedOutgoing(
 719                account,
 720                rtpSessionProposal.with,
 721                rtpSessionProposal.sessionId,
 722                null,
 723                System.currentTimeMillis());
 724        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 725    }
 726
 727    public String initializeRtpSession(
 728            final Account account, final Jid with, final Set<Media> media) {
 729        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with);
 730        final JingleRtpConnection rtpConnection =
 731                new JingleRtpConnection(this, id, account.getJid());
 732        rtpConnection.setProposedMedia(media);
 733        this.connections.put(id, rtpConnection);
 734        rtpConnection.sendSessionInitiate();
 735        return id.sessionId;
 736    }
 737
 738    public void proposeJingleRtpSession(
 739            final Account account, final Jid with, final Set<Media> media) {
 740        synchronized (this.rtpSessionProposals) {
 741            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 742                    this.rtpSessionProposals.entrySet()) {
 743                RtpSessionProposal proposal = entry.getKey();
 744                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 745                    final DeviceDiscoveryState preexistingState = entry.getValue();
 746                    if (preexistingState != null
 747                            && preexistingState != DeviceDiscoveryState.FAILED) {
 748                        final RtpEndUserState endUserState = preexistingState.toEndUserState();
 749                        toneManager.transition(endUserState, media);
 750                        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 751                                account, with, proposal.sessionId, endUserState);
 752                        return;
 753                    }
 754                }
 755            }
 756            if (isBusy()) {
 757                if (hasMatchingRtpSession(account, with, media)) {
 758                    Log.d(
 759                            Config.LOGTAG,
 760                            "ignoring request to propose jingle session because the other party already created one for us");
 761                    return;
 762                }
 763                throw new IllegalStateException(
 764                        "There is already a running RTP session. This should have been caught by the UI");
 765            }
 766            final RtpSessionProposal proposal =
 767                    RtpSessionProposal.of(account, with.asBareJid(), media);
 768            this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
 769            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 770                    account, proposal.with, proposal.sessionId, RtpEndUserState.FINDING_DEVICE);
 771            final MessagePacket messagePacket =
 772                    mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 773            mXmppConnectionService.sendMessagePacket(account, messagePacket);
 774        }
 775    }
 776
 777    public boolean hasMatchingProposal(final Account account, final Jid with) {
 778        synchronized (this.rtpSessionProposals) {
 779            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 780                    this.rtpSessionProposals.entrySet()) {
 781                final RtpSessionProposal proposal = entry.getKey();
 782                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 783                    return true;
 784                }
 785            }
 786        }
 787        return false;
 788    }
 789
 790    public void deliverIbbPacket(Account account, IqPacket packet) {
 791        final String sid;
 792        final Element payload;
 793        if (packet.hasChild("open", Namespace.IBB)) {
 794            payload = packet.findChild("open", Namespace.IBB);
 795            sid = payload.getAttribute("sid");
 796        } else if (packet.hasChild("data", Namespace.IBB)) {
 797            payload = packet.findChild("data", Namespace.IBB);
 798            sid = payload.getAttribute("sid");
 799        } else if (packet.hasChild("close", Namespace.IBB)) {
 800            payload = packet.findChild("close", Namespace.IBB);
 801            sid = payload.getAttribute("sid");
 802        } else {
 803            payload = null;
 804            sid = null;
 805        }
 806        if (sid != null) {
 807            for (final AbstractJingleConnection connection : this.connections.values()) {
 808                if (connection instanceof JingleFileTransferConnection) {
 809                    final JingleFileTransferConnection fileTransfer =
 810                            (JingleFileTransferConnection) connection;
 811                    final JingleTransport transport = fileTransfer.getTransport();
 812                    if (transport instanceof JingleInBandTransport) {
 813                        final JingleInBandTransport inBandTransport =
 814                                (JingleInBandTransport) transport;
 815                        if (inBandTransport.matches(account, sid)) {
 816                            inBandTransport.deliverPayload(packet, payload);
 817                        }
 818                        return;
 819                    }
 820                }
 821            }
 822        }
 823        Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
 824        account.getXmppConnection()
 825                .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
 826    }
 827
 828    public void notifyRebound(final Account account) {
 829        for (final AbstractJingleConnection connection : this.connections.values()) {
 830            connection.notifyRebound();
 831        }
 832        final XmppConnection xmppConnection = account.getXmppConnection();
 833        if (xmppConnection != null && xmppConnection.getFeatures().sm()) {
 834            resendSessionProposals(account);
 835        }
 836    }
 837
 838    public WeakReference<JingleRtpConnection> findJingleRtpConnection(
 839            Account account, Jid with, String sessionId) {
 840        final AbstractJingleConnection.Id id =
 841                AbstractJingleConnection.Id.of(account, with, sessionId);
 842        final AbstractJingleConnection connection = connections.get(id);
 843        if (connection instanceof JingleRtpConnection) {
 844            return new WeakReference<>((JingleRtpConnection) connection);
 845        }
 846        return null;
 847    }
 848
 849    private void resendSessionProposals(final Account account) {
 850        synchronized (this.rtpSessionProposals) {
 851            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 852                    this.rtpSessionProposals.entrySet()) {
 853                final RtpSessionProposal proposal = entry.getKey();
 854                if (entry.getValue() == DeviceDiscoveryState.SEARCHING
 855                        && proposal.account == account) {
 856                    Log.d(
 857                            Config.LOGTAG,
 858                            account.getJid().asBareJid()
 859                                    + ": resending session proposal to "
 860                                    + proposal.with);
 861                    final MessagePacket messagePacket =
 862                            mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 863                    mXmppConnectionService.sendMessagePacket(account, messagePacket);
 864                }
 865            }
 866        }
 867    }
 868
 869    public void updateProposedSessionDiscovered(
 870            Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
 871        synchronized (this.rtpSessionProposals) {
 872            final RtpSessionProposal sessionProposal =
 873                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 874            final DeviceDiscoveryState currentState =
 875                    sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
 876            if (currentState == null) {
 877                Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
 878                return;
 879            }
 880            if (currentState == DeviceDiscoveryState.DISCOVERED) {
 881                Log.d(
 882                        Config.LOGTAG,
 883                        "session proposal already at discovered. not going to fall back");
 884                return;
 885            }
 886            this.rtpSessionProposals.put(sessionProposal, target);
 887            final RtpEndUserState endUserState = target.toEndUserState();
 888            toneManager.transition(endUserState, sessionProposal.media);
 889            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 890                    account, sessionProposal.with, sessionProposal.sessionId, endUserState);
 891            Log.d(
 892                    Config.LOGTAG,
 893                    account.getJid().asBareJid()
 894                            + ": flagging session "
 895                            + sessionId
 896                            + " as "
 897                            + target);
 898        }
 899    }
 900
 901    public void rejectRtpSession(final String sessionId) {
 902        for (final AbstractJingleConnection connection : this.connections.values()) {
 903            if (connection.getId().sessionId.equals(sessionId)) {
 904                if (connection instanceof JingleRtpConnection) {
 905                    try {
 906                        ((JingleRtpConnection) connection).rejectCall();
 907                        return;
 908                    } catch (final IllegalStateException e) {
 909                        Log.w(
 910                                Config.LOGTAG,
 911                                "race condition on rejecting call from notification",
 912                                e);
 913                    }
 914                }
 915            }
 916        }
 917    }
 918
 919    public void endRtpSession(final String sessionId) {
 920        for (final AbstractJingleConnection connection : this.connections.values()) {
 921            if (connection.getId().sessionId.equals(sessionId)) {
 922                if (connection instanceof JingleRtpConnection) {
 923                    ((JingleRtpConnection) connection).endCall();
 924                }
 925            }
 926        }
 927    }
 928
 929    public void failProceed(Account account, final Jid with, final String sessionId, final String message) {
 930        final AbstractJingleConnection.Id id =
 931                AbstractJingleConnection.Id.of(account, with, sessionId);
 932        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 933        if (existingJingleConnection instanceof JingleRtpConnection) {
 934            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed(message);
 935        }
 936    }
 937
 938    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
 939        if (connections.containsValue(connection)) {
 940            return;
 941        }
 942        final IllegalStateException e =
 943                new IllegalStateException(
 944                        "JingleConnection has not been registered with connection manager");
 945        Log.e(Config.LOGTAG, "ensureConnectionIsRegistered() failed. Going to throw", e);
 946        throw e;
 947    }
 948
 949    void setTerminalSessionState(
 950            AbstractJingleConnection.Id id, final RtpEndUserState state, final Set<Media> media) {
 951        this.terminatedSessions.put(
 952                PersistableSessionId.of(id), new TerminatedRtpSession(state, media));
 953    }
 954
 955    public TerminatedRtpSession getTerminalSessionState(final Jid with, final String sessionId) {
 956        return this.terminatedSessions.getIfPresent(new PersistableSessionId(with, sessionId));
 957    }
 958
 959    private static class PersistableSessionId {
 960        private final Jid with;
 961        private final String sessionId;
 962
 963        private PersistableSessionId(Jid with, String sessionId) {
 964            this.with = with;
 965            this.sessionId = sessionId;
 966        }
 967
 968        public static PersistableSessionId of(AbstractJingleConnection.Id id) {
 969            return new PersistableSessionId(id.with, id.sessionId);
 970        }
 971
 972        @Override
 973        public boolean equals(Object o) {
 974            if (this == o) return true;
 975            if (o == null || getClass() != o.getClass()) return false;
 976            PersistableSessionId that = (PersistableSessionId) o;
 977            return Objects.equal(with, that.with) && Objects.equal(sessionId, that.sessionId);
 978        }
 979
 980        @Override
 981        public int hashCode() {
 982            return Objects.hashCode(with, sessionId);
 983        }
 984    }
 985
 986    public static class TerminatedRtpSession {
 987        public final RtpEndUserState state;
 988        public final Set<Media> media;
 989
 990        TerminatedRtpSession(RtpEndUserState state, Set<Media> media) {
 991            this.state = state;
 992            this.media = media;
 993        }
 994    }
 995
 996    public enum DeviceDiscoveryState {
 997        SEARCHING,
 998        SEARCHING_ACKNOWLEDGED,
 999        DISCOVERED,
1000        FAILED;
1001
1002        public RtpEndUserState toEndUserState() {
1003            switch (this) {
1004                case SEARCHING:
1005                case SEARCHING_ACKNOWLEDGED:
1006                    return RtpEndUserState.FINDING_DEVICE;
1007                case DISCOVERED:
1008                    return RtpEndUserState.RINGING;
1009                default:
1010                    return RtpEndUserState.CONNECTIVITY_ERROR;
1011            }
1012        }
1013    }
1014
1015    public static class RtpSessionProposal implements OngoingRtpSession {
1016        public final Jid with;
1017        public final String sessionId;
1018        public final Set<Media> media;
1019        private final Account account;
1020
1021        private RtpSessionProposal(Account account, Jid with, String sessionId) {
1022            this(account, with, sessionId, Collections.emptySet());
1023        }
1024
1025        private RtpSessionProposal(Account account, Jid with, String sessionId, Set<Media> media) {
1026            this.account = account;
1027            this.with = with;
1028            this.sessionId = sessionId;
1029            this.media = media;
1030        }
1031
1032        public static RtpSessionProposal of(Account account, Jid with, Set<Media> media) {
1033            return new RtpSessionProposal(account, with, nextRandomId(), media);
1034        }
1035
1036        @Override
1037        public boolean equals(Object o) {
1038            if (this == o) return true;
1039            if (o == null || getClass() != o.getClass()) return false;
1040            RtpSessionProposal proposal = (RtpSessionProposal) o;
1041            return Objects.equal(account.getJid(), proposal.account.getJid())
1042                    && Objects.equal(with, proposal.with)
1043                    && Objects.equal(sessionId, proposal.sessionId);
1044        }
1045
1046        @Override
1047        public int hashCode() {
1048            return Objects.hashCode(account.getJid(), with, sessionId);
1049        }
1050
1051        @Override
1052        public Account getAccount() {
1053            return account;
1054        }
1055
1056        @Override
1057        public Jid getWith() {
1058            return with;
1059        }
1060
1061        @Override
1062        public String getSessionId() {
1063            return sessionId;
1064        }
1065    }
1066}