diff --git a/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java b/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java
index affd621..a8c1344 100644
--- a/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java
+++ b/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java
@@ -47,7 +47,13 @@ public class MultiplexingTcpHostHarvester
* READ_TIMEOUT milliseconds will be considered failed and will
* be closed.
*/
- private static int READ_TIMEOUT = 10000;
+ private static final int READ_TIMEOUT = 10000;
+
+ /**
+ * The constant which specifies how often to perform purging on
+ * {@link #components}.
+ */
+ private static final int PURGE_INTERVAL = 20;
/**
* The list of ServerSocketChannels that we will accept on.
@@ -106,6 +112,23 @@ public class MultiplexingTcpHostHarvester
private final Map> components
= new HashMap>();
+ /**
+ * A counter used to decide when to purge {@link #components}.
+ */
+ private int purgeCounter = 0;
+
+ /**
+ * Maps a public address to a local address.
+ */
+ private final Map mappedAddresses
+ = new HashMap();
+
+ /**
+ * Sets of additional ports, for which server reflexive candidates will be
+ * added.
+ */
+ private Set mappedPorts = new HashSet();
+
/**
* Initializes a new MultiplexingTcpHostHarvester, which is to
* listen on port number port on all IP addresses on all
@@ -272,7 +295,17 @@ public class MultiplexingTcpHostHarvester
{
WeakReference wr = components.get(localUfrag);
- return wr == null ? null : wr.get();
+ if (wr != null)
+ {
+ Component component = wr.get();
+ if (component == null)
+ {
+ components.remove(localUfrag);
+ }
+
+ return component;
+ }
+ return null;
}
}
@@ -282,6 +315,9 @@ public class MultiplexingTcpHostHarvester
* Saves a (weak) reference to Component, so that it can be
* notified if/when a socket for one of it LocalCandidates is
* accepted.
+ *
+ * This method does not perform any network operations and should return
+ * quickly.
*/
@Override
public Collection harvest(Component component)
@@ -309,11 +345,38 @@ public class MultiplexingTcpHostHarvester
{
components.put(agent.getLocalUfrag(),
new WeakReference(component));
+ purgeComponents();
}
return candidates;
}
+ /**
+ * Removes entries from {@link #components} for which the
+ * WeakReference has been cleared.
+ */
+ private void purgeComponents()
+ {
+ purgeCounter += 1;
+ if (purgeCounter % PURGE_INTERVAL == 0)
+ {
+ List toRemove = new LinkedList();
+ synchronized (components)
+ {
+ for (Map.Entry> entry
+ : components.entrySet())
+ {
+ WeakReference wr = entry.getValue();
+ if (wr == null || wr.get() == null)
+ toRemove.add(entry.getKey());
+ }
+
+ for (String key : toRemove)
+ components.remove(key);
+ }
+ }
+ }
+
/**
* Creates and returns the list of LocalCandidates which are to be added
* by this MultiplexingTcpHostHarvesterto a specific
@@ -326,7 +389,9 @@ public class MultiplexingTcpHostHarvester
*/
private List createLocalCandidates(Component component)
{
- List candidates = new LinkedList();
+ List hostCandidates
+ = new LinkedList();
+ // Add the host candidates for the addresses we really listen on
for (TransportAddress transportAddress : localAddresses)
{
TcpHostCandidate candidate
@@ -335,9 +400,115 @@ public class MultiplexingTcpHostHarvester
if (ssltcp)
candidate.setSSL(true);
- candidates.add(candidate);
+ hostCandidates.add(candidate);
}
- return candidates;
+
+ // Add srflx candidates for any mapped addresses
+ List mappedCandidates
+ = new LinkedList();
+ for (Map.Entry mapping
+ : mappedAddresses.entrySet())
+ {
+ InetAddress localAddress = mapping.getValue();
+ for (TcpHostCandidate base : hostCandidates)
+ {
+ if (localAddress
+ .equals(base.getTransportAddress().getAddress()))
+ {
+ InetAddress publicAddress = mapping.getKey();
+ ServerReflexiveCandidate mappedCandidate
+ = new ServerReflexiveCandidate(
+ new TransportAddress(publicAddress,
+ base.getTransportAddress()
+ .getPort(),
+ Transport.TCP),
+ base,
+ base.getStunServerAddress(),
+ CandidateExtendedType.STATICALLY_MAPPED_CANDIDATE);
+ if (base.isSSL())
+ mappedCandidate.setSSL(true);
+
+ mappedCandidates.add(mappedCandidate);
+ }
+ }
+ }
+
+ // Add srflx candidates for mapped ports
+ List portMappedCandidates
+ = new LinkedList();
+ for (TcpHostCandidate base : hostCandidates)
+ {
+ for (Integer port : mappedPorts)
+ {
+ ServerReflexiveCandidate portMappedCandidate
+ = new ServerReflexiveCandidate(
+ new TransportAddress(
+ base.getTransportAddress().getAddress(),
+ port,
+ Transport.TCP),
+ base,
+ base.getStunServerAddress(),
+ CandidateExtendedType.STATICALLY_MAPPED_CANDIDATE);
+ if (base.isSSL())
+ portMappedCandidate.setSSL(true);
+
+ portMappedCandidates.add(portMappedCandidate);
+ }
+ }
+ // Mapped ports for mapped addresses
+ for (LocalCandidate mappedCandidate : mappedCandidates)
+ {
+ TcpHostCandidate base = (TcpHostCandidate) mappedCandidate.getBase();
+ for (Integer port : mappedPorts)
+ {
+ ServerReflexiveCandidate portMappedCandidate
+ = new ServerReflexiveCandidate(
+ new TransportAddress(
+ mappedCandidate.getTransportAddress()
+ .getAddress(),
+ port,
+ Transport.TCP),
+ base,
+ base.getStunServerAddress(),
+ CandidateExtendedType.STATICALLY_MAPPED_CANDIDATE);
+ if (base.isSSL())
+ portMappedCandidate.setSSL(true);
+
+ portMappedCandidates.add(portMappedCandidate);
+ }
+ }
+
+ LinkedList allCandidates
+ = new LinkedList();
+ allCandidates.addAll(hostCandidates);
+ allCandidates.addAll(mappedCandidates);
+ allCandidates.addAll(portMappedCandidates);
+ return allCandidates;
+ }
+
+ /**
+ * Adds port as an additional port. When harvesting, additional server
+ * reflexive candidates will be added with this port.
+ * @param port the port to add.
+ */
+ public void addMappedPort(int port)
+ {
+ mappedPorts.add(port);
+ }
+
+ /**
+ * Adds a mapping between publicAddress and localAddress.
+ * This means that on harvest, along with any host candidates that have
+ * publicAddress, a server reflexive candidate will be added
+ * (with the same port as the host candidate).
+ *
+ * @param publicAddress the public address.
+ * @param localAddress the local address.
+ */
+ public void addMappedAddress(InetAddress publicAddress,
+ InetAddress localAddress)
+ {
+ mappedAddresses.put(publicAddress, localAddress);
}
/**