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(Account account, Jid with) {
66 return new Id(account, with, JingleConnectionManager.nextRandomId());
67 }
68
69 public static Id of(Message message) {
70 return new Id(
71 message.getConversation().getAccount(),
72 message.getCounterpart(),
73 JingleConnectionManager.nextRandomId()
74 );
75 }
76
77 public Contact getContact() {
78 return account.getRoster().getContact(with);
79 }
80
81 @Override
82 public boolean equals(Object o) {
83 if (this == o) return true;
84 if (o == null || getClass() != o.getClass()) return false;
85 Id id = (Id) o;
86 return Objects.equal(account.getUuid(), id.account.getUuid()) &&
87 Objects.equal(with, id.with) &&
88 Objects.equal(sessionId, id.sessionId);
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hashCode(account.getUuid(), with, sessionId);
94 }
95
96 @Override
97 public Account getAccount() {
98 return account;
99 }
100
101 @Override
102 public Jid getWith() {
103 return with;
104 }
105
106 @Override
107 public String getSessionId() {
108 return sessionId;
109 }
110
111 @Override
112 public String toString() {
113 return MoreObjects.toStringHelper(this)
114 .add("account", account.getJid())
115 .add("with", with)
116 .add("sessionId", sessionId)
117 .toString();
118 }
119 }
120
121
122 public enum State {
123 NULL, //default value; nothing has been sent or received yet
124 PROPOSED,
125 ACCEPTED,
126 PROCEED,
127 REJECTED,
128 RETRACTED,
129 RETRACTED_RACED, //used when receiving a retract after we already asked to proceed
130 SESSION_INITIALIZED, //equal to 'PENDING'
131 SESSION_INITIALIZED_PRE_APPROVED,
132 SESSION_ACCEPTED, //equal to 'ACTIVE'
133 TERMINATED_SUCCESS, //equal to 'ENDED' (after successful call) ui will just close
134 TERMINATED_DECLINED_OR_BUSY, //equal to 'ENDED' (after other party declined the call)
135 TERMINATED_CONNECTIVITY_ERROR, //equal to 'ENDED' (but after network failures; ui will display retry button)
136 TERMINATED_CANCEL_OR_TIMEOUT, //more or less the same as retracted; caller pressed end call before session was accepted
137 TERMINATED_APPLICATION_FAILURE
138 }
139}