diff --git a/build.xml b/build.xml index 10bcd31..ce7b5f3 100644 --- a/build.xml +++ b/build.xml @@ -129,7 +129,7 @@ - + @@ -152,6 +152,6 @@ - + diff --git a/src/org/ice4j/socket/SafeCloseDatagramSocket.java b/src/org/ice4j/socket/SafeCloseDatagramSocket.java new file mode 100644 index 0000000..e3b927e --- /dev/null +++ b/src/org/ice4j/socket/SafeCloseDatagramSocket.java @@ -0,0 +1,126 @@ +/* + * 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.socket; + +import java.io.*; +import java.net.*; + +/** + * Represents a DatagramSocket which fixes the following problem: when + * {@link DatagramSocket#close()} is called while another thread is blocked in + * its {@link DatagramSocket#receive(DatagramPacket)}, calling + * {@link DatagramSocket#bind(SocketAddress) throws an "Already bound" exception + * until (the native counterpart of) the receive method returns. + * + * @author Lubomir Marinov + */ +public class SafeCloseDatagramSocket + extends DatagramSocket +{ + + /** + * The number of {@link #receive(DatagramPacket)} calls that have to return + * before {@link #close()} returns. + */ + private int inReceive = 0; + + /** + * The Object which synchronizes the access to {@link #inReceive} + * and implements the related inter-thread communication. + */ + private final Object inReceiveSyncRoot = new Object(); + + /** + * Creates a datagram socket, bound to the specified local socket address. + *

+ * If, if the address is null, creates an unbound socket. + *

+ * + * @param bindaddr local socket address to bind, or null for an + * unbound socket + * @throws SocketException if the socket could not be opened, or the socket + * could not bind to the specified local port + */ + public SafeCloseDatagramSocket(SocketAddress bindaddr) + throws SocketException + { + super(bindaddr); + } + + /** + * Closes this datagram socket. + *

+ * Any thread currently blocked in {@link #receive} upon this socket will + * throw a {@link SocketException} and this datagram socket will wait for it + * to return. + *

+ * + * @see DatagramSocket#close() + */ + @Override + public void close() + { + super.close(); + + synchronized (inReceiveSyncRoot) + { + boolean interrupted = false; + + while (inReceive > 0) + try + { + inReceiveSyncRoot.wait(); + } + catch (InterruptedException iex) + { + interrupted = true; + } + if (interrupted) + Thread.currentThread().interrupt(); + } + } + + /** + * Receives a datagram packet from this socket. When this method returns, + * the DatagramPacket's buffer is filled with the data received. + * The datagram packet also contains the sender's IP address, and the port + * number on the sender's machine. + *

+ * This method blocks until a datagram is received. The length + * field of the datagram packet object contains the length of the received + * message. If the message is longer than the packet's length, the message + * is truncated. + *

+ * + * @param p the DatagramPacket into which to place the incoming + * data + * @throws IOException if an I/O error occurs + * @see DatagramSocket#receive(DatagramPacket) + */ + @Override + public void receive(DatagramPacket p) + throws IOException + { + synchronized (inReceiveSyncRoot) + { + inReceive++; + } + try + { + super.receive(p); + } + finally + { + synchronized (inReceiveSyncRoot) + { + inReceive--; + inReceiveSyncRoot.notifyAll(); + } + } + } +} diff --git a/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java b/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java index 421918c..67ce863 100644 --- a/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java +++ b/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java @@ -13,6 +13,7 @@ import java.util.logging.*; import org.ice4j.*; import org.ice4j.attribute.*; import org.ice4j.message.*; +import org.ice4j.socket.*; import org.ice4j.stack.*; /** @@ -165,7 +166,7 @@ public class NetworkConfigurationDiscoveryProcess { stunStack = StunStack.getInstance(); - sock = new DatagramSocket(localAddress); + sock = new SafeCloseDatagramSocket(localAddress); stunStack.addSocket(sock); diff --git a/test/org/ice4j/MessageEventDispatchingTest.java b/test/org/ice4j/MessageEventDispatchingTest.java index 38644b6..5c2b452 100644 --- a/test/org/ice4j/MessageEventDispatchingTest.java +++ b/test/org/ice4j/MessageEventDispatchingTest.java @@ -8,11 +8,11 @@ package org.ice4j; import java.net.*; import java.util.*; -import java.util.logging.*; import junit.framework.*; import org.ice4j.message.*; +import org.ice4j.socket.*; import org.ice4j.stack.*; /** @@ -91,9 +91,9 @@ public class MessageEventDispatchingTest extends TestCase stunStack = StunStack.getInstance(); - clientSock = new DatagramSocket(clientAddress); - serverSock = new DatagramSocket(serverAddress); - serverSock2 = new DatagramSocket(serverAddress2); + clientSock = new SafeCloseDatagramSocket(clientAddress); + serverSock = new SafeCloseDatagramSocket(serverAddress); + serverSock2 = new SafeCloseDatagramSocket(serverAddress2); stunStack.addSocket(clientSock); stunStack.addSocket(serverSock); diff --git a/test/org/ice4j/TransactionSupportTests.java b/test/org/ice4j/TransactionSupportTests.java index f416e2f..d712ddc 100644 --- a/test/org/ice4j/TransactionSupportTests.java +++ b/test/org/ice4j/TransactionSupportTests.java @@ -8,11 +8,11 @@ package org.ice4j; import java.net.*; import java.util.*; -import java.util.logging.*; import junit.framework.*; import org.ice4j.message.*; +import org.ice4j.socket.*; import org.ice4j.stack.*; /** @@ -63,8 +63,8 @@ public class TransactionSupportTests extends TestCase { super.setUp(); - clientSock = new DatagramSocket(clientAddress); - serverSock = new DatagramSocket(serverAddress); + clientSock = new SafeCloseDatagramSocket(clientAddress); + serverSock = new SafeCloseDatagramSocket(serverAddress); StunStack.getInstance().addSocket(clientSock); StunStack.getInstance().addSocket(serverSock); diff --git a/test/org/ice4j/stack/ShallowStackTest.java b/test/org/ice4j/stack/ShallowStackTest.java index 91d2a21..ce8d160 100644 --- a/test/org/ice4j/stack/ShallowStackTest.java +++ b/test/org/ice4j/stack/ShallowStackTest.java @@ -14,6 +14,7 @@ import junit.framework.*; import org.ice4j.*; import org.ice4j.message.*; +import org.ice4j.socket.*; /** * All unit stack tests should be provided later. I just don't have the time now. @@ -60,7 +61,7 @@ public class ShallowStackTest extends TestCase { stunStack = StunStack.getInstance(); //access point - localSock = new DatagramSocket(localAddress); + localSock = new SafeCloseDatagramSocket(localAddress); stunStack.addSocket(localSock); //init the dummy server @@ -70,14 +71,13 @@ public class ShallowStackTest extends TestCase { protected void tearDown() throws Exception { - msgFixture.tearDown(); stunStack.removeSocket(localAddress); localSock.close(); dummyServerSocket.close(); - + msgFixture.tearDown(); msgFixture = null; super.tearDown(); } diff --git a/test/org/ice4j/stunclient/ResponseSequenceServer.java b/test/org/ice4j/stunclient/ResponseSequenceServer.java index 89fdf7e..2f19b4e 100644 --- a/test/org/ice4j/stunclient/ResponseSequenceServer.java +++ b/test/org/ice4j/stunclient/ResponseSequenceServer.java @@ -13,6 +13,7 @@ import java.util.logging.*; import org.ice4j.*; import org.ice4j.message.*; +import org.ice4j.socket.*; import org.ice4j.stack.*; /** @@ -56,7 +57,7 @@ public class ResponseSequenceServer { stunStack = StunStack.getInstance(); - localSocket = new DatagramSocket(serverAddress); + localSocket = new SafeCloseDatagramSocket(serverAddress); stunStack.addSocket(localSocket); stunStack.addRequestListener(serverAddress, this);