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.

This commit is contained in:
seb 2012-02-07 13:29:51 +00:00
parent 8b759a4dd0
commit 2591b21992
6 changed files with 123 additions and 4 deletions

View File

@ -213,6 +213,11 @@ public class Agent
*/ */
private int generation = 0; private int generation = 0;
/**
* Indicates that ICE will be shutdown.
*/
private boolean shutdown = false;
/** /**
* Creates an empty <tt>Agent</tt> with no streams, and no address. * Creates an empty <tt>Agent</tt> with no streams, and no address.
*/ */
@ -451,6 +456,7 @@ public class Agent
synchronized(startLock) synchronized(startLock)
{ {
logger.info("Start ICE connectivity establishment"); logger.info("Start ICE connectivity establishment");
shutdown = false;
pruneNonMatchedStreams(); pruneNonMatchedStreams();
initCheckLists(); initCheckLists();
@ -1921,6 +1927,11 @@ public class Agent
{ {
logger.info("Free ICE agent"); logger.info("Free ICE agent");
shutdown = true;
if(stunKeepAliveThread != null)
stunKeepAliveThread.interrupt();
/* /*
* Set the IceProcessingState#TERMINATED state on this Agent unless it * Set the IceProcessingState#TERMINATED state on this Agent unless it
* is in a termination state already. * is in a termination state already.
@ -1945,6 +1956,10 @@ public class Agent
} }
logger.info("remove stream " + stream.getName()); 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 == while(state == IceProcessingState.COMPLETED || state ==
IceProcessingState.TERMINATED) IceProcessingState.TERMINATED)
{ {
if(shutdown)
break;
List<IceMediaStream> streams = getStreams(); List<IceMediaStream> streams = getStreams();
for(IceMediaStream stream : streams) for(IceMediaStream stream : streams)
@ -2040,6 +2058,9 @@ public class Agent
streams = null; streams = null;
if(shutdown)
break;
try try
{ {
Thread.sleep(3000); Thread.sleep(3000);

View File

@ -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 * Creates a STUN {@link Request} containing the necessary PRIORITY and
* CONTROLLING/CONTROLLED attributes. Also stores a reference to * 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 // it may be an error due to remote peer that does not processed
// our candidates IQ and we start connectivity checks too quickly // our candidates IQ and we start connectivity checks too quickly
//(that's why we get a STALE CREDENTIALS because remote peer doesn't // (that's why we get a STALE CREDENTIALS because remote peer does
// know the candidate ufrag). So let's send another check for that // not know the candidate ufrag). So let's send another check for
// pair // that pair
logger.info("stale credentials for GTalk check for " + logger.info("stale credentials for GTalk check for " +
pair.toShortString()); pair.toShortString());
startCheckForPair(pair);
// try again after some delay
startDelayedCheckForPair(pair, 50);
return; return;
} }
else else

View File

@ -8,6 +8,7 @@
package org.ice4j.ice; package org.ice4j.ice;
import org.ice4j.*; import org.ice4j.*;
import org.ice4j.ice.harvest.*;
import org.ice4j.socket.*; import org.ice4j.socket.*;
/** /**
@ -26,6 +27,10 @@ import org.ice4j.socket.*;
public class ServerReflexiveCandidate public class ServerReflexiveCandidate
extends LocalCandidate extends LocalCandidate
{ {
/**
* The STUN candidate harvest.
*/
private final StunCandidateHarvest stunHarvest;
/** /**
* Creates a <tt>ServerReflexiveCandidate</tt> for the specified transport * Creates a <tt>ServerReflexiveCandidate</tt> for the specified transport
@ -41,6 +46,32 @@ public class ServerReflexiveCandidate
public ServerReflexiveCandidate(TransportAddress address, public ServerReflexiveCandidate(TransportAddress address,
HostCandidate base, HostCandidate base,
TransportAddress stunSrvrAddr) TransportAddress stunSrvrAddr)
{
super(address,
base.getParentComponent(),
CandidateType.SERVER_REFLEXIVE_CANDIDATE);
setBase(base);
setStunServerAddress(stunSrvrAddr);
stunHarvest = null;
}
/**
* Creates a <tt>ServerReflexiveCandidate</tt> for the specified transport
* address, and base.
*
* @param address the {@link TransportAddress} that this <tt>Candidate</tt>
* 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, super(address,
base.getParentComponent(), base.getParentComponent(),
@ -48,6 +79,7 @@ public class ServerReflexiveCandidate
setBase(base); setBase(base);
setStunServerAddress(stunSrvrAddr); setStunServerAddress(stunSrvrAddr);
this.stunHarvest = stunHarvest;
} }
/** /**
@ -61,4 +93,20 @@ public class ServerReflexiveCandidate
{ {
return getBase().getIceSocketWrapper(); return getBase().getIceSocketWrapper();
} }
/**
* Frees resources allocated by this candidate such as its
* <tt>DatagramSocket</tt>, for example. The <tt>socket</tt> of this
* <tt>LocalCandidate</tt> is closed only if it is not the <tt>socket</tt>
* of the <tt>base</tt> of this <tt>LocalCandidate</tt>.
*/
protected void free()
{
super.free();
if(stunHarvest != null)
{
stunHarvest.close();
}
}
} }

View File

@ -1315,4 +1315,14 @@ public class StunCandidateHarvest
else else
return false; return false;
} }
/**
* Close the harvest.
*/
public void close()
{
// stop keep alive thread
setSendKeepAliveMessageInterval(
SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED);
}
} }

View File

@ -247,6 +247,17 @@ class NetAccessManager
ap.stop(); ap.stop();
} }
/**
* Stops <tt>NetAccessManager</tt> and all of its <tt>MessageProcessor</tt>.
*/
public void stop()
{
for(MessageProcessor mp : messageProcessors)
{
mp.stop();
}
}
//---------------thread pool implementation -------------------------------- //---------------thread pool implementation --------------------------------
/** /**
* Adjusts the number of concurrently running MessageProcessors. * Adjusts the number of concurrently running MessageProcessors.

View File

@ -792,6 +792,8 @@ public class StunStack
if(tran != null) if(tran != null)
tran.expire(); tran.expire();
} }
netAccessManager.stop();
} }
/** /**