Keeps the Conn Check Server going until we free the agent at the end of a call, so that we can answer STUN binding requests (that Chrome's WebRTC impl seems to be using to determine if we're still there

This commit is contained in:
emcho 2013-03-21 00:30:42 +00:00
parent 4cc6d4ab72
commit a9d4dc4406
2 changed files with 139 additions and 93 deletions

View File

@ -565,8 +565,23 @@ public class Agent
*/ */
public boolean isStarted() public boolean isStarted()
{ {
return state != IceProcessingState.WAITING && return state != IceProcessingState.WAITING
state != IceProcessingState.TERMINATED; && state != IceProcessingState.COMPLETED
&& state != IceProcessingState.TERMINATED;
}
/**
* Indicates whether this {@link Agent} has finished ICE processing.
*
* @return <tt>true</tt> if ICE processing is in the {@link
* IceProcessingState#FAILED}, {@link IceProcessingState#COMPLETED} or
* {@link IceProcessingState#TERMINATED} and <tt>false</tt> otherwise.
*/
public boolean isOver()
{
return state == IceProcessingState.COMPLETED
|| state != IceProcessingState.TERMINATED
|| state != IceProcessingState.FAILED;
} }
/** /**
@ -1273,6 +1288,15 @@ public class Agent
String localUFrag, String localUFrag,
boolean useCandidate) boolean useCandidate)
{ {
if (isOver())
{
//means we already completed ICE and are happily running media
//the only reason we are called is most probably because the remote
//party is still sending Binding Requests our way and making sure we
//are still alive.
return;
}
String ufrag = null; String ufrag = null;
LocalCandidate localCandidate = null; LocalCandidate localCandidate = null;
@ -1604,7 +1628,7 @@ public class Agent
boolean allListsEnded = true; boolean allListsEnded = true;
boolean atLeastOneListSucceeded = false; boolean atLeastOneListSucceeded = false;
if(state == IceProcessingState.COMPLETED) if(getState() == IceProcessingState.COMPLETED)
return; return;
List<IceMediaStream> streams = getStreams(); List<IceMediaStream> streams = getStreams();
@ -1626,84 +1650,93 @@ public class Agent
} }
} }
if(!allListsEnded)
return;
if(!atLeastOneListSucceeded)
{
//all lists ended but none succeeded. No love today ;(
logger.info("ICE state is FAILED");
terminate(IceProcessingState.FAILED);
return;
}
//Once the state of each check list is Completed: //Once the state of each check list is Completed:
//The agent sets the state of ICE processing overall to Completed. //The agent sets the state of ICE processing overall to Completed.
if(allListsEnded) if(getState() != IceProcessingState.RUNNING)
{ {
if(atLeastOneListSucceeded) //Oh, seems like we already did this.
return;
}
logger.info("ICE state is COMPLETED");
setState(IceProcessingState.COMPLETED);
// keep ICE running (answer binding request)
if(stunKeepAliveThread == null)
{
// schedule STUN checks for selected candidates
scheduleSTUNKeepAlive();
}
if(compatibilityMode == CompatibilityMode.RFC5245)
{
scheduleTermination();
}
//print logs for the types of addresses we chose.
logCandTypes();
}
/**
* Goes through all streams and components and prints into the logs the type
* of local candidates that were selected as well as the server that
* were used (if any) to obtain them.
*/
private void logCandTypes()
{
List<IceMediaStream> strms = getStreams();
for(IceMediaStream stream : strms)
{
for(Component component : stream.getComponents())
{ {
if(getState() == IceProcessingState.RUNNING) CandidatePair selectedPair = component.getSelectedPair();
StringBuffer buf = new StringBuffer( "Harvester selected for ");
buf.append(component.toShortString());
buf.append(" ");
if(selectedPair == null)
{ {
logger.info("ICE state is COMPLETED"); buf.append("none (conn checks failed)");
logger.info(buf.toString());
setState(IceProcessingState.COMPLETED); continue;
// keep ICE running (answer binding request)
if(stunKeepAliveThread == null)
{
// schedule STUN checks for selected candidates
scheduleSTUNKeepAlive();
}
if(compatibilityMode == CompatibilityMode.RFC5245)
{
scheduleTermination();
}
List<IceMediaStream> strms = getStreams();
// logs the harvester type and server used for each of
// of the components
for(IceMediaStream stream : strms)
{
for(Component c : stream.getComponents())
{
StringBuffer buf = new StringBuffer(
"Harvester selected for ");
buf.append(c.toShortString());
buf.append(" ");
if(c.getSelectedPair() == null)
{
buf.append("none ");
buf.append("(it means that conncheck failed)");
logger.info(buf.toString());
continue;
}
TransportAddress serverAddr = c.getSelectedPair().
getLocalCandidate().getStunServerAddress();
TransportAddress relayAddr =
c.getSelectedPair().getLocalCandidate().
getRelayServerAddress();
buf.append(c.getSelectedPair().getLocalCandidate().
getType());
if(serverAddr != null)
{
buf.append(" (server = ");
buf.append(c.getSelectedPair().
getLocalCandidate().
getStunServerAddress());
buf.append(")");
}
else if(relayAddr != null)
{
buf.append(" (relay = ");
buf.append(c.getSelectedPair().
getLocalCandidate().
getRelayServerAddress());
buf.append(")");
}
logger.info(buf.toString());
}
}
} }
}
else Candidate localCnd = selectedPair.getLocalCandidate();
{
logger.info("ICE state is FAILED"); TransportAddress serverAddr = localCnd.getStunServerAddress();
terminate(IceProcessingState.FAILED); TransportAddress relayAddr = localCnd.getRelayServerAddress();
buf.append(localCnd.getType());
if(serverAddr != null)
{
buf.append(" (STUN server = ");
buf.append(localCnd.getStunServerAddress());
buf.append(")");
}
else if(relayAddr != null)
{
buf.append(" (relay = ");
buf.append(localCnd.getRelayServerAddress());
buf.append(")");
}
logger.info(buf.toString());
} }
} }
} }
@ -1855,7 +1888,7 @@ public class Agent
} }
/** /**
* Initializes and starts the {@link STUNKeepAliveThread} * Initializes and starts the {@link StunKeepAliveThread}
*/ */
private void scheduleSTUNKeepAlive() private void scheduleSTUNKeepAlive()
{ {
@ -1937,9 +1970,13 @@ public class Agent
&& !IceProcessingState.TERMINATED.equals(terminationState)) && !IceProcessingState.TERMINATED.equals(terminationState))
throw new IllegalArgumentException("terminationState"); throw new IllegalArgumentException("terminationState");
// stop listening for checks // stop making any checks.
connCheckClient.stop(); connCheckClient.stop();
connCheckServer.stop();
//do not stop the conn check server here because it may still need to
//process STUN Binding Requests that remote agents may send our way.
//we'll do this in "free()" instead.
//connCheckServer.stop();
setState(terminationState); setState(terminationState);
} }
@ -1963,10 +2000,10 @@ public class Agent
} }
/** /**
* Prepares this <tt>Agent</tt> for garbage collection by setting the * Prepares this <tt>Agent</tt> for garbage collection by ending all related
* {@link IceProcessingState#TERMINATED} state on it unless this * processes and freeing its <tt>IceMediaStream</tt>s, <tt>Component</tt>s
* <tt>Agent</tt> is in termination state already and freeing its * and <tt>Candidate</tt>s. This method will also place the agent in the
* <tt>IceMediaStream</tt>s, <tt>Component</tt>s and <tt>Candidate</tt>s. * terminated state in case it wasn't already there.
*/ */
public void free() public void free()
{ {
@ -1974,18 +2011,24 @@ public class Agent
shutdown = true; shutdown = true;
//stop sending keep alives (STUN Binding Indications).
if(stunKeepAliveThread != null) if(stunKeepAliveThread != null)
stunKeepAliveThread.interrupt(); stunKeepAliveThread.interrupt();
//stop responding to STUN Binding Requests.
connCheckServer.stop();
/* /*
* Set the IceProcessingState#TERMINATED state on this Agent unless it * Set the IceProcessingState#TERMINATED state on this Agent unless it
* is in a termination state already. * is in a termination state already.
*/ */
IceProcessingState state = getState(); IceProcessingState state = getState();
if (!IceProcessingState.FAILED.equals(state) if ( !IceProcessingState.FAILED.equals(state)
&& !IceProcessingState.TERMINATED.equals(state)) && !IceProcessingState.TERMINATED.equals(state))
{
terminate(IceProcessingState.TERMINATED); terminate(IceProcessingState.TERMINATED);
}
logger.info("remove streams"); logger.info("remove streams");
// Free its IceMediaStreams, Components and Candidates. // Free its IceMediaStreams, Components and Candidates.
@ -2090,20 +2133,23 @@ public class Agent
{ {
if(compatibilityMode == CompatibilityMode.GTALK) if(compatibilityMode == CompatibilityMode.GTALK)
{ {
//apparently GTalk insists on seeing binding
//requests or else it believes we are dead
//Emil: I am not sure this is true. I think it would
//be enough for us to respond to their binding
//requests. At least that's how it seems to work for
//Chrome and WebRTC.
//TODO: check and maybe remove
connCheckClient.startCheckForPair(pair); connCheckClient.startCheckForPair(pair);
} }
else if(compatibilityMode == CompatibilityMode.RFC5245) else if(compatibilityMode == CompatibilityMode.RFC5245)
{ {
connCheckClient.sendBindingIndicationForPair(pair); connCheckClient.sendBindingIndicationForPair(pair);
} }
pair = null;
} }
} }
stream = null;
} }
streams = null;
if(shutdown) if(shutdown)
break; break;

View File

@ -91,9 +91,9 @@ class ConnectivityCheckServer
UsernameAttribute uname = (UsernameAttribute)request UsernameAttribute uname = (UsernameAttribute)request
.getAttribute(Attribute.USERNAME); .getAttribute(Attribute.USERNAME);
if(uname == null || if( uname == null
(parentAgent.getCompatibilityMode() == CompatibilityMode.RFC5245 || ( parentAgent.getCompatibilityMode() == CompatibilityMode.RFC5245
&& !checkLocalUserName(new String(uname.getUsername())))) && !checkLocalUserName(new String(uname.getUsername()))))
{ {
return; return;
} }
@ -143,8 +143,8 @@ class ConnectivityCheckServer
remoteUfrag = username.substring(0, colon); remoteUfrag = username.substring(0, colon);
} }
if(parentAgent.getCompatibilityMode() == CompatibilityMode.GTALK && if( parentAgent.getCompatibilityMode() == CompatibilityMode.GTALK
parentAgent.findCandidatePair(localUFrag, remoteUfrag) == null) && parentAgent.findCandidatePair(localUFrag, remoteUfrag) == null)
{ {
logger.info("No candidate pair that match local and remote ufrag"); logger.info("No candidate pair that match local and remote ufrag");