diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index dcca2ac..7f406b0 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -82,11 +82,11 @@ public class Agent = new HostCandidateHarvester(); /** - * The list of harvesters (i.e. STUN, TURN, and others) that the agent - * should use when gathering candidates for components. + * The set of harvesters (i.e. STUN, TURN, and others) that the agent should + * use when gathering candidates for components. */ - private final List harvesters - = new ArrayList(); + private final CandidateHarvesterSet harvesters + = new CandidateHarvesterSet(); /** * We use the FoundationsRegistry to keep track of the foundations @@ -288,8 +288,12 @@ public class Agent BindException { if(transport != Transport.UDP) - throw new IllegalArgumentException("This implementation does not " - +" currently support transport: " + transport); + { + throw new IllegalArgumentException( + "This implementation does not currently support transport: " + + transport); + } + Component component = stream.createComponent(transport); gatherCandidates(component, preferredPort, minPort, maxPort ); @@ -314,7 +318,7 @@ public class Agent * before giving up and throwinG an exception. * * @throws IllegalArgumentException if either minPort or - * maxPort is not a valid port number or if minPort > + * maxPort is not a valid port number or if minPort > * maxPort. * @throws IOException if an error occurs while the underlying resolver lib * is gathering candidates and we end up without even a single one. @@ -327,15 +331,11 @@ public class Agent IOException { hostCandidateHarvester.harvest( - component, preferredPort, minPort, maxPort); + component, + preferredPort, minPort, maxPort); - //apply other harvesters here: - //todo: run harvesters in a parallel manner - synchronized(harvesters) - { - for (CandidateHarvester h : harvesters) - h.harvest(component); - } + //apply other harvesters here + harvesters.harvest(component); computeFoundations(component); @@ -544,10 +544,7 @@ public class Agent */ public void addCandidateHarvester(CandidateHarvester harvester) { - synchronized(harvesters) - { - harvesters.add(harvester); - } + harvesters.add(harvester); } /** diff --git a/src/org/ice4j/ice/CheckList.java b/src/org/ice4j/ice/CheckList.java index 0f92121..dad9746 100644 --- a/src/org/ice4j/ice/CheckList.java +++ b/src/org/ice4j/ice/CheckList.java @@ -50,8 +50,8 @@ public class CheckList * Contains {@link PropertyChangeListener}s registered with this {@link * Agent} and following its changes of state. */ - private List stateListeners - = new LinkedList(); + private final List stateListeners + = new LinkedList(); /** * The name of the {@link PropertyChangeEvent} that we use to deliver @@ -242,17 +242,14 @@ public class CheckList * * @return A String representation of this collection. */ + @Override public String toString() { - StringBuffer buff = new StringBuffer("CheckList. (num pairs="); - buff.append(size() + ")\n"); + StringBuilder buff = new StringBuilder("CheckList. (num pairs="); + buff.append(size()).append(")\n"); - Iterator pairs = iterator(); - - while(pairs.hasNext()) - { - buff.append(pairs.next().toString()).append("\n"); - } + for (CandidatePair pair : this) + buff.append(pair).append("\n"); return buff.toString(); } @@ -306,12 +303,8 @@ public class CheckList } //now put the pairs we've selected in the Waiting state. - Iterator pairsIter = pairsToWait.values().iterator(); - - while(pairsIter.hasNext()) - { - pairsIter.next().setStateWaiting(); - } + for (CandidatePair pairToWait : pairsToWait.values()) + pairToWait.setStateWaiting(); } /** @@ -323,9 +316,7 @@ public class CheckList { //first, determine the pairs that we'd need to put in the waiting state. for(CandidatePair pair : this) - { pair.computePriority(); - } } /** diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index 41d30d2..6747f68 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -92,8 +92,8 @@ public class IceMediaStream * Contains {@link PropertyChangeListener}s registered with this {@link * Agent} and following the various events it may be generating. */ - private List streamListeners - = new LinkedList(); + private final List streamListeners + = new LinkedList(); /** * The maximum number of candidate pairs that we should have in our check @@ -160,6 +160,7 @@ public class IceMediaStream * * @return a String representation of this media stream. */ + @Override public String toString() { StringBuffer buff = new StringBuffer( "media stream:") @@ -167,11 +168,8 @@ public class IceMediaStream buff.append(" (component count=").append(getComponentCount()) .append(")"); - List components = getComponents(); - for (Component cmp : components) - { + for (Component cmp : getComponents()) buff.append("\n").append(cmp); - } return buff.toString(); } @@ -654,7 +652,7 @@ public class IceMediaStream components.remove(pair.getParentComponent()); } - return components.size() == 0; + return components.isEmpty(); } /** @@ -668,9 +666,7 @@ public class IceMediaStream */ protected boolean allComponentsHaveSelected() { - List components = getComponents(); - - for(Component component : components) + for(Component component : getComponents()) if(component.getSelectedPair() == null) return false; diff --git a/src/org/ice4j/ice/harvest/CandidateHarvester.java b/src/org/ice4j/ice/harvest/CandidateHarvester.java index 9cf4853..24ea5aa 100644 --- a/src/org/ice4j/ice/harvest/CandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/CandidateHarvester.java @@ -7,6 +7,8 @@ */ package org.ice4j.ice.harvest; +import java.util.*; + import org.ice4j.ice.*; /** @@ -15,16 +17,26 @@ import org.ice4j.ice.*; * org.ice4j.ice.Component}. * * @author Emil Ivov + * @author Lyubomir Marinov */ public interface CandidateHarvester { /** * Gathers all candidate addresses of the type that this - * CandidateHarvester + * CandidateHarvester supports. The gathered candidate addresses + * are to be added by this CandidateHarvester to the specified + * Component using + * {@link Component#addLocalCandidate(LocalCandidate)} as soon as they are + * discovered. * * @param component the {@link Component} that we'd like to gather candidate * addresses for. + * @return the LocalCandidates gathered by this + * CandidateHarvester. Though they are to be added by this + * CandidateHarvester to the specified component as soon + * as they are discovered, they should also be returned in order to make + * sure that the gathering will be considered successful. */ - public void harvest(Component component); + public Collection harvest(Component component); } diff --git a/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java b/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java new file mode 100644 index 0000000..6f450a5 --- /dev/null +++ b/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java @@ -0,0 +1,448 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the SIP Communicator community (http://sip-communicator.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.ice.harvest; + +import java.util.*; +import java.util.concurrent.*; + +import org.ice4j.ice.*; + +/** + * Implements {@link Set} of CandidateHarvesters which runs the + * gathering of candidate addresses performed by its elements in parallel. + * + * @author Lyubomir Marinov + */ +public class CandidateHarvesterSet + extends AbstractSet +{ + /** + * The CandidateHarvesters which are the elements of this + * Set. + */ + private final Collection elements + = new LinkedList(); + + /** + * Initializes a new CandidateHarvesterSet instance. + */ + public CandidateHarvesterSet() + { + } + + /** + * Adds a specific CandidateHarvester to this + * CandidateHarvesterSet and returns true if it is not + * already present. Otherwise, leaves this set unchanged and returns + * false. + * + * @param harvester the CandidateHarvester to be added to this + * CandidateHarvesterSet + * @return true if this CandidateHarvesterSet did not + * already contain the specified harvester; otherwise, + * false + * @see Set#add(Object) + */ + @Override + public boolean add(CandidateHarvester harvester) + { + synchronized (elements) + { + for (CandidateHarvesterSetElement element : elements) + if (element.harvesterEquals(harvester)) + return false; + + elements.add(new CandidateHarvesterSetElement(harvester)); + return true; + } + } + + /** + * Gathers candidate addresses for a specific Component. + * CandidateHarvesterSet delegates to the + * CandidateHarvesters which are its Set elements. + * + * @param component the Component to gather candidate addresses for + * @see CandidateHarvester#harvest(Component) + */ + public void harvest(Component component) + { + synchronized (elements) + { + harvest( + elements.iterator(), + component, + Executors.newCachedThreadPool()); + } + } + + /** + * Gathers candidate addresses for a specific Component using + * specific CandidateHarvesters. + * + * @param harvesters the CandidateHarvesters to gather candidate + * addresses for the specified Component + * @param component the Component to gather candidate addresses for + * @param executorService the ExecutorService to schedule the + * execution of the gathering of candidate addresses performed by the + * specified harvesters + */ + private void harvest( + final Iterator harvesters, + final Component component, + ExecutorService executorService) + { + + /** + * Represents a task to be executed by the specified executorService and + * to call {@link CandidateHarvester#harvestComponent)} on the specified + * harvesters. + */ + class CandidateHarvesterSetTask + implements Runnable + { + /** + * The CandidateHarvester on which + * {@link CandidateHarvester#harvest(Component)} is to be or is + * being called. + */ + private CandidateHarvesterSetElement harvester; + + /** + * Initializes a new CandidateHarvesterSetTask which is to + * call {@link CandidateHarvester#harvest(Component)} on a specific + * harvester and then as many harvesters as possible. + * + * @param harvester the CandidateHarvester on which the + * new instance is to call + * CandidateHarvester#harvest(Component) first + */ + public CandidateHarvesterSetTask( + CandidateHarvesterSetElement harvester) + { + this.harvester = harvester; + } + + /** + * Gets the CandidateHarvester on which + * {@link CandidateHarvester#harvest(Component)} is being called. + * + * @return the CandidateHarvester on which + * CandidateHarvester#harvest(Component) is being called + */ + public CandidateHarvesterSetElement getHarvester() + { + return harvester; + } + + public void run() + { + while (true) + { + if (harvester.isEnabled()) + { + try + { + harvester.harvest(component); + } + catch (Throwable t) + { + harvester.setEnabled(false); + + if (t instanceof ThreadDeath) + throw (ThreadDeath) t; + } + } + + /* + * CandidateHarvester#harvest(Component) has been called on + * the harvester and its success or failure has been noted. + * Now forget the harvester because any failure to continue + * execution is surely not its fault. + */ + harvester = null; + + synchronized (harvesters) + { + if (harvesters.hasNext()) + harvester = harvesters.next(); + else + break; + } + } + } + } + + /* + * Start asynchronously executing the + * CandidateHarvester#harvest(Component) method of the harvesters. + */ + Map> tasks + = new HashMap>(); + + while (true) + { + /* + * Find the next CandidateHarvester which is to start gathering + * candidates. + */ + CandidateHarvesterSetElement harvester; + + synchronized (harvesters) + { + if (harvesters.hasNext()) + harvester = harvesters.next(); + else + break; + } + if (!harvester.isEnabled()) + continue; + + // Asynchronously start gathering candidates using the harvester. + CandidateHarvesterSetTask task + = new CandidateHarvesterSetTask(harvester); + + tasks.put(task, executorService.submit(task)); + } + + /* + * Wait for all harvesters to be given a chance to execute their + * CandidateHarvester#harvest(Component) method. + */ + Iterator>> taskIter + = tasks.entrySet().iterator(); + + while (taskIter.hasNext()) + { + Map.Entry> task + = taskIter.next(); + Future future = task.getValue(); + + while (true) + { + try + { + future.get(); + break; + } + catch (CancellationException ce) + { + /* + * It got cancelled so we cannot say that the fault is with + * its current harvester. + */ + break; + } + catch (ExecutionException ee) + { + /* + * A problem appeared during the execution of the task. + * CandidateHarvesterSetTask clears its harvester property + * for the purpose of determining whether the problem has + * appeared while working with a harvester. + */ + CandidateHarvesterSetElement harvester + = task.getKey().getHarvester(); + + if (harvester != null) + harvester.setEnabled(false); + break; + } + catch (InterruptedException ie) + { + continue; + } + } + taskIter.remove(); + } + } + + /** + * Returns an Iterator over the CandidateHarvesters which + * are elements in this CandidateHarvesterSet. The elements are + * returned in no particular order. + * + * @return an Iterator over the CandidateHarvesters which + * are elements in this CandidateHarvesterSet + * @see Set#iterator() + */ + public Iterator iterator() + { + final Iterator elementIter + = elements.iterator(); + + return + new Iterator() + { + /** + * Determines whether this iteration has more elements. + * + * @return true if this iteration has more elements; + * otherwise, false + * @see Iterator#hasNext() + */ + public boolean hasNext() + { + return elementIter.hasNext(); + } + + /** + * Returns the next element in this iteration. + * + * @return the next element in this iteration + * @throws NoSuchElementException if this iteration has no more + * elements + * @see Iterator#next() + */ + public CandidateHarvester next() + throws NoSuchElementException + { + return elementIter.next().harvester; + } + + /** + * Removes from the underlying CandidateHarvesterSet + * the last CandidateHarvester (element) returned by + * this Iterator. CandidateHarvestSet does not + * implement the remove operation at the time of this + * writing i.e. it always throws + * UnsupportedOperationException. + * + * @throws IllegalStateException if the next method has + * not yet been called, or the remove method has + * already been called after the last call to the next + * method + * @throws UnsupportedOperationException if the remove + * operation is not supported by this Iterator + * @see Iterator#remove() + */ + public void remove() + throws IllegalStateException, + UnsupportedOperationException + { + throw new UnsupportedOperationException("remove"); + } + }; + } + + /** + * Returns the number of CandidateHarvesters which are elements in + * this CandidateHarvesterSet. + * + * @return the number of CandidateHarvesters which are elements in + * this CandidateHarvesterSet + * @see Set#size() + */ + public int size() + { + synchronized (elements) + { + return elements.size(); + } + } + + /** + * Represents a CandidateHarvester as an element in a + * CandidateHarvesterSet. + */ + private static class CandidateHarvesterSetElement + { + /** + * The indicator which determines whether + * {@link CandidateHarvester#harvest(Component)} is to be called on + * {@link #harvester}. + */ + private boolean enabled = true; + + /** + * The CandidateHarvester which is an element in a + * CandidateHarvesterSet. + */ + private final CandidateHarvester harvester; + + /** + * Initializes a new CandidateHarvesterSetElement instance + * which is to represent a specific CandidateHarvester as an + * element in a CandidateHarvesterSet. + * + * @param harvester the CandidateHarvester which is to be + * represented as an element in a CandidateHarvesterSet by the + * new instance + */ + public CandidateHarvesterSetElement(CandidateHarvester harvester) + { + this.harvester = harvester; + } + + /** + * Calls {@link CandidateHarvester#harvest(Component)} on the associated + * CandidateHarvester if enabled. + * + * @param component the Component to gather candidates for + */ + public void harvest(Component component) + { + if (isEnabled()) + { + Collection candidates + = harvester.harvest(component); + + /* + * If the CandidateHarvester has not gathered any candidates, it + * is considered failed and will not be used again in order to + * not risk it slowing down the overall harvesting. + */ + if ((candidates == null) || candidates.isEmpty()) + setEnabled(false); + } + } + + /** + * Determines whether the associated CandidateHarvester is + * considered to be the same as a specific CandidateHarvester. + * + * @param harvester the CandidateHarvester to be compared to + * the associated CandidateHarvester + * @return true if the associated CandidateHarvester + * is considered to be the same as the specified harvester; + * otherwise, false + */ + public boolean harvesterEquals(CandidateHarvester harvester) + { + return this.harvester.equals(harvester); + } + + /** + * Gets the indicator which determines whether + * {@link CandidateHarvester#harvest(Component)} is to be called on the + * associated CandidateHarvester. + * + * @return true if + * CandidateHarvester#harvest(Component) is to be called on the + * associated CandidateHarvester; otherwise, false + */ + public boolean isEnabled() + { + return enabled; + } + + /** + * Sets the indicator which determines whether + * {@link CandidateHarvester#harvest(Component)} is to be called on the + * associated CandidateHarvester. + * + * @param enabled true if + * CandidateHarvester#harvest(Component) is to be called on the + * associated CandidateHarvester; otherwise, false + */ + public void setEnabled(boolean enabled) + { + this.enabled = enabled; + } + } +} diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index d07be01..16d2a14 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -208,9 +208,11 @@ public class StunCandidateHarvester * so that it would resolve them. * * @param component the {@link Component} that we'd like to gather candidate - * STUN Candidates for. + * STUN Candidates for + * @return the LocalCandidates gathered by this + * CandidateHarvester */ - public void harvest(Component component) + public Collection harvest(Component component) { stunStack = component.getParentStream().getParentAgent().getStunStack(); @@ -219,6 +221,29 @@ public class StunCandidateHarvester startResolvingCandidate((HostCandidate) cand); waitForResolutionEnd(); + + /* + * Report the LocalCandidates gathered by this CandidateHarvester so + * that the harvest is sure to be considered successful. + */ + Collection candidates = new HashSet(); + + synchronized (completedHarvests) + { + for (StunCandidateHarvest completedHarvest : completedHarvests) + { + LocalCandidate[] completedHarvestCandidates + = completedHarvest.getCandidates(); + + if ((completedHarvestCandidates != null) + && (completedHarvestCandidates.length != 0)) + { + candidates.addAll( + Arrays.asList(completedHarvestCandidates)); + } + } + } + return candidates; } /** diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index a6449ff..71c4c67 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -367,7 +367,8 @@ class StunClientTransaction stackCallback, evt.getRawMessage(), (Response) evt.getMessage(), - this.request, getTransactionID())); + this.request, + getTransactionID())); } /**