From 9d55baa5948670a6022dc2bbf83046d9ede8744c Mon Sep 17 00:00:00 2001 From: emcho Date: Sat, 1 May 2010 21:34:16 +0000 Subject: [PATCH] Completes support for triggered checks and peer-reflexive candidate discovery --- src/org/ice4j/ice/Agent.java | 134 ++++++++++++++++-- src/org/ice4j/ice/CandidatePair.java | 60 +++++++- src/org/ice4j/ice/CandidatePairState.java | 2 +- src/org/ice4j/ice/Component.java | 23 +++ src/org/ice4j/ice/IceMediaStream.java | 63 ++++++++ .../ice/checks/ConnectivityCheckClient.java | 64 ++++++++- .../ice/checks/ConnectivityCheckServer.java | 10 ++ .../ice4j/stack/StunClientTransaction.java | 18 ++- src/org/ice4j/stack/StunStack.java | 19 +++ 9 files changed, 371 insertions(+), 22 deletions(-) diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 8a4add8..2b8a0c4 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -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 Agent 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 triggeredCheckQueue 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 triggeredCheckQueue - = new Vector(); - /** * The List 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 Candidate with the specified + * remoteAddress if it belongs to any of this {@link Agent}'s + * streams or null if it doesn't. + * + * @param remoteAddress the {@link TransportAddress} we are looking for. + * + * @return the remote Candidate with the specified + * remoteAddress if it belongs to any of this {@link Agent}'s + * streams or null if it doesn't. + */ + public Candidate findRemoteCandidate(TransportAddress remoteAddress) + { + Collection 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 null 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 null 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 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 localAddress 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); } } diff --git a/src/org/ice4j/ice/CandidatePair.java b/src/org/ice4j/ice/CandidatePair.java index 65a6122..defa2b0 100644 --- a/src/org/ice4j/ice/CandidatePair.java +++ b/src/org/ice4j/ice/CandidatePair.java @@ -8,6 +8,8 @@ package org.ice4j.ice; import java.util.*; +import org.ice4j.stack.*; + /** * CandidatePairs map local to remote Candidates 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 CandidatePair instance mapping localCandidate * to remoteCandidate. @@ -150,13 +159,44 @@ public class CandidatePair /** * Sets the CandidatePairState of this pair to state. This * method should only be called by the ice agent, during the execution of - * the ICE procedures. + * the ICE procedures. Note that passing a null transaction for the + * {@link CandidatePairState#IN_PROGRESS} or a non-null 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 In-Progress + * state and null otherwise. + * + * @throws IllegalArgumentException if state is {@link CandidatePairState + * #IN_PROGRESS} and tranID is null. */ - 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 null 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 null if it's in + * any other state. + */ + public TransactionID getConnectivityCheckTransaction() + { + return connCheckTranID; + } } diff --git a/src/org/ice4j/ice/CandidatePairState.java b/src/org/ice4j/ice/CandidatePairState.java index 18a081d..d012324 100644 --- a/src/org/ice4j/ice/CandidatePairState.java +++ b/src/org/ice4j/ice/CandidatePairState.java @@ -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 diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java index 51a493d..cc9c5d2 100644 --- a/src/org/ice4j/ice/Component.java +++ b/src/org/ice4j/ice/Component.java @@ -599,4 +599,27 @@ public class Component return null; } + + /** + * Returns the remote Candidate with the specified + * remoteAddress if it belongs to this {@link Component} or + * null if it doesn't. + * + * @param remoteAddress the {@link TransportAddress} we are looking for. + * + * @return the local LocalCandidate with the specified + * localAddress if it belongs to this component or null + * if it doesn't. + */ + public Candidate findRemoteCandidate(TransportAddress remoteAddress) + { + + for( Candidate remoteCnd : localCandidates) + { + if(remoteCnd.getTransportAddress().equals(remoteAddress)) + return remoteCnd; + } + + return null; + } } diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index 0d447f2..fbdfd99 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -493,4 +493,67 @@ public class IceMediaStream return null; } + + /** + * Returns the local Candidate with the specified + * remoteAddress if it belongs to any of this stream's {@link + * Component}s or null if it doesn't. + * + * @param remoteAddress the {@link TransportAddress} we are looking for. + * + * @return the local Candidate with the specified + * remoteAddress if it belongs to any of this stream's {@link + * Component}s or null if it doesn't. + * + */ + public Candidate findRemoteCandidate(TransportAddress remoteAddress) + { + Collection 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 null 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 null 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; + } } diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java index 467380d..042230a 100644 --- a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java @@ -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 triggeredCheckQueue 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 triggeredCheckQueue + = new LinkedList(); /** * Creates a new ConnectivityCheckHandler 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 pair 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 + * parentClient + * + * @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() + { + + } + } } diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckServer.java b/src/org/ice4j/ice/checks/ConnectivityCheckServer.java index 9c2c493..cc270af 100644 --- a/src/org/ice4j/ice/checks/ConnectivityCheckServer.java +++ b/src/org/ice4j/ice/checks/ConnectivityCheckServer.java @@ -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 triggeredCheckQueue 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 triggeredCheckQueue + = new Vector(); + /** * Creates a new ConnectivityCheckServer setting * parentAgent as the agent that will be used for retrieving diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index 0f8d26e..bfa1fff 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -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); } /** diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index 2abd926..67f1a91 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -123,6 +123,25 @@ public class StunStack netAccessManager.removeSocket(localAddr); } + /** + * Cancels the {@link StunClientTransaction} with the specified + * transactionID. 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 localAddr so that they * won't send messages through any longer and so that we could remove the