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
24 // concurrent
25
26 private ConcurrentHashMap<String, Element> primaryCandidates = new ConcurrentHashMap<String, Element>();
27
28 private SecureRandom random = new SecureRandom();
29
30 public JingleConnectionManager(XmppConnectionService service) {
31 this.xmppConnectionService = service;
32 }
33
34 public void deliverPacket(Account account, JinglePacket packet) {
35 for (JingleConnection connection : connections) {
36 if (connection.getAccountJid().equals(account.getJid()) && connection
37 .getSessionId().equals(packet.getSessionId()) && connection
38 .getCounterPart().equals(packet.getFrom())) {
39 connection.deliverPacket(packet);
40 return;
41 }
42 }
43 Log.d("xmppService","delivering packet failed "+packet.toString());
44 }
45
46 public JingleConnection createNewConnection(Message message) {
47 JingleConnection connection = new JingleConnection(this);
48 connection.init(message);
49 connections.add(connection);
50 return connection;
51 }
52
53 public JingleConnection createNewConnection(JinglePacket packet) {
54 JingleConnection connection = new JingleConnection(this);
55 connections.add(connection);
56 return connection;
57 }
58
59 public XmppConnectionService getXmppConnectionService() {
60 return this.xmppConnectionService;
61 }
62
63 public void getPrimaryCandidate(Account account,
64 final OnPrimaryCandidateFound listener) {
65 if (!this.primaryCandidates.containsKey(account.getJid())) {
66 String xmlns = "http://jabber.org/protocol/bytestreams";
67 final String proxy = account.getXmppConnection()
68 .findDiscoItemByFeature(xmlns);
69 if (proxy != null) {
70 IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
71 iq.setTo(proxy);
72 iq.query(xmlns);
73 account.getXmppConnection().sendIqPacket(iq,
74 new OnIqPacketReceived() {
75
76 @Override
77 public void onIqPacketReceived(Account account,
78 IqPacket packet) {
79 Element streamhost = packet
80 .query()
81 .findChild("streamhost",
82 "http://jabber.org/protocol/bytestreams");
83 if (streamhost != null) {
84 Log.d("xmppService", "streamhost found "
85 + streamhost.toString());
86 Element candidate = new Element("candidate");
87 candidate.setAttribute("cid",
88 nextRandomId());
89 candidate.setAttribute("host",
90 streamhost.getAttribute("host"));
91 candidate.setAttribute("port",
92 streamhost.getAttribute("port"));
93 candidate.setAttribute("type", "proxy");
94 candidate.setAttribute("jid", proxy);
95 candidate
96 .setAttribute("priority", "655360");
97 primaryCandidates.put(account.getJid(),
98 candidate);
99 listener.onPrimaryCandidateFound(true,
100 candidate);
101 } else {
102 listener.onPrimaryCandidateFound(false,
103 null);
104 }
105 }
106 });
107 } else {
108 listener.onPrimaryCandidateFound(false, null);
109 }
110
111 } else {
112 listener.onPrimaryCandidateFound(true,
113 this.primaryCandidates.get(account.getJid()));
114 }
115 }
116
117 public String nextRandomId() {
118 return new BigInteger(50, random).toString(32);
119 }
120}