diff --git a/src/org/ice4j/ice/Candidate.java b/src/org/ice4j/ice/Candidate.java index 72f996b..dc1afaf 100644 --- a/src/org/ice4j/ice/Candidate.java +++ b/src/org/ice4j/ice/Candidate.java @@ -408,9 +408,9 @@ public class Candidate { //prefer link local addresses to global ones if(addr.isLinkLocalAddress()) - return 40; - else return 30; + else + return 40; } else { diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java index a500e04..2033b03 100644 --- a/src/org/ice4j/ice/Component.java +++ b/src/org/ice4j/ice/Component.java @@ -67,6 +67,13 @@ public class Component */ private List remoteCandidates = new LinkedList(); + /** + * A Comparator that we use for sorting Candidates by + * their priority. + */ + private CandidatePrioritizer candidatePrioritizer + = new CandidatePrioritizer(); + /** * Creates a new Component with the specified componentID * as a child of the specified MediaStream. @@ -318,36 +325,86 @@ public class Component } /** - * + * Computes the priorities of all Candidates and then sorts them + * accordingly. */ public void prioritizeCandidates() { - //Arrays.sort - /* synchronized(localCandidates) { - TreeSet prioritizedCands = new TreeSet(new Comparator() - { - public int compare(Candidate c1, Candidate c2) - { - return -1; - } + Candidate[] candidates = new Candidate[localCandidates.size()]; + localCandidates.toArray(candidates); - public boolean equal(Candidate c1, Candidate c2) - { - return true; - } - }); - - for (Candidate cand : localCandidates) + //first compute the actual priorities + for (Candidate cand : candidates) { cand.computePriority(); - prioritizedCands.add(cand); } + //sort + Arrays.sort(candidates, candidatePrioritizer); + + //now re-add the candidates in the order they've been sorted in. localCandidates.clear(); - localCandidates.addAll(prioritizedCands); + for (Candidate cand : candidates) + localCandidates.add(cand); + } + } + + /** + * Compares candidates based on their priority. + */ + private static class CandidatePrioritizer + implements Comparator + { + /** + * Compares the two Candidates based on their priority and + * returns a negative integer, zero, or a positive integer as the first + * Candidate has a lower, equal, or greater priority than the + * second. + * + * @param c1 the first Candidate to compare. + * @param c2 the second Candidate to compare. + * + * @return a negative integer, zero, or a positive integer as the first + * Candidate has a lower, equal, or greater priority than the + * second. + */ + public int compare(Candidate c1, Candidate c2) + { + if(c1.getPriority() < c2.getPriority()) + return 1; + else if(c1.getPriority() == c2.getPriority()) + return 0; + else //if(c1.getPriority() > c2.getPriority()) + return -1; + } + + /** + * Indicates whether some other object is "equal 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 CandidatePrioritizer); } - */ } } diff --git a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java index 20b05ec..abec65f 100644 --- a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -98,7 +98,8 @@ public class HostCandidateHarvester +"\naddr:" + addr +"\npreferredPort:" + preferredPort +"\nminPort:" + minPort - +"\nmaxPort:" + maxPort); + +"\nmaxPort:" + maxPort + +"\nContinuing with next address"); continue; } @@ -203,7 +204,9 @@ public class HostCandidateHarvester { logger.log(Level.INFO, "Retrying a bind because of a failure to bind to address " - + laddr + " and port " + port, se); + + laddr + " and port " + port); + logger.log(Level.FINEST, "", se); + } port ++;