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