From 2591b2199275bb6a1e75f533c63abb4467a410cb Mon Sep 17 00:00:00 2001 From: seb Date: Tue, 7 Feb 2012 13:29:51 +0000 Subject: [PATCH] When receive a STALE-CREDENTIALS error, delay the reemission of the connectivity check for that candidate pair. Fix thread stopping when ICE Agent is free()d otherwise some threads (Stun4J Message Processor, StunKeepAliveThread, ...) stay even after the call is finished. --- src/org/ice4j/ice/Agent.java | 21 ++++++++ .../ice4j/ice/ConnectivityCheckClient.java | 35 ++++++++++++-- .../ice4j/ice/ServerReflexiveCandidate.java | 48 +++++++++++++++++++ .../ice/harvest/StunCandidateHarvest.java | 10 ++++ src/org/ice4j/stack/NetAccessManager.java | 11 +++++ src/org/ice4j/stack/StunStack.java | 2 + 6 files changed, 123 insertions(+), 4 deletions(-) diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index a1a5499..72213bc 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -213,6 +213,11 @@ public class Agent */ private int generation = 0; + /** + * Indicates that ICE will be shutdown. + */ + private boolean shutdown = false; + /** * Creates an empty Agent with no streams, and no address. */ @@ -451,6 +456,7 @@ public class Agent synchronized(startLock) { logger.info("Start ICE connectivity establishment"); + shutdown = false; pruneNonMatchedStreams(); initCheckLists(); @@ -1921,6 +1927,11 @@ public class Agent { logger.info("Free ICE agent"); + shutdown = true; + + if(stunKeepAliveThread != null) + stunKeepAliveThread.interrupt(); + /* * Set the IceProcessingState#TERMINATED state on this Agent unless it * is in a termination state already. @@ -1945,6 +1956,10 @@ public class Agent } logger.info("remove stream " + stream.getName()); } + + getStunStack().shutDown(); + + logger.info("ICE agent freed"); } /** @@ -2013,6 +2028,9 @@ public class Agent while(state == IceProcessingState.COMPLETED || state == IceProcessingState.TERMINATED) { + if(shutdown) + break; + List streams = getStreams(); for(IceMediaStream stream : streams) @@ -2040,6 +2058,9 @@ public class Agent streams = null; + if(shutdown) + break; + try { Thread.sleep(3000); diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java index 9aed93d..4218137 100644 --- a/src/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/ConnectivityCheckClient.java @@ -151,6 +151,31 @@ class ConnectivityCheckClient } } + /** + * Start check for pair after some delay seconds + * + * @param candidatePair + * @param delay delay in millisecond + */ + private void startDelayedCheckForPair(final CandidatePair candidatePair, + final int delay) + { + new Thread() + { + public void run() + { + try + { + Thread.sleep(delay); + } + catch(Exception e) + { + } + startCheckForPair(candidatePair); + } + }.start(); + } + /** * Creates a STUN {@link Request} containing the necessary PRIORITY and * CONTROLLING/CONTROLLED attributes. Also stores a reference to @@ -834,12 +859,14 @@ class ConnectivityCheckClient { // it may be an error due to remote peer that does not processed // our candidates IQ and we start connectivity checks too quickly - //(that's why we get a STALE CREDENTIALS because remote peer doesn't - // know the candidate ufrag). So let's send another check for that - // pair + // (that's why we get a STALE CREDENTIALS because remote peer does + // not know the candidate ufrag). So let's send another check for + // that pair logger.info("stale credentials for GTalk check for " + pair.toShortString()); - startCheckForPair(pair); + + // try again after some delay + startDelayedCheckForPair(pair, 50); return; } else diff --git a/src/org/ice4j/ice/ServerReflexiveCandidate.java b/src/org/ice4j/ice/ServerReflexiveCandidate.java index 8fd34cb..1f99cd7 100644 --- a/src/org/ice4j/ice/ServerReflexiveCandidate.java +++ b/src/org/ice4j/ice/ServerReflexiveCandidate.java @@ -8,6 +8,7 @@ package org.ice4j.ice; import org.ice4j.*; +import org.ice4j.ice.harvest.*; import org.ice4j.socket.*; /** @@ -26,6 +27,10 @@ import org.ice4j.socket.*; public class ServerReflexiveCandidate extends LocalCandidate { + /** + * The STUN candidate harvest. + */ + private final StunCandidateHarvest stunHarvest; /** * Creates a ServerReflexiveCandidate for the specified transport @@ -41,6 +46,32 @@ public class ServerReflexiveCandidate public ServerReflexiveCandidate(TransportAddress address, HostCandidate base, TransportAddress stunSrvrAddr) + { + super(address, + base.getParentComponent(), + CandidateType.SERVER_REFLEXIVE_CANDIDATE); + + setBase(base); + setStunServerAddress(stunSrvrAddr); + stunHarvest = null; + } + + /** + * Creates a ServerReflexiveCandidate for the specified transport + * address, and base. + * + * @param address the {@link TransportAddress} that this Candidate + * is representing. + * @param base the {@link HostCandidate} that this server reflexive + * candidate was obtained through. + * @param stunSrvrAddr the {@link TransportAddress} of the stun server that + * reflected this candidate. + * @param stunHarvest the {@link StunCandidateHarvest} + */ + public ServerReflexiveCandidate(TransportAddress address, + HostCandidate base, + TransportAddress stunSrvrAddr, + StunCandidateHarvest stunHarvest) { super(address, base.getParentComponent(), @@ -48,6 +79,7 @@ public class ServerReflexiveCandidate setBase(base); setStunServerAddress(stunSrvrAddr); + this.stunHarvest = stunHarvest; } /** @@ -61,4 +93,20 @@ public class ServerReflexiveCandidate { return getBase().getIceSocketWrapper(); } + + /** + * Frees resources allocated by this candidate such as its + * DatagramSocket, for example. The socket of this + * LocalCandidate is closed only if it is not the socket + * of the base of this LocalCandidate. + */ + protected void free() + { + super.free(); + + if(stunHarvest != null) + { + stunHarvest.close(); + } + } } diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvest.java b/src/org/ice4j/ice/harvest/StunCandidateHarvest.java index bfe57b0..9d61eab 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvest.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvest.java @@ -1315,4 +1315,14 @@ public class StunCandidateHarvest else return false; } + + /** + * Close the harvest. + */ + public void close() + { + // stop keep alive thread + setSendKeepAliveMessageInterval( + SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED); + } } diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java index 0335461..d836078 100644 --- a/src/org/ice4j/stack/NetAccessManager.java +++ b/src/org/ice4j/stack/NetAccessManager.java @@ -247,6 +247,17 @@ class NetAccessManager ap.stop(); } + /** + * Stops NetAccessManager and all of its MessageProcessor. + */ + public void stop() + { + for(MessageProcessor mp : messageProcessors) + { + mp.stop(); + } + } + //---------------thread pool implementation -------------------------------- /** * Adjusts the number of concurrently running MessageProcessors. diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index 4c90e23..be849fa 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -792,6 +792,8 @@ public class StunStack if(tran != null) tran.expire(); } + + netAccessManager.stop(); } /**