diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 59ddc6c..b191671 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -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 Ta as specified by the application or -1 @@ -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 pair's nomination flag and schedules a triggered check. * Applications only need to use this method if they disable this - * Agent's internal nomination and implement their own nomination - * strategy. + * Agent'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 Agent 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); } /** diff --git a/src/org/ice4j/ice/CheckList.java b/src/org/ice4j/ice/CheckList.java index 7b87cac..ff619cb 100644 --- a/src/org/ice4j/ice/CheckList.java +++ b/src/org/ice4j/ice/CheckList.java @@ -53,6 +53,12 @@ public class CheckList private List stateListeners = new LinkedList(); + /** + * 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) { diff --git a/src/org/ice4j/ice/DefaultNominator.java b/src/org/ice4j/ice/DefaultNominator.java index 5dad18e..466ad5c 100644 --- a/src/org/ice4j/ice/DefaultNominator.java +++ b/src/org/ice4j/ice/DefaultNominator.java @@ -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 parentAgent as + * a reference to the Agent 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 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; } } diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index 86e8b78..851b890 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -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) { diff --git a/src/org/ice4j/ice/NominationStrategy.java b/src/org/ice4j/ice/NominationStrategy.java index 2ad4493..0672caf 100644 --- a/src/org/ice4j/ice/NominationStrategy.java +++ b/src/org/ice4j/ice/NominationStrategy.java @@ -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.