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 if (packet.isAction("session-initiate")) {
36 JingleConnection connection = new JingleConnection(this);
37 connection.init(account,packet);
38 connections.add(connection);
39 } else {
40 for (JingleConnection connection : connections) {
41 if (connection.getAccountJid().equals(account.getFullJid()) && connection
42 .getSessionId().equals(packet.getSessionId()) && connection
43 .getCounterPart().equals(packet.getFrom())) {
44 connection.deliverPacket(packet);
45 return;
46 } else {
47 Log.d("xmppService","no match sid:"+connection.getSessionId()+"="+packet.getSessionId()+" counterpart:"+connection.getCounterPart()+"="+packet.getFrom()+" account:"+connection.getAccountJid()+"="+packet.getTo());
48 }
49 }
50 Log.d("xmppService","delivering packet failed "+packet.toString());
51 }
52 }
53
54 public JingleConnection createNewConnection(Message message) {
55 JingleConnection connection = new JingleConnection(this);
56 connection.init(message);
57 connections.add(connection);
58 return connection;
59 }
60
61 public JingleConnection createNewConnection(JinglePacket packet) {
62 JingleConnection connection = new JingleConnection(this);
63 connections.add(connection);
64 return connection;
65 }
66
67 public XmppConnectionService getXmppConnectionService() {
68 return this.xmppConnectionService;
69 }
70
71 public void getPrimaryCandidate(Account account,
72 final OnPrimaryCandidateFound listener) {
73 if (!this.primaryCandidates.containsKey(account.getJid())) {
74 String xmlns = "http://jabber.org/protocol/bytestreams";
75 final String proxy = account.getXmppConnection()
76 .findDiscoItemByFeature(xmlns);
77 if (proxy != null) {
78 IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
79 iq.setTo(proxy);
80 iq.query(xmlns);
81 account.getXmppConnection().sendIqPacket(iq,
82 new OnIqPacketReceived() {
83
84 @Override
85 public void onIqPacketReceived(Account account,
86 IqPacket packet) {
87 Element streamhost = packet
88 .query()
89 .findChild("streamhost",
90 "http://jabber.org/protocol/bytestreams");
91 if (streamhost != null) {
92 Log.d("xmppService", "streamhost found "
93 + streamhost.toString());
94 Element candidate = new Element("candidate");
95 candidate.setAttribute("cid",
96 nextRandomId());
97 candidate.setAttribute("host",
98 streamhost.getAttribute("host"));
99 candidate.setAttribute("port",
100 streamhost.getAttribute("port"));
101 candidate.setAttribute("type", "proxy");
102 candidate.setAttribute("jid", proxy);
103 candidate
104 .setAttribute("priority", "655360");
105 primaryCandidates.put(account.getJid(),
106 candidate);
107 listener.onPrimaryCandidateFound(true,
108 candidate);
109 } else {
110 listener.onPrimaryCandidateFound(false,
111 null);
112 }
113 }
114 });
115 } else {
116 listener.onPrimaryCandidateFound(false, null);
117 }
118
119 } else {
120 listener.onPrimaryCandidateFound(true,
121 this.primaryCandidates.get(account.getJid()));
122 }
123 }
124
125 public String getPrimaryCandidateId(Account account) {
126 if (this.primaryCandidates.containsKey(account.getJid())) {
127 return this.primaryCandidates.get(account.getJid()).getAttribute("cid");
128 } else {
129 return null;
130 }
131 }
132
133 public String nextRandomId() {
134 return new BigInteger(50, random).toString(32);
135 }
136
137 public long getAutoAcceptFileSize() {
138 return this.xmppConnectionService.getPreferences().getLong("auto_accept_file_size", 0);
139 }
140}