From 85415978b080a1f7af67bde45856569a07b4749b Mon Sep 17 00:00:00 2001 From: emcho Date: Sun, 7 Mar 2010 20:14:42 +0000 Subject: [PATCH] Adds a separate class for check lists so that we could set their state. Implements check/pair state initialization --- src/org/ice4j/ice/CandidatePair.java | 10 ++ src/org/ice4j/ice/CheckList.java | 23 +++++ src/org/ice4j/ice/CheckListState.java | 76 +++++++++++++++ src/org/ice4j/ice/IceMediaStream.java | 127 +++++++++++++++++++------- 4 files changed, 201 insertions(+), 35 deletions(-) create mode 100644 src/org/ice4j/ice/CheckList.java create mode 100644 src/org/ice4j/ice/CheckListState.java diff --git a/src/org/ice4j/ice/CandidatePair.java b/src/org/ice4j/ice/CandidatePair.java index 948c1b4..6ee45e4 100644 --- a/src/org/ice4j/ice/CandidatePair.java +++ b/src/org/ice4j/ice/CandidatePair.java @@ -365,4 +365,14 @@ public class CandidatePair return obj instanceof PairComparator; } } + + /** + * Returns the Component that this pair belongs to. + * + * @return the Component that this pair belongs to. + */ + public Component getParentComponent() + { + return getLocalCandidate().getParentComponent(); + } } diff --git a/src/org/ice4j/ice/CheckList.java b/src/org/ice4j/ice/CheckList.java new file mode 100644 index 0000000..a7883cd --- /dev/null +++ b/src/org/ice4j/ice/CheckList.java @@ -0,0 +1,23 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the SIP Communicator community (http://sip-communicator.org). + * + * Distributable under LGPL license. See terms of license at gnu.org. + */ +package org.ice4j.ice; + +import java.util.*; + +/** + * A check list is a list of CandidatePairs with a state (i.e. a + * CheckListState). The pairs in a check list are those that an ICE + * agent will run STUN connectivity checks for. There is one check list per + * in-use media stream resulting from the offer/answer exchange. + * + * @author Emil Ivov + */ +public class CheckList + extends LinkedList +{ + +} diff --git a/src/org/ice4j/ice/CheckListState.java b/src/org/ice4j/ice/CheckListState.java new file mode 100644 index 0000000..7a1fdb8 --- /dev/null +++ b/src/org/ice4j/ice/CheckListState.java @@ -0,0 +1,76 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the SIP Communicator community (http://sip-communicator.org). + * + * Distributable under LGPL license. See terms of license at gnu.org. + */ +package org.ice4j.ice; + +/** + * Everty CheckList is associated with a state, which captures the + * state of ICE checks for that media stream. There are three states: + *

+ * Running: In this state, ICE checks are still in progress for this + * media stream. + *

+ * Completed: In this state, ICE checks have produced nominated pairs + * for each component of the media stream. Consequently, ICE has succeeded and + * media can be sent. + *

+ * Failed: In this state, the ICE checks have not completed successfully for + * this media stream. + *

+ * When a check list is first constructed as the consequence of an offer/answer + * exchange, it is placed in the Running state. + * + * @author Emil Ivov + */ +public enum CheckListState +{ + /** + * In this state, ICE checks are still in progress for this media stream. + */ + RUNNING("Running"), + + /** + * In this state, ICE checks have produced nominated pairs for each + * component of the media stream. Consequently, ICE has succeeded and + * media can be sent. + */ + COMPLETED("Completed"), + + /** + * In this state, the ICE checks have not completed successfully for this + * media stream. + */ + FAILED("Failed"); + + /** + * The name of this CheckListState instance. + */ + private final String stateName; + + /** + * Creates a CheckListState instance with the specified name. + * + * @param stateName the name of the CheckListState instance + * we'd like to create. + */ + private CheckListState(String stateName) + { + this.stateName = stateName; + } + + /** + * Returns the name of this CheckListStae (i.e.. "Running", + * "Completed", or "Failed"). + * + * @return the name of this CheckListStae (i.e.. "Running", + * "Completed", or "Failed"). + */ + @Override + public String toString() + { + return stateName; + } +} diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index ff57102..fda70f0 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -53,8 +53,7 @@ public class IceMediaStream * is one check list per in-use media stream resulting from the offer/answer * exchange. */ - private final List checkList - = new LinkedList(); + private final CheckList checkList = new CheckList(); /** * The agent that this media stream belongs to. @@ -277,6 +276,51 @@ public class IceMediaStream orderCheckList(); pruneCheckList(); + computeInitialCheckListPairStates(); + } + } + + /** + * Creates, initializes and orders the list of candidate pairs that would + * be used for the connectivity checks for all components in this stream. + * + * @param checkList the list that we need to update with the new pairs. + */ + protected void createCheckList(List checkList) + { + List componentsList = getComponents(); + + for(Component cmp : componentsList) + { + createCheckList(cmp, checkList); + } + } + + /** + * Creates and adds to checkList all the CandidatePairs + * in component. + * + * @param component the Component whose candidates we need to + * pair and extract. + * @param checkList the list that we need to update with the new pairs. + */ + private void createCheckList(Component component, + List checkList) + { + List localCnds = component.getLocalCandidates(); + List remoteCnds = component.getRemoteCandidates(); + + for(LocalCandidate localCnd : localCnds) + { + for(Candidate remoteCnd : remoteCnds) + { + if(!localCnd.canReach(remoteCnd)) + continue; + + CandidatePair pair = new CandidatePair(localCnd, remoteCnd); + + checkList.add(pair); + } } } @@ -345,46 +389,59 @@ public class IceMediaStream } /** - * Creates, initializes and orders the list of candidate pairs that would - * be used for the connectivity checks for all components in this stream. - * - * @param checkList the list that we need to update with the new pairs. + * Computes and resets states of all pairs in this check list. For all pairs + * with the same foundation, we set the state of the pair with the lowest + * component ID to Waiting. If there is more than one such pair, the one + * with the highest priority is used. */ - protected void createCheckList(List checkList) + protected void computeInitialCheckListPairStates() { - List componentsList = getComponents(); + Map pairsToWait + = new Hashtable(); - for(Component cmp : componentsList) + //first, determine the pairs that we'd need to put in the waiting state. + for(CandidatePair pair : checkList) { - createCheckList(cmp, checkList); - } - } + //we need to check whether the pair is already in the wait list. if + //so we'll compare it with this one and determine which of the two + //needs to stay. + CandidatePair prevPair = pairsToWait.get(pair.getFoundation()); - /** - * Creates and adds to checkList all the CandidatePairs - * in component. - * - * @param component the Component whose candidates we need to - * pair and extract. - * @param checkList the list that we need to update with the new pairs. - */ - private void createCheckList(Component component, - List checkList) - { - List localCnds = component.getLocalCandidates(); - List remoteCnds = component.getRemoteCandidates(); - - for(LocalCandidate localCnd : localCnds) - { - for(Candidate remoteCnd : remoteCnds) + if(prevPair == null) { - if(!localCnd.canReach(remoteCnd)) - continue; - - CandidatePair pair = new CandidatePair(localCnd, remoteCnd); - - checkList.add(pair); + //first pair with this foundation. + pairsToWait.put(pair.getFoundation(), pair); + continue; } + + //we already have a pair with the same foundation. determine which + //of the two has the lower component id and higher priority and + //keep that one in the list. + if( prevPair.getParentComponent() == pair.getParentComponent()) + { + if(pair.getPriority() > prevPair.getPriority()) + { + //need to replace the pair in the list. + pairsToWait.put(pair.getFoundation(), pair); + } + } + else + { + if(pair.getParentComponent().getComponentID() + < prevPair.getParentComponent().getComponentID()) + { + //need to replace the pair in the list. + pairsToWait.put(pair.getFoundation(), pair); + } + } + } + + //now put the pairs we've selected in the Waiting state. + Iterator pairsIter = pairsToWait.values().iterator(); + + while(pairsIter.hasNext()) + { + pairsIter.next().setState(CandidatePairState.WAITING); } }