diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 6afe4af..584e0a0 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -637,6 +637,18 @@ public class Agent public void setControlling(boolean isControlling) { this.isControlling = isControlling; + + //in case we have already initialized our check lists we'd need to + //recompute pair priorities. + List streams = getStreams(); + for(IceMediaStream stream : streams) + { + CheckList list = stream.getCheckList(); + + if (list != null) + list.recomputePairPriorities(); + } + } /** diff --git a/src/org/ice4j/ice/CandidatePair.java b/src/org/ice4j/ice/CandidatePair.java index defa2b0..01548c5 100644 --- a/src/org/ice4j/ice/CandidatePair.java +++ b/src/org/ice4j/ice/CandidatePair.java @@ -37,7 +37,7 @@ public class CandidatePair /** * Priority of the candidate-pair */ - private final long priority; + private long priority; /** * A Comparator using the compareTo method of the @@ -77,7 +77,7 @@ public class CandidatePair this.localCandidate = localCandidate; this.remoteCandidate = remoteCandidate; - priority = computePriority(); + computePriority(); } /** @@ -254,19 +254,17 @@ public class CandidatePair * is assigned, the agent sorts the candidate pairs in decreasing order of * priority. If two pairs have identical priority, the ordering amongst * them is arbitrary. - * - * @return a long indicating the priority of this candidate pair. */ - private long computePriority() + protected void computePriority() { //use G and D as local and remote candidate priority names to fit the //definition in the RFC. long G = getControllingAgentCandidate().getPriority(); long D = getControlledAgentCandidate().getPriority(); - return (long)Math.pow(2, 32)*Math.min(G,D) - + 2*Math.max(G,D) - + (G>D?1l:0l); + this.priority = (long)Math.pow(2, 32)*Math.min(G,D) + + 2*Math.max(G,D) + + (G>D?1l:0l); } /** diff --git a/src/org/ice4j/ice/CheckList.java b/src/org/ice4j/ice/CheckList.java index 53f1ea6..ada5f9b 100644 --- a/src/org/ice4j/ice/CheckList.java +++ b/src/org/ice4j/ice/CheckList.java @@ -260,4 +260,18 @@ public class CheckList pairsIter.next().setState(CandidatePairState.WAITING, null); } } + + /** + * Recomputes priorities of all pairs in this CheckList. Methord is + * useful when an agent changes its isControlling property as a + * result of a role conflict. + */ + protected synchronized void recomputePairPriorities() + { + //first, determine the pairs that we'd need to put in the waiting state. + for(CandidatePair pair : this) + { + pair.computePriority(); + } + } } diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java index c62ac9e..aacc34b 100644 --- a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java @@ -161,21 +161,80 @@ public class ConnectivityCheckClient } } - public void processResponse(StunResponseEvent response) + /** + * Handles the response as per the procedures described in RFC 5245 + * or in other words, by either changing the state of the corresponding pair + * to FAILED, or SUCCEDED, or rescheduling a check in case of a role + * conflict. + * + * @param evt the {@link StunResponseEvent} that contains the newly + * received response. + */ + public void processResponse(StunResponseEvent evt) { - if(response.getResponse().getMessageType() + //make sure that the response came from the right place. + if (!checkSymmetricAddresses(evt)) + { + CandidatePair pair = ((CandidatePair)evt.getTransactionID() + .getApplicationData()); + + logger.fine("Received a non-symmetric response for pair: "+ pair + +". Failing"); + pair.setState(CandidatePairState.FAILED, null); + } + + //handle error responses. + if(evt.getResponse().getMessageType() == Response.BINDING_ERROR_RESPONSE) { - if(! response.getResponse().contains(Attribute.ERROR_CODE)) + if(! evt.getResponse().contains(Attribute.ERROR_CODE)) { logger.fine("Received a malformed error response."); return; //malformed error response } - processErrorResponse(response); + processErrorResponse(evt); } + + } + /** + * Returns true if the {@link Response} in evt had a + * source or a destination address that did not match those of the + * {@link Request}, or false otherwise. + * RFC 5245: The agent MUST check that the source IP address and port of + * the response equal the destination IP address and port to which the + * Binding request was sent, and that the destination IP address and + * port of the response match the source IP address and port from which + * the Binding request was sent. In other words, the source and + * destination transport addresses in the request and responses are + * symmetric. If they are not symmetric, the agent sets the state of + * the pair to Failed. + * + * @param evt the {@link StunResponseEvent} that contains the {@link + * Response} we need to examine + * + * @return true if the {@link Response} in evt had a + * source or a destination address that did not match those of the + * {@link Request}, or false otherwise. + */ + private boolean checkSymmetricAddresses(StunResponseEvent evt) + { + CandidatePair pair = ((CandidatePair)evt.getTransactionID() + .getApplicationData()); + + return pair.getLocalCandidate().equals(evt.getLocalAddress()) + && pair.getRemoteCandidate().equals(evt.getRemoteAddress()); + } + + /** + * In case of a role conflict, changes the state of the agent and + * reschedules the check, in all other cases sets the corresponding peer + * state to FAILED. + * + * @param evt the event that delivered the error response. + */ private void processErrorResponse(StunResponseEvent evt) { Response response = evt.getResponse(); @@ -184,6 +243,9 @@ public class ConnectivityCheckClient ErrorCodeAttribute errorCode = (ErrorCodeAttribute)response .getAttribute(Attribute.ERROR_CODE); + CandidatePair pair = ((CandidatePair)evt.getTransactionID() + .getApplicationData()); + logger.finest("Received error code " + errorCode.getErrorCode()); //RESOLVE ROLE_CONFLICTS @@ -192,14 +254,19 @@ public class ConnectivityCheckClient boolean wasControlling = originalRequest .contains(Attribute.ICE_CONTROLLING); -System.out.println("Swithing to isControlling=" + !wasControlling); + logger.finer("Swithing to isControlling=" + !wasControlling); parentAgent.setControlling(!wasControlling); - CandidatePair pair = ((CandidatePair)evt.getTransactionID() - .getApplicationData()); - pair.getParentComponent().getParentStream().getCheckList() .scheduleTriggeredCheck(pair); + + return; + } + else + { + logger.fine("Received an unrecoverable error response for pair " + + pair + " will mark the pair as FAILED."); + pair.setState(CandidatePairState.FAILED, null); } }