2014-04-08 21:15:55 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.ArrayList;
|
2014-04-19 00:19:26 +00:00
|
|
|
import java.util.HashMap;
|
2014-04-08 21:15:55 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
|
|
|
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
|
|
|
|
|
|
|
public class JingleConnectionManager {
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
private XmppConnectionService xmppConnectionService;
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-19 00:19:26 +00:00
|
|
|
private List<JingleConnection> connections = new ArrayList<JingleConnection>();
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-19 00:19:26 +00:00
|
|
|
private HashMap<String, JingleCandidate> primaryCandidates = new HashMap<String, JingleCandidate>();
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
private SecureRandom random = new SecureRandom();
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
public JingleConnectionManager(XmppConnectionService service) {
|
|
|
|
this.xmppConnectionService = service;
|
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
public void deliverPacket(Account account, JinglePacket packet) {
|
2014-04-13 09:32:45 +00:00
|
|
|
if (packet.isAction("session-initiate")) {
|
|
|
|
JingleConnection connection = new JingleConnection(this);
|
|
|
|
connection.init(account,packet);
|
2014-04-13 19:10:36 +00:00
|
|
|
connections.add(connection);
|
2014-04-13 09:32:45 +00:00
|
|
|
} else {
|
|
|
|
for (JingleConnection connection : connections) {
|
2014-04-13 19:10:36 +00:00
|
|
|
if (connection.getAccountJid().equals(account.getFullJid()) && connection
|
2014-04-13 09:32:45 +00:00
|
|
|
.getSessionId().equals(packet.getSessionId()) && connection
|
|
|
|
.getCounterPart().equals(packet.getFrom())) {
|
|
|
|
connection.deliverPacket(packet);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
}
|
2014-05-01 20:33:49 +00:00
|
|
|
account.getXmppConnection().sendIqPacket(packet.generateRespone(IqPacket.TYPE_ERROR), null);
|
2014-04-10 12:12:08 +00:00
|
|
|
}
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
public JingleConnection createNewConnection(Message message) {
|
|
|
|
JingleConnection connection = new JingleConnection(this);
|
|
|
|
connection.init(message);
|
2014-05-13 12:49:09 +00:00
|
|
|
this.connections.add(connection);
|
2014-04-08 21:15:55 +00:00
|
|
|
return connection;
|
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
|
|
|
public JingleConnection createNewConnection(JinglePacket packet) {
|
|
|
|
JingleConnection connection = new JingleConnection(this);
|
2014-05-13 12:49:09 +00:00
|
|
|
this.connections.add(connection);
|
2014-04-10 12:12:08 +00:00
|
|
|
return connection;
|
|
|
|
}
|
2014-05-13 12:49:09 +00:00
|
|
|
|
|
|
|
public void finishConnection(JingleConnection connection) {
|
|
|
|
this.connections.remove(connection);
|
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
public XmppConnectionService getXmppConnectionService() {
|
|
|
|
return this.xmppConnectionService;
|
|
|
|
}
|
|
|
|
|
2014-04-11 19:13:09 +00:00
|
|
|
public void getPrimaryCandidate(Account account,
|
|
|
|
final OnPrimaryCandidateFound listener) {
|
|
|
|
if (!this.primaryCandidates.containsKey(account.getJid())) {
|
2014-04-08 21:15:55 +00:00
|
|
|
String xmlns = "http://jabber.org/protocol/bytestreams";
|
2014-04-10 12:12:08 +00:00
|
|
|
final String proxy = account.getXmppConnection()
|
|
|
|
.findDiscoItemByFeature(xmlns);
|
|
|
|
if (proxy != null) {
|
2014-04-08 21:15:55 +00:00
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
|
|
|
iq.setTo(proxy);
|
|
|
|
iq.query(xmlns);
|
2014-04-10 12:12:08 +00:00
|
|
|
account.getXmppConnection().sendIqPacket(iq,
|
|
|
|
new OnIqPacketReceived() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account,
|
|
|
|
IqPacket packet) {
|
|
|
|
Element streamhost = packet
|
|
|
|
.query()
|
|
|
|
.findChild("streamhost",
|
|
|
|
"http://jabber.org/protocol/bytestreams");
|
|
|
|
if (streamhost != null) {
|
2014-04-17 12:52:10 +00:00
|
|
|
JingleCandidate candidate = new JingleCandidate(nextRandomId(),true);
|
|
|
|
candidate.setHost(streamhost.getAttribute("host"));
|
|
|
|
candidate.setPort(Integer.parseInt(streamhost.getAttribute("port")));
|
|
|
|
candidate.setType(JingleCandidate.TYPE_PROXY);
|
|
|
|
candidate.setJid(proxy);
|
|
|
|
candidate.setPriority(655360+65535);
|
2014-04-11 19:13:09 +00:00
|
|
|
primaryCandidates.put(account.getJid(),
|
|
|
|
candidate);
|
|
|
|
listener.onPrimaryCandidateFound(true,
|
|
|
|
candidate);
|
2014-04-10 12:12:08 +00:00
|
|
|
} else {
|
2014-04-11 19:13:09 +00:00
|
|
|
listener.onPrimaryCandidateFound(false,
|
2014-04-10 12:12:08 +00:00
|
|
|
null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-04-08 21:15:55 +00:00
|
|
|
} else {
|
2014-04-11 19:13:09 +00:00
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
} else {
|
2014-04-11 19:13:09 +00:00
|
|
|
listener.onPrimaryCandidateFound(true,
|
|
|
|
this.primaryCandidates.get(account.getJid()));
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
public String nextRandomId() {
|
|
|
|
return new BigInteger(50, random).toString(32);
|
|
|
|
}
|
2014-04-13 16:09:40 +00:00
|
|
|
|
|
|
|
public long getAutoAcceptFileSize() {
|
2014-04-18 23:51:50 +00:00
|
|
|
String config = this.xmppConnectionService.getPreferences().getString("auto_accept_file_size", "524288");
|
2014-04-14 18:35:11 +00:00
|
|
|
try {
|
|
|
|
return Long.parseLong(config);
|
|
|
|
} catch (NumberFormatException e) {
|
2014-04-18 23:51:50 +00:00
|
|
|
return 524288;
|
2014-04-14 18:35:11 +00:00
|
|
|
}
|
2014-04-13 16:09:40 +00:00
|
|
|
}
|
2014-04-22 11:11:53 +00:00
|
|
|
|
|
|
|
public void deliverIbbPacket(Account account, IqPacket packet) {
|
|
|
|
String sid = null;
|
|
|
|
Element payload = null;
|
|
|
|
if (packet.hasChild("open","http://jabber.org/protocol/ibb")) {
|
|
|
|
payload = packet.findChild("open","http://jabber.org/protocol/ibb");
|
|
|
|
sid = payload.getAttribute("sid");
|
|
|
|
} else if (packet.hasChild("data","http://jabber.org/protocol/ibb")) {
|
|
|
|
payload = packet.findChild("data","http://jabber.org/protocol/ibb");
|
|
|
|
sid = payload.getAttribute("sid");
|
|
|
|
}
|
|
|
|
if (sid!=null) {
|
|
|
|
for (JingleConnection connection : connections) {
|
|
|
|
if (connection.hasTransportId(sid)) {
|
|
|
|
JingleTransport transport = connection.getTransport();
|
|
|
|
if (transport instanceof JingleInbandTransport) {
|
|
|
|
JingleInbandTransport inbandTransport = (JingleInbandTransport) transport;
|
|
|
|
inbandTransport.deliverPayload(packet,payload);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.d("xmppService","couldnt deliver payload: "+payload.toString());
|
|
|
|
} else {
|
|
|
|
Log.d("xmppService","no sid found in incomming ibb packet");
|
|
|
|
}
|
|
|
|
}
|
2014-06-29 11:44:59 +00:00
|
|
|
|
|
|
|
public void cancelInTransmission() {
|
|
|
|
for(JingleConnection connection : this.connections) {
|
|
|
|
if (connection.getStatus() == JingleConnection.STATUS_TRANSMITTING) {
|
|
|
|
connection.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|