Implements a default NOMINATE_FIRST_VALID nomination strategy.

This commit is contained in:
emcho 2010-05-18 07:23:15 +00:00
parent 1eb48f91a5
commit b8c6760b96
5 changed files with 107 additions and 10 deletions

View File

@ -84,7 +84,14 @@ public class Agent
/**
* Our internal nominator implementing several nomination strategies.
*/
private final DefaultNominator nominator = new DefaultNominator(this);
private final DefaultNominator nominator;
/**
* The name of the {@link PropertyChangeEvent} that we use to deliver
* events on changes in the state of ICE processing in this agent.
*/
public static final String PROPERTY_ICE_PROCESSING_STATE
= "IceProcessingState";
/**
* the value of <tt>Ta</tt> as specified by the application or <tt>-1</tt>
@ -197,6 +204,9 @@ public class Agent
//add the FINGERPRINT attribute to all messages.
System.setProperty(StackProperties.ALWAYS_SIGN, "true");
nominator = new DefaultNominator(this);
}
/**
@ -322,6 +332,9 @@ public class Agent
synchronized(startLock)
{
initCheckLists();
//change state before we actually send checks so that we don't
//miss responses and hence the possibility to nominate a pair.
setState(IceProcessingState.RUNNING);
connCheckClient.startChecks();
}
@ -409,7 +422,7 @@ public class Agent
}
PropertyChangeEvent evt = new PropertyChangeEvent(
this, "IceProcessingState", oldState, newState);
this, PROPERTY_ICE_PROCESSING_STATE, oldState, newState);
for(PropertyChangeListener l : listenersCopy)
{
@ -1027,8 +1040,8 @@ public class Agent
/**
* Raises <tt>pair</tt>'s nomination flag and schedules a triggered check.
* Applications only need to use this method if they disable this
* <tt>Agent</tt>'s internal nomination and implement their own nomination
* strategy.
* <tt>Agent</tt>'s internal nomination and implement their own nominator
* and turn off nominations in this agent.
*
* @param pair the {@link CandidatePair} that we'd like to nominate and that
* we'd like to schedule a triggered check for.
@ -1036,7 +1049,7 @@ public class Agent
* @throws IllegalStateException if this <tt>Agent</tt> is not a controlling
* agent and can therefore not nominate pairs.
*
* @see Agent#setAutoNominationEnabled(boolean)
* @see Agent#setNominationStrategy(NominationStrategy)
*/
public void nominate(CandidatePair pair)
throws IllegalStateException
@ -1060,9 +1073,16 @@ public class Agent
}
}
public void setNominationStrategy()
/**
* Specifies the {@link NominationStrategy} that we should use in order to
* decide if and when we should nominate valid pairs.
*
* @param strategy the strategy that we'd like to use for nominating
* valid {@link CandidatePair}s.
*/
public void setNominationStrategy(NominationStrategy strategy)
{
this.nominator.setStrategy(strategy);
}
/**

View File

@ -53,6 +53,12 @@ public class CheckList
private List<PropertyChangeListener> stateListeners
= 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";
/**
* Creates a check list with the specified name.
*
@ -449,7 +455,7 @@ public class CheckList
}
PropertyChangeEvent evt = new PropertyChangeEvent(
this, "CheckListState", oldState, newState);
this, PROPERTY_CHECK_LIST_STATE, oldState, newState);
for(PropertyChangeListener l : listenersCopy)
{

View File

@ -7,23 +7,88 @@
package org.ice4j.ice;
import java.beans.*;
import java.util.*;
/**
* Implements nomination strategies
* Implements ice4j internal nomination strategies.
*
* @author Emil Ivov
*/
public class DefaultNominator
implements PropertyChangeListener
{
/**
* The Agent that created us.
*/
private final Agent parentAgent;
/**
* The strategy that this nominator should use to nominate valid pairs.
*/
private NominationStrategy strategy
= NominationStrategy.NOMINATE_FIRST_VALID;
/**
* 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
* nominate pairs.
*
* @param parentAgent the {@link Agent} that created us.
*/
public DefaultNominator(Agent parentAgent)
{
this.parentAgent = parentAgent;
parentAgent.addStateChangeListener(this);
}
/**
* Tracks changes of state in {@link IceMediaStream}s and {@link
* CheckList}s.
*
* @param evt the event that we should use in case it means we should
* nominate someone.
*/
public void propertyChange(PropertyChangeEvent evt)
{
if(Agent.PROPERTY_ICE_PROCESSING_STATE.equals(evt.getPropertyName()))
{
IceProcessingState newState = (IceProcessingState)evt.getNewValue();
if(newState != IceProcessingState.RUNNING)
return;
List<IceMediaStream> streams = parentAgent.getStreams();
for(IceMediaStream stream : streams)
{
stream.addStreamChangeListener(this);
stream.getCheckList().addStateChangeListener(this);
}
}
if (!parentAgent.isControlling())
{
//CONTROLLED agents cannot nominate pairs
return;
}
if(IceMediaStream.PROPERTY_PAIR_VALIDATED.equals(evt.getPropertyName()))
{
CandidatePair validPair = (CandidatePair)evt.getNewValue();
parentAgent.nominate(validPair);
}
}
/**
* The {@link NominationStrategy} that this nominator should use when
* deciding whether or not a valid {@link CandidatePair} is suitable for
* nomination.
*
* @param strategy the {@link NominationStrategy} we should be using.
*/
public void setStrategy(NominationStrategy strategy)
{
this.strategy = strategy;
}
}

View File

@ -719,7 +719,7 @@ public class IceMediaStream
}
PropertyChangeEvent evt = new PropertyChangeEvent(
this, "CheckListState", oldValue, newValue);
this, PROPERTY_PAIR_VALIDATED, oldValue, newValue);
for(PropertyChangeListener l : listenersCopy)
{

View File

@ -21,6 +21,12 @@ import org.ice4j.*;
*/
public enum NominationStrategy
{
/**
* Indicates that ice4j's nominator should nominate valid pairs and that
* the application will be handling this.
*/
NONE("None"),
/**
* The strategy consists in nominating the first candidate pair that's
* confirmed as valid.