diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 976a845..228af80 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -20,8 +20,8 @@ import org.ice4j.ice.harvest.*; import org.ice4j.stack.*; /** - * An Agent could be described as the main class (i.e. the chef d'orchestre) - * of an ICE implementation. + * An Agent could be described as the main class (i.e. the chef + * d'orchestre) of an ICE implementation. *

* 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 diff --git a/src/org/ice4j/ice/CheckList.java b/src/org/ice4j/ice/CheckList.java index 79244ad..1586f5b 100644 --- a/src/org/ice4j/ice/CheckList.java +++ b/src/org/ice4j/ice/CheckList.java @@ -60,12 +60,25 @@ public class CheckList private final List stateListeners = new LinkedList(); + /** + * Contains {@link PropertyChangeListener}s registered with this {@link + * Agent} and following its changes of state. + */ + private final List checkListeners + = 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"; + /** + * 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 CheckListener. It will be notified when ordinary checks + * ended. + * + * @param l CheckListener to add + */ + public void addChecksListener(PropertyChangeListener l) + { + synchronized(checkListeners) + { + if(!checkListeners.contains(l)) + { + checkListeners.add(l); + } + } + } + + /** + * Remove a CheckListener. + * + * @param l CheckListener 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 listenersCopy; + + synchronized(checkListeners) + { + listenersCopy + = new LinkedList(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. diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java index 8f8b31b..2b076bc 100644 --- a/src/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/ConnectivityCheckClient.java @@ -726,6 +726,7 @@ class ConnectivityCheckClient * processTimeout() or processFailure() method. */ logger.finest("will skip a check beat."); + checkList.fireEndOfOrdinaryChecks(); } } } diff --git a/src/org/ice4j/ice/DefaultNominator.java b/src/org/ice4j/ice/DefaultNominator.java index bceb752..7536dd2 100644 --- a/src/org/ice4j/ice/DefaultNominator.java +++ b/src/org/ice4j/ice/DefaultNominator.java @@ -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 validatedCandidates = + new HashMap(); + /** * Creates a new instance of this nominator using parentAgent as * a reference to the Agent 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); + } + } } diff --git a/src/org/ice4j/ice/NominationStrategy.java b/src/org/ice4j/ice/NominationStrategy.java index 61fd43e..28c01cb 100644 --- a/src/org/ice4j/ice/NominationStrategy.java +++ b/src/org/ice4j/ice/NominationStrategy.java @@ -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. diff --git a/src/org/ice4j/stack/MessageProcessor.java b/src/org/ice4j/stack/MessageProcessor.java index 2ca9dd8..859ec35 100644 --- a/src/org/ice4j/stack/MessageProcessor.java +++ b/src/org/ice4j/stack/MessageProcessor.java @@ -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); }