From 99e0572c061d2084a25835445b94476a9e3cde81 Mon Sep 17 00:00:00 2001 From: "lubomir.marinov" Date: Wed, 6 Oct 2010 08:20:51 +0000 Subject: [PATCH] Modifies the ICE Agent to not self-destruct upon termination and to rather advise explicit preparation for garbage collection. --- build.xml | 1 - src/org/ice4j/ice/Agent.java | 95 +++++++++++++------ .../ice/harvest/HostCandidateHarvester.java | 35 ++++--- test/test/Ice.java | 21 ++-- 4 files changed, 101 insertions(+), 51 deletions(-) diff --git a/build.xml b/build.xml index 1c4f37a..04e848a 100644 --- a/build.xml +++ b/build.xml @@ -112,7 +112,6 @@ - diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 22e5058..dcca2ac 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -26,7 +26,13 @@ import org.ice4j.stack.*; * 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 * exchange. + *

*

+ * Note: An Agent 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. + *

* * @author Emil Ivov * @author Lyubomir Marinov @@ -241,8 +247,8 @@ public class Agent { IceMediaStream mediaStream = new IceMediaStream(Agent.this, mediaStreamName); - mediaStreams.put(mediaStreamName, mediaStream); + mediaStreams.put(mediaStreamName, mediaStream); return mediaStream; } @@ -327,10 +333,8 @@ public class Agent //todo: run harvesters in a parallel manner synchronized(harvesters) { - for (CandidateHarvester h : harvesters ) - { + for (CandidateHarvester h : harvesters) h.harvest(component); - } } computeFoundations(component); @@ -374,15 +378,12 @@ public class Agent for(Component cmp : stream.getComponents()) { if(cmp.getRemoteCandidateCount() == 0) - { stream.removeComponent(cmp); - } } if(stream.getComponentCount() == 0) removeStream(stream); } - } /** @@ -531,9 +532,7 @@ public class Agent List candidates = component.getLocalCandidates(); for (Candidate cand : candidates) - { foundationsRegistry.assignFoundation(cand); - } } /** @@ -759,6 +758,7 @@ public class Agent { int i=0; Collection streams = mediaStreams.values(); + for (IceMediaStream stream : streams) { if (stream.getCheckList().isActive()) @@ -930,14 +930,13 @@ public class Agent Collection streamsCollection = mediaStreams.values(); - for( IceMediaStream stream : streamsCollection) + for (IceMediaStream stream : streamsCollection) { - CandidatePair pair = stream.findCandidatePair( - localAddress, remoteAddress); + CandidatePair pair + = stream.findCandidatePair(localAddress, remoteAddress); + if( pair != null ) - { return pair; - } } } @@ -1165,9 +1164,7 @@ public class Agent CheckList checkList = parentStream.getCheckList(); if( checkList.getState() == CheckListState.RUNNING ) - { checkList.handleNominationConfirmed(nominatedPair); - } //Once there is at least one nominated pair in the valid list for //every component of the media stream and the state of the @@ -1240,9 +1237,7 @@ public class Agent Collection streamsCol = mediaStreams.values(); for( IceMediaStream stream : streamsCol) - { num += stream.countHostCandidates(); - } } return num; @@ -1404,9 +1399,10 @@ public class Agent @Override public synchronized void run() { - long terminationDelay = Integer.getInteger( - StackProperties.TERMINATION_DELAY, - DEFAULT_TERMINATION_DELAY); + long terminationDelay + = Integer.getInteger( + StackProperties.TERMINATION_DELAY, + DEFAULT_TERMINATION_DELAY); if (terminationDelay >= 0) { @@ -1414,12 +1410,12 @@ public class Agent { wait(terminationDelay); } - catch (InterruptedException iex) + catch (InterruptedException ie) { logger.log( Level.FINEST, "Interrupted while waiting. Will " +"speed up termination", - iex); + ie); } } @@ -1428,18 +1424,18 @@ public class Agent } /** - * Prepares everything associated with this {@link Agent} for garbage - * collection and moves it into the terminated state. + * Terminates this Agent by stopping the handling of connectivity + * checks and setting a specific termination state on it. * * @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} */ private void terminate(IceProcessingState terminationState) { - // free candidates - for(IceMediaStream stream : getStreams()) - removeStream(stream); + if (!IceProcessingState.FAILED.equals(terminationState) + && !IceProcessingState.TERMINATED.equals(terminationState)) + throw new IllegalArgumentException("terminationState"); // stop listening for checks connCheckClient.stop(); @@ -1448,6 +1444,47 @@ public class Agent 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 Agent for garbage collection by setting the + * {@link IceProcessingState#TERMINATED} state on it unless this + * Agent is in termination state already and freeing its + * IceMediaStreams, Components and Candidates. + */ + 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 * index, starting at 0, that enables the parties to keep track of updates diff --git a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java index 2b7c115..d6a2bc0 100644 --- a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -30,7 +30,7 @@ public class HostCandidateHarvester /** * Our class logger. */ - private final Logger logger + private static final Logger logger = Logger.getLogger(HostCandidateHarvester.class.getName()); /** @@ -95,12 +95,15 @@ public class HostCandidateHarvester //there seems to be a problem with this particular address //let's just move on for now and hope we will find better if (logger.isLoggable(Level.WARNING)) - logger.warning("Failed to create a socket for:" - +"\naddr:" + addr - +"\npreferredPort:" + preferredPort - +"\nminPort:" + minPort - +"\nmaxPort:" + maxPort - +"\nContinuing with next address"); + { + logger.warning( + "Failed to create a socket for:" + +"\naddr:" + addr + +"\npreferredPort:" + preferredPort + +"\nminPort:" + minPort + +"\nmaxPort:" + maxPort + +"\nContinuing with next address"); + } continue; } @@ -204,18 +207,22 @@ public class HostCandidateHarvester = new MultiplexingDatagramSocket(port, laddr); if(logger.isLoggable(Level.FINEST)) - logger.log(Level.FINEST, - "just bound to: " + sock.getLocalSocketAddress()); + { + logger.log( + Level.FINEST, + "just bound to: " + sock.getLocalSocketAddress()); + } return sock; } catch (SocketException se) { - logger.log(Level.INFO, - "Retrying a bind because of a failure to bind to address " - + laddr + " and port " + port - + " (" + se.getMessage() +")"); + logger.log( + Level.INFO, + "Retrying a bind because of a failure to bind to" + + " address " + laddr + + " and port " + port + + " (" + se.getMessage() +")"); logger.log(Level.INFO, "", se); - } port ++; diff --git a/test/test/Ice.java b/test/test/Ice.java index a4cd617..b0767f3 100644 --- a/test/test/Ice.java +++ b/test/test/Ice.java @@ -20,7 +20,7 @@ import org.ice4j.security.*; * both agents and make one of them run checks against the other. * * @author Emil Ivov - * @author Lubomir Marinov + * @author Lyubomir Marinov */ public class Ice { @@ -118,16 +118,17 @@ public class Ice "Agent entered the " + iceProcessingState + " state."); if(iceProcessingState == IceProcessingState.COMPLETED) { - System.out.println("Total ICE processing time: " - + (processingEndTime - startTime) + "ms"); + System.out.println( + "Total ICE processing time: " + + (processingEndTime - startTime) + "ms"); Agent agent = (Agent)evt.getSource(); List streams = agent.getStreams(); for(IceMediaStream stream : streams) { String streamName = stream.getName(); - System.out.println("Pairs selected for stream: " - + streamName); + System.out.println( + "Pairs selected for stream: " + streamName); List components = stream.getComponents(); for(Component cmp : components) @@ -142,8 +143,7 @@ public class Ice for(IceMediaStream stream : streams) { String streamName = stream.getName(); - System.out.println("Check list for stream: " - + streamName); + System.out.println("Check list for stream: " + streamName); //uncomment for a more verbose output System.out.println(stream.getCheckList()); } @@ -151,6 +151,13 @@ public class Ice else if(iceProcessingState == IceProcessingState.TERMINATED || 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); } }