diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 3c7c037..096be13 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -237,11 +237,11 @@ public class Agent } /** - * Creates a new media stream and stores it + * Creates a new media stream and stores it. * - * @param mediaStreamName the name of the media stream + * @param mediaStreamName the name of the media stream * - * @return a reference to the newly created IceMediaStream. + * @return the newly created and stored IceMediaStream */ public IceMediaStream createMediaStream(String mediaStreamName) { @@ -296,7 +296,20 @@ public class Agent Component component = stream.createComponent(transport); - gatherCandidates(component, preferredPort, minPort, maxPort ); + gatherCandidates(component, preferredPort, minPort, maxPort); + + /* + * Lyubomir: After we've gathered the LocalCandidate for a Component and + * before we've make them available to the caller, we have to make sure + * that the ConnectivityCheckServer is started. If there's been a + * previous connectivity establishment which has completed, it has + * stopped the ConnectivityCheckServer. If the ConnectivtyCheckServer is + * not started after we've made the gathered LocalCandidates available + * to the caller, the caller may send them and a connectivity check may + * arrive from the remote Agent. + */ + connCheckServer.start(); + return component; } @@ -501,7 +514,8 @@ public class Agent protected void initCheckLists() { //first init the check list. - List streams = getStreams(); + List streams + = getStreamsWithPendingConnectivityEstablishment(); //init the maximum number of check list entries per stream. int maxCheckListSize = Integer.getInteger( @@ -731,6 +745,44 @@ public class Agent } } + /** + * Gets the IceMediaStreams registered with this Agent for + * which connectivity establishment is pending. For example, after a set of + * IceMediaStreams is registered with this Agent, + * connectivity establishment completes for them and then a new set of + * IceMediaStreams is registered with this Agent, the + * IceMediaStreams with pending connectivity establishment are + * those from the second set. + * + * @return a List of the IceMediaStreams registered with + * this Agent for which connectivity is pending. + */ + List getStreamsWithPendingConnectivityEstablishment() + { + + /* + * Lyubomir: We want to support establishing connectivity for streams + * which have been created after connectivity has been established for + * previously created streams. That is why we will remove the streams + * which have their connectivity checks completed or failed i.e. these + * streams have been handled by a previous connectivity establishment. + */ + List streams = getStreams(); + Iterator streamIter = streams.iterator(); + + while (streamIter.hasNext()) + { + IceMediaStream stream = streamIter.next(); + CheckList checkList = stream.getCheckList(); + CheckListState checkListState = checkList.getState(); + + if (CheckListState.COMPLETED.equals(checkListState) + || CheckListState.FAILED.equals(checkListState)) + streamIter.remove(); + } + return streams; + } + /** * Gets the StunStack used by this Agent. * diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java index f089a85..dd277ec 100644 --- a/src/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/ConnectivityCheckClient.java @@ -70,7 +70,10 @@ class ConnectivityCheckClient public void startChecks() { CheckList firstCheckList - = parentAgent.getStreams().get(0).getCheckList(); + = parentAgent + .getStreamsWithPendingConnectivityEstablishment() + .get(0) + .getCheckList(); startChecks(firstCheckList); } diff --git a/src/org/ice4j/ice/ConnectivityCheckServer.java b/src/org/ice4j/ice/ConnectivityCheckServer.java index d812a23..b97bba5 100644 --- a/src/org/ice4j/ice/ConnectivityCheckServer.java +++ b/src/org/ice4j/ice/ConnectivityCheckServer.java @@ -19,6 +19,7 @@ import org.ice4j.stack.*; * checks. * * @author Emil Ivov + * @author Lyubomir Marinov */ class ConnectivityCheckServer implements RequestListener, @@ -28,14 +29,20 @@ class ConnectivityCheckServer * The Logger used by the ConnectivityCheckServer * class and its instances for logging output. */ - private static final Logger logger = Logger - .getLogger(ConnectivityCheckServer.class.getName()); + private static final Logger logger + = Logger.getLogger(ConnectivityCheckServer.class.getName()); /** * The agent that created us. */ private final Agent parentAgent; + /** + * The indicator which determines whether this + * ConnectivityCheckServer is currently started. + */ + private boolean started = false; + /** * The StunStack that we will use for connectivity checks. */ @@ -53,8 +60,9 @@ class ConnectivityCheckServer this.parentAgent = parentAgent; stunStack = this.parentAgent.getStunStack(); - stunStack.addRequestListener(this); stunStack.getCredentialsManager().registerAuthority(this); + + start(); } /** @@ -366,10 +374,26 @@ class ConnectivityCheckServer } /** - * Stops this server. + * Starts this ConnectivityCheckServer. If it is not currently + * running, does nothing. + */ + public void start() + { + if (!started) + { + stunStack.addRequestListener(this); + started = true; + } + } + + /** + * Stops this ConnectivityCheckServer. A stopped + * ConnectivityCheckServer can be restarted by calling + * {@link #start()} on it. */ public void stop() { stunStack.removeRequestListener(this); + started = false; } } diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index 89fc290..0397c4b 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -322,12 +322,8 @@ public class IceMediaStream */ protected void createCheckList(List checkList) { - List componentsList = getComponents(); - - for(Component cmp : componentsList) - { + for(Component cmp : getComponents()) createCheckList(cmp, checkList); - } } /** @@ -348,12 +344,12 @@ public class IceMediaStream { for(Candidate remoteCnd : remoteCnds) { - if(!localCnd.canReach(remoteCnd)) - continue; + if(localCnd.canReach(remoteCnd)) + { + CandidatePair pair = new CandidatePair(localCnd, remoteCnd); - CandidatePair pair = new CandidatePair(localCnd, remoteCnd); - - checkList.add(pair); + checkList.add(pair); + } } } } @@ -632,9 +628,9 @@ public class IceMediaStream } /** - * Returns false if there is at least one nominated {@link + * Returns true if there is at least one nominated {@link * CandidatePair} in the valid list for every {@link Component} of this - * stream and true otherwise. + * stream and false otherwise. * * @return true if there is at least one nominated {@link * CandidatePair} in the valid list for every {@link Component} of this