Completes failure handling. (RFC 5245: Section 7.1.3.1)

This commit is contained in:
emcho 2010-05-08 21:35:54 +00:00
parent bc285371e0
commit 9fe7cce92e
4 changed files with 107 additions and 16 deletions

View File

@ -637,6 +637,18 @@ public class Agent
public void setControlling(boolean isControlling) public void setControlling(boolean isControlling)
{ {
this.isControlling = isControlling; this.isControlling = isControlling;
//in case we have already initialized our check lists we'd need to
//recompute pair priorities.
List<IceMediaStream> streams = getStreams();
for(IceMediaStream stream : streams)
{
CheckList list = stream.getCheckList();
if (list != null)
list.recomputePairPriorities();
}
} }
/** /**

View File

@ -37,7 +37,7 @@ public class CandidatePair
/** /**
* Priority of the candidate-pair * Priority of the candidate-pair
*/ */
private final long priority; private long priority;
/** /**
* A <tt>Comparator</tt> using the <tt>compareTo</tt> method of the * A <tt>Comparator</tt> using the <tt>compareTo</tt> method of the
@ -77,7 +77,7 @@ public class CandidatePair
this.localCandidate = localCandidate; this.localCandidate = localCandidate;
this.remoteCandidate = remoteCandidate; 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 * is assigned, the agent sorts the candidate pairs in decreasing order of
* priority. If two pairs have identical priority, the ordering amongst * priority. If two pairs have identical priority, the ordering amongst
* them is arbitrary. * 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 //use G and D as local and remote candidate priority names to fit the
//definition in the RFC. //definition in the RFC.
long G = getControllingAgentCandidate().getPriority(); long G = getControllingAgentCandidate().getPriority();
long D = getControlledAgentCandidate().getPriority(); long D = getControlledAgentCandidate().getPriority();
return (long)Math.pow(2, 32)*Math.min(G,D) this.priority = (long)Math.pow(2, 32)*Math.min(G,D)
+ 2*Math.max(G,D) + 2*Math.max(G,D)
+ (G>D?1l:0l); + (G>D?1l:0l);
} }
/** /**

View File

@ -260,4 +260,18 @@ public class CheckList
pairsIter.next().setState(CandidatePairState.WAITING, null); pairsIter.next().setState(CandidatePairState.WAITING, null);
} }
} }
/**
* Recomputes priorities of all pairs in this <tt>CheckList</tt>. Methord is
* useful when an agent changes its <tt>isControlling</tt> 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();
}
}
} }

View File

@ -161,21 +161,80 @@ public class ConnectivityCheckClient
} }
} }
public void processResponse(StunResponseEvent response) /**
* Handles the <tt>response</tt> 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) == 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."); logger.fine("Received a malformed error response.");
return; //malformed error response return; //malformed error response
} }
processErrorResponse(response); processErrorResponse(evt);
} }
} }
/**
* Returns <tt>true</tt> if the {@link Response} in <tt>evt</tt> had a
* source or a destination address that did not match those of the
* {@link Request}, or <tt>false</tt> 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 <tt>true</tt> if the {@link Response} in <tt>evt</tt> had a
* source or a destination address that did not match those of the
* {@link Request}, or <tt>false</tt> 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) private void processErrorResponse(StunResponseEvent evt)
{ {
Response response = evt.getResponse(); Response response = evt.getResponse();
@ -184,6 +243,9 @@ public class ConnectivityCheckClient
ErrorCodeAttribute errorCode = (ErrorCodeAttribute)response ErrorCodeAttribute errorCode = (ErrorCodeAttribute)response
.getAttribute(Attribute.ERROR_CODE); .getAttribute(Attribute.ERROR_CODE);
CandidatePair pair = ((CandidatePair)evt.getTransactionID()
.getApplicationData());
logger.finest("Received error code " + errorCode.getErrorCode()); logger.finest("Received error code " + errorCode.getErrorCode());
//RESOLVE ROLE_CONFLICTS //RESOLVE ROLE_CONFLICTS
@ -192,14 +254,19 @@ public class ConnectivityCheckClient
boolean wasControlling = originalRequest boolean wasControlling = originalRequest
.contains(Attribute.ICE_CONTROLLING); .contains(Attribute.ICE_CONTROLLING);
System.out.println("Swithing to isControlling=" + !wasControlling); logger.finer("Swithing to isControlling=" + !wasControlling);
parentAgent.setControlling(!wasControlling); parentAgent.setControlling(!wasControlling);
CandidatePair pair = ((CandidatePair)evt.getTransactionID()
.getApplicationData());
pair.getParentComponent().getParentStream().getCheckList() pair.getParentComponent().getParentStream().getCheckList()
.scheduleTriggeredCheck(pair); .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);
} }
} }