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    protected final JingleConnectionManager jingleConnectionManager;
15    protected final XmppConnectionService xmppConnectionService;
16    protected final Id id;
17
18    public AbstractJingleConnection(final JingleConnectionManager jingleConnectionManager, final Id id) {
19        this.jingleConnectionManager = jingleConnectionManager;
20        this.xmppConnectionService = jingleConnectionManager.getXmppConnectionService();
21        this.id = id;
22    }
23
24    abstract void deliverPacket(JinglePacket jinglePacket);
25
26    public Id getId() {
27        return id;
28    }
29
30
31    public static class Id {
32        public final Account account;
33        public final Jid with;
34        public final String sessionId;
35
36        private Id(final Account account, final Jid with, final String sessionId) {
37            Preconditions.checkNotNull(with);
38            Preconditions.checkArgument(with.isFullJid());
39            this.account = account;
40            this.with = with;
41            this.sessionId = sessionId;
42        }
43
44        public static Id of(Account account, JinglePacket jinglePacket) {
45            return new Id(account, jinglePacket.getFrom(), jinglePacket.getSessionId());
46        }
47
48        public static Id of(Account account, Jid with, final String sessionId) {
49            return new Id(account, with, sessionId);
50        }
51
52        public static Id of(Message message) {
53            return new Id(
54                    message.getConversation().getAccount(),
55                    message.getCounterpart(),
56                    JingleConnectionManager.nextRandomId()
57            );
58        }
59
60        @Override
61        public boolean equals(Object o) {
62            if (this == o) return true;
63            if (o == null || getClass() != o.getClass()) return false;
64            Id id = (Id) o;
65            return Objects.equal(account.getJid(), id.account.getJid()) &&
66                    Objects.equal(with, id.with) &&
67                    Objects.equal(sessionId, id.sessionId);
68        }
69
70        @Override
71        public int hashCode() {
72            return Objects.hashCode(account.getJid(), with, sessionId);
73        }
74    }
75
76
77    public enum State {
78        NULL, //default value; nothing has been sent or received yet
79        PROPOSED,
80        ACCEPTED,
81        PROCEED,
82        SESSION_INITIALIZED, //equal to 'PENDING'
83        SESSION_ACCEPTED, //equal to 'ACTIVE'
84        TERMINATED //equal to 'ENDED'
85    }
86}