1package eu.siacs.conversations.xmpp.jingle;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.HashMap;
6import java.util.List;
7
8import android.util.Log;
9
10import eu.siacs.conversations.entities.Account;
11import eu.siacs.conversations.entities.Message;
12import eu.siacs.conversations.services.XmppConnectionService;
13import eu.siacs.conversations.xml.Element;
14import eu.siacs.conversations.xmpp.OnIqPacketReceived;
15import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
16import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
17import eu.siacs.conversations.xmpp.stanzas.IqPacket;
18
19public class JingleConnection {
20
21 private JingleConnectionManager mJingleConnectionManager;
22 private XmppConnectionService mXmppConnectionService;
23
24 public static final int STATUS_INITIATED = 0;
25 public static final int STATUS_ACCEPTED = 1;
26 public static final int STATUS_TERMINATED = 2;
27 public static final int STATUS_FAILED = 99;
28
29 private int status = -1;
30 private Message message;
31 private String sessionId;
32 private Account account;
33 private String initiator;
34 private String responder;
35 private List<Element> candidates = new ArrayList<Element>();
36 private HashMap<String, SocksConnection> connections = new HashMap<String, SocksConnection>();
37 private File file = null;
38
39 private OnIqPacketReceived responseListener = new OnIqPacketReceived() {
40
41 @Override
42 public void onIqPacketReceived(Account account, IqPacket packet) {
43 if (packet.getType() == IqPacket.TYPE_ERROR) {
44 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
45 status = STATUS_FAILED;
46 }
47 }
48 };
49
50 public JingleConnection(JingleConnectionManager mJingleConnectionManager) {
51 this.mJingleConnectionManager = mJingleConnectionManager;
52 this.mXmppConnectionService = mJingleConnectionManager.getXmppConnectionService();
53 this.sessionId = this.mJingleConnectionManager.nextRandomId();
54 }
55
56 public String getSessionId() {
57 return this.sessionId;
58 }
59
60 public String getAccountJid() {
61 return this.account.getJid();
62 }
63
64 public String getCounterPart() {
65 return this.message.getCounterpart();
66 }
67
68 public void deliverPacket(JinglePacket packet) {
69
70 if (packet.isAction("session-terminate")) {
71 if (status == STATUS_INITIATED) {
72 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_REJECTED);
73 }
74 status = STATUS_TERMINATED;
75 } else if (packet.isAction("session-accept")) {
76 accept(packet);
77 } else if (packet.isAction("transport-info")) {
78 transportInfo(packet);
79 } else {
80 Log.d("xmppService","packet arrived in connection. action was "+packet.getAction());
81 }
82 }
83
84 public void init(Message message) {
85 this.message = message;
86 this.account = message.getConversation().getAccount();
87 this.initiator = this.account.getFullJid();
88 this.responder = this.message.getCounterpart();
89 if (this.candidates.size() > 0) {
90 this.sendInitRequest();
91 } else {
92 this.mJingleConnectionManager.getPrimaryCandidate(account, new OnPrimaryCandidateFound() {
93
94 @Override
95 public void onPrimaryCandidateFound(boolean success, Element canditate) {
96 if (success) {
97 candidates.add(canditate);
98 }
99 sendInitRequest();
100 }
101 });
102 }
103
104 }
105
106 private void sendInitRequest() {
107 JinglePacket packet = this.bootstrapPacket();
108 packet.setAction("session-initiate");
109 packet.setInitiator(this.account.getFullJid());
110 Content content = new Content();
111 if (message.getType() == Message.TYPE_IMAGE) {
112 content.setAttribute("creator", "initiator");
113 content.setAttribute("name", "a-file-offer");
114 this.file = this.mXmppConnectionService.getFileBackend().getImageFile(message);
115 content.offerFile(file,message.getBody());
116 content.setCandidates(this.mJingleConnectionManager.nextRandomId(),this.candidates);
117 packet.setContent(content);
118 Log.d("xmppService",packet.toString());
119 account.getXmppConnection().sendIqPacket(packet, this.responseListener);
120 this.status = STATUS_INITIATED;
121 }
122 }
123
124 private JinglePacket bootstrapPacket() {
125 JinglePacket packet = new JinglePacket();
126 packet.setFrom(account.getFullJid());
127 packet.setTo(this.message.getCounterpart()); //fixme, not right in all cases;
128 packet.setSessionId(this.sessionId);
129 return packet;
130 }
131
132 private void accept(JinglePacket packet) {
133 Log.d("xmppService","session-accept: "+packet.toString());
134 Content content = packet.getJingleContent();
135 this.candidates.addAll(content.getCanditates());
136 this.status = STATUS_ACCEPTED;
137 this.connectWithCandidates();
138 IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
139 Log.d("xmppService","response "+response.toString());
140 account.getXmppConnection().sendIqPacket(response, null);
141 }
142
143 private void transportInfo(JinglePacket packet) {
144 Content content = packet.getJingleContent();
145 Log.d("xmppService","transport info : "+content.toString());
146 String cid = content.getUsedCandidate();
147 if (cid!=null) {
148 final File file = this.mXmppConnectionService.getFileBackend().getImageFile(this.message);
149 final SocksConnection connection = this.connections.get(cid);
150 if (connection.isProxy()) {
151 IqPacket activation = new IqPacket(IqPacket.TYPE_SET);
152 activation.setTo(connection.getJid());
153 activation.query("http://jabber.org/protocol/bytestreams").setAttribute("sid", this.getSessionId());
154 activation.query().addChild("activate").setContent(this.getResponder());
155 Log.d("xmppService","connection is proxy. need to activate "+activation.toString());
156 this.account.getXmppConnection().sendIqPacket(activation, new OnIqPacketReceived() {
157
158 @Override
159 public void onIqPacketReceived(Account account, IqPacket packet) {
160 Log.d("xmppService","activation result: "+packet.toString());
161 connection.send(file);
162 }
163 });
164 } else {
165 connection.send(file);
166 }
167 }
168 }
169
170 private void connectWithCandidates() {
171 for(Element canditate : this.candidates) {
172 String host = canditate.getAttribute("host");
173 int port = Integer.parseInt(canditate.getAttribute("port"));
174 String type = canditate.getAttribute("type");
175 String jid = canditate.getAttribute("jid");
176 SocksConnection socksConnection = new SocksConnection(this, host, jid, port,type);
177 socksConnection.connect();
178 this.connections.put(canditate.getAttribute("cid"), socksConnection);
179 }
180 }
181
182 private void sendCandidateUsed(String cid) {
183
184 }
185
186 public String getInitiator() {
187 return this.initiator;
188 }
189
190 public String getResponder() {
191 return this.responder;
192 }
193}