From a078606ab4c5628d873538900e5d961e15068228 Mon Sep 17 00:00:00 2001 From: Thiago Camargo Date: Mon, 12 Mar 2007 03:25:06 +0000 Subject: [PATCH] ECHO Refactoring git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7460 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smackx/jingle/IncomingJingleSession.java | 25 +++++-- .../smackx/jingle/JingleManager.java | 18 ++--- .../smackx/jingle/JingleSession.java | 2 +- .../smackx/jingle/OutgoingJingleSession.java | 25 +++++-- .../smackx/jingle/nat/BasicResolver.java | 3 +- .../jingle/nat/BasicTransportManager.java | 4 +- .../smackx/jingle/nat/BridgedResolver.java | 4 +- .../jingle/nat/BridgedTransportManager.java | 2 +- .../smackx/jingle/nat/FixedResolver.java | 3 +- .../smackx/jingle/nat/ICEResolver.java | 5 +- .../jingle/nat/ICETransportManager.java | 4 +- .../jingle/nat/JingleTransportManager.java | 7 +- .../smackx/jingle/nat/STUNResolver.java | 3 +- .../jingle/nat/STUNTransportManager.java | 5 +- .../smackx/jingle/nat/TransportCandidate.java | 73 +++++++++++++------ .../jingle/nat/TransportNegotiator.java | 2 +- .../smackx/jingle/nat/TransportResolver.java | 5 +- .../smackx/jingle/JingleSessionTest.java | 11 +-- .../smackx/jingle/nat/STUNResolverTest.java | 11 ++- .../jingle/nat/TransportCandidateTest.java | 2 +- .../jingle/nat/TransportResolverTest.java | 2 +- 21 files changed, 136 insertions(+), 80 deletions(-) diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/IncomingJingleSession.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/IncomingJingleSession.java index b7b15cbaf..6dab8a255 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/IncomingJingleSession.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/IncomingJingleSession.java @@ -64,6 +64,7 @@ import org.jivesoftware.smackx.jingle.media.PayloadType; import org.jivesoftware.smackx.jingle.nat.TransportCandidate; import org.jivesoftware.smackx.jingle.nat.TransportNegotiator; import org.jivesoftware.smackx.jingle.nat.TransportResolver; +import org.jivesoftware.smackx.jingle.nat.JingleTransportManager; import org.jivesoftware.smackx.packet.Jingle; import org.jivesoftware.smackx.packet.JingleContentDescription; import org.jivesoftware.smackx.packet.JingleContentDescription.JinglePayloadType; @@ -96,21 +97,31 @@ public class IncomingJingleSession extends JingleSession { /** * Constructor for a Jingle incoming session * - * @param conn the XMPP connection - * @param responder the responder - * @param resolver The transport resolver + * @param conn the XMPP connection + * @param responder the responder + * @param transportManager The transport manager */ protected IncomingJingleSession(XMPPConnection conn, String responder, - List payloadTypes, TransportResolver resolver) { + List payloadTypes, JingleTransportManager transportManager, String sid) { super(conn, responder, conn.getUser()); + setSid(sid); + // Create the states... accepting = new Accepting(this); pending = new Pending(this); active = new Active(this); + TransportResolver resolver = null; + try { + resolver = transportManager.getResolver(this); + } + catch (XMPPException e) { + e.printStackTrace(); + } + setMediaNeg(new MediaNegotiator(this, payloadTypes)); if (resolver.getType().equals(TransportResolver.Type.rawupd)) { setTransportNeg(new TransportNegotiator.RawUdp(this, resolver)); @@ -126,12 +137,12 @@ public class IncomingJingleSession extends JingleSession { * * @param conn the XMPP connection * @param responder the responder - * @param resolver The transport resolver + * @param transportManager The transport manager * @param jingleMediaManager The Media Manager for this Session */ protected IncomingJingleSession(XMPPConnection conn, String responder, - List payloadTypes, TransportResolver resolver, JingleMediaManager jingleMediaManager) { - this(conn, responder, payloadTypes, resolver); + List payloadTypes, JingleTransportManager transportManager, JingleMediaManager jingleMediaManager, String sid) { + this(conn, responder, payloadTypes, transportManager, sid); this.jingleMediaManager = jingleMediaManager; } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleManager.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleManager.java index e8d35b65b..ef81bef9a 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleManager.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleManager.java @@ -307,7 +307,7 @@ public class JingleManager implements JingleSessionListener { */ public JingleManager(XMPPConnection connection) { this(connection, new JingleTransportManager() { - protected TransportResolver createResolver() { + protected TransportResolver createResolver(JingleSession session) { return new BasicResolver(); } }); @@ -321,7 +321,7 @@ public class JingleManager implements JingleSessionListener { */ public JingleManager(XMPPConnection connection, final TransportResolver resolver) { this(connection, new JingleTransportManager() { - protected TransportResolver createResolver() { + protected TransportResolver createResolver(JingleSession session) { return resolver; } }); @@ -496,7 +496,7 @@ public class JingleManager implements JingleSessionListener { jingleSession.removeListener(this); jingleSessions.remove(jingleSession); jingleSession.close(); - System.err.println("Declined:"+reason); + System.err.println("Declined:" + reason); } public void sessionRedirected(String redirection, JingleSession jingleSession) { @@ -609,12 +609,10 @@ public class JingleManager implements JingleSessionListener { OutgoingJingleSession session; - TransportResolver resolver = jingleTransportManager.getResolver(); - if (jingleMediaManager != null) - session = new OutgoingJingleSession(connection, responder, payloadTypes, resolver, jingleMediaManager); + session = new OutgoingJingleSession(connection, responder, payloadTypes, jingleTransportManager, jingleMediaManager); else - session = new OutgoingJingleSession(connection, responder, payloadTypes, jingleTransportManager.getResolver()); + session = new OutgoingJingleSession(connection, responder, payloadTypes, jingleTransportManager); triggerSessionCreated(session); @@ -649,14 +647,12 @@ public class JingleManager implements JingleSessionListener { IncomingJingleSession session; - TransportResolver resolver = jingleTransportManager.getResolver(); - if (jingleMediaManager != null) session = new IncomingJingleSession(connection, request - .getFrom(), payloadTypes, resolver, jingleMediaManager); + .getFrom(), payloadTypes, jingleTransportManager, jingleMediaManager, request.getSessionID()); else session = new IncomingJingleSession(connection, request - .getFrom(), payloadTypes, resolver); + .getFrom(), payloadTypes, jingleTransportManager, request.getSessionID()); triggerSessionCreated(session); diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java index f3d9a73e8..4ba0c2361 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java @@ -239,7 +239,7 @@ public abstract class JingleSession extends JingleNegotiator { /** * Generate a unique session ID. */ - protected String generateSessionId() { + protected static String generateSessionId() { return String.valueOf(Math.abs(randomGenerator.nextLong())); } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/OutgoingJingleSession.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/OutgoingJingleSession.java index 4e36c43b2..db8d4ebf1 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/OutgoingJingleSession.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/OutgoingJingleSession.java @@ -63,6 +63,7 @@ import org.jivesoftware.smackx.jingle.media.PayloadType; import org.jivesoftware.smackx.jingle.nat.TransportCandidate; import org.jivesoftware.smackx.jingle.nat.TransportNegotiator; import org.jivesoftware.smackx.jingle.nat.TransportResolver; +import org.jivesoftware.smackx.jingle.nat.JingleTransportManager; import org.jivesoftware.smackx.packet.Jingle; import org.jivesoftware.smackx.packet.JingleContentDescription; import org.jivesoftware.smackx.packet.JingleContentDescription.JinglePayloadType; @@ -93,13 +94,13 @@ public class OutgoingJingleSession extends JingleSession { /** * Constructor for a Jingle outgoing session. * - * @param conn the XMPP connection - * @param responder the other endpoint - * @param payloadTypes A list of payload types, in order of preference. - * @param resolver The transport resolver. + * @param conn the XMPP connection + * @param responder the other endpoint + * @param payloadTypes A list of payload types, in order of preference. + * @param transportManager The transport manager. */ protected OutgoingJingleSession(XMPPConnection conn, String responder, - List payloadTypes, TransportResolver resolver) { + List payloadTypes, JingleTransportManager transportManager) { super(conn, conn.getUser(), responder); @@ -110,6 +111,14 @@ public class OutgoingJingleSession extends JingleSession { pending = new Pending(this); active = new Active(this); + TransportResolver resolver = null; + try { + resolver = transportManager.getResolver(this); + } + catch (XMPPException e) { + e.printStackTrace(); + } + // Create description and transport negotiatiors... setMediaNeg(new MediaNegotiator(this, payloadTypes)); if (resolver.getType().equals(TransportResolver.Type.rawupd)) { @@ -126,12 +135,12 @@ public class OutgoingJingleSession extends JingleSession { * @param conn the XMPP connection * @param responder the other endpoint * @param payloadTypes A list of payload types, in order of preference. - * @param resolver The transport resolver. + * @param transportManager The transport manager. * @param jingleMediaManager The Media Manager for this Session */ protected OutgoingJingleSession(XMPPConnection conn, String responder, - List payloadTypes, TransportResolver resolver, JingleMediaManager jingleMediaManager) { - this(conn, responder, payloadTypes, resolver); + List payloadTypes, JingleTransportManager transportManager, JingleMediaManager jingleMediaManager) { + this(conn, responder, payloadTypes, transportManager); this.jingleMediaManager = jingleMediaManager; } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BasicResolver.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BasicResolver.java index 903da8d9e..26db955f3 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BasicResolver.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BasicResolver.java @@ -21,6 +21,7 @@ package org.jivesoftware.smackx.jingle.nat; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.jingle.JingleSession; import java.net.InetAddress; import java.net.NetworkInterface; @@ -47,7 +48,7 @@ public class BasicResolver extends TransportResolver { * The BasicResolver takes the IP addresses of the interfaces and uses the * first non-loopback, non-linklocal and non-sitelocal address. */ - public synchronized void resolve() throws XMPPException { + public synchronized void resolve(JingleSession session) throws XMPPException { setResolveInit(); diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BasicTransportManager.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BasicTransportManager.java index a88837ff2..35f7a3d9d 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BasicTransportManager.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BasicTransportManager.java @@ -1,5 +1,7 @@ package org.jivesoftware.smackx.jingle.nat; +import org.jivesoftware.smackx.jingle.JingleSession; + /** * $RCSfile$ * $Revision: $ @@ -26,7 +28,7 @@ package org.jivesoftware.smackx.jingle.nat; */ public class BasicTransportManager extends JingleTransportManager{ - protected TransportResolver createResolver() { + protected TransportResolver createResolver(JingleSession session) { return new BasicResolver(); } } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BridgedResolver.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BridgedResolver.java index 5573d6103..89f1758ed 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BridgedResolver.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BridgedResolver.java @@ -21,6 +21,7 @@ package org.jivesoftware.smackx.jingle.nat; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.jingle.JingleSession; import java.util.Random; @@ -54,7 +55,7 @@ public class BridgedResolver extends TransportResolver{ *

* The BridgedResolver takes the IP addresse and ports of a jmf proxy service. */ - public synchronized void resolve() throws XMPPException { + public synchronized void resolve(JingleSession session) throws XMPPException { setResolveInit(); @@ -67,7 +68,6 @@ public class BridgedResolver extends TransportResolver{ BasicResolver basicResolver = new BasicResolver(); basicResolver.initializeAndWait(); - basicResolver.resolve(); TransportCandidate localCandidate = new TransportCandidate.Fixed( rtpBridge.getIp(), rtpBridge.getPortA()); diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BridgedTransportManager.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BridgedTransportManager.java index 2b68d4243..09ff8aa2d 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BridgedTransportManager.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/BridgedTransportManager.java @@ -43,7 +43,7 @@ public class BridgedTransportManager extends JingleTransportManager implements J } // Return the correspondent resolver - protected TransportResolver createResolver() { + protected TransportResolver createResolver(JingleSession session) { BridgedResolver bridgedResolver = new BridgedResolver(this.xmppConnection); return bridgedResolver; } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/FixedResolver.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/FixedResolver.java index b97ec7624..b417e72d4 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/FixedResolver.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/FixedResolver.java @@ -20,6 +20,7 @@ package org.jivesoftware.smackx.jingle.nat; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.jingle.JingleSession; /** * The FixedResolver is a resolver where @@ -53,7 +54,7 @@ public class FixedResolver extends TransportResolver { /** * Resolve the IP address. */ - public synchronized void resolve() throws XMPPException { + public synchronized void resolve(JingleSession session) throws XMPPException { if (!isResolving()) { setResolveInit(); diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICEResolver.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICEResolver.java index b58a26cbb..08254b584 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICEResolver.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICEResolver.java @@ -25,6 +25,7 @@ import de.javawi.jstun.test.demo.ice.ICENegociator; import de.javawi.jstun.util.UtilityException; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smackx.jingle.JingleSession; import java.net.UnknownHostException; import java.net.SocketException; @@ -76,7 +77,7 @@ public class ICEResolver extends TransportResolver { /** * Resolve the IP and obtain a valid transport method. */ - public synchronized void resolve() throws XMPPException { + public synchronized void resolve(JingleSession session) throws XMPPException { this.setResolveInit(); for (TransportCandidate candidate : this.getCandidatesList()) { @@ -105,7 +106,7 @@ public class ICEResolver extends TransportResolver { transportCandidate.setLocalIp(candidate.getBase().getAddress().getInetAddress().getHostAddress()); transportCandidate.setPort(getFreePort()); try { - transportCandidate.addCandidateEcho(); + transportCandidate.addCandidateEcho(session); } catch (SocketException e) { e.printStackTrace(); diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICETransportManager.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICETransportManager.java index 04cd5d466..b91ec9432 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICETransportManager.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICETransportManager.java @@ -40,9 +40,9 @@ public class ICETransportManager extends JingleTransportManager implements Jingl } } - protected TransportResolver createResolver() { + protected TransportResolver createResolver(JingleSession session) { try { - iceResolver.resolve(); + iceResolver.resolve(session); } catch (XMPPException e) { e.printStackTrace(); diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/JingleTransportManager.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/JingleTransportManager.java index 287c8d9be..386aa3884 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/JingleTransportManager.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/JingleTransportManager.java @@ -20,6 +20,7 @@ package org.jivesoftware.smackx.jingle.nat; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.jingle.JingleSession; /** * Transport manager for Jingle. @@ -53,8 +54,8 @@ public abstract class JingleTransportManager { * * @return */ - public TransportResolver getResolver() throws XMPPException { - TransportResolver resolver = createResolver(); + public TransportResolver getResolver(JingleSession session) throws XMPPException { + TransportResolver resolver = createResolver(session); if (resolver == null) { resolver = new BasicResolver(); } @@ -68,6 +69,6 @@ public abstract class JingleTransportManager { * * @return */ - protected abstract TransportResolver createResolver(); + protected abstract TransportResolver createResolver(JingleSession session); } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/STUNResolver.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/STUNResolver.java index c07b8403f..261735d6b 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/STUNResolver.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/STUNResolver.java @@ -23,6 +23,7 @@ import de.javawi.jstun.test.BindingLifetimeTest; import de.javawi.jstun.test.DiscoveryInfo; import de.javawi.jstun.test.DiscoveryTest; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.jingle.JingleSession; import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -271,7 +272,7 @@ public class STUNResolver extends TransportResolver { /** * Resolve the IP and obtain a valid transport method. */ - public synchronized void resolve() throws XMPPException { + public synchronized void resolve(JingleSession session) throws XMPPException { setResolveInit(); diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/STUNTransportManager.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/STUNTransportManager.java index 8775f1ad7..259b661ce 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/STUNTransportManager.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/STUNTransportManager.java @@ -20,6 +20,7 @@ package org.jivesoftware.smackx.jingle.nat; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.jingle.JingleSession; /** * A Jingle Transport Manager implementation to be used on NAT networks with STUN Service NOT Blocked. @@ -39,9 +40,9 @@ public class STUNTransportManager extends JingleTransportManager { } } - protected TransportResolver createResolver() { + protected TransportResolver createResolver(JingleSession session) { try { - stunResolver.resolve(); + stunResolver.resolve(session); } catch (XMPPException e) { e.printStackTrace(); } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportCandidate.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportCandidate.java index 6cc6bcc79..520e02671 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportCandidate.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportCandidate.java @@ -53,6 +53,7 @@ package org.jivesoftware.smackx.jingle.nat; import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smackx.jingle.JingleSession; import java.io.IOException; import java.net.*; @@ -60,6 +61,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Arrays; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; /** * Transport candidate. @@ -97,8 +100,8 @@ public abstract class TransportCandidate { // Listeners for events private final List listeners = new ArrayList(); - public void addCandidateEcho() throws SocketException, UnknownHostException { - candidateEcho = new CandidateEcho(this); + public void addCandidateEcho(JingleSession session) throws SocketException, UnknownHostException { + candidateEcho = new CandidateEcho(this, session); echoThread = new Thread(candidateEcho); echoThread.start(); } @@ -647,17 +650,52 @@ public abstract class TransportCandidate { public class CandidateEcho implements Runnable { DatagramSocket socket = null; - byte password[] = null; + String localUser = null; + String remoteUser = null; + String id = null; + byte send[] = null; + byte receive[] = null; + DatagramPacket sendPacket = null; List listeners = new ArrayList(); List resultListeners = new ArrayList(); boolean enabled = true; boolean ended = false; long tries = 10; - public CandidateEcho(TransportCandidate candidate) throws UnknownHostException, SocketException { + public CandidateEcho(TransportCandidate candidate, JingleSession session) throws UnknownHostException, SocketException { this.socket = new DatagramSocket(candidate.getPort(), InetAddress.getByName(candidate.getLocalIp())); - Random r = new Random(); - password = longToByteArray((Math.abs(r.nextLong()))); + this.localUser = session.getInitiator(); + this.remoteUser = session.getResponder(); + this.id = session.getSid(); + + int keySplitIndex = ((int) Math.ceil(((float) id.length()) / 2)); + + + int size = 4 + localUser.length() * 2 + (id.length() - keySplitIndex) * 2; + ByteBuffer bufLocal = ByteBuffer.allocate(size); + // Create a character ByteBuffer Wrap + CharBuffer cbufLocal = bufLocal.asCharBuffer(); + cbufLocal.append(id.substring(0, keySplitIndex)); + cbufLocal.append(';'); + cbufLocal.append(localUser); + + size = 4 + remoteUser.length() * 2 + keySplitIndex * 2; + ByteBuffer bufRemote = ByteBuffer.allocate(size); + // Create a character ByteBuffer Wrap + CharBuffer cbufRemote = bufRemote.asCharBuffer(); + cbufRemote.append(id.substring(keySplitIndex)); + cbufRemote.append(';'); + cbufRemote.append(remoteUser); + + if (session.getConnection().getUser().equals(session.getInitiator())) { + this.send = bufLocal.array(); + this.receive = bufRemote.array(); + } + else { + this.receive = bufLocal.array(); + this.send = bufRemote.array(); + } + } public void run() { @@ -665,7 +703,7 @@ public abstract class TransportCandidate { System.out.println("Listening for ECHO: " + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort()); while (true) { - DatagramPacket packet = new DatagramPacket(new byte[8], 8); + DatagramPacket packet = new DatagramPacket(new byte[this.receive.length], this.receive.length); socket.receive(packet); @@ -681,8 +719,10 @@ public abstract class TransportCandidate { long delay = 1000 / tries / 2; if (delay < 0) delay = 10; - if (!Arrays.equals(packet.getData(), password)) + if (Arrays.equals(packet.getData(), receive)) for (int i = 0; i < tries; i++) { + packet.setData(send); + packet.setLength(send.length); socket.send(packet); if (!enabled) break; try { @@ -731,7 +771,7 @@ public abstract class TransportCandidate { DatagramListener listener = new DatagramListener() { public boolean datagramReceived(DatagramPacket datagramPacket) { if (datagramPacket.getAddress().equals(address) && datagramPacket.getPort() == port) { - if (Arrays.equals(datagramPacket.getData(), password)) { + if (Arrays.equals(datagramPacket.getData(), receive)) { testResult.setResult(true); ended = true; return true; @@ -743,7 +783,7 @@ public abstract class TransportCandidate { this.addListener(listener); - DatagramPacket packet = new DatagramPacket(password, password.length); + DatagramPacket packet = new DatagramPacket(send, send.length); packet.setAddress(address); packet.setPort(port); @@ -781,7 +821,7 @@ public abstract class TransportCandidate { DatagramListener listener = new DatagramListener() { public boolean datagramReceived(DatagramPacket datagramPacket) { if (datagramPacket.getAddress().equals(address) && datagramPacket.getPort() == port) { - if (Arrays.equals(datagramPacket.getData(), password)) { + if (Arrays.equals(datagramPacket.getData(), receive)) { TestResult testResult = new TestResult(); testResult.setResult(true); fireTestResult(testResult); @@ -795,7 +835,7 @@ public abstract class TransportCandidate { addListener(listener); - DatagramPacket packet = new DatagramPacket(password, password.length); + DatagramPacket packet = new DatagramPacket(send, send.length); packet.setAddress(address); packet.setPort(port); @@ -842,13 +882,4 @@ public abstract class TransportCandidate { } - public static byte[] longToByteArray(long valor) { - byte[] result = new byte[8]; - for (int i = 0; i < result.length; i++) { - result[7 - i] = (byte) (valor & 0xFF); - valor = valor >> 8; - } - return result; - } - } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java index b13fe5017..8bcc8593f 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java @@ -507,7 +507,7 @@ public abstract class TransportNegotiator extends JingleNegotiator { if (!(resolver.isResolving() || resolver.isResolved())) { // Resolve our IP and port System.out.println("RESOLVER CALLED"); - resolver.resolve(); + resolver.resolve(session); } } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportResolver.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportResolver.java index fb34a28d2..7b720ed50 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportResolver.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportResolver.java @@ -53,6 +53,7 @@ package org.jivesoftware.smackx.jingle.nat; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.jingle.JingleSession; import java.io.IOException; import java.net.ServerSocket; @@ -89,7 +90,7 @@ public abstract class TransportResolver { public Type type = Type.rawupd; // the time, in milliseconds, before a check aborts - public static final int CHECK_TIMEOUT = 5000; + public static final int CHECK_TIMEOUT = 3000; // Listeners for events private final ArrayList listeners = new ArrayList(); @@ -126,7 +127,7 @@ public abstract class TransportResolver { /** * Start a the resolution. */ - public abstract void resolve() throws XMPPException; + public abstract void resolve(JingleSession session) throws XMPPException; /** * Clear the list of candidates and start a new resolution process. diff --git a/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleSessionTest.java b/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleSessionTest.java index 5292844a1..b4830ed25 100644 --- a/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleSessionTest.java +++ b/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleSessionTest.java @@ -2,6 +2,7 @@ package org.jivesoftware.smackx.jingle; import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smackx.jingle.nat.BasicResolver; +import org.jivesoftware.smackx.jingle.nat.BasicTransportManager; public class JingleSessionTest extends SmackTestCase { @@ -10,9 +11,9 @@ public class JingleSessionTest extends SmackTestCase { } public void testEqualsObject() { - JingleSession js1 = new OutgoingJingleSession(getConnection(0), "res1", null, new BasicResolver()); - JingleSession js2 = new OutgoingJingleSession(getConnection(1), "res1", null, new BasicResolver()); - JingleSession js3 = new OutgoingJingleSession(getConnection(2), "res2", null, new BasicResolver()); + JingleSession js1 = new OutgoingJingleSession(getConnection(0), "res1", null, new BasicTransportManager()); + JingleSession js2 = new OutgoingJingleSession(getConnection(1), "res1", null, new BasicTransportManager()); + JingleSession js3 = new OutgoingJingleSession(getConnection(2), "res2", null, new BasicTransportManager()); System.out.println(js1.getSid()); System.out.println(js2.getSid()); @@ -34,8 +35,8 @@ public class JingleSessionTest extends SmackTestCase { String ini2 = "initiator2"; String sid2 = "sid2"; - JingleSession js1 = new OutgoingJingleSession(getConnection(0), sid1, null, new BasicResolver()); - JingleSession js2 = new OutgoingJingleSession(getConnection(1), sid2, null, new BasicResolver()); + JingleSession js1 = new OutgoingJingleSession(getConnection(0), sid1, null, new BasicTransportManager()); + JingleSession js2 = new OutgoingJingleSession(getConnection(1), sid2, null, new BasicTransportManager()); // For a packet, we should be able to get a session that handles that... assertNotNull(JingleSession.getInstanceFor(getConnection(0))); diff --git a/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/STUNResolverTest.java b/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/STUNResolverTest.java index ea463dd39..49e28284e 100644 --- a/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/STUNResolverTest.java +++ b/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/STUNResolverTest.java @@ -205,10 +205,9 @@ public class STUNResolverTest extends SmackTestCase { }); try { - stunResolver.initialize(); + stunResolver.initializeAndWait(); Thread.sleep(55000); assertTrue(valCounter() > 0); - stunResolver.resolve(); } catch (Exception e) { e.printStackTrace(); @@ -257,8 +256,8 @@ public class STUNResolverTest extends SmackTestCase { }; // Explicit resolution - tr1.resolve(); - tr2.resolve(); + tr1.resolve(null); + tr2.resolve(null); final JingleManager man0 = new JingleManager(getConnection(0), tr1); final JingleManager man1 = new JingleManager(getConnection(1), tr2); @@ -356,8 +355,8 @@ public class STUNResolverTest extends SmackTestCase { TransportCandidate.Fixed c2 = new TransportCandidate.Fixed("localhost", 22444); try { - c1.addCandidateEcho(); - c2.addCandidateEcho(); + c1.addCandidateEcho(null); + c2.addCandidateEcho(null); try { Thread.sleep(100); diff --git a/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/TransportCandidateTest.java b/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/TransportCandidateTest.java index 89800a77b..1dfad5fd8 100644 --- a/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/TransportCandidateTest.java +++ b/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/TransportCandidateTest.java @@ -62,7 +62,7 @@ public class TransportCandidateTest extends SmackTestCase { }; try { - tc.addCandidateEcho(); + tc.addCandidateEcho(null); assertTrue(tc.getCandidateEcho().test(InetAddress.getByName("localhost"),10020)); } catch (SocketException e) { diff --git a/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/TransportResolverTest.java b/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/TransportResolverTest.java index a7fc45cb4..0c9edee99 100644 --- a/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/TransportResolverTest.java +++ b/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/TransportResolverTest.java @@ -36,7 +36,7 @@ public class TransportResolverTest extends SmackTestCase { assertFalse(tr.isResolved()); try { - tr.resolve(); + tr.resolve(null); } catch (XMPPException e) { e.printStackTrace(); fail("Error resolving");