Completes support for triggered checks and peer-reflexive candidate discovery
This commit is contained in:
parent
5574bd4433
commit
9d55baa594
@ -17,6 +17,7 @@ import java.util.logging.*;
|
||||
import org.ice4j.*;
|
||||
import org.ice4j.ice.checks.*;
|
||||
import org.ice4j.ice.harvest.*;
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
/**
|
||||
* An <tt>Agent</tt> could be described as the main class (i.e. the chef d'orchestre)
|
||||
@ -32,12 +33,6 @@ import org.ice4j.ice.harvest.*;
|
||||
public class Agent
|
||||
implements IncomingCheckProcessor
|
||||
{
|
||||
/**
|
||||
* Our class logger.
|
||||
*/
|
||||
private static final Logger logger
|
||||
= Logger.getLogger(Agent.class.getName());
|
||||
|
||||
/**
|
||||
* The default maximum size for check lists.
|
||||
*/
|
||||
@ -72,15 +67,6 @@ public class Agent
|
||||
private final FoundationsRegistry foundationsRegistry
|
||||
= new FoundationsRegistry();
|
||||
|
||||
/**
|
||||
* The <tt>triggeredCheckQueue</tt> 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<CandidatePair> triggeredCheckQueue
|
||||
= new Vector<CandidatePair>();
|
||||
|
||||
/**
|
||||
* The <tt>List</tt> of remote addresses that we have discovered through
|
||||
* incoming connectivity checks, before actually receiving a session
|
||||
@ -677,6 +663,69 @@ public class Agent
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remote <tt>Candidate</tt> with the specified
|
||||
* <tt>remoteAddress</tt> if it belongs to any of this {@link Agent}'s
|
||||
* streams or <tt>null</tt> if it doesn't.
|
||||
*
|
||||
* @param remoteAddress the {@link TransportAddress} we are looking for.
|
||||
*
|
||||
* @return the remote <tt>Candidate</tt> with the specified
|
||||
* <tt>remoteAddress</tt> if it belongs to any of this {@link Agent}'s
|
||||
* streams or <tt>null</tt> if it doesn't.
|
||||
*/
|
||||
public Candidate findRemoteCandidate(TransportAddress remoteAddress)
|
||||
{
|
||||
Collection<IceMediaStream> streamsCollection = mediaStreams.values();
|
||||
|
||||
for( IceMediaStream stream : streamsCollection)
|
||||
{
|
||||
Candidate cnd = stream.findRemoteCandidate(remoteAddress);
|
||||
|
||||
if(cnd != null)
|
||||
return cnd;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link CandidatePair} with the specified remote and local
|
||||
* addresses or <tt>null</tt> if neither of the {@link CheckList}s in this
|
||||
* {@link Agent}'s streams contain such a pair.
|
||||
*
|
||||
* @param localAddress the local {@link TransportAddress} of the pair we
|
||||
* are looking for.
|
||||
* @param remoteAddress the remote {@link TransportAddress} of the pair we
|
||||
* are looking for.
|
||||
*
|
||||
* @return the {@link CandidatePair} with the specified remote and local
|
||||
* addresses or <tt>null</tt> if neither of the {@link CheckList}s in this
|
||||
* {@link Agent}'s streams contain such a pair.
|
||||
*/
|
||||
public CandidatePair findCandidatePair(TransportAddress localAddress,
|
||||
TransportAddress remoteAddress)
|
||||
{
|
||||
|
||||
synchronized(mediaStreams)
|
||||
{
|
||||
Collection<IceMediaStream> streamsCollection
|
||||
= mediaStreams.values();
|
||||
|
||||
for( IceMediaStream stream : streamsCollection)
|
||||
{
|
||||
CandidatePair pair = stream.findCandidatePair(
|
||||
localAddress, remoteAddress);
|
||||
if( pair != null )
|
||||
{
|
||||
return pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies the implementation that the {@link ConnectivityCheckServer} has
|
||||
* just received a message on <tt>localAddress</tt> originating at
|
||||
@ -737,6 +786,61 @@ public class Agent
|
||||
*/
|
||||
private void triggerCheck(CandidatePair triggeredPair)
|
||||
{
|
||||
//first check whether we already know about the remote address in case
|
||||
//we've just discovered a peer-reflexive candidate.
|
||||
CandidatePair knownPair = this.findCandidatePair(
|
||||
triggeredPair.getLocalCandidate().getTransportAddress(),
|
||||
triggeredPair.getRemoteCandidate().getTransportAddress());
|
||||
|
||||
if (knownPair != null)
|
||||
{
|
||||
triggeredPair = knownPair;
|
||||
|
||||
//we already know about the remote address so we only need to
|
||||
//trigger a check for the existing pair
|
||||
|
||||
/**
|
||||
* RFC 5245: If the state of that pair is Succeeded, nothing
|
||||
* further is done.
|
||||
*/
|
||||
if (knownPair.getState() == CandidatePairState.SUCCEEDED )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* RFC 5245: If the state of that pair is In-Progress, the agent
|
||||
* cancels the in-progress transaction.
|
||||
**/
|
||||
if (knownPair.getState() == CandidatePairState.IN_PROGRESS )
|
||||
{
|
||||
TransactionID checkTransaction
|
||||
= knownPair.getConnectivityCheckTransaction();
|
||||
|
||||
StunStack.getInstance().cancelTransaction(checkTransaction);
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//it appears that we've just discovered a peer-reflexive address.
|
||||
/*
|
||||
* RFC 5245: If the pair is not already on the check list:
|
||||
* The pair is inserted into the check list based on its priority
|
||||
* Its state is set to Waiting [and it] is enqueued into the
|
||||
* triggered check queue.
|
||||
* Emil: This means we actually don't have anything to do here.
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* RFC 5245: The agent MUST create a new connectivity check for that
|
||||
* pair (representing a new STUN Binding request transaction) by
|
||||
* enqueueing the pair in the triggered check queue. The state of
|
||||
* the pair is then changed to Waiting.
|
||||
* Emil: This actually applies for all case.
|
||||
*/
|
||||
connCheckClient.scheduleTriggeredCheck(triggeredPair);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,8 @@ package org.ice4j.ice;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
/**
|
||||
* <tt>CandidatePair</tt>s map local to remote <tt>Candidate</tt>s so that they
|
||||
* could be added to check lists. Connectivity in ICE is always verified by
|
||||
@ -54,6 +56,13 @@ public class CandidatePair
|
||||
*/
|
||||
private CandidatePairState state = CandidatePairState.FROZEN;
|
||||
|
||||
/**
|
||||
* The {@link TransactionID} of the client transaction for a connectivity
|
||||
* check over this pair in case it is in the {@link CandidatePairState#
|
||||
* IN_PROGRESS} state.
|
||||
*/
|
||||
private TransactionID connCheckTranID = null;
|
||||
|
||||
/**
|
||||
* Creates a <tt>CandidatePair</tt> instance mapping <tt>localCandidate</tt>
|
||||
* to <tt>remoteCandidate</tt>.
|
||||
@ -150,13 +159,44 @@ public class CandidatePair
|
||||
/**
|
||||
* Sets the <tt>CandidatePairState</tt> of this pair to <tt>state</tt>. This
|
||||
* method should only be called by the ice agent, during the execution of
|
||||
* the ICE procedures.
|
||||
* the ICE procedures. Note that passing a <tt>null</tt> transaction for the
|
||||
* {@link CandidatePairState#IN_PROGRESS} or a non-<tt>null</tt> for any
|
||||
* other state would cause an {@link IllegalArgumentException} to be thrown.
|
||||
* I know this is not the most elegant way to handle it but this method is
|
||||
* supposed to only be used internally and I prefer the exception rather
|
||||
* than implementing one method for every state transition.
|
||||
*
|
||||
* @param state the state that this candidate pair is to enter.
|
||||
* @param tranID the {@link TransactionID} that we are using for the
|
||||
* connectivity check in case we are entering the <tt>In-Progress</tt>
|
||||
* state and <tt>null</tt> otherwise.
|
||||
*
|
||||
* @throws IllegalArgumentException if state is {@link CandidatePairState
|
||||
* #IN_PROGRESS} and <tt>tranID</tt> is <tt>null</tt>.
|
||||
*/
|
||||
protected void setState(CandidatePairState state)
|
||||
public synchronized void setState(CandidatePairState state,
|
||||
TransactionID tranID)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
this.state = state;
|
||||
|
||||
if(state != CandidatePairState.IN_PROGRESS)
|
||||
{
|
||||
if (tranID != null)
|
||||
throw new IllegalArgumentException(
|
||||
"How could you have a transaction for a pair"
|
||||
+ " that's not in the In-Progress state?");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tranID == null)
|
||||
throw new IllegalArgumentException("Putting a pair into the "
|
||||
+"In-Progress state MUST be accomapnied with "
|
||||
+"the TransactionID of the conn check.");
|
||||
}
|
||||
|
||||
this.connCheckTranID = tranID;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -367,4 +407,20 @@ public class CandidatePair
|
||||
{
|
||||
return getLocalCandidate().getParentComponent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link TransactionID} used in the connectivity check
|
||||
* associated with this {@link CandidatePair} when it's in the
|
||||
* {@link CandidatePairState#IN_PROGRESS} or <tt>null</tt> if it's in
|
||||
* any other state.
|
||||
*
|
||||
* @return the {@link TransactionID} used in the connectivity check
|
||||
* associated with this {@link CandidatePair} when it's in the
|
||||
* {@link CandidatePairState#IN_PROGRESS} or <tt>null</tt> if it's in
|
||||
* any other state.
|
||||
*/
|
||||
public TransactionID getConnectivityCheckTransaction()
|
||||
{
|
||||
return connCheckTranID;
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ public enum CandidatePairState
|
||||
* means that a check has been sent for this pair, but the transaction is
|
||||
* in progress.
|
||||
*/
|
||||
PROGRESS("In-Progress"),
|
||||
IN_PROGRESS("In-Progress"),
|
||||
|
||||
/**
|
||||
* Indicates that the candidate pair is in a "Succeeded" state which means
|
||||
|
||||
@ -599,4 +599,27 @@ public class Component
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remote <tt>Candidate</tt> with the specified
|
||||
* <tt>remoteAddress</tt> if it belongs to this {@link Component} or
|
||||
* <tt>null</tt> if it doesn't.
|
||||
*
|
||||
* @param remoteAddress the {@link TransportAddress} we are looking for.
|
||||
*
|
||||
* @return the local <tt>LocalCandidate</tt> with the specified
|
||||
* <tt>localAddress</tt> if it belongs to this component or <tt>null</tt>
|
||||
* if it doesn't.
|
||||
*/
|
||||
public Candidate findRemoteCandidate(TransportAddress remoteAddress)
|
||||
{
|
||||
|
||||
for( Candidate remoteCnd : localCandidates)
|
||||
{
|
||||
if(remoteCnd.getTransportAddress().equals(remoteAddress))
|
||||
return remoteCnd;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -493,4 +493,67 @@ public class IceMediaStream
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the local <tt>Candidate</tt> with the specified
|
||||
* <tt>remoteAddress</tt> if it belongs to any of this stream's {@link
|
||||
* Component}s or <tt>null</tt> if it doesn't.
|
||||
*
|
||||
* @param remoteAddress the {@link TransportAddress} we are looking for.
|
||||
*
|
||||
* @return the local <tt>Candidate</tt> with the specified
|
||||
* <tt>remoteAddress</tt> if it belongs to any of this stream's {@link
|
||||
* Component}s or <tt>null</tt> if it doesn't.
|
||||
*
|
||||
*/
|
||||
public Candidate findRemoteCandidate(TransportAddress remoteAddress)
|
||||
{
|
||||
Collection<Component> cmpCol = components.values();
|
||||
|
||||
for( Component cmp : cmpCol)
|
||||
{
|
||||
Candidate cnd = cmp.findRemoteCandidate(remoteAddress);
|
||||
|
||||
if(cnd != null)
|
||||
return cnd;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the {@link CandidatePair} with the specified remote and local
|
||||
* addresses or <tt>null</tt> if neither of the {@link CheckList}s in this
|
||||
* stream contain such a pair.
|
||||
*
|
||||
* @param localAddress the local {@link TransportAddress} of the pair we
|
||||
* are looking for.
|
||||
* @param remoteAddress the remote {@link TransportAddress} of the pair we
|
||||
* are looking for.
|
||||
*
|
||||
* @return the {@link CandidatePair} with the specified remote and local
|
||||
* addresses or <tt>null</tt> if neither of the {@link CheckList}s in this
|
||||
* stream contain such a pair.
|
||||
*/
|
||||
public CandidatePair findCandidatePair(TransportAddress localAddress,
|
||||
TransportAddress remoteAddress)
|
||||
{
|
||||
|
||||
synchronized(checkList)
|
||||
{
|
||||
for( CandidatePair pair : checkList)
|
||||
{
|
||||
if( pair.getLocalCandidate().getTransportAddress()
|
||||
.equals(localAddress)
|
||||
&& pair.getRemoteCandidate().getTransportAddress()
|
||||
.equals(remoteAddress) )
|
||||
{
|
||||
return pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package org.ice4j.ice.checks;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
import org.ice4j.*;
|
||||
@ -36,7 +37,16 @@ public class ConnectivityCheckClient
|
||||
/**
|
||||
* The stun stack that we will use for connectivity checks.
|
||||
*/
|
||||
StunStack stunStack = StunStack.getInstance();
|
||||
private StunStack stunStack = StunStack.getInstance();
|
||||
|
||||
/**
|
||||
* The <tt>triggeredCheckQueue</tt> 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<CandidatePair> triggeredCheckQueue
|
||||
= new LinkedList<CandidatePair>();
|
||||
|
||||
/**
|
||||
* Creates a new <tt>ConnectivityCheckHandler</tt> setting
|
||||
@ -113,7 +123,10 @@ public class ConnectivityCheckClient
|
||||
tran = stunStack.sendRequest(request,
|
||||
pair.getRemoteCandidate().getTransportAddress(), stunSocket,
|
||||
this);
|
||||
System.out.println("checking pair " + pair + " with tran=" + tran.toString());
|
||||
|
||||
if(logger.isLoggable(Level.FINEST))
|
||||
logger.finest("checking pair " + pair + " with tran="
|
||||
+ tran.toString());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
@ -139,5 +152,52 @@ System.out.println("checking pair " + pair + " with tran=" + tran.toString());
|
||||
System.out.println("failure event=" + event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds <tt>pair</tt> 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.
|
||||
*/
|
||||
private static class PaceMaker extends Thread
|
||||
{
|
||||
/**
|
||||
* The {@link ConnectivityCheckClient} that created us.
|
||||
*/
|
||||
private final ConnectivityCheckClient parentClient;
|
||||
|
||||
/**
|
||||
* Creates a new {@link PaceMaker} for the specified
|
||||
* <tt>parentClient</tt>
|
||||
*
|
||||
* @param parentClient the {@link ConnectivityCheckClient} that who's
|
||||
* checks we are going to run.
|
||||
*/
|
||||
public PaceMaker(ConnectivityCheckClient parentClient)
|
||||
{
|
||||
super("ICE PaceMaker:" + parentClient.parentAgent.getLocalUfrag());
|
||||
this.parentClient = parentClient;
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
*/
|
||||
package org.ice4j.ice.checks;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
import org.ice4j.*;
|
||||
@ -44,6 +45,15 @@ public class ConnectivityCheckServer
|
||||
*/
|
||||
private StunStack stunStack = StunStack.getInstance();
|
||||
|
||||
/**
|
||||
* The <tt>triggeredCheckQueue</tt> 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<CandidatePair> triggeredCheckQueue
|
||||
= new Vector<CandidatePair>();
|
||||
|
||||
/**
|
||||
* Creates a new <tt>ConnectivityCheckServer</tt> setting
|
||||
* <tt>parentAgent</tt> as the agent that will be used for retrieving
|
||||
|
||||
@ -293,14 +293,28 @@ class StunClientTransaction
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Cancels the transaction. Once this method is called the transaction is
|
||||
* considered terminated and will stop retransmissions.
|
||||
*
|
||||
* @param waitForResponse indicates whether we should wait for the current
|
||||
* RTO to expire before ending the transaction or immediately terminate.
|
||||
*/
|
||||
synchronized void cancel(boolean waitForResponse)
|
||||
{
|
||||
this.cancelled = true;
|
||||
|
||||
if(!waitForResponse)
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the transaction. Once this method is called the transaction is
|
||||
* considered terminated and will stop retransmissions.
|
||||
*/
|
||||
synchronized void cancel()
|
||||
{
|
||||
this.cancelled = true;
|
||||
notifyAll();
|
||||
cancel(false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -123,6 +123,25 @@ public class StunStack
|
||||
netAccessManager.removeSocket(localAddr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the {@link StunClientTransaction} with the specified
|
||||
* <tt>transactionID</tt>. Cancellation means that the stack will not
|
||||
* retransmit the request, will not treat the lack of response to be a
|
||||
* failure, but will wait the duration of the transaction timeout for a
|
||||
* response.
|
||||
*
|
||||
* @param transactionID the {@link TransactionID} of the
|
||||
* {@link StunClientTransaction} to cancel
|
||||
*/
|
||||
public void cancelTransaction(TransactionID transactionID)
|
||||
{
|
||||
StunClientTransaction clientTransaction
|
||||
= clientTransactions.get(transactionID);
|
||||
|
||||
if(clientTransaction != null)
|
||||
clientTransaction.cancel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops all transactions for the specified <tt>localAddr</tt> so that they
|
||||
* won't send messages through any longer and so that we could remove the
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user