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 ("
 442                                    + sessionId
 443                                    + ") proposal found for "
 444                                    + from
 445                                    + " to deliver proceed");
 446                    if (remoteMsgId == null) {
 447                        return;
 448                    }
 449                    final MessagePacket errorMessage = new MessagePacket();
 450                    errorMessage.setTo(from);
 451                    errorMessage.setId(remoteMsgId);
 452                    errorMessage.setType(MessagePacket.TYPE_ERROR);
 453                    final Element error = errorMessage.addChild("error");
 454                    error.setAttribute("code", "404");
 455                    error.setAttribute("type", "cancel");
 456                    error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
 457                    mXmppConnectionService.sendMessagePacket(account, errorMessage);
 458                }
 459            }
 460        } else if (addressedDirectly && "reject".equals(message.getName())) {
 461            final RtpSessionProposal proposal =
 462                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 463            synchronized (rtpSessionProposals) {
 464                if (proposal != null && rtpSessionProposals.remove(proposal) != null) {
 465                    writeLogMissedOutgoing(
 466                            account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
 467                    toneManager.transition(RtpEndUserState.DECLINED_OR_BUSY, proposal.media);
 468                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 469                            account,
 470                            proposal.with,
 471                            proposal.sessionId,
 472                            RtpEndUserState.DECLINED_OR_BUSY);
 473                } else {
 474                    Log.d(
 475                            Config.LOGTAG,
 476                            account.getJid().asBareJid()
 477                                    + ": no rtp session proposal found for "
 478                                    + from
 479                                    + " to deliver reject");
 480                }
 481            }
 482        } else if (addressedDirectly && "ringing".equals(message.getName())) {
 483            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + from + " started ringing");
 484            updateProposedSessionDiscovered(
 485                    account, from, sessionId, DeviceDiscoveryState.DISCOVERED);
 486        } else {
 487            Log.d(
 488                    Config.LOGTAG,
 489                    account.getJid()
 490                            + ": retrieved out of order jingle message from "
 491                            + from
 492                            + message
 493                            + ", addressedDirectly="
 494                            + addressedDirectly);
 495        }
 496    }
 497
 498    private RtpSessionProposal getRtpSessionProposal(
 499            final Account account, Jid from, String sessionId) {
 500        for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
 501            if (rtpSessionProposal.sessionId.equals(sessionId)
 502                    && rtpSessionProposal.with.equals(from)
 503                    && rtpSessionProposal.account.getJid().equals(account.getJid())) {
 504                return rtpSessionProposal;
 505            }
 506        }
 507        return null;
 508    }
 509
 510    private void writeLogMissedOutgoing(
 511            final Account account,
 512            Jid with,
 513            final String sessionId,
 514            String serverMsgId,
 515            long timestamp) {
 516        final Conversation conversation =
 517                mXmppConnectionService.findOrCreateConversation(
 518                        account, with.asBareJid(), false, false);
 519        final Message message =
 520                new Message(conversation, Message.STATUS_SEND, Message.TYPE_RTP_SESSION, sessionId);
 521        message.setBody(new RtpSessionStatus(false, 0).toString());
 522        message.setServerMsgId(serverMsgId);
 523        message.setTime(timestamp);
 524        writeMessage(message);
 525    }
 526
 527    private void writeLogMissedIncoming(
 528            final Account account,
 529            Jid with,
 530            final String sessionId,
 531            String serverMsgId,
 532            long timestamp) {
 533        final Conversation conversation =
 534                mXmppConnectionService.findOrCreateConversation(
 535                        account, with.asBareJid(), false, false);
 536        final Message message =
 537                new Message(
 538                        conversation, Message.STATUS_RECEIVED, Message.TYPE_RTP_SESSION, sessionId);
 539        message.setBody(new RtpSessionStatus(false, 0).toString());
 540        message.setServerMsgId(serverMsgId);
 541        message.setTime(timestamp);
 542        writeMessage(message);
 543    }
 544
 545    private void writeMessage(final Message message) {
 546        final Conversational conversational = message.getConversation();
 547        if (conversational instanceof Conversation) {
 548            ((Conversation) conversational).add(message);
 549            mXmppConnectionService.databaseBackend.createMessage(message);
 550            mXmppConnectionService.updateConversationUi();
 551        } else {
 552            throw new IllegalStateException("Somehow the conversation in a message was a stub");
 553        }
 554    }
 555
 556    public void startJingleFileTransfer(final Message message) {
 557        Preconditions.checkArgument(
 558                message.isFileOrImage(), "Message is not of type file or image");
 559        final Transferable old = message.getTransferable();
 560        if (old != null) {
 561            old.cancel();
 562        }
 563        final Account account = message.getConversation().getAccount();
 564        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message);
 565        final JingleFileTransferConnection connection =
 566                new JingleFileTransferConnection(this, id, account.getJid());
 567        mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
 568        this.connections.put(id, connection);
 569        connection.init(message);
 570    }
 571
 572    public Optional<OngoingRtpSession> getOngoingRtpConnection(final Contact contact) {
 573        for (final Map.Entry<AbstractJingleConnection.Id, AbstractJingleConnection> entry :
 574                this.connections.entrySet()) {
 575            if (entry.getValue() instanceof JingleRtpConnection) {
 576                final AbstractJingleConnection.Id id = entry.getKey();
 577                if (id.account == contact.getAccount()
 578                        && id.with.asBareJid().equals(contact.getJid().asBareJid())) {
 579                    return Optional.of(id);
 580                }
 581            }
 582        }
 583        synchronized (this.rtpSessionProposals) {
 584            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 585                    this.rtpSessionProposals.entrySet()) {
 586                final RtpSessionProposal proposal = entry.getKey();
 587                if (proposal.account == contact.getAccount()
 588                        && contact.getJid().asBareJid().equals(proposal.with)) {
 589                    final DeviceDiscoveryState preexistingState = entry.getValue();
 590                    if (preexistingState != null
 591                            && preexistingState != DeviceDiscoveryState.FAILED) {
 592                        return Optional.of(proposal);
 593                    }
 594                }
 595            }
 596        }
 597        return Optional.absent();
 598    }
 599
 600    void finishConnection(final AbstractJingleConnection connection) {
 601        this.connections.remove(connection.getId());
 602    }
 603
 604    void finishConnectionOrThrow(final AbstractJingleConnection connection) {
 605        final AbstractJingleConnection.Id id = connection.getId();
 606        if (this.connections.remove(id) == null) {
 607            throw new IllegalStateException(
 608                    String.format("Unable to finish connection with id=%s", id.toString()));
 609        }
 610    }
 611
 612    public boolean fireJingleRtpConnectionStateUpdates() {
 613        boolean firedUpdates = false;
 614        for (final AbstractJingleConnection connection : this.connections.values()) {
 615            if (connection instanceof JingleRtpConnection) {
 616                final JingleRtpConnection jingleRtpConnection = (JingleRtpConnection) connection;
 617                if (jingleRtpConnection.isTerminated()) {
 618                    continue;
 619                }
 620                jingleRtpConnection.fireStateUpdate();
 621                firedUpdates = true;
 622            }
 623        }
 624        return firedUpdates;
 625    }
 626
 627    void getPrimaryCandidate(
 628            final Account account,
 629            final boolean initiator,
 630            final OnPrimaryCandidateFound listener) {
 631        if (Config.DISABLE_PROXY_LOOKUP) {
 632            listener.onPrimaryCandidateFound(false, null);
 633            return;
 634        }
 635        if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
 636            final Jid proxy =
 637                    account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
 638            if (proxy != null) {
 639                IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
 640                iq.setTo(proxy);
 641                iq.query(Namespace.BYTE_STREAMS);
 642                account.getXmppConnection()
 643                        .sendIqPacket(
 644                                iq,
 645                                new OnIqPacketReceived() {
 646
 647                                    @Override
 648                                    public void onIqPacketReceived(
 649                                            Account account, IqPacket packet) {
 650                                        final Element streamhost =
 651                                                packet.query()
 652                                                        .findChild(
 653                                                                "streamhost",
 654                                                                Namespace.BYTE_STREAMS);
 655                                        final String host =
 656                                                streamhost == null
 657                                                        ? null
 658                                                        : streamhost.getAttribute("host");
 659                                        final String port =
 660                                                streamhost == null
 661                                                        ? null
 662                                                        : streamhost.getAttribute("port");
 663                                        if (host != null && port != null) {
 664                                            try {
 665                                                JingleCandidate candidate =
 666                                                        new JingleCandidate(nextRandomId(), true);
 667                                                candidate.setHost(host);
 668                                                candidate.setPort(Integer.parseInt(port));
 669                                                candidate.setType(JingleCandidate.TYPE_PROXY);
 670                                                candidate.setJid(proxy);
 671                                                candidate.setPriority(
 672                                                        655360 + (initiator ? 30 : 0));
 673                                                primaryCandidates.put(
 674                                                        account.getJid().asBareJid(), candidate);
 675                                                listener.onPrimaryCandidateFound(true, candidate);
 676                                            } catch (final NumberFormatException e) {
 677                                                listener.onPrimaryCandidateFound(false, null);
 678                                            }
 679                                        } else {
 680                                            listener.onPrimaryCandidateFound(false, null);
 681                                        }
 682                                    }
 683                                });
 684            } else {
 685                listener.onPrimaryCandidateFound(false, null);
 686            }
 687
 688        } else {
 689            listener.onPrimaryCandidateFound(
 690                    true, this.primaryCandidates.get(account.getJid().asBareJid()));
 691        }
 692    }
 693
 694    public void retractSessionProposal(final Account account, final Jid with) {
 695        synchronized (this.rtpSessionProposals) {
 696            RtpSessionProposal matchingProposal = null;
 697            for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
 698                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 699                    matchingProposal = proposal;
 700                    break;
 701                }
 702            }
 703            if (matchingProposal != null) {
 704                retractSessionProposal(matchingProposal);
 705            }
 706        }
 707    }
 708
 709    private void retractSessionProposal(RtpSessionProposal rtpSessionProposal) {
 710        final Account account = rtpSessionProposal.account;
 711        toneManager.transition(RtpEndUserState.ENDED, rtpSessionProposal.media);
 712        Log.d(
 713                Config.LOGTAG,
 714                account.getJid().asBareJid()
 715                        + ": retracting rtp session proposal with "
 716                        + rtpSessionProposal.with);
 717        this.rtpSessionProposals.remove(rtpSessionProposal);
 718        final MessagePacket messagePacket =
 719                mXmppConnectionService.getMessageGenerator().sessionRetract(rtpSessionProposal);
 720        writeLogMissedOutgoing(
 721                account,
 722                rtpSessionProposal.with,
 723                rtpSessionProposal.sessionId,
 724                null,
 725                System.currentTimeMillis());
 726        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 727    }
 728
 729    public String initializeRtpSession(
 730            final Account account, final Jid with, final Set<Media> media) {
 731        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with);
 732        final JingleRtpConnection rtpConnection =
 733                new JingleRtpConnection(this, id, account.getJid());
 734        rtpConnection.setProposedMedia(media);
 735        this.connections.put(id, rtpConnection);
 736        rtpConnection.sendSessionInitiate();
 737        return id.sessionId;
 738    }
 739
 740    public void proposeJingleRtpSession(
 741            final Account account, final Jid with, final Set<Media> media) {
 742        synchronized (this.rtpSessionProposals) {
 743            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 744                    this.rtpSessionProposals.entrySet()) {
 745                RtpSessionProposal proposal = entry.getKey();
 746                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 747                    final DeviceDiscoveryState preexistingState = entry.getValue();
 748                    if (preexistingState != null
 749                            && preexistingState != DeviceDiscoveryState.FAILED) {
 750                        final RtpEndUserState endUserState = preexistingState.toEndUserState();
 751                        toneManager.transition(endUserState, media);
 752                        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 753                                account, with, proposal.sessionId, endUserState);
 754                        return;
 755                    }
 756                }
 757            }
 758            if (isBusy()) {
 759                if (hasMatchingRtpSession(account, with, media)) {
 760                    Log.d(
 761                            Config.LOGTAG,
 762                            "ignoring request to propose jingle session because the other party already created one for us");
 763                    return;
 764                }
 765                throw new IllegalStateException(
 766                        "There is already a running RTP session. This should have been caught by the UI");
 767            }
 768            final RtpSessionProposal proposal =
 769                    RtpSessionProposal.of(account, with.asBareJid(), media);
 770            this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
 771            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 772                    account, proposal.with, proposal.sessionId, RtpEndUserState.FINDING_DEVICE);
 773            final MessagePacket messagePacket =
 774                    mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 775            mXmppConnectionService.sendMessagePacket(account, messagePacket);
 776        }
 777    }
 778
 779    public boolean hasMatchingProposal(final Account account, final Jid with) {
 780        synchronized (this.rtpSessionProposals) {
 781            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 782                    this.rtpSessionProposals.entrySet()) {
 783                final RtpSessionProposal proposal = entry.getKey();
 784                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 785                    return true;
 786                }
 787            }
 788        }
 789        return false;
 790    }
 791
 792    public void deliverIbbPacket(Account account, IqPacket packet) {
 793        final String sid;
 794        final Element payload;
 795        if (packet.hasChild("open", Namespace.IBB)) {
 796            payload = packet.findChild("open", Namespace.IBB);
 797            sid = payload.getAttribute("sid");
 798        } else if (packet.hasChild("data", Namespace.IBB)) {
 799            payload = packet.findChild("data", Namespace.IBB);
 800            sid = payload.getAttribute("sid");
 801        } else if (packet.hasChild("close", Namespace.IBB)) {
 802            payload = packet.findChild("close", Namespace.IBB);
 803            sid = payload.getAttribute("sid");
 804        } else {
 805            payload = null;
 806            sid = null;
 807        }
 808        if (sid != null) {
 809            for (final AbstractJingleConnection connection : this.connections.values()) {
 810                if (connection instanceof JingleFileTransferConnection) {
 811                    final JingleFileTransferConnection fileTransfer =
 812                            (JingleFileTransferConnection) connection;
 813                    final JingleTransport transport = fileTransfer.getTransport();
 814                    if (transport instanceof JingleInBandTransport) {
 815                        final JingleInBandTransport inBandTransport =
 816                                (JingleInBandTransport) transport;
 817                        if (inBandTransport.matches(account, sid)) {
 818                            inBandTransport.deliverPayload(packet, payload);
 819                        }
 820                        return;
 821                    }
 822                }
 823            }
 824        }
 825        Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
 826        account.getXmppConnection()
 827                .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
 828    }
 829
 830    public void notifyRebound(final Account account) {
 831        for (final AbstractJingleConnection connection : this.connections.values()) {
 832            connection.notifyRebound();
 833        }
 834        final XmppConnection xmppConnection = account.getXmppConnection();
 835        if (xmppConnection != null && xmppConnection.getFeatures().sm()) {
 836            resendSessionProposals(account);
 837        }
 838    }
 839
 840    public WeakReference<JingleRtpConnection> findJingleRtpConnection(
 841            Account account, Jid with, String sessionId) {
 842        final AbstractJingleConnection.Id id =
 843                AbstractJingleConnection.Id.of(account, with, sessionId);
 844        final AbstractJingleConnection connection = connections.get(id);
 845        if (connection instanceof JingleRtpConnection) {
 846            return new WeakReference<>((JingleRtpConnection) connection);
 847        }
 848        return null;
 849    }
 850
 851    private void resendSessionProposals(final Account account) {
 852        synchronized (this.rtpSessionProposals) {
 853            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 854                    this.rtpSessionProposals.entrySet()) {
 855                final RtpSessionProposal proposal = entry.getKey();
 856                if (entry.getValue() == DeviceDiscoveryState.SEARCHING
 857                        && proposal.account == account) {
 858                    Log.d(
 859                            Config.LOGTAG,
 860                            account.getJid().asBareJid()
 861                                    + ": resending session proposal to "
 862                                    + proposal.with);
 863                    final MessagePacket messagePacket =
 864                            mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 865                    mXmppConnectionService.sendMessagePacket(account, messagePacket);
 866                }
 867            }
 868        }
 869    }
 870
 871    public void updateProposedSessionDiscovered(
 872            Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
 873        synchronized (this.rtpSessionProposals) {
 874            final RtpSessionProposal sessionProposal =
 875                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 876            final DeviceDiscoveryState currentState =
 877                    sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
 878            if (currentState == null) {
 879                Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
 880                return;
 881            }
 882            if (currentState == DeviceDiscoveryState.DISCOVERED) {
 883                Log.d(
 884                        Config.LOGTAG,
 885                        "session proposal already at discovered. not going to fall back");
 886                return;
 887            }
 888            this.rtpSessionProposals.put(sessionProposal, target);
 889            final RtpEndUserState endUserState = target.toEndUserState();
 890            toneManager.transition(endUserState, sessionProposal.media);
 891            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 892                    account, sessionProposal.with, sessionProposal.sessionId, endUserState);
 893            Log.d(
 894                    Config.LOGTAG,
 895                    account.getJid().asBareJid()
 896                            + ": flagging session "
 897                            + sessionId
 898                            + " as "
 899                            + target);
 900        }
 901    }
 902
 903    public void rejectRtpSession(final String sessionId) {
 904        for (final AbstractJingleConnection connection : this.connections.values()) {
 905            if (connection.getId().sessionId.equals(sessionId)) {
 906                if (connection instanceof JingleRtpConnection) {
 907                    try {
 908                        ((JingleRtpConnection) connection).rejectCall();
 909                        return;
 910                    } catch (final IllegalStateException e) {
 911                        Log.w(
 912                                Config.LOGTAG,
 913                                "race condition on rejecting call from notification",
 914                                e);
 915                    }
 916                }
 917            }
 918        }
 919    }
 920
 921    public void endRtpSession(final String sessionId) {
 922        for (final AbstractJingleConnection connection : this.connections.values()) {
 923            if (connection.getId().sessionId.equals(sessionId)) {
 924                if (connection instanceof JingleRtpConnection) {
 925                    ((JingleRtpConnection) connection).endCall();
 926                }
 927            }
 928        }
 929    }
 930
 931    public void failProceed(Account account, final Jid with, final String sessionId, final String message) {
 932        final AbstractJingleConnection.Id id =
 933                AbstractJingleConnection.Id.of(account, with, sessionId);
 934        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 935        if (existingJingleConnection instanceof JingleRtpConnection) {
 936            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed(message);
 937        }
 938    }
 939
 940    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
 941        if (connections.containsValue(connection)) {
 942            return;
 943        }
 944        final IllegalStateException e =
 945                new IllegalStateException(
 946                        "JingleConnection has not been registered with connection manager");
 947        Log.e(Config.LOGTAG, "ensureConnectionIsRegistered() failed. Going to throw", e);
 948        throw e;
 949    }
 950
 951    void setTerminalSessionState(
 952            AbstractJingleConnection.Id id, final RtpEndUserState state, final Set<Media> media) {
 953        this.terminatedSessions.put(
 954                PersistableSessionId.of(id), new TerminatedRtpSession(state, media));
 955    }
 956
 957    public TerminatedRtpSession getTerminalSessionState(final Jid with, final String sessionId) {
 958        return this.terminatedSessions.getIfPresent(new PersistableSessionId(with, sessionId));
 959    }
 960
 961    private static class PersistableSessionId {
 962        private final Jid with;
 963        private final String sessionId;
 964
 965        private PersistableSessionId(Jid with, String sessionId) {
 966            this.with = with;
 967            this.sessionId = sessionId;
 968        }
 969
 970        public static PersistableSessionId of(AbstractJingleConnection.Id id) {
 971            return new PersistableSessionId(id.with, id.sessionId);
 972        }
 973
 974        @Override
 975        public boolean equals(Object o) {
 976            if (this == o) return true;
 977            if (o == null || getClass() != o.getClass()) return false;
 978            PersistableSessionId that = (PersistableSessionId) o;
 979            return Objects.equal(with, that.with) && Objects.equal(sessionId, that.sessionId);
 980        }
 981
 982        @Override
 983        public int hashCode() {
 984            return Objects.hashCode(with, sessionId);
 985        }
 986    }
 987
 988    public static class TerminatedRtpSession {
 989        public final RtpEndUserState state;
 990        public final Set<Media> media;
 991
 992        TerminatedRtpSession(RtpEndUserState state, Set<Media> media) {
 993            this.state = state;
 994            this.media = media;
 995        }
 996    }
 997
 998    public enum DeviceDiscoveryState {
 999        SEARCHING,
1000        SEARCHING_ACKNOWLEDGED,
1001        DISCOVERED,
1002        FAILED;
1003
1004        public RtpEndUserState toEndUserState() {
1005            switch (this) {
1006                case SEARCHING:
1007                case SEARCHING_ACKNOWLEDGED:
1008                    return RtpEndUserState.FINDING_DEVICE;
1009                case DISCOVERED:
1010                    return RtpEndUserState.RINGING;
1011                default:
1012                    return RtpEndUserState.CONNECTIVITY_ERROR;
1013            }
1014        }
1015    }
1016
1017    public static class RtpSessionProposal implements OngoingRtpSession {
1018        public final Jid with;
1019        public final String sessionId;
1020        public final Set<Media> media;
1021        private final Account account;
1022
1023        private RtpSessionProposal(Account account, Jid with, String sessionId) {
1024            this(account, with, sessionId, Collections.emptySet());
1025        }
1026
1027        private RtpSessionProposal(Account account, Jid with, String sessionId, Set<Media> media) {
1028            this.account = account;
1029            this.with = with;
1030            this.sessionId = sessionId;
1031            this.media = media;
1032        }
1033
1034        public static RtpSessionProposal of(Account account, Jid with, Set<Media> media) {
1035            return new RtpSessionProposal(account, with, nextRandomId(), media);
1036        }
1037
1038        @Override
1039        public boolean equals(Object o) {
1040            if (this == o) return true;
1041            if (o == null || getClass() != o.getClass()) return false;
1042            RtpSessionProposal proposal = (RtpSessionProposal) o;
1043            return Objects.equal(account.getJid(), proposal.account.getJid())
1044                    && Objects.equal(with, proposal.with)
1045                    && Objects.equal(sessionId, proposal.sessionId);
1046        }
1047
1048        @Override
1049        public int hashCode() {
1050            return Objects.hashCode(account.getJid(), with, sessionId);
1051        }
1052
1053        @Override
1054        public Account getAccount() {
1055            return account;
1056        }
1057
1058        @Override
1059        public Jid getWith() {
1060            return with;
1061        }
1062
1063        @Override
1064        public String getSessionId() {
1065            return sessionId;
1066        }
1067    }
1068}