From 8d69aab45b8240546ee250078b5e32569a824126 Mon Sep 17 00:00:00 2001 From: emcho Date: Sat, 1 May 2010 21:34:50 +0000 Subject: [PATCH] Implements a connectivity check pace maker --- src/org/ice4j/ice/Agent.java | 31 ++++- src/org/ice4j/ice/CheckList.java | 94 +++++++++++++- .../ice/checks/ConnectivityCheckClient.java | 116 ++++++++++++------ 3 files changed, 197 insertions(+), 44 deletions(-) diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 45c9303..c629ce1 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -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 streams = mediaStreams.values(); + for (IceMediaStream stream : streams) + { + if (stream.getCheckList().getState() + == CheckListState.RUNNING) + i++; + } + + return i; + } + } + /** * Returns a String 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); } /** diff --git a/src/org/ice4j/ice/CheckList.java b/src/org/ice4j/ice/CheckList.java index 2d178f7..0b020e7 100644 --- a/src/org/ice4j/ice/CheckList.java +++ b/src/org/ice4j/ice/CheckList.java @@ -31,6 +31,15 @@ public class CheckList */ private CheckListState state = CheckListState.RUNNING; + /** + * The triggeredCheckQueue 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 triggeredCheckQueue + = new LinkedList(); + /** * Returns the state of this check list. * @@ -46,11 +55,94 @@ public class CheckList * * @param state the CheckListState for this list. */ - protected void setState(CheckListState state) + public void setState(CheckListState state) { this.state = state; } + /** + * Adds pair 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 + * null if that queue is empty. + * + * @return the first {@link CandidatePair} in the triggered check queue or + * null 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 Waiting state or, if there is + * no such pair, the highest priority Frozen {@link CandidatePair}. + * + * @return the next {@link CandidatePair} that is eligible for a regular + * connectivity check, which would either be the highest priority + * Waiting pair or, when there's no such pair, the highest priority + * Frozen pair or null 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 String representation of this check list. It * consists of a list of the CandidatePairs in the order they diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java index e19aa8c..6a7edb3 100644 --- a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java @@ -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 triggeredCheckQueue 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 triggeredCheckQueue - = new LinkedList(); + private final List paceMakers = new LinkedList(); + + /** + * Contains the system time at the moment we last started a {@link + * PaceMaker}. + */ + private long lastPaceMakerStarted = 0; /** * Creates a new ConnectivityCheckHandler 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 pair 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 PaceMaker will be running + * checks for. + */ + private final CheckList checkList; + /** * Creates a new {@link PaceMaker} for the specified * parentClient * * @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); + } } }