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