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 abstract void notifyRebound();
40
41
42 public static class Id {
43 public final Account account;
44 public final Jid with;
45 public final String sessionId;
46
47 private Id(final Account account, final Jid with, final String sessionId) {
48 Preconditions.checkNotNull(with);
49 Preconditions.checkArgument(with.isFullJid());
50 this.account = account;
51 this.with = with;
52 this.sessionId = sessionId;
53 }
54
55 public static Id of(Account account, JinglePacket jinglePacket) {
56 return new Id(account, jinglePacket.getFrom(), jinglePacket.getSessionId());
57 }
58
59 public static Id of(Account account, Jid with, final String sessionId) {
60 return new Id(account, with, sessionId);
61 }
62
63 public static Id of(Message message) {
64 return new Id(
65 message.getConversation().getAccount(),
66 message.getCounterpart(),
67 JingleConnectionManager.nextRandomId()
68 );
69 }
70
71 @Override
72 public boolean equals(Object o) {
73 if (this == o) return true;
74 if (o == null || getClass() != o.getClass()) return false;
75 Id id = (Id) o;
76 return Objects.equal(account.getJid(), id.account.getJid()) &&
77 Objects.equal(with, id.with) &&
78 Objects.equal(sessionId, id.sessionId);
79 }
80
81 @Override
82 public int hashCode() {
83 return Objects.hashCode(account.getJid(), with, sessionId);
84 }
85 }
86
87
88 public enum State {
89 NULL, //default value; nothing has been sent or received yet
90 PROPOSED,
91 ACCEPTED,
92 PROCEED,
93 REJECTED,
94 RETRACTED,
95 SESSION_INITIALIZED, //equal to 'PENDING'
96 SESSION_INITIALIZED_PRE_APPROVED,
97 SESSION_ACCEPTED, //equal to 'ACTIVE'
98 TERMINATED_SUCCESS, //equal to 'ENDED' (after successful call) ui will just close
99 TERMINATED_DECLINED_OR_BUSY, //equal to 'ENDED' (after other party declined the call)
100 TERMINATED_CONNECTIVITY_ERROR, //equal to 'ENDED' (but after network failures; ui will display retry button)
101 TERMINATED_CANCEL_OR_TIMEOUT, //more or less the same as retracted; caller pressed end call before session was accepted
102 TERMINATED_APPLICATION_FAILURE
103 }
104}