diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 47fd2d6..1974458 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -7,6 +7,7 @@ */ package org.ice4j.ice; +import java.beans.*; import java.io.*; import java.math.*; import java.net.*; @@ -135,15 +136,24 @@ public class Agent = new ConnectivityCheckServer(this); /** - * Indicates whether this agent has both sent and received candidate lists - * and started connectivity establishment. This is important in cases like - * determining whether a remote address we've just discovered is peer - * reflexive or not. If iceStarted is true and we don't know about the + * Indicates the state of ICE processing in this Agent. An + * Agent is in the Waiting state until it has both sent and + * received candidate lists and started connectivity establishment. The + * difference between the Waiting and the Running states is important in + * cases like determining whether a remote address we've just discovered is + * peer reflexive or not. If iceStarted is true and we don't know about the * address then we should add it to the list of candidates. Otherwise * we should wait for the remote party to send their media description * before being able to determine. */ - private boolean iceStarted = false; + private IceProcessingState state = IceProcessingState.WAITING; + + /** + * Contains {@link PropertyChangeListener}s registered with this {@link + * Agent} and following its changes of state. + */ + private List stateListeners + = new LinkedList(); /** * Creates an empty Agent with no streams, and no address @@ -315,7 +325,87 @@ public class Agent */ public boolean isStarted() { - return iceStarted; + return state != IceProcessingState.WAITING; + } + + /** + * Returns the state of ICE processing for this Agent. + * + * @return the state of ICE processing for this Agent. + */ + public IceProcessingState getState() + { + return state; + } + + /** + * Adds l to the list of listeners tracking changes of the + * {@link IceProcessingState} of this Agent + * + * @param l the listener to register. + */ + public void addStateChangeListener(PropertyChangeListener l) + { + synchronized(stateListeners) + { + if(!stateListeners.contains(l)) + this.stateListeners.add(l); + } + } + + /** + * Removes l from the list of listeners tracking changes of the + * {@link IceProcessingState} of this Agent + * + * @param l the listener to remove. + */ + public void removeStateChangeListener(PropertyChangeListener l) + { + synchronized(stateListeners) + { + this.stateListeners.remove(l); + } + } + + /** + * Creates a new {@link PropertyChangeEvent} and delivers it to all + * currently registered state listeners. + * + * @param oldState the {@link IceProcessingState} we had before the change + * @param newState the {@link IceProcessingState} we had after the change + */ + private void fireStateChange(IceProcessingState oldState, + IceProcessingState newState) + { + List listenersCopy; + + synchronized(stateListeners) + { + listenersCopy + = new LinkedList(stateListeners); + } + + PropertyChangeEvent evt = new PropertyChangeEvent( + this, "IceProcessingState", oldState, newState); + + for(PropertyChangeListener l : listenersCopy) + { + l.propertyChange(evt); + } + } + + /** + * Sets the {@link IceProcessingState} of this Agent to + * newState and triggers the corresponding change event. + * + * @param newState the new state of ICE processing for this Agent. + */ + private void setState(IceProcessingState newState) + { + IceProcessingState oldState = state; + + this.state = state; + fireStateChange(oldState, newState) } /** diff --git a/test/test/Ice.java b/test/test/Ice.java index 7d7121d..da20f8c 100644 --- a/test/test/Ice.java +++ b/test/test/Ice.java @@ -7,6 +7,7 @@ */ package test; +import java.beans.*; import java.lang.reflect.*; import java.net.*; import java.util.*; @@ -35,6 +36,8 @@ public class Ice Agent localAgent = createAgent(9090); Agent remotePeer = createAgent(6060); + localAgent.addStateChangeListener(new IceProcessingListener()); + //let them fight ... fights forge character. //localAgent.setControlling(true); //remotePeer.setControlling(false); @@ -62,7 +65,32 @@ public class Ice System.out.println("Total execution time: " + (endTime - startTime) + "ms"); - Thread.sleep(25000); + //Give processing enough time to finish. We'll System.exit() anyway + //as soon as localAgent enters a final state. + Thread.sleep(60000); + } + + /** + * The listener that would end example execution once we enter the + * completed state. + */ + public static final class IceProcessingListener + implements PropertyChangeListener + { + /** + * System.exit()s as soon as ICE processing enters a final state. + * + * @param evt the {@link PropertyChangeEvent} containing the old and new + * states of ICE processing. + */ + public void propertyChange(PropertyChangeEvent evt) + { + if(evt.getNewValue() == IceProcessingState.COMPLETED + || evt.getNewValue() == IceProcessingState.FAILED) + { + System.exit(0); + } + } } /**