From 6cccdccac8f1ffe06e436097c03417c1bd7f5e23 Mon Sep 17 00:00:00 2001 From: Thiago Camargo Date: Fri, 23 Feb 2007 16:07:23 +0000 Subject: [PATCH] ICE Candidate now uses UDP Echo before PING git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7248 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smackx/jingle/JingleSession.java | 7 ++- .../smackx/jingle/nat/ICECandidate.java | 58 +++++++++++++++++++ .../smackx/jingle/nat/ICEResolver.java | 6 ++ .../smackx/jingle/nat/TransportCandidate.java | 9 +-- .../jingle/nat/TransportNegotiator.java | 2 +- .../smackx/jingle/nat/BasicResolverTest.java | 6 +- 6 files changed, 80 insertions(+), 8 deletions(-) diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java index 2b4e87965..f84ab9efc 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java @@ -299,6 +299,7 @@ public abstract class JingleSession extends JingleNegotiator { /** * Overides JingleNegiociator Method to add listener capabilities + * * @param newState new State */ protected void setState(State newState) { @@ -880,7 +881,7 @@ public abstract class JingleSession extends JingleNegotiator { protected void removePacketListener() { if (packetListener != null) { getConnection().removePacketListener(packetListener); - + System.out.println("REMOVE PACKET LISTENER"); } } @@ -1063,6 +1064,10 @@ public abstract class JingleSession extends JingleNegotiator { if (jingleMediaManager != null) { jingleMediaSession = jingleMediaManager.createMediaSession(pt, rc, lc); if (jingleMediaSession != null) { + + rc.removeCandidateEcho(); + lc.removeCandidateEcho(); + jingleMediaSession.startTrasmit(); jingleMediaSession.startReceive(); } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICECandidate.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICECandidate.java index 33b2a03ef..1e9424a93 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICECandidate.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICECandidate.java @@ -51,6 +51,10 @@ */ package org.jivesoftware.smackx.jingle.nat; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.List; + /** * ICE Transport candidate. *

@@ -235,6 +239,60 @@ public class ICECandidate extends TransportCandidate implements Comparable { this.type = type; } + /** + * Check if a transport candidate is usable. The transport resolver should + * check if the transport candidate the other endpoint has provided is + * usable. + *

+ * ICE Candidate can check connectivity using UDP echo Test. + */ + public void check(final List localCandidates) { + //TODO candidate is being checked trigger + //candidatesChecking.add(cand); + + Thread checkThread = new Thread(new Runnable() { + public void run() { + boolean isUsable = false; + + for (TransportCandidate candidate : localCandidates) { + CandidateEcho echo = candidate.getCandidateEcho(); + if (echo != null) { + try { + InetAddress address = InetAddress.getByName(getIp()); + if (echo.test(address, getPort())) isUsable = true; + if (isUsable) break; + } + catch (UnknownHostException e) { + e.printStackTrace(); + } + } + } + + if (isUsable) { + System.out.println("Checked using UDP Echo:" + getLocalIp()); + triggerCandidateChecked(isUsable); + return; + } + + InetAddress candAddress; + try { + candAddress = InetAddress.getByName(getIp()); + isUsable = candAddress.isReachable(TransportResolver.CHECK_TIMEOUT); + } + catch (Exception e) { + isUsable = false; + } + triggerCandidateChecked(isUsable); + + //TODO candidate is being checked trigger + //candidatesChecking.remove(cand); + } + }, "Transport candidate check"); + + checkThread.setName("Transport candidate test"); + checkThread.start(); + } + /* * (non-Javadoc) * 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 0ddc2fb9b..e4c52be2d 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICEResolver.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/ICEResolver.java @@ -80,6 +80,12 @@ public class ICEResolver extends TransportResolver { TransportCandidate transportCandidate = new ICECandidate(candidate.getAddress().getInetAddress().getHostAddress(), 1, candidate.getNetwork(), "1", candidate.getPort(), "1", candidate.getPriority(), typeString); transportCandidate.setLocalIp(candidate.getBase().getAddress().getInetAddress().getHostAddress()); transportCandidate.setPort(getFreePort()); + try { + transportCandidate.addCandidateEcho(); + } + catch (SocketException e) { + e.printStackTrace(); + } this.addCandidate(transportCandidate); System.out.println("C: " + candidate.getAddress().getInetAddress() + "|" + candidate.getBase().getAddress().getInetAddress() + " p:" + candidate.getPriority()); 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 dcb8a6ccb..f4202cad1 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportCandidate.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportCandidate.java @@ -104,7 +104,8 @@ public abstract class TransportCandidate { } public void removeCandidateEcho() { - candidateEcho.cancel(); + if (candidateEcho != null) + candidateEcho.cancel(); candidateEcho = null; echoThread = null; } @@ -370,7 +371,7 @@ public abstract class TransportCandidate { *

* Subclasses should provide better methods if they can... */ - public void check() { + public void check(final List localCandidates) { //TODO candidate is being checked trigger //candidatesChecking.add(cand); @@ -402,7 +403,7 @@ public abstract class TransportCandidate { * * @param result The result. */ - private void triggerCandidateChecked(boolean result) { + void triggerCandidateChecked(boolean result) { for (TransportResolverListener.Checker trl : getListenersList()) { trl.candidateChecked(this, result); @@ -461,7 +462,7 @@ public abstract class TransportCandidate { super(ip, port, generation); } } - + /** * Type-safe enum for the transportElement protocol */ 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 cbcd79017..3a2d30700 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java @@ -219,7 +219,7 @@ public abstract class TransportNegotiator extends JingleNegotiator { } }); - offeredCandidate.check(); + offeredCandidate.check(resolver.getCandidatesList()); } /** diff --git a/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/BasicResolverTest.java b/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/BasicResolverTest.java index 16fc889db..f00db201c 100644 --- a/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/BasicResolverTest.java +++ b/jingle/extension/test/org/jivesoftware/smackx/jingle/nat/BasicResolverTest.java @@ -2,6 +2,8 @@ package org.jivesoftware.smackx.jingle.nat; import org.jivesoftware.smack.test.SmackTestCase; +import java.util.ArrayList; + public class BasicResolverTest extends SmackTestCase { private int counter; @@ -54,7 +56,7 @@ public class BasicResolverTest extends SmackTestCase { } }); - tc.check(); + tc.check(new ArrayList()); try { Thread.sleep(TransportResolver.CHECK_TIMEOUT); @@ -83,7 +85,7 @@ public class BasicResolverTest extends SmackTestCase { } }); - tc.check(); + tc.check(new ArrayList()); try { Thread.sleep(TransportResolver.CHECK_TIMEOUT);