Adds CheckList ordering and pruning
This commit is contained in:
parent
af2aa86532
commit
4b7b2e6687
@ -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 <tt>Candidate</tt> for this <tt>Candidate</tt>.
|
||||
*/
|
||||
public Candidate getBase()
|
||||
public LocalCandidate getBase()
|
||||
{
|
||||
return base;
|
||||
}
|
||||
@ -220,7 +220,7 @@ public abstract class Candidate
|
||||
*
|
||||
* @param base the base <tt>Candidate</tt> of this <tt>Candidate</tt>.
|
||||
*/
|
||||
public void setBase(Candidate base)
|
||||
public void setBase(LocalCandidate base)
|
||||
{
|
||||
this.base = base;
|
||||
}
|
||||
|
||||
@ -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 <tt>Comparator</tt> using the <tt>compareTo</tt> method of the
|
||||
* <tt>CandidatePair</tt>.
|
||||
*/
|
||||
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 <tt>LocalCandidate</tt> of this <tt>CandidatePair</tt>.
|
||||
*
|
||||
* @param localCnd the local <tt>Candidate</tt> of this
|
||||
* <tt>CandidatePair</tt>.
|
||||
*/
|
||||
protected void setLocalCandidate( LocalCandidate localCnd)
|
||||
{
|
||||
this.localCandidate = localCnd;;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remote candidate of this <tt>CandidatePair</tt>.
|
||||
*
|
||||
@ -108,6 +126,17 @@ public class CandidatePair
|
||||
return remoteCandidate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the <tt>RemoteCandidate</tt> of this <tt>CandidatePair</tt>.
|
||||
*
|
||||
* @param remoteCnd the local <tt>Candidate</tt> of this
|
||||
* <tt>CandidatePair</tt>.
|
||||
*/
|
||||
protected void setRemoteCandidate( Candidate remoteCnd)
|
||||
{
|
||||
this.remoteCandidate = remoteCnd;;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the state of this <tt>CandidatePair</tt>. 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 <tt>CandidatePair</tt> with the specified object for order.
|
||||
* Returns a negative integer, zero, or a positive integer as this
|
||||
* <tt>CandidatePair</tt> is less than, equal to, or greater than the
|
||||
* specified object.<p>
|
||||
* <tt>CandidatePair</tt>'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.<p>
|
||||
*
|
||||
* @param candidatePair the Object to be compared.
|
||||
* @return a negative integer, zero, or a positive integer as this
|
||||
* <tt>CandidatePair</tt> is less than, equal to, or greater than the
|
||||
* specified object.
|
||||
* <tt>CandidatePair</tt>'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 <tt>CandidatePair</tt> to <tt>targetPair</tt> and returns
|
||||
* <tt>true</tt> if pairs have equal local and equal remote candidates and
|
||||
* <tt>false</tt> otherwise.
|
||||
*
|
||||
* @param targetPair the <tt>Object</tt> that we'd like to compare this
|
||||
* target pair to.
|
||||
*
|
||||
* @return <tt>true</tt> if pairs have equal local and equal remote
|
||||
* candidates and <tt>false</tt> 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 <tt>Comparator</tt> using the <tt>compareTo</tt> method of the
|
||||
* <tt>CandidatePair</tt>
|
||||
*/
|
||||
public static class PairComparator implements Comparator<CandidatePair>
|
||||
{
|
||||
/**
|
||||
* Compares <tt>pair1</tt> and <tt>pair2</tt> for order. Returns a
|
||||
* negative integer, zero, or a positive integer as <tt>pair1</tt>'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 <tt>CandidatePair</tt> to be compared.
|
||||
* @param pair2 the second <tt>CandidatePair</tt> 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
|
||||
* <tt>Object.equals(Object)</tt>. Additionally, this method can return
|
||||
* <tt>true</tt> <i>only</i> if the specified Object is also a comparator
|
||||
* and it imposes the same ordering as this comparator. Thus,
|
||||
* <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
|
||||
* o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
|
||||
* <tt>o1</tt> and <tt>o2</tt>.<p>
|
||||
*
|
||||
* Note that it is <i>always</i> safe <i>not</i> to override
|
||||
* <tt>Object.equals(Object)</tt>. 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 <code>true</code> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,6 +54,18 @@ public class IceMediaStream
|
||||
private final List<CandidatePair> checkList
|
||||
= new LinkedList<CandidatePair>();
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* <p/>
|
||||
* 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<CandidatePair> tmpCheckList
|
||||
= new ArrayList<CandidatePair>(checkList.size());
|
||||
|
||||
Iterator<CandidatePair> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user