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 counterPart;
34        public final String sessionId;
35
36        private Id(final Account account, final Jid counterPart, final String sessionId) {
37            Preconditions.checkNotNull(counterPart);
38            Preconditions.checkArgument(counterPart.isFullJid());
39            this.account = account;
40            this.counterPart = counterPart;
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(Message message) {
49            return new Id(
50                    message.getConversation().getAccount(),
51                    message.getCounterpart(),
52                    JingleConnectionManager.nextRandomId()
53            );
54        }
55
56        @Override
57        public boolean equals(Object o) {
58            if (this == o) return true;
59            if (o == null || getClass() != o.getClass()) return false;
60            Id id = (Id) o;
61            return Objects.equal(account.getJid(), id.account.getJid()) &&
62                    Objects.equal(counterPart, id.counterPart) &&
63                    Objects.equal(sessionId, id.sessionId);
64        }
65
66        @Override
67        public int hashCode() {
68            return Objects.hashCode(account.getJid(), counterPart, sessionId);
69        }
70    }
71}