Add a new nomination strategy which consists in nominate first host or server reflexive candidate. If relayed candidates are first validated, wait some time to see if no host or server reflexive candidates get validated and be nominated instead of relayed ones. Note that this strategy is not the default and thus need to be explicitely set via Agent's setNominationStrategy method to be used.

This commit is contained in:
seb 2010-12-17 17:44:27 +00:00
parent 47d0918dce
commit 18e6036b97
6 changed files with 270 additions and 5 deletions

View File

@ -20,8 +20,8 @@ import org.ice4j.ice.harvest.*;
import org.ice4j.stack.*;
/**
* An <tt>Agent</tt> could be described as the main class (i.e. the chef d'orchestre)
* of an ICE implementation.
* An <tt>Agent</tt> could be described as the main class (i.e. the chef
* d'orchestre) of an ICE implementation.
* <p>
* As defined in RFC 3264, an agent is the protocol implementation involved in
* the offer/answer exchange. There are two agents involved in an offer/answer

View File

@ -60,12 +60,25 @@ public class CheckList
private final List<PropertyChangeListener> stateListeners
= new LinkedList<PropertyChangeListener>();
/**
* Contains {@link PropertyChangeListener}s registered with this {@link
* Agent} and following its changes of state.
*/
private final List<PropertyChangeListener> checkListeners
= new LinkedList<PropertyChangeListener>();
/**
* The name of the {@link PropertyChangeEvent} that we use to deliver
* changes on the state of this check list.
*/
public static final String PROPERTY_CHECK_LIST_STATE = "CheckListState";
/**
* The name of the {@link PropertyChangeEvent} that we use to deliver
* changes on the end of checks of this check list.
*/
public static final String PROPERTY_CHECK_LIST_CHECKS = "CheckListChecks";
/**
* Creates a check list with the specified name.
*
@ -492,6 +505,65 @@ public class CheckList
}
}
/**
* Add a <tt>CheckListener</tt>. It will be notified when ordinary checks
* ended.
*
* @param l <tt>CheckListener</tt> to add
*/
public void addChecksListener(PropertyChangeListener l)
{
synchronized(checkListeners)
{
if(!checkListeners.contains(l))
{
checkListeners.add(l);
}
}
}
/**
* Remove a <tt>CheckListener</tt>.
*
* @param l <tt>CheckListener</tt> to remove
*/
public void removeChecksListener(PropertyChangeListener l)
{
synchronized(checkListeners)
{
if(checkListeners.contains(l))
{
checkListeners.remove(l);
}
}
}
/**
* Creates a new {@link PropertyChangeEvent} and delivers it to all
* currently registered checks listeners.
*
* @param oldState old state
* @param newState new state
*/
protected void fireEndOfOrdinaryChecks()
{
List<PropertyChangeListener> listenersCopy;
synchronized(checkListeners)
{
listenersCopy
= new LinkedList<PropertyChangeListener>(checkListeners);
}
PropertyChangeEvent evt = new PropertyChangeEvent(
this, PROPERTY_CHECK_LIST_CHECKS, false, true);
for(PropertyChangeListener l : listenersCopy)
{
l.propertyChange(evt);
}
}
/**
* Returns a reference to the {@link IceMediaStream} that created and that
* maintains this check list.

View File

@ -726,6 +726,7 @@ class ConnectivityCheckClient
* processTimeout() or processFailure() method.
*/
logger.finest("will skip a check beat.");
checkList.fireEndOfOrdinaryChecks();
}
}
}

View File

@ -35,6 +35,13 @@ public class DefaultNominator
private NominationStrategy strategy
= NominationStrategy.NOMINATE_FIRST_VALID;
/**
* Map that will remember association between validated relayed candidate
* and a timer. It is use with the NOMINATE_FIRST_HIGHEST_VALID strategy.
*/
private Map<String, TimerTask> validatedCandidates =
new HashMap<String, TimerTask>();
/**
* Creates a new instance of this nominator using <tt>parentAgent</tt> as
* a reference to the <tt>Agent</tt> instance that we should use to
@ -83,7 +90,9 @@ public class DefaultNominator
strategyNominateFirstValid(evt);
else if (strategy == NominationStrategy.NOMINATE_HIGHEST_PRIO)
strategyNominateHighestPrio(evt);
else if (strategy ==
NominationStrategy.NOMINATE_FIRST_HOST_OR_REFLEXIVE_VALID)
strategyNominateFirstHostOrReflexiveValid(evt);
}
/**
@ -154,4 +163,177 @@ public class DefaultNominator
{
this.strategy = strategy;
}
/**
* Implements a nomination strategy that consists in nominating directly
* host or server reflexive pair that has become valid for a
* check list. For relayed pair, a timer is armed to see if no other host or
* server reflexive pair gets validated prior to timeout, the relayed ones
* gets nominated.
*
* @param evt the {@link PropertyChangeEvent} containing the pair which
* has been validated.
*/
private void strategyNominateFirstHostOrReflexiveValid(
PropertyChangeEvent evt)
{
if(IceMediaStream.PROPERTY_PAIR_VALIDATED.equals(evt.getPropertyName()))
{
CandidatePair validPair = (CandidatePair)evt.getSource();
TimerTask task = validatedCandidates.get(
validPair.getParentComponent().toShortString());
boolean isRelayed =
validPair.getLocalCandidate() instanceof HostCandidate;
if(isRelayed && task == null)
{
/* armed a timer and see if a host or server reflexive pair
* gets nominated. Otherwise nominate the relayed candidate pair
*/
Timer timer = new Timer();
task = new RelayedCandidateTask(validPair);
logger.info("Wait timeout to nominate relayed candidate");
timer.schedule(task, 0);
synchronized(validatedCandidates)
{
validatedCandidates.put(
validPair.getParentComponent().toShortString(),
task);
}
}
else if(!isRelayed)
{
// host or server reflexive candidate pair
if(task != null)
{
task.cancel();
logger.info("Found a better candidate pair to nominate for "
+ validPair.getParentComponent().toShortString());
}
logger.info("Nominate (first highest valid): " +
validPair.toShortString());
parentAgent.nominate(validPair);
}
}
}
/**
* TimerTask that will wait a certain amount of time to let other candidate
* pair to be validated and possibly be better than the relayed candidate.
*
* @author Sebastien Vincent
*/
private class RelayedCandidateTask
extends TimerTask
implements PropertyChangeListener
{
/**
* Wait time in milliseconds.
*/
private static final int WAIT_TIME = 1000;
/**
* The relayed candidate pair.
*/
private final CandidatePair pair;
/**
* If the task has been canceled.
*/
private boolean canceled = false;
/**
* Constructor.
*
* @param pair relayed candidate pair
*/
public RelayedCandidateTask(CandidatePair pair)
{
this.pair = pair;
pair.getParentComponent().getParentStream().getCheckList().
addChecksListener(this);
}
/**
* Tracks end of checks of the {@link CheckList}.
*
* @param evt the event
*/
public void propertyChange(PropertyChangeEvent evt)
{
/* check list has running out of ordinary checks, see if all other
* candidates are FAILED, in which case we nominate immediately
* the relayed candidate
*/
CheckList checkList = (CheckList)evt.getSource();
boolean allFailed = true;
synchronized(checkList)
{
for(CandidatePair c : checkList)
{
if(c != pair && c.getState() != CandidatePairState.FAILED)
{
allFailed = false;
}
}
}
if(allFailed)
{
// all other pairs are failed to do not waste time, cancel
// timer and nominate ourself (the relayed candidate).
this.cancel();
logger.info("Nominate (first highest valid): " +
pair.toShortString());
parentAgent.nominate(pair);
}
}
/**
* Cancel task.
*/
@Override
public boolean cancel()
{
canceled = true;
return super.cancel();
}
/**
* Task entry point.
*/
public void run()
{
try
{
Thread.sleep(WAIT_TIME);
}
catch(InterruptedException e)
{
canceled = true;
}
pair.getParentComponent().getParentStream().getCheckList().
removeChecksListener(this);
validatedCandidates.remove(
pair.getParentComponent().toShortString());
if(canceled)
{
return;
}
logger.info("Nominate (first highest valid): " +
pair.toShortString());
// task has not been canceled after WAIT_TIME milliseconds so
// nominate the pair
parentAgent.nominate(pair);
}
}
}

View File

@ -38,6 +38,15 @@ public enum NominationStrategy
*/
NOMINATE_HIGHEST_PRIO("NominateHighestPriority"),
/**
* The strategy consists in nominating the first host or server reflexive
* that's confirmed as valid pair. When a relayed candidate pair is
* validated first, a timer is armed and only if no host or server
* reflexive pair gets validated prior to timeout, the relayed ones
* gets nominated.
*/
NOMINATE_FIRST_HOST_OR_REFLEXIVE_VALID("NominateFirstHostOrReflexiveValid"),
/**
* The strategy consists in nominating the pair that showed the best
* shortest round trip time once all checks in a list completed.

View File

@ -75,7 +75,7 @@ class MessageProcessor
throw new NullPointerException("netAccessManager");
MessageQueue messageQueue = netAccessManager.getMessageQueue();
if (messageQueue == null)
{
throw new IllegalArgumentException(
@ -155,7 +155,8 @@ class MessageProcessor
try
{
StunMessageEvent stunMessageEvent
= new StunMessageEvent(stunStack, rawMessage, stunMessage);
= new StunMessageEvent(stunStack, rawMessage,
stunMessage);
messageEventHandler.handleMessageEvent(stunMessageEvent);
}