Modifies the ICE Agent to not self-destruct upon termination and to rather advise explicit preparation for garbage collection.

This commit is contained in:
lubomir.marinov 2010-10-06 08:20:51 +00:00
parent 3f6e5c7564
commit 99e0572c06
4 changed files with 101 additions and 51 deletions

View File

@ -112,7 +112,6 @@
<fileset dir="${dest}"> <fileset dir="${dest}">
<patternset refid="dest.exclude"/> <patternset refid="dest.exclude"/>
<include name="**/*.*"/> <include name="**/*.*"/>
</fileset> </fileset>
</jar> </jar>
</target> </target>

View File

@ -26,7 +26,13 @@ import org.ice4j.stack.*;
* As defined in RFC 3264, an agent is the protocol implementation involved in * As defined in RFC 3264, an agent is the protocol implementation involved in
* the offer/answer exchange. There are two agents involved in an offer/answer * the offer/answer exchange. There are two agents involved in an offer/answer
* exchange. * exchange.
* </p>
* <p> * <p>
* <b>Note</b>: An <tt>Agent</tt> instance should be explicitly prepared for
* garbage collection by calling {@link #free()} on it if timely freeing of the
* associated resources is of importance; otherwise, it will wait for the
* garbage collector to call {@link #finalize()} on it.
* </p>
* *
* @author Emil Ivov * @author Emil Ivov
* @author Lyubomir Marinov * @author Lyubomir Marinov
@ -241,8 +247,8 @@ public class Agent
{ {
IceMediaStream mediaStream IceMediaStream mediaStream
= new IceMediaStream(Agent.this, mediaStreamName); = new IceMediaStream(Agent.this, mediaStreamName);
mediaStreams.put(mediaStreamName, mediaStream);
mediaStreams.put(mediaStreamName, mediaStream);
return mediaStream; return mediaStream;
} }
@ -328,10 +334,8 @@ public class Agent
synchronized(harvesters) synchronized(harvesters)
{ {
for (CandidateHarvester h : harvesters) for (CandidateHarvester h : harvesters)
{
h.harvest(component); h.harvest(component);
} }
}
computeFoundations(component); computeFoundations(component);
@ -374,15 +378,12 @@ public class Agent
for(Component cmp : stream.getComponents()) for(Component cmp : stream.getComponents())
{ {
if(cmp.getRemoteCandidateCount() == 0) if(cmp.getRemoteCandidateCount() == 0)
{
stream.removeComponent(cmp); stream.removeComponent(cmp);
} }
}
if(stream.getComponentCount() == 0) if(stream.getComponentCount() == 0)
removeStream(stream); removeStream(stream);
} }
} }
/** /**
@ -531,10 +532,8 @@ public class Agent
List<LocalCandidate> candidates = component.getLocalCandidates(); List<LocalCandidate> candidates = component.getLocalCandidates();
for (Candidate cand : candidates) for (Candidate cand : candidates)
{
foundationsRegistry.assignFoundation(cand); foundationsRegistry.assignFoundation(cand);
} }
}
/** /**
* Adds <tt>harvester</tt> to the list of harvesters that this agent will * Adds <tt>harvester</tt> to the list of harvesters that this agent will
@ -759,6 +758,7 @@ public class Agent
{ {
int i=0; int i=0;
Collection<IceMediaStream> streams = mediaStreams.values(); Collection<IceMediaStream> streams = mediaStreams.values();
for (IceMediaStream stream : streams) for (IceMediaStream stream : streams)
{ {
if (stream.getCheckList().isActive()) if (stream.getCheckList().isActive())
@ -932,14 +932,13 @@ public class Agent
for (IceMediaStream stream : streamsCollection) for (IceMediaStream stream : streamsCollection)
{ {
CandidatePair pair = stream.findCandidatePair( CandidatePair pair
localAddress, remoteAddress); = stream.findCandidatePair(localAddress, remoteAddress);
if( pair != null ) if( pair != null )
{
return pair; return pair;
} }
} }
}
return null; return null;
} }
@ -1165,9 +1164,7 @@ public class Agent
CheckList checkList = parentStream.getCheckList(); CheckList checkList = parentStream.getCheckList();
if( checkList.getState() == CheckListState.RUNNING ) if( checkList.getState() == CheckListState.RUNNING )
{
checkList.handleNominationConfirmed(nominatedPair); checkList.handleNominationConfirmed(nominatedPair);
}
//Once there is at least one nominated pair in the valid list for //Once there is at least one nominated pair in the valid list for
//every component of the media stream and the state of the //every component of the media stream and the state of the
@ -1240,10 +1237,8 @@ public class Agent
Collection<IceMediaStream> streamsCol = mediaStreams.values(); Collection<IceMediaStream> streamsCol = mediaStreams.values();
for( IceMediaStream stream : streamsCol) for( IceMediaStream stream : streamsCol)
{
num += stream.countHostCandidates(); num += stream.countHostCandidates();
} }
}
return num; return num;
} }
@ -1404,7 +1399,8 @@ public class Agent
@Override @Override
public synchronized void run() public synchronized void run()
{ {
long terminationDelay = Integer.getInteger( long terminationDelay
= Integer.getInteger(
StackProperties.TERMINATION_DELAY, StackProperties.TERMINATION_DELAY,
DEFAULT_TERMINATION_DELAY); DEFAULT_TERMINATION_DELAY);
@ -1414,12 +1410,12 @@ public class Agent
{ {
wait(terminationDelay); wait(terminationDelay);
} }
catch (InterruptedException iex) catch (InterruptedException ie)
{ {
logger.log( logger.log(
Level.FINEST, "Interrupted while waiting. Will " Level.FINEST, "Interrupted while waiting. Will "
+"speed up termination", +"speed up termination",
iex); ie);
} }
} }
@ -1428,18 +1424,18 @@ public class Agent
} }
/** /**
* Prepares everything associated with this {@link Agent} for garbage * Terminates this <tt>Agent</tt> by stopping the handling of connectivity
* collection and moves it into the terminated state. * checks and setting a specific termination state on it.
* *
* @param terminationState the state that we'd like processing to terminate * @param terminationState the state that we'd like processing to terminate
* with or in other words {@link IceProcessingState#TERMINATED} or * with i.e. either {@link IceProcessingState#TERMINATED} or
* {@link IceProcessingState#FAILED} * {@link IceProcessingState#FAILED}
*/ */
private void terminate(IceProcessingState terminationState) private void terminate(IceProcessingState terminationState)
{ {
// free candidates if (!IceProcessingState.FAILED.equals(terminationState)
for(IceMediaStream stream : getStreams()) && !IceProcessingState.TERMINATED.equals(terminationState))
removeStream(stream); throw new IllegalArgumentException("terminationState");
// stop listening for checks // stop listening for checks
connCheckClient.stop(); connCheckClient.stop();
@ -1448,6 +1444,47 @@ public class Agent
setState(terminationState); setState(terminationState);
} }
/**
* Called by the garbage collector when garbage collection determines that
* there are no more references to this instance. Calls {@link #free()} on
* this instance.
*
* @throws Throwable if anything goes wrong and the finalization of this
* instance is to be halted
* @see #free()
*/
@Override
protected void finalize()
throws Throwable
{
free();
super.finalize();
}
/**
* Prepares this <tt>Agent</tt> for garbage collection by setting the
* {@link IceProcessingState#TERMINATED} state on it unless this
* <tt>Agent</tt> is in termination state already and freeing its
* <tt>IceMediaStream</tt>s, <tt>Component</tt>s and <tt>Candidate</tt>s.
*/
public void free()
{
/*
* Set the IceProcessingState#TERMINATED state on this Agent unless it
* is in a termination state already.
*/
IceProcessingState state = getState();
if (!IceProcessingState.FAILED.equals(state)
&& !IceProcessingState.TERMINATED.equals(state))
terminate(IceProcessingState.TERMINATED);
// Free its IceMediaStreams, Components and Candidates.
for(IceMediaStream stream : getStreams())
removeStream(stream);
}
/** /**
* Returns the current generation of this ICE Agent. A generation is an * Returns the current generation of this ICE Agent. A generation is an
* index, starting at 0, that enables the parties to keep track of updates * index, starting at 0, that enables the parties to keep track of updates

View File

@ -30,7 +30,7 @@ public class HostCandidateHarvester
/** /**
* Our class logger. * Our class logger.
*/ */
private final Logger logger private static final Logger logger
= Logger.getLogger(HostCandidateHarvester.class.getName()); = Logger.getLogger(HostCandidateHarvester.class.getName());
/** /**
@ -95,12 +95,15 @@ public class HostCandidateHarvester
//there seems to be a problem with this particular address //there seems to be a problem with this particular address
//let's just move on for now and hope we will find better //let's just move on for now and hope we will find better
if (logger.isLoggable(Level.WARNING)) if (logger.isLoggable(Level.WARNING))
logger.warning("Failed to create a socket for:" {
logger.warning(
"Failed to create a socket for:"
+"\naddr:" + addr +"\naddr:" + addr
+"\npreferredPort:" + preferredPort +"\npreferredPort:" + preferredPort
+"\nminPort:" + minPort +"\nminPort:" + minPort
+"\nmaxPort:" + maxPort +"\nmaxPort:" + maxPort
+"\nContinuing with next address"); +"\nContinuing with next address");
}
continue; continue;
} }
@ -204,18 +207,22 @@ public class HostCandidateHarvester
= new MultiplexingDatagramSocket(port, laddr); = new MultiplexingDatagramSocket(port, laddr);
if(logger.isLoggable(Level.FINEST)) if(logger.isLoggable(Level.FINEST))
logger.log(Level.FINEST, {
logger.log(
Level.FINEST,
"just bound to: " + sock.getLocalSocketAddress()); "just bound to: " + sock.getLocalSocketAddress());
}
return sock; return sock;
} }
catch (SocketException se) catch (SocketException se)
{ {
logger.log(Level.INFO, logger.log(
"Retrying a bind because of a failure to bind to address " Level.INFO,
+ laddr + " and port " + port "Retrying a bind because of a failure to bind to"
+ " address " + laddr
+ " and port " + port
+ " (" + se.getMessage() +")"); + " (" + se.getMessage() +")");
logger.log(Level.INFO, "", se); logger.log(Level.INFO, "", se);
} }
port ++; port ++;

View File

@ -20,7 +20,7 @@ import org.ice4j.security.*;
* both agents and make one of them run checks against the other. * both agents and make one of them run checks against the other.
* *
* @author Emil Ivov * @author Emil Ivov
* @author Lubomir Marinov * @author Lyubomir Marinov
*/ */
public class Ice public class Ice
{ {
@ -118,7 +118,8 @@ public class Ice
"Agent entered the " + iceProcessingState + " state."); "Agent entered the " + iceProcessingState + " state.");
if(iceProcessingState == IceProcessingState.COMPLETED) if(iceProcessingState == IceProcessingState.COMPLETED)
{ {
System.out.println("Total ICE processing time: " System.out.println(
"Total ICE processing time: "
+ (processingEndTime - startTime) + "ms"); + (processingEndTime - startTime) + "ms");
Agent agent = (Agent)evt.getSource(); Agent agent = (Agent)evt.getSource();
List<IceMediaStream> streams = agent.getStreams(); List<IceMediaStream> streams = agent.getStreams();
@ -126,8 +127,8 @@ public class Ice
for(IceMediaStream stream : streams) for(IceMediaStream stream : streams)
{ {
String streamName = stream.getName(); String streamName = stream.getName();
System.out.println("Pairs selected for stream: " System.out.println(
+ streamName); "Pairs selected for stream: " + streamName);
List<Component> components = stream.getComponents(); List<Component> components = stream.getComponents();
for(Component cmp : components) for(Component cmp : components)
@ -142,8 +143,7 @@ public class Ice
for(IceMediaStream stream : streams) for(IceMediaStream stream : streams)
{ {
String streamName = stream.getName(); String streamName = stream.getName();
System.out.println("Check list for stream: " System.out.println("Check list for stream: " + streamName);
+ streamName);
//uncomment for a more verbose output //uncomment for a more verbose output
System.out.println(stream.getCheckList()); System.out.println(stream.getCheckList());
} }
@ -151,6 +151,13 @@ public class Ice
else if(iceProcessingState == IceProcessingState.TERMINATED else if(iceProcessingState == IceProcessingState.TERMINATED
|| iceProcessingState == IceProcessingState.FAILED) || iceProcessingState == IceProcessingState.FAILED)
{ {
/*
* Though the process will be instructed to die, demonstrate
* that Agent instances are to be explicitly prepared for
* garbage collection.
*/
((Agent) evt.getSource()).free();
System.exit(0); System.exit(0);
} }
} }