1package eu.siacs.conversations.xmpp.jingle;
2
3import java.math.BigInteger;
4import java.security.SecureRandom;
5import java.util.ArrayList;
6import java.util.List;
7import java.util.concurrent.ConcurrentHashMap;
8
9import android.util.Log;
10
11import eu.siacs.conversations.entities.Account;
12import eu.siacs.conversations.entities.Message;
13import eu.siacs.conversations.services.XmppConnectionService;
14import eu.siacs.conversations.xml.Element;
15import eu.siacs.conversations.xmpp.OnIqPacketReceived;
16import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
17import eu.siacs.conversations.xmpp.stanzas.IqPacket;
18
19public class JingleConnectionManager {
20
21 private XmppConnectionService xmppConnectionService;
22
23 private List<JingleConnection> connections = new ArrayList<JingleConnection>(); //make concurrent
24
25 private ConcurrentHashMap<String, Element> primaryCanditates = new ConcurrentHashMap<String, Element>();
26
27 private SecureRandom random = new SecureRandom();
28
29 public JingleConnectionManager(XmppConnectionService service) {
30 this.xmppConnectionService = service;
31 }
32
33 public void deliverPacket(Account account, JinglePacket packet) {
34 String id = generateInternalId(account.getJid(), packet.getFrom(), packet.getSessionId());
35 }
36
37 public JingleConnection createNewConnection(Message message) {
38 Account account = message.getConversation().getAccount();
39 JingleConnection connection = new JingleConnection(this);
40 String id = generateInternalId(account.getJid(), message.getCounterpart(), connection.getSessionId());
41 connection.init(message);
42 return connection;
43 }
44
45 private String generateInternalId(String account, String counterpart, String sid) {
46 return account+"#"+counterpart+"#"+sid;
47
48 }
49
50 public XmppConnectionService getXmppConnectionService() {
51 return this.xmppConnectionService;
52 }
53
54 public void getPrimaryCanditate(Account account, final OnPrimaryCanditateFound listener) {
55 if (!this.primaryCanditates.containsKey(account.getJid())) {
56 String xmlns = "http://jabber.org/protocol/bytestreams";
57 String proxy = account.getXmppConnection().findDiscoItemByFeature(xmlns);
58 if (proxy!=null) {
59 IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
60 iq.setTo(proxy);
61 iq.query(xmlns);
62 account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {
63
64 @Override
65 public void onIqPacketReceived(Account account, IqPacket packet) {
66 Element streamhost = packet.query().findChild("streamhost","http://jabber.org/protocol/bytestreams");
67 if (streamhost!=null) {
68 Log.d("xmppService","streamhost found "+streamhost.toString());
69 Element canditate = new Element("canditate");
70 canditate.setAttribute("cid",nextRandomId());
71 canditate.setAttribute("host", streamhost.getAttribute("host"));
72 canditate.setAttribute("port",streamhost.getAttribute("port"));
73 canditate.setAttribute("type", "proxy");
74 primaryCanditates.put(account.getJid(), canditate);
75 listener.onPrimaryCanditateFound(true, canditate);
76 } else {
77 listener.onPrimaryCanditateFound(false, null);
78 }
79 }
80 });
81 } else {
82 listener.onPrimaryCanditateFound(false, null);
83 }
84
85 } else {
86 listener.onPrimaryCanditateFound(true, this.primaryCanditates.get(account.getJid()));
87 }
88 }
89
90 public String nextRandomId() {
91 return new BigInteger(50, random).toString(32);
92 }
93}