From 834b10cc6926f051d264efc6fca595cf8a701669 Mon Sep 17 00:00:00 2001 From: emcho Date: Sat, 8 May 2010 21:37:10 +0000 Subject: [PATCH] Implements candidate nomination as per sections 7.2.1.5. and 7.2.3.2.4. from RFC 5245 --- src/org/ice4j/ice/Agent.java | 97 ++++++++++++------- src/org/ice4j/ice/CandidatePair.java | 26 +++++ .../ice/checks/ConnectivityCheckClient.java | 17 ++++ .../ice/checks/ConnectivityCheckServer.java | 12 +-- .../ice/checks/IncomingCheckProcessor.java | 44 --------- 5 files changed, 108 insertions(+), 88 deletions(-) delete mode 100644 src/org/ice4j/ice/checks/IncomingCheckProcessor.java diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 4f95406..661dcc6 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -12,15 +12,14 @@ import java.math.*; import java.net.*; import java.security.*; import java.util.*; -import java.util.logging.*; import org.ice4j.*; +import org.ice4j.attribute.*; import org.ice4j.ice.checks.*; import org.ice4j.ice.harvest.*; +import org.ice4j.message.*; import org.ice4j.stack.*; -import sun.management.resources.*; - /** * An Agent could be described as the main class (i.e. the chef d'orchestre) * of an ICE implementation. @@ -33,7 +32,6 @@ import sun.management.resources.*; * @author Emil Ivov */ public class Agent - implements IncomingCheckProcessor { /** * The default maximum size for check lists. @@ -776,12 +774,14 @@ public class Agent * @param priority the priority that the remote party assigned to * @param remoteUFrag the user fragment that we should be using when and if * we decide to send a check to remoteAddress. - * + * @param useCandidate indicates whether the incoming check {@link Request} + * contained the USE-CANDIDATE ICE attribute. */ public void incomingCheckReceived(TransportAddress remoteAddress, TransportAddress localAddress, long priority, - String remoteUFrag) + String remoteUFrag, + boolean useCandidate) { LocalCandidate localCandidate = findLocalCandidate(localAddress); Component parentComponent = localCandidate.getParentComponent(); @@ -795,6 +795,9 @@ public class Agent CandidatePair triggeredPair = new CandidatePair(localCandidate, remoteCandidate); + if(useCandidate) + triggeredPair.setUseCandidateReceived(); + synchronized(startLock) { if(isStarted()) @@ -817,40 +820,52 @@ public class Agent * there's already a pair with the specified remote and local addresses, * puts it in the queue instead. * - * @param triggeredPair the pair containing the local and remote candidate + * @param triggerPair the pair containing the local and remote candidate * that we'd need to trigger a check for. */ - private void triggerCheck(CandidatePair triggeredPair) + private void triggerCheck(CandidatePair triggerPair) { //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()); + triggerPair.getLocalCandidate().getTransportAddress(), + triggerPair.getRemoteCandidate().getTransportAddress()); - IceMediaStream parentStream = triggeredPair.getLocalCandidate() + IceMediaStream parentStream = triggerPair.getLocalCandidate() .getParentComponent().getParentStream(); if (knownPair != null) { - triggeredPair = knownPair; + //if the incoming request contained a USE-CANDIDATE attribute then + //make sure we don't lose this piece of info. + if (triggerPair.useCandidateReceived()) + knownPair.setUseCandidateReceived(); + + triggerPair = 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 ) { + //7.2.1.5. Updating the Nominated Flag + if (!isControlling() && triggerPair.useCandidateReceived()) + { + //If the state of this pair is Succeeded, it means that a + //previous check generated by this pair produced a + //successful response. This would have caused the agent to + //construct a valid pair when that success response was + //received. The agent now sets the nominated flag in the + //valid pair to true. + if( triggerPair.getState() == CandidatePairState.SUCCEEDED ) + nominatePair( triggerPair ); + } + return; } - /** - * RFC 5245: If the state of that pair is In-Progress, the agent - * cancels the in-progress transaction. - **/ + // 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 @@ -863,23 +878,35 @@ public class Agent 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. - */ - parentStream.addToCheckList(triggeredPair); + // 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. + // + parentStream.addToCheckList(triggerPair); } - /** - * 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. - */ - parentStream.getCheckList().scheduleTriggeredCheck(triggeredPair); + // 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 cases. + parentStream.getCheckList().scheduleTriggeredCheck(triggerPair); + } + + /** + * Looks for the specified {@link CandidatePair} with in the valid lists + * of the media streams and sets its nominated flag to true. This + * may end ICE processing for this media stream as per Section 8 in RFC + * 5245. The method has no effect if pairToNominate is not on any + * valid list. + * + * @param pairToNominate the pair that we'd like to nominate in case it's on + * a valid list somewhere. + */ + public void nominatePair(CandidatePair pairToNominate) + { +//implement todo !!! } /** diff --git a/src/org/ice4j/ice/CandidatePair.java b/src/org/ice4j/ice/CandidatePair.java index 2bd6254..4d167fb 100644 --- a/src/org/ice4j/ice/CandidatePair.java +++ b/src/org/ice4j/ice/CandidatePair.java @@ -39,6 +39,12 @@ public class CandidatePair */ private long priority; + /** + * A flag indicating whether we have seen an incoming check request that + * contained the USE-CANDIDATE attribute for this pair. + */ + private boolean useCandidate = false; + /** * A Comparator using the compareTo method of the * CandidatePair. @@ -476,4 +482,24 @@ public class CandidatePair { return connCheckTranID; } + + /** + * Raises the useCandidate flag for this pair. + */ + public void setUseCandidateReceived() + { + this.useCandidate = true; + } + + /** + * Returns true if someone has previously raised this pair's + * useCandidate flag and false otherwise. + * + * @return true if someone has previously raised this pair's + * useCandidate flag and false otherwise. + */ + public boolean useCandidateReceived() + { + return useCandidate; + } } diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java index e948615..7ae4358 100644 --- a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java @@ -347,6 +347,23 @@ public class ConnectivityCheckClient if (wasFrozen) startChecks(checkList); } + + //If the agent was a controlling agent, and it had included a USE- + //CANDIDATE attribute in the Binding request, the valid pair generated + //from that check has its nominated flag set to true. + if(parentAgent.isControlling() + && request.contains(Attribute.USE_CANDIDATE)) + { + parentAgent.nominatePair( validPair ); + } + //If the agent is the controlled agent, the response may be the result + //of a triggered check that was sent in response to a request that + //itself had the USE-CANDIDATE attribute. This case is described in + //Section 7.2.1.5, and may now result in setting the nominated flag for + //the pair learned from the original request. + else if(checkedPair.useCandidateReceived()) + parentAgent.nominatePair( checkedPair ); + } /** diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckServer.java b/src/org/ice4j/ice/checks/ConnectivityCheckServer.java index 0030ef1..3921473 100644 --- a/src/org/ice4j/ice/checks/ConnectivityCheckServer.java +++ b/src/org/ice4j/ice/checks/ConnectivityCheckServer.java @@ -109,11 +109,11 @@ public class ConnectivityCheckServer } long priority = extractPriority(request); + boolean useCandidate = request.contains(Attribute.USE_CANDIDATE); //tell our address handler we saw a new remote address; - addressHandler.incomingCheckReceived(evt.getRemoteAddress(), - evt.getLocalAddress(), - priority, remoteUfrag); + parentAgent.incomingCheckReceived(evt.getRemoteAddress(), + evt.getLocalAddress(), priority, remoteUfrag, useCandidate); Response response = MessageFactory.createBindingResponse( request, evt.getRemoteAddress()); @@ -129,12 +129,6 @@ public class ConnectivityCheckServer //then chances are the 500 will fail too. throw new RuntimeException("Failed to send a response", e); } - - if (!parentAgent.isControlling() - && request.contains(Attribute.USE_CANDIDATE)) - { -/////////////!!!!!!!!!!!!!!! TODO continue here. - } } /** diff --git a/src/org/ice4j/ice/checks/IncomingCheckProcessor.java b/src/org/ice4j/ice/checks/IncomingCheckProcessor.java deleted file mode 100644 index 04035b9..0000000 --- a/src/org/ice4j/ice/checks/IncomingCheckProcessor.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. - * Maintained by the SIP Communicator community (http://sip-communicator.org). - * - * Distributable under LGPL license. See terms of license at gnu.org. - */ -package org.ice4j.ice.checks; - -import org.ice4j.*; - -/** - * This interface is used by {@link ConnectivityCheckServer}s to deliver remote - * addresses that they see while handling incoming connectivity checks. - * Implementations of this interface should determine whether these addresses - * were (or will be) advertised by the remote peer and, if not, consider them - * as new, peer reflexive candidates. - *

- * This is also the interface that allows us to queue triggered checks. - * - * @author Emil Ivov - */ -public interface IncomingCheckProcessor -{ - /** - * Notifies the implementation that the {@link ConnectivityCheckServer} has - * just received a message on localAddress originating at - * remoteAddress carrying the specified priority. This - * will cause us to schedule a triggered check for the corresponding - * remote candidate and potentially to the discovery of a PEER-REFLEXIVE - * candidate. - * - * @param remoteAddress the address that we've just seen, and that is - * potentially a peer-reflexive address. - * @param localAddress the address that we were contacted on. - * @param priority the priority that the remote party assigned to - * @param remoteUFrag the user fragment that we should be using when and if - * we decide to send a check to remoteAddress. - * - */ - public void incomingCheckReceived(TransportAddress remoteAddress, - TransportAddress localAddress, - long priority, - String remoteUFrag); -}