Implements a connectivity check pace maker

This commit is contained in:
emcho 2010-05-01 21:34:50 +00:00
parent ea6c442e78
commit 8d69aab45b
3 changed files with 197 additions and 44 deletions

View File

@ -569,6 +569,29 @@ public class Agent
}
}
/**
* Returns the number of {@link CheckList}s that are currently active.
*
* @return the number of {@link CheckList}s that are currently active.
*
*/
public int getActiveCheckListCount()
{
synchronized(mediaStreams)
{
int i=0;
Collection<IceMediaStream> streams = mediaStreams.values();
for (IceMediaStream stream : streams)
{
if (stream.getCheckList().getState()
== CheckListState.RUNNING)
i++;
}
return i;
}
}
/**
* Returns a <tt>String</tt> representation of this agent.
*
@ -800,6 +823,9 @@ public class Agent
triggeredPair.getLocalCandidate().getTransportAddress(),
triggeredPair.getRemoteCandidate().getTransportAddress());
IceMediaStream parentStream = triggeredPair.getLocalCandidate()
.getParentComponent().getParentStream();
if (knownPair != null)
{
triggeredPair = knownPair;
@ -838,9 +864,6 @@ public class Agent
* Its state is set to Waiting [and it] is enqueued into the
* triggered check queue.
*/
IceMediaStream parentStream = triggeredPair.getLocalCandidate()
.getParentComponent().getParentStream();
parentStream.addToCheckList(triggeredPair);
}
@ -851,7 +874,7 @@ public class Agent
* the pair is then changed to Waiting.
* Emil: This actually applies for all case.
*/
connCheckClient.scheduleTriggeredCheck(triggeredPair);
parentStream.getCheckList().scheduleTriggeredCheck(triggeredPair);
}
/**

View File

@ -31,6 +31,15 @@ public class CheckList
*/
private CheckListState state = CheckListState.RUNNING;
/**
* The <tt>triggeredCheckQueue</tt> is a FIFO queue containing candidate
* pairs for which checks are to be sent at the next available opportunity.
* A pair would get into a triggered check queue as soon as we receive
* a check on its local candidate.
*/
private final List<CandidatePair> triggeredCheckQueue
= new LinkedList<CandidatePair>();
/**
* Returns the state of this check list.
*
@ -46,11 +55,94 @@ public class CheckList
*
* @param state the <tt>CheckListState</tt> for this list.
*/
protected void setState(CheckListState state)
public void setState(CheckListState state)
{
this.state = state;
}
/**
* Adds <tt>pair</tt> to the local triggered check queue unless it's already
* there. Additionally, the method sets the pair's state to {@link
* CandidatePairState#WAITING}.
*
* @param pair the pair to schedule a triggered check for.
*/
protected void scheduleTriggeredCheck(CandidatePair pair)
{
synchronized(triggeredCheckQueue)
{
if(!triggeredCheckQueue.contains(pair))
{
triggeredCheckQueue.add(pair);
pair.setState(CandidatePairState.WAITING, null);
}
}
}
/**
* Returns the first {@link CandidatePair} in the triggered check queue or
* <tt>null</tt> if that queue is empty.
*
* @return the first {@link CandidatePair} in the triggered check queue or
* <tt>null</tt> if that queue is empty.
*/
public CandidatePair popTriggeredCheck()
{
synchronized(triggeredCheckQueue)
{
if(triggeredCheckQueue.size() > 0)
return triggeredCheckQueue.remove(0);
}
return null;
}
/**
* Returns the next {@link CandidatePair} that is eligible for a regular
* connectivity check. According to RFC 5245 this would be the highest
* priority pair that is in the <tt>Waiting</tt> state or, if there is
* no such pair, the highest priority <tt>Frozen</tt> {@link CandidatePair}.
*
* @return the next {@link CandidatePair} that is eligible for a regular
* connectivity check, which would either be the highest priority
* <tt>Waiting</tt> pair or, when there's no such pair, the highest priority
* <tt>Frozen</tt> pair or <tt>null</tt> otherwise
*
*/
public synchronized CandidatePair getNextOrdinaryPairToCheck()
{
if (size() < 1)
return null;
CandidatePair highestPriorityWaitingPair = null;
CandidatePair highestPriorityFrozenPair = null;
for (CandidatePair pair : this)
{
if (pair.getState() == CandidatePairState.WAITING)
{
if(highestPriorityWaitingPair == null
|| pair.getPriority()
> highestPriorityWaitingPair.getPriority())
{
highestPriorityWaitingPair = pair;
}
}
else if (pair.getState() == CandidatePairState.FROZEN)
{
if(highestPriorityFrozenPair == null
|| pair.getPriority()
> highestPriorityFrozenPair.getPriority())
highestPriorityFrozenPair = pair;
}
}
if(highestPriorityWaitingPair != null)
return highestPriorityWaitingPair;
else
return highestPriorityFrozenPair; //return even if null
}
/**
* Returns a <tt>String</tt> representation of this check list. It
* consists of a list of the <tt>CandidatePair</tt>s in the order they

View File

@ -37,16 +37,18 @@ public class ConnectivityCheckClient
/**
* The stun stack that we will use for connectivity checks.
*/
private StunStack stunStack = StunStack.getInstance();
private static final StunStack stunStack = StunStack.getInstance();
/**
* The <tt>triggeredCheckQueue</tt> is a FIFO queue containing candidate
* pairs for which checks are to be sent at the next available opportunity.
* A pair would get into a triggered check queue as soon as we receive
* a check on its local candidate.
* The {@link PaceMaker}s that are currently running checks in this client.
*/
private final List<CandidatePair> triggeredCheckQueue
= new LinkedList<CandidatePair>();
private final List<PaceMaker> paceMakers = new LinkedList<PaceMaker>();
/**
* Contains the system time at the moment we last started a {@link
* PaceMaker}.
*/
private long lastPaceMakerStarted = 0;
/**
* Creates a new <tt>ConnectivityCheckHandler</tt> setting
@ -62,14 +64,21 @@ public class ConnectivityCheckClient
public void startChecks(CheckList checkList)
{
for(CandidatePair pair : checkList)
{
startCheckForPair(pair);
}
//RFC 5245 says: Implementations SHOULD take care to spread out these
//timers so that they do not fire at the same time for each media
//stream.
//Emil: so let's make sure we spread the pace makers
System.currentTimeMillis();
PaceMaker paceMaker = new PaceMaker(this, checkList);
paceMakers.add(paceMaker);
paceMaker.start();
}
private void startCheckForPair(CandidatePair pair)
private TransactionID startCheckForPair(CandidatePair pair)
{
//we don't need to do a canReach() verification here as it has been
//already verified during the gathering process.
@ -127,6 +136,8 @@ public class ConnectivityCheckClient
if(logger.isLoggable(Level.FINEST))
logger.finest("checking pair " + pair + " with tran="
+ tran.toString());
return tran;
}
catch (Exception exception)
{
@ -134,7 +145,8 @@ public class ConnectivityCheckClient
"Failed to send " + request + " through "
+ stunSocket.getLocalSocketAddress(),
exception);
return;
return null;
}
}
@ -153,27 +165,8 @@ public class ConnectivityCheckClient
}
/**
* Adds <tt>pair</tt> to the local triggered check queue unless it's already
* there. Additionally, the method sets the pair's state to {@link
* CandidatePairState#WAITING}.
*
* @param pair the pair to schedule a triggered check for.
*/
public void scheduleTriggeredCheck(CandidatePair pair)
{
synchronized(triggeredCheckQueue)
{
if(!triggeredCheckQueue.contains(pair))
{
triggeredCheckQueue.add(pair);
pair.setState(CandidatePairState.WAITING, null);
}
}
}
/**
* The thread that actually sends the checks in the pace defined in RFC
* 5245.
* The thread that actually sends the checks for a particular check list
* in the pace defined in RFC 5245.
*/
private static class PaceMaker extends Thread
{
@ -183,17 +176,27 @@ public class ConnectivityCheckClient
*/
private final ConnectivityCheckClient parentClient;
/**
* The {@link CheckList} that this <tt>PaceMaker</tt> will be running
* checks for.
*/
private final CheckList checkList;
/**
* Creates a new {@link PaceMaker} for the specified
* <tt>parentClient</tt>
*
* @param parentClient the {@link ConnectivityCheckClient} that who's
* checks we are going to run.
* @param checkList the {@link CheckList} that we'll be sending checks
* for
*/
public PaceMaker(ConnectivityCheckClient parentClient)
public PaceMaker(ConnectivityCheckClient parentClient,
CheckList checkList)
{
super("ICE PaceMaker:" + parentClient.parentAgent.getLocalUfrag());
this.parentClient = parentClient;
this.checkList = checkList;
}
/**
@ -205,14 +208,49 @@ public class ConnectivityCheckClient
{
while(isRunning)
{
try
long waitFor = parentClient.parentAgent.calculateTa()
* parentClient.parentAgent.getActiveCheckListCount();
if(waitFor > 0)
{
wait(parentClient.parentAgent.calculateTa());
//waitFor will be 0 for the first check since we won't have
//any active check lists at that point yet.
try
{
wait(parentClient.parentAgent.calculateTa()
* parentClient.parentAgent
.getActiveCheckListCount());
}
catch (InterruptedException e)
{
logger.log(Level.FINER, "PaceMake got interrupted", e);
}
}
catch (InterruptedException e)
checkList.setState(CheckListState.RUNNING);
CandidatePair pairToCheck = checkList.popTriggeredCheck();
TransactionID transactionID = null;
if(pairToCheck != null)
transactionID = parentClient.startCheckForPair(pairToCheck);
pairToCheck = checkList.getNextOrdinaryPairToCheck();
if(pairToCheck == null)
{
logger.log(Level.FINER, "PaceMake got interrupted", e);
//we are done sending checks for this list. we'll send its
//final state in either the processResponse()
//processTimeout() or processFailure() method.
return;
}
if(transactionID == null)
pairToCheck.setState(CandidatePairState.FAILED, null);
else
pairToCheck.setState(
CandidatePairState.IN_PROGRESS, transactionID);
}
}
}