From 4b7b2e668741a0d4afd1a23889d2d0b3efc5dcf5 Mon Sep 17 00:00:00 2001 From: emcho Date: Sat, 6 Mar 2010 16:05:44 +0000 Subject: [PATCH] Adds CheckList ordering and pruning --- src/org/ice4j/ice/Candidate.java | 6 +- src/org/ice4j/ice/CandidatePair.java | 130 ++++++++++++++++++++++++-- src/org/ice4j/ice/IceMediaStream.java | 89 ++++++++++++++++++ test/test/Ice.java | 5 + 4 files changed, 220 insertions(+), 10 deletions(-) diff --git a/src/org/ice4j/ice/Candidate.java b/src/org/ice4j/ice/Candidate.java index 55594a2..098bc25 100644 --- a/src/org/ice4j/ice/Candidate.java +++ b/src/org/ice4j/ice/Candidate.java @@ -57,7 +57,7 @@ public abstract class Candidate * a base, equal to that candidate itself. Similarly, the base of a * relayed candidate is that candidate itself. */ - private Candidate base = null; + private LocalCandidate base = null; /** * A unique priority number that MUST be a positive integer between 1 and @@ -206,7 +206,7 @@ public abstract class Candidate * * @return the base Candidate for this Candidate. */ - public Candidate getBase() + public LocalCandidate getBase() { return base; } @@ -220,7 +220,7 @@ public abstract class Candidate * * @param base the base Candidate of this Candidate. */ - public void setBase(Candidate base) + public void setBase(LocalCandidate base) { this.base = base; } diff --git a/src/org/ice4j/ice/CandidatePair.java b/src/org/ice4j/ice/CandidatePair.java index 5a2773b..8b4d8b5 100644 --- a/src/org/ice4j/ice/CandidatePair.java +++ b/src/org/ice4j/ice/CandidatePair.java @@ -6,6 +6,7 @@ */ package org.ice4j.ice; +import java.util.*; import java.util.logging.*; /** @@ -32,18 +33,24 @@ public class CandidatePair /** * The local candidate of this pair. */ - private final LocalCandidate localCandidate; + private LocalCandidate localCandidate; /** * The remote candidate of this pair. */ - private final Candidate remoteCandidate; + private Candidate remoteCandidate; /** * Priority of the candidate-pair */ private final long priority; + /** + * A Comparator using the compareTo method of the + * CandidatePair. + */ + public static final PairComparator comparator = new PairComparator(); + /** * Each candidate pair has a a state that is assigned once the check list * for each media stream has been computed. The ICE RFC defines five @@ -98,6 +105,17 @@ public class CandidatePair return localCandidate; } + /** + * Sets the LocalCandidate of this CandidatePair. + * + * @param localCnd the local Candidate of this + * CandidatePair. + */ + protected void setLocalCandidate( LocalCandidate localCnd) + { + this.localCandidate = localCnd;; + } + /** * Returns the remote candidate of this CandidatePair. * @@ -108,6 +126,17 @@ public class CandidatePair return remoteCandidate; } + /** + * Sets the RemoteCandidate of this CandidatePair. + * + * @param remoteCnd the local Candidate of this + * CandidatePair. + */ + protected void setRemoteCandidate( Candidate remoteCnd) + { + this.remoteCandidate = remoteCnd;; + } + /** * Returns the state of this CandidatePair. Each candidate pair has * a state that is assigned once the check list for each media stream has @@ -218,20 +247,52 @@ public class CandidatePair /** * Compares this CandidatePair with the specified object for order. * Returns a negative integer, zero, or a positive integer as this - * CandidatePair is less than, equal to, or greater than the - * specified object.

+ * CandidatePair's priority is greater than, equal to, or less than + * the one of the specified object thus insuring that higher priority pairs + * will come first.

* * @param candidatePair the Object to be compared. * @return a negative integer, zero, or a positive integer as this - * CandidatePair is less than, equal to, or greater than the - * specified object. + * CandidatePair's priority is greater than, equal to, or less than + * the one of the specified object. * * @throws ClassCastException if the specified object's type prevents it * from being compared to this Object. */ public int compareTo(CandidatePair candidatePair) { - return (int)(getPriority() - candidatePair.getPriority()); + long thisPri = getPriority(); + long otherPri = candidatePair.getPriority(); + + return (thisPri < otherPri + ? 1 + : (thisPri==otherPri + ? 0 + : -1)); + } + + /** + * Compares this CandidatePair to targetPair and returns + * true if pairs have equal local and equal remote candidates and + * false otherwise. + * + * @param targetPair the Object that we'd like to compare this + * target pair to. + * + * @return true if pairs have equal local and equal remote + * candidates and false otherwise. + */ + public boolean equals(Object targetPair) + { + if (! (targetPair instanceof CandidatePair) + || targetPair == null + || !localCandidate.equals(((CandidatePair)targetPair) + .localCandidate) + || !remoteCandidate.equals(((CandidatePair)targetPair) + .remoteCandidate)) + return false; + + return true; } /** @@ -246,4 +307,59 @@ public class CandidatePair + "):\n\tLocalCandidate=" + getLocalCandidate() + "\n\tRemoteCandidate=" + getRemoteCandidate(); } + + /** + * A Comparator using the compareTo method of the + * CandidatePair + */ + public static class PairComparator implements Comparator + { + /** + * Compares pair1 and pair2 for order. Returns a + * negative integer, zero, or a positive integer as pair1's + * priority is greater than, equal to, or less than the one of the + * pair2, thus insuring that higher priority pairs will come first. + * + * @param pair1 the first CandidatePair to be compared. + * @param pair2 the second CandidatePair to be compared. + * + * @return a negative integer, zero, or a positive integer as the first + * pair's priority priority is greater than, equal to, or less than + * the one of the second pair. + * + * @throws ClassCastException if the specified object's type prevents it + * from being compared to this Object. + */ + public int compare(CandidatePair pair1, CandidatePair pair2) + { + return pair1.compareTo(pair2); + } + + /** + * Indicates whether some other object is "equal to" to this + * Comparator. This method must obey the general contract of + * Object.equals(Object). Additionally, this method can return + * true only if the specified Object is also a comparator + * and it imposes the same ordering as this comparator. Thus, + * comp1.equals(comp2) implies that sgn(comp1.compare(o1, + * o2))==sgn(comp2.compare(o1, o2)) for every object reference + * o1 and o2.

+ * + * Note that it is always safe not to override + * Object.equals(Object). However, overriding this method may, + * in some cases, improve performance by allowing programs to determine + * that two distinct Comparators impose the same order. + * + * @param obj the reference object with which to compare. + * @return true only if the specified object is also + * a comparator and it imposes the same ordering as this + * comparator. + * @see java.lang.Object#equals(java.lang.Object) + * @see java.lang.Object#hashCode() + */ + public boolean equals(Object obj) + { + return obj instanceof PairComparator; + } + } } diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index 7463fe1..8b05fcd 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -54,6 +54,18 @@ public class IceMediaStream private final List checkList = new LinkedList(); + /** + * The name of the System property that allows us to set a custom maximum + * for check list sizes. + */ + public static final String PNAME_MAX_CHECK_LIST_SIZE + = "org.ice4j.MAX_CHECK_LIST_SIZE"; + + /** + * The default maximum size for check lists. + */ + public static final int DEFAULT_MAX_CHECK_LIST_SIZE = 100; + /** * The agent that this media stream belongs to. */ @@ -294,6 +306,83 @@ public class IceMediaStream checkList.add(pair); } } + + orderCheckList(); + pruneCheckList(); + } + + /** + * Orders this stream's pair check list in decreasing order of pair + * priority. If two pairs have identical priority, the ordering amongst + * them is arbitrary. + */ + private void orderCheckList() + { + synchronized(checkList) + { + Collections.sort(checkList, CandidatePair.comparator); + } + } + + + /** + * Removes or, as per the ICE spec, "prunes" pairs that we don't need to + * run checks for. For example, since we cannot send requests directly + * from a reflexive candidate, but only from its base, we go through the + * sorted list of candidate pairs and in every pair where the local + * candidate is server reflexive, we replace the local server reflexive + * candidate with its base. Once this has been done, we remove each pair + * where the local and remote candidates are identical to the local and + * remote candidates of a pair higher up on the priority list. + *

+ * In addition, in order to limit the attacks described in Section 18.5.2 + * of the ICE spec, we limit the total number of pairs and hence + * (connectivity checks) to a specific value, (100 by default). + */ + private void pruneCheckList() + { + synchronized(checkList) + { + //a list that we only use for storing pairs that we've already gone + //through. The list is destroyed at the end of this method. + List tmpCheckList + = new ArrayList(checkList.size()); + + Iterator ckListIter = checkList.iterator(); + + int maxCheckListSize = Integer.getInteger( + PNAME_MAX_CHECK_LIST_SIZE, DEFAULT_MAX_CHECK_LIST_SIZE); + + while(ckListIter.hasNext()) + { + CandidatePair pair = ckListIter.next(); + + //drop all pairs above MAX_CHECK_LIST_SIZE. + if(tmpCheckList.size() > maxCheckListSize) + { + ckListIter.remove(); + continue; + } + + //replace local server reflexive candidates with their base. + LocalCandidate localCnd = pair.getLocalCandidate(); + if( localCnd.getType() + == CandidateType.SERVER_REFLEXIVE_CANDIDATE) + { + pair.setLocalCandidate(localCnd.getBase()); + + //if the new pair corresponds to another one with a higher + //priority, then remove it. + if(tmpCheckList.contains(pair)) + { + ckListIter.remove(); + continue; + } + } + + tmpCheckList.add(pair); + } + } } /** diff --git a/test/test/Ice.java b/test/test/Ice.java index 6895866..35c2bcb 100644 --- a/test/test/Ice.java +++ b/test/test/Ice.java @@ -35,10 +35,15 @@ public class Ice Agent localAgent = createAgent(9090); Agent remotePeer = createAgent(6060); + localAgent.setControlling(true); + remotePeer.setControlling(false); + long endTime = System.currentTimeMillis(); transferRemoteCandidates(localAgent, remotePeer); + System.out.println("Total candidate gathering time: " + + (endTime - startTime) + "ms"); System.out.println("LocalAgent:\n" + localAgent); localAgent.startChecks();