diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java
index 413f810..b05330b 100644
--- a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java
+++ b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java
@@ -196,6 +196,11 @@ public abstract class AbstractUdpListener
*/
private final Thread thread;
+ /**
+ * Triggers the termination of the threads of this instance.
+ */
+ private boolean close = false;
+
/**
* Initializes a new SinglePortUdpHarvester instance which is to
* bind on the specified local address.
@@ -254,6 +259,15 @@ public abstract class AbstractUdpListener
thread.start();
}
+ /**
+ * Triggers the termination of the threads of this instance.
+ */
+ public void close()
+ {
+ close = true;
+ socket.close(); // causes socket#receive to stop blocking.
+ }
+
/**
* Perpetually reads datagrams from {@link #socket} and handles them
* accordingly.
@@ -271,7 +285,10 @@ public abstract class AbstractUdpListener
do
{
- // TODO: implement stopping the thread with a switch?
+ if (close)
+ {
+ break;
+ }
buf = getFreeBuffer();
@@ -286,7 +303,10 @@ public abstract class AbstractUdpListener
}
catch (IOException ioe)
{
- logger.severe("Failed to receive from socket: " + ioe);
+ if (!close)
+ {
+ logger.severe("Failed to receive from socket: " + ioe);
+ }
break;
}
buf.len = pkt.getLength();
@@ -316,7 +336,12 @@ public abstract class AbstractUdpListener
}
while (true);
- // TODO we are all done, clean up.
+ // now clean up and exit
+ for (MySocket candidateSocket : new ArrayList<>(sockets.values()))
+ {
+ candidateSocket.close();
+ }
+ socket.close();
}
/**
diff --git a/src/test/java/org/ice4j/ice/harvest/SinglePortUdpHarvesterTest.java b/src/test/java/org/ice4j/ice/harvest/SinglePortUdpHarvesterTest.java
new file mode 100644
index 0000000..ff7a863
--- /dev/null
+++ b/src/test/java/org/ice4j/ice/harvest/SinglePortUdpHarvesterTest.java
@@ -0,0 +1,111 @@
+/*
+ * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
+ *
+ * Copyright @ 2018 Atlassian Pty Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.ice4j.ice.harvest;
+
+import org.ice4j.*;
+import org.junit.*;
+
+import java.net.*;
+
+/**
+ * Various tests that verify the functionality provided by {@link SinglePortUdpHarvester}.
+ *
+ * @author Guus der Kinderen, guus.der.kinderen@gmail.com
+ */
+public class SinglePortUdpHarvesterTest
+{
+ /**
+ * Verifies that, without closing, the address used by a harvester cannot be re-used.
+ *
+ * @see https://github.com/jitsi/ice4j/issues/139
+ */
+ @Test( expected = java.net.BindException.class )
+ public void testRebindWithoutClose() throws Exception
+ {
+ // Setup test fixture.
+ final TransportAddress address = new TransportAddress( "127.0.0.1", 10000, Transport.UDP );
+ SinglePortUdpHarvester firstHarvester;
+ try
+ {
+ firstHarvester = new SinglePortUdpHarvester( address );
+ }
+ catch ( java.net.BindException ex )
+ {
+ // This is not expected at this stage (the port is likely already in use by another process, voiding this
+ // test). Rethrow as a different exception than the BindException, that is expected to be thrown later in
+ // this test.
+ throw new Exception( "Test fixture is invalid.", ex );
+ }
+
+ // Execute system under test.
+ SinglePortUdpHarvester secondHarvester = null;
+ try
+ {
+ secondHarvester = new SinglePortUdpHarvester( address );
+ }
+ // Verification of the results is implicit - this Test expectes BindException to be thrown at this point.
+
+ // Tear down
+ finally
+ {
+ firstHarvester.close();
+ if ( secondHarvester != null )
+ {
+ secondHarvester.close();
+ }
+ }
+ }
+
+ /**
+ * Verifies that, after closing, the address used by a harvester can be re-used.
+ *
+ * @see https://github.com/jitsi/ice4j/issues/139
+ */
+ @Test
+ public void testRebindWithClose() throws Exception
+ {
+ // Setup test fixture.
+ final TransportAddress address = new TransportAddress( "127.0.0.1", 10001, Transport.UDP );
+ final SinglePortUdpHarvester firstHarvester = new SinglePortUdpHarvester( address );
+ firstHarvester.close();
+ Thread.sleep( 500 ); // give thread time to close/clean up.
+
+ // Execute system under test.
+ SinglePortUdpHarvester secondHarvester = null;
+
+ try
+ {
+ secondHarvester = new SinglePortUdpHarvester( address );
+ }
+
+ // Verify results.
+ catch ( BindException ex )
+ {
+ Assert.fail( "A bind exception should not have been thrown, as the original harvester was propertly closed.");
+ }
+
+ // Tear down.
+ finally
+ {
+ if ( secondHarvester != null )
+ {
+ secondHarvester.close();
+ }
+ }
+ }
+}