AbstractJingleConnection.java

  1package eu.siacs.conversations.xmpp.jingle;
  2
  3import com.google.common.base.Objects;
  4import com.google.common.base.Preconditions;
  5
  6import eu.siacs.conversations.entities.Account;
  7import eu.siacs.conversations.entities.Message;
  8import eu.siacs.conversations.services.XmppConnectionService;
  9import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
 10import rocks.xmpp.addr.Jid;
 11
 12public abstract class AbstractJingleConnection {
 13
 14    public static final String JINGLE_MESSAGE_PROPOSE_ID_PREFIX = "jm-propose-";
 15    public static final String JINGLE_MESSAGE_PROCEED_ID_PREFIX = "jm-proceed-";
 16
 17    protected final JingleConnectionManager jingleConnectionManager;
 18    protected final XmppConnectionService xmppConnectionService;
 19    protected final Id id;
 20    protected final Jid initiator;
 21
 22    AbstractJingleConnection(final JingleConnectionManager jingleConnectionManager, final Id id, final Jid initiator) {
 23        this.jingleConnectionManager = jingleConnectionManager;
 24        this.xmppConnectionService = jingleConnectionManager.getXmppConnectionService();
 25        this.id = id;
 26        this.initiator = initiator;
 27    }
 28
 29    boolean isInitiator() {
 30        return initiator.equals(id.account.getJid());
 31    }
 32
 33    abstract void deliverPacket(JinglePacket jinglePacket);
 34
 35    public Id getId() {
 36        return id;
 37    }
 38
 39
 40    public static class Id {
 41        public final Account account;
 42        public final Jid with;
 43        public final String sessionId;
 44
 45        private Id(final Account account, final Jid with, final String sessionId) {
 46            Preconditions.checkNotNull(with);
 47            Preconditions.checkArgument(with.isFullJid());
 48            this.account = account;
 49            this.with = with;
 50            this.sessionId = sessionId;
 51        }
 52
 53        public static Id of(Account account, JinglePacket jinglePacket) {
 54            return new Id(account, jinglePacket.getFrom(), jinglePacket.getSessionId());
 55        }
 56
 57        public static Id of(Account account, Jid with, final String sessionId) {
 58            return new Id(account, with, sessionId);
 59        }
 60
 61        public static Id of(Message message) {
 62            return new Id(
 63                    message.getConversation().getAccount(),
 64                    message.getCounterpart(),
 65                    JingleConnectionManager.nextRandomId()
 66            );
 67        }
 68
 69        @Override
 70        public boolean equals(Object o) {
 71            if (this == o) return true;
 72            if (o == null || getClass() != o.getClass()) return false;
 73            Id id = (Id) o;
 74            return Objects.equal(account.getJid(), id.account.getJid()) &&
 75                    Objects.equal(with, id.with) &&
 76                    Objects.equal(sessionId, id.sessionId);
 77        }
 78
 79        @Override
 80        public int hashCode() {
 81            return Objects.hashCode(account.getJid(), with, sessionId);
 82        }
 83    }
 84
 85
 86    public enum State {
 87        NULL, //default value; nothing has been sent or received yet
 88        PROPOSED,
 89        ACCEPTED,
 90        PROCEED,
 91        REJECTED,
 92        RETRACTED,
 93        SESSION_INITIALIZED, //equal to 'PENDING'
 94        SESSION_INITIALIZED_PRE_APPROVED,
 95        SESSION_ACCEPTED, //equal to 'ACTIVE'
 96        TERMINATED_SUCCESS, //equal to 'ENDED' (after successful call) ui will just close
 97        TERMINATED_DECLINED_OR_BUSY, //equal to 'ENDED' (after other party declined the call)
 98        TERMINATED_CONNECTIVITY_ERROR, //equal to 'ENDED' (but after network failures; ui will display retry button)
 99        TERMINATED_CANCEL_OR_TIMEOUT, //more or less the same as retracted; caller pressed end call before session was accepted
100        TERMINATED_APPLICATION_FAILURE
101    }
102}