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 ;
import java.util.List ;
import java.util.concurrent.ConcurrentHashMap ;
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
private List < JingleConnection > connections = new ArrayList < JingleConnection > ( ) ; // make
// concurrent
2014-04-11 19:13:09 +00:00
private ConcurrentHashMap < String , Element > primaryCandidates = new ConcurrentHashMap < String , Element > ( ) ;
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-13 19:10:36 +00:00
} else {
Log . d ( " xmppService " , " no match sid: " + connection . getSessionId ( ) + " = " + packet . getSessionId ( ) + " counterpart: " + connection . getCounterPart ( ) + " = " + packet . getFrom ( ) + " account: " + connection . getAccountJid ( ) + " = " + packet . getTo ( ) ) ;
2014-04-13 09:32:45 +00:00
}
2014-04-10 12:12:08 +00:00
}
2014-04-13 09:32:45 +00:00
Log . d ( " xmppService " , " delivering packet failed " + packet . toString ( ) ) ;
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-04-10 12:12:08 +00:00
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 ) ;
connections . add ( connection ) ;
return connection ;
}
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 ) {
Log . d ( " xmppService " , " streamhost found "
+ streamhost . toString ( ) ) ;
2014-04-11 19:13:09 +00:00
Element candidate = new Element ( " candidate " ) ;
candidate . setAttribute ( " cid " ,
2014-04-10 12:12:08 +00:00
nextRandomId ( ) ) ;
2014-04-11 19:13:09 +00:00
candidate . setAttribute ( " host " ,
2014-04-10 12:12:08 +00:00
streamhost . getAttribute ( " host " ) ) ;
2014-04-11 19:13:09 +00:00
candidate . setAttribute ( " port " ,
2014-04-10 12:12:08 +00:00
streamhost . getAttribute ( " port " ) ) ;
2014-04-11 19:13:09 +00:00
candidate . setAttribute ( " type " , " proxy " ) ;
candidate . setAttribute ( " jid " , proxy ) ;
candidate
2014-04-10 12:12:08 +00:00
. setAttribute ( " priority " , " 655360 " ) ;
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-13 19:10:36 +00:00
public String getPrimaryCandidateId ( Account account ) {
if ( this . primaryCandidates . containsKey ( account . getJid ( ) ) ) {
return this . primaryCandidates . get ( account . getJid ( ) ) . getAttribute ( " cid " ) ;
} else {
return null ;
}
}
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 ( ) {
return this . xmppConnectionService . getPreferences ( ) . getLong ( " auto_accept_file_size " , 0 ) ;
}
2014-04-08 21:15:55 +00:00
}