diff --git a/src/org/ice4j/ice/LocalCandidate.java b/src/org/ice4j/ice/LocalCandidate.java
index 744261b..d3df6af 100644
--- a/src/org/ice4j/ice/LocalCandidate.java
+++ b/src/org/ice4j/ice/LocalCandidate.java
@@ -320,6 +320,7 @@ public abstract class LocalCandidate
*
* @return local ufrag
*/
+ @Override
public String getUfrag()
{
return ufrag;
@@ -374,6 +375,7 @@ public abstract class LocalCandidate
* - null for a peer reflexive candidate : there is no way to know the
* related address.
*/
+ @Override
protected LocalCandidate findRelatedCandidate(
TransportAddress relatedAddress)
{
diff --git a/src/org/ice4j/ice/ServerReflexiveCandidate.java b/src/org/ice4j/ice/ServerReflexiveCandidate.java
index 8adac57..b93118d 100644
--- a/src/org/ice4j/ice/ServerReflexiveCandidate.java
+++ b/src/org/ice4j/ice/ServerReflexiveCandidate.java
@@ -100,6 +100,7 @@ public class ServerReflexiveCandidate
* Candidate
* @see LocalCandidate#getIceSocketWrapper()
*/
+ @Override
public IceSocketWrapper getIceSocketWrapper()
{
return getBase().getIceSocketWrapper();
@@ -111,13 +112,12 @@ public class ServerReflexiveCandidate
* LocalCandidate is closed only if it is not the socket
* of the base of this LocalCandidate.
*/
- protected void free()
+ @Override
+ public void free()
{
super.free();
if(stunHarvest != null)
- {
stunHarvest.close();
- }
}
}
diff --git a/src/org/ice4j/ice/harvest/GoogleTurnCandidateHarvester.java b/src/org/ice4j/ice/harvest/GoogleTurnCandidateHarvester.java
index 45c01ec..11235aa 100644
--- a/src/org/ice4j/ice/harvest/GoogleTurnCandidateHarvester.java
+++ b/src/org/ice4j/ice/harvest/GoogleTurnCandidateHarvester.java
@@ -72,10 +72,8 @@ public class GoogleTurnCandidateHarvester
protected GoogleTurnCandidateHarvest createHarvest(
HostCandidate hostCandidate)
{
- return new GoogleTurnCandidateHarvest(
- this,
- hostCandidate,
- this.password);
+ return
+ new GoogleTurnCandidateHarvest(this, hostCandidate, getPassword());
}
/**
diff --git a/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java b/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java
index 31a33e0..0c5d524 100644
--- a/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java
+++ b/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java
@@ -105,10 +105,8 @@ public class GoogleTurnSSLCandidateHarvester
protected GoogleTurnCandidateHarvest createHarvest(
HostCandidate hostCandidate)
{
- return new GoogleTurnCandidateHarvest(
- this,
- hostCandidate,
- this.getPassword());
+ return
+ new GoogleTurnCandidateHarvest(this, hostCandidate, getPassword());
}
/**
diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvest.java b/src/org/ice4j/ice/harvest/StunCandidateHarvest.java
index fa5739e..07c07e3 100644
--- a/src/org/ice4j/ice/harvest/StunCandidateHarvest.java
+++ b/src/org/ice4j/ice/harvest/StunCandidateHarvest.java
@@ -8,6 +8,7 @@
package org.ice4j.ice.harvest;
import java.io.*;
+import java.lang.ref.*;
import java.util.*;
import java.util.logging.*;
@@ -121,12 +122,12 @@ public class StunCandidateHarvest
private Thread sendKeepAliveMessageThread;
/**
- * The time stamp of the last call to {@link #sendKeepAliveMessage()} which
- * completed without throwing an exception (note: it doesn't mean that the
- * keep-alive message was a STUN Request and it received a success
- * STUN Response).
+ * The time (stamp) in milliseconds of the last call to
+ * {@link #sendKeepAliveMessage()} which completed without throwing an
+ * exception. Note: It doesn't mean that the keep-alive message was a
+ * STUN Request and it received a success STUN Response.
*/
- private long sendKeepAliveMessageTimeStamp = -1;
+ private long sendKeepAliveMessageTime = -1;
/**
* Initializes a new StunCandidateHarvest which is to represent the
@@ -155,18 +156,26 @@ public class StunCandidateHarvest
* @param candidate the LocalCandidate to be added to the list of
* LocalCandidates harvested for {@link #hostCandidate} by this
* harvest
+ * @return true if the list of LocalCandidates changed as
+ * a result of the method invocation; otherwise, false
*/
- protected void addCandidate(LocalCandidate candidate)
+ protected boolean addCandidate(LocalCandidate candidate)
{
- if (!candidates.contains(candidate))
+ boolean added;
+
+ //try to add the candidate to the component and then only add it to the
+ //harvest if it wasn't deemed redundant
+ if (!candidates.contains(candidate)
+ && hostCandidate.getParentComponent().addLocalCandidate(
+ candidate))
{
- //try to add the candidate to the component and then only add it to
- //the harvest if it wasn't deemed redundant
- if( hostCandidate.getParentComponent().addLocalCandidate(candidate))
- {
- candidates.add(candidate);
- }
+ added = candidates.add(candidate);
}
+ else
+ {
+ added = false;
+ }
+ return added;
}
/**
@@ -245,6 +254,35 @@ public class StunCandidateHarvest
return completedResolvingCandidate;
}
+ /**
+ * Determines whether a specific LocalCandidate is contained in the
+ * list of LocalCandidates harvested for {@link #hostCandidate} by
+ * this harvest.
+ *
+ * @param candidate the LocalCandidate to look for in the list of
+ * LocalCandidates harvested for {@link #hostCandidate} by this
+ * harvest
+ * @return true if the list of LocalCandidates contains
+ * the specified candidate; otherwise, false
+ */
+ protected boolean containsCandidate(LocalCandidate candidate)
+ {
+ if (candidate != null)
+ {
+ LocalCandidate[] candidates = getCandidates();
+
+ if ((candidates != null) && (candidates.length != 0))
+ {
+ for (LocalCandidate c : candidates)
+ {
+ if (candidate.equals(c))
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
/**
* Creates new Candidates determined by a specific STUN
* Response.
@@ -338,16 +376,25 @@ public class StunCandidateHarvest
{
synchronized (sendKeepAliveMessageSyncRoot)
{
- sendKeepAliveMessageThread
- = new Thread()
- {
- @Override
- public void run()
- {
- runInSendKeepAliveMessageThread();
- }
- };
- sendKeepAliveMessageThread.start();
+ Thread t = new SendKeepAliveMessageThread(this);
+ t.setDaemon(true);
+ t.setName(
+ getClass().getName() + ".sendKeepAliveMessageThread: "
+ + hostCandidate);
+
+ boolean started = false;
+
+ sendKeepAliveMessageThread = t;
+ try
+ {
+ t.start();
+ started = true;
+ }
+ finally
+ {
+ if (!started && (sendKeepAliveMessageThread == t))
+ sendKeepAliveMessageThread = null;
+ }
}
}
@@ -371,7 +418,35 @@ public class StunCandidateHarvest
= createServerReflexiveCandidate(addr);
if (srvrRflxCand != null)
- addCandidate(srvrRflxCand);
+ {
+ try
+ {
+ addCandidate(srvrRflxCand);
+ }
+ finally
+ {
+ // Free srvrRflxCand if it has not been consumed.
+ if (!containsCandidate(srvrRflxCand))
+ {
+ try
+ {
+ srvrRflxCand.free();
+ }
+ catch (Exception ex)
+ {
+ if (logger.isLoggable(Level.FINE))
+ {
+ logger.log(
+ Level.FINE,
+ "Failed to free"
+ + " ServerReflexiveCandidate: "
+ + srvrRflxCand,
+ ex);
+ }
+ }
+ }
+ }
+ }
}
}
@@ -399,6 +474,32 @@ public class StunCandidateHarvest
CandidateExtendedType.STUN_SERVER_REFLEXIVE_CANDIDATE);
}
+ /**
+ * Runs in {@link #sendKeepAliveMessageThread} to notify this instance that
+ * sendKeepAliveMessageThread is about to exit.
+ */
+ private void exitSendKeepAliveMessageThread()
+ {
+ synchronized (sendKeepAliveMessageSyncRoot)
+ {
+ if (sendKeepAliveMessageThread == Thread.currentThread())
+ sendKeepAliveMessageThread = null;
+
+ /*
+ * Well, if the currentThread is finishing and this instance is
+ * still to send keep-alive messages, we'd better start another
+ * Thread for the purpose to continue the work that the
+ * currentThread was supposed to carry out.
+ */
+ if ((sendKeepAliveMessageThread == null)
+ && (sendKeepAliveMessageInterval
+ != SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED))
+ {
+ createSendKeepAliveMessageThread();
+ }
+ }
+ }
+
/**
* Gets the number of Candidates harvested for
* {@link #hostCandidate} during this harvest.
@@ -708,6 +809,7 @@ public class StunCandidateHarvest
* transaction and the runtime type of which specifies the failure reason
* @see AbstractResponseCollector#processFailure(BaseStunMessageEvent)
*/
+ @Override
protected void processFailure(BaseStunMessageEvent event)
{
TransactionID transactionID = event.getTransactionID();
@@ -754,6 +856,7 @@ public class StunCandidateHarvest
* STUN response
* @see ResponseCollector#processResponse(StunResponseEvent)
*/
+ @Override
public void processResponse(StunResponseEvent event)
{
TransactionID transactionID = event.getTransactionID();
@@ -1007,97 +1110,74 @@ public class StunCandidateHarvest
/**
* Runs in {@link #sendKeepAliveMessageThread} and sends STUN
* keep-alive Messages to the STUN server associated with the
- * StunCandidateHarvester of this instance
+ * StunCandidateHarvester of this instance.
+ *
+ * @return true if the method is to be invoked again; otherwise,
+ * false
*/
- private void runInSendKeepAliveMessageThread()
+ private boolean runInSendKeepAliveMessageThread()
{
- try
+ synchronized (sendKeepAliveMessageSyncRoot)
{
- while (true)
+ // Since we're going to #wait, make sure we're not canceled yet.
+ if (sendKeepAliveMessageThread != Thread.currentThread())
+ return false;
+ if (sendKeepAliveMessageInterval
+ == SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED)
{
- synchronized (sendKeepAliveMessageSyncRoot)
- {
- /*
- * Since we're going to #wait, make sure we're not canceled
- * yet.
- */
- if (sendKeepAliveMessageInterval
- == SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED)
- break;
+ return false;
+ }
- /*
- * Determine the amount of milliseconds that we'll have to
- * #wait.
- */
- long timeout;
+ // Determine the amount of milliseconds that we'll have to #wait.
+ long timeout;
- if (sendKeepAliveMessageTimeStamp == -1)
- {
- /*
- * If we're just starting, don't just go and send a new
- * STUN keep-alive message but rather wait for the whole
- * interval.
- */
- timeout = sendKeepAliveMessageInterval;
- }
- else
- {
- timeout
- = sendKeepAliveMessageTimeStamp
- + sendKeepAliveMessageInterval
- - System.currentTimeMillis();
- }
- // At long last, #wait if necessary.
- if (timeout > 0)
- {
- try
- {
- sendKeepAliveMessageSyncRoot.wait(timeout);
- }
- catch (InterruptedException iex)
- {
- }
- /*
- * Apart from being the time to send the STUN keep-alive
- * message, it could be that we've experienced a
- * spurious wake-up or that we've been canceled.
- */
- continue;
- }
- }
-
- sendKeepAliveMessageTimeStamp = System.currentTimeMillis();
+ if (sendKeepAliveMessageTime == -1)
+ {
+ /*
+ * If we're just starting, don't just go and send a new STUN
+ * keep-alive message but rather wait for the whole interval.
+ */
+ timeout = sendKeepAliveMessageInterval;
+ }
+ else
+ {
+ timeout
+ = sendKeepAliveMessageTime
+ + sendKeepAliveMessageInterval
+ - System.currentTimeMillis();
+ }
+ // At long last, #wait if necessary.
+ if (timeout > 0)
+ {
try
{
- sendKeepAliveMessage();
+ sendKeepAliveMessageSyncRoot.wait(timeout);
}
- catch (StunException sex)
+ catch (InterruptedException iex)
{
- logger.log(
- Level.INFO,
- "Failed to send STUN keep-alive message.",
- sex);
}
- }
- }
- finally
- {
- synchronized (sendKeepAliveMessageSyncRoot)
- {
- if (sendKeepAliveMessageThread == Thread.currentThread())
- sendKeepAliveMessageThread = null;
/*
- * Well, if the currentThread is finishing and this instance is
- * still to send keep-alive messages, we'd better start another
- * Thread for the purpose to continue the work that the
- * currentThread was supposed to carry out.
+ * Apart from being the time to send the STUN keep-alive
+ * message, it could be that we've experienced a spurious
+ * wake-up or that we've been canceled.
*/
- if ((sendKeepAliveMessageThread == null)
- && (sendKeepAliveMessageInterval
- != SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED))
- createSendKeepAliveMessageThread();
+ return true;
}
}
+
+ sendKeepAliveMessageTime = System.currentTimeMillis();
+ try
+ {
+ sendKeepAliveMessage();
+ }
+ catch (StunException sex)
+ {
+ logger.log(
+ Level.INFO,
+ "Failed to send STUN keep-alive message.",
+ sex);
+ }
+ return true;
}
/**
@@ -1141,19 +1221,19 @@ public class StunCandidateHarvest
* not be utilized.
*/
if (keepAliveMessage == null)
+ {
return false;
- if (keepAliveMessage instanceof Request)
+ }
+ else if (keepAliveMessage instanceof Request)
{
return
(sendRequest((Request) keepAliveMessage, false, null) != null);
}
else
{
- throw
- new StunException(
- StunException.ILLEGAL_ARGUMENT,
- "Failed to create keep-alive STUN message "
- + "for candidate: "
+ throw new StunException(
+ StunException.ILLEGAL_ARGUMENT,
+ "Failed to create keep-alive STUN message for candidate: "
+ candidate);
}
}
@@ -1330,4 +1410,61 @@ public class StunCandidateHarvest
setSendKeepAliveMessageInterval(
SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED);
}
+
+ /**
+ * Sends STUN keep-alive Messages to the STUN server associated
+ * with the StunCandidateHarvester of this instance.
+ *
+ * @author Lyubomir Marinov
+ */
+ private static class SendKeepAliveMessageThread
+ extends Thread
+ {
+ /**
+ * The StunCandidateHarvest which has initialized this
+ * instance. The StunCandidateHarvest is referenced by a
+ * WeakReference in an attempt to reduce the risk that the
+ * Thread may live regardless of the fact that the specified
+ * StunCandidateHarvest may no longer be reachable.
+ */
+ private final WeakReference harvest;
+
+ /**
+ * Initializes a new SendKeepAliveMessageThread instance with a
+ * specific StunCandidateHarvest.
+ *
+ * @param harvest the StunCandidateHarvest to initialize the
+ * new instance with
+ */
+ public SendKeepAliveMessageThread(StunCandidateHarvest harvest)
+ {
+ this.harvest = new WeakReference(harvest);
+ }
+
+ @Override
+ public void run()
+ {
+ try
+ {
+ do
+ {
+ StunCandidateHarvest harvest = this.harvest.get();
+
+ if ((harvest == null)
+ || !harvest.runInSendKeepAliveMessageThread())
+ {
+ break;
+ }
+ }
+ while (true);
+ }
+ finally
+ {
+ StunCandidateHarvest harvest = this.harvest.get();
+
+ if (harvest != null)
+ harvest.exitSendKeepAliveMessageThread();
+ }
+ }
+ }
}
diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java
index da1a385..32471e6 100644
--- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java
+++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java
@@ -124,8 +124,8 @@ public class StunCandidateHarvester
{
startedHarvests.remove(harvest);
- //if this was the last candidate, we are done with the STUN
- //resolution and need to notify the waiters.
+ // If this was the last candidate, we are done with the STUN
+ // resolution and need to notify the waiters.
if (startedHarvests.isEmpty())
doNotify = true;
}
@@ -254,13 +254,11 @@ public class StunCandidateHarvester
LocalCandidate[] completedHarvestCandidates
= completedHarvest.getCandidates();
- if (completedHarvestCandidates != null)
+ if ((completedHarvestCandidates != null)
+ && (completedHarvestCandidates.length != 0))
{
- if(completedHarvestCandidates.length != 0)
- {
- candidates.addAll(
+ candidates.addAll(
Arrays.asList(completedHarvestCandidates));
- }
}
}
@@ -268,10 +266,9 @@ public class StunCandidateHarvester
}
logger.finest(
- "Completed " + component.toShortString() + " harvest: " + toString()
- + ". Found " + candidates.size()
- + " candidates: " + listCandidates(candidates));
-
+ "Completed " + component.toShortString() + " harvest: "
+ + toString() + ". Found " + candidates.size()
+ + " candidates: " + listCandidates(candidates));
return candidates;
}
@@ -304,8 +301,9 @@ public class StunCandidateHarvester
if(cand == null)
{
- logger.info("server/candidate address type mismatch, skipping " +
- "candidate in this harvester");
+ logger.info(
+ "server/candidate address type mismatch,"
+ + " skipping candidate in this harvester");
return;
}
@@ -343,9 +341,26 @@ public class StunCandidateHarvester
{
if (!started)
{
- startedHarvests.remove(harvest);
- logger.warning("harvest did not start, removed: " +
- harvest);
+ try
+ {
+ startedHarvests.remove(harvest);
+ logger.warning(
+ "harvest did not start, removed: " + harvest);
+ }
+ finally
+ {
+ /*
+ * For the sake of completeness, explicitly close the
+ * harvest.
+ */
+ try
+ {
+ harvest.close();
+ }
+ catch (Exception ex)
+ {
+ }
+ }
}
}
}
@@ -363,6 +378,7 @@ public class StunCandidateHarvester
// Handle spurious wakeups.
while (!startedHarvests.isEmpty())
+ {
try
{
startedHarvests.wait();
@@ -370,10 +386,12 @@ public class StunCandidateHarvester
catch (InterruptedException iex)
{
logger.info(
- "interrupted waiting for harvests to complete, no. " +
- "startedHarvests = " + startedHarvests.size());
+ "interrupted waiting for harvests to complete,"
+ + " no. startedHarvests = "
+ + startedHarvests.size());
interrupted = true;
}
+ }
// Restore the interrupted status.
if (interrupted)
Thread.currentThread().interrupt();