fix: Does not close Multiplexing on Multiplexed.close()

Waits until all of the Multiplexed sockets belonging to a Multiplexing
are closed before closing the Multiplexing socket. Also introduces the
"persistent" flag of Multiplexing, which keeps it open even after all
of its Multiplexed sockets are closed.

fix: Makes MultiplexingDatagramSocket#persistent final.
This commit is contained in:
Boris Grozev 2017-07-11 17:27:58 -05:00
parent f6e3deb9a5
commit 5188097479
3 changed files with 140 additions and 5 deletions

View File

@ -119,7 +119,10 @@ public class MultiplexedDatagramSocket
{ {
multiplexing.close(this); multiplexing.close(this);
super.close(); // We intentionally do not call super.close(), because it eventually
// delegates to #multiplexing. We don't want to close #multiplexing
// just yet, because it may have other filtered sockets attached to it
// (or it needs to be kept open for other reasons)
} }
/** /**

View File

@ -99,8 +99,9 @@ public class MultiplexingDatagramSocket
/** /**
* The list of <tt>DatagramPacket</tt>s to be received through this * The list of <tt>DatagramPacket</tt>s to be received through this
* <tt>DatagramSocket</tt> i.e. not accepted by the <tt>DatagramFilter</tt>s * <tt>DatagramSocket</tt> i.e. not accepted by the list of
* of {@link #sockets} at the time of the reading from the network. * {@link MultiplexedDatagramSocket} of this instance at the time of the
* reading from the network.
*/ */
private final List<DatagramPacket> received private final List<DatagramPacket> received
= new SocketReceiveBuffer() = new SocketReceiveBuffer()
@ -125,6 +126,15 @@ public class MultiplexingDatagramSocket
*/ */
private int soTimeout = 0; private int soTimeout = 0;
/**
* Whether this socket should be kept open even when all of its
* {@link MultiplexedDatagramSocket} are closed (if the value is
* {@code true}), or it should be closed when the last of its
* {@link MultiplexedDatagramSocket} is closed (if the value is
* {@code false}).
*/
private final boolean persistent;
/** /**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is * Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering and binds it to any available * to enable <tt>DatagramPacket</tt> filtering and binds it to any available
@ -138,6 +148,25 @@ public class MultiplexingDatagramSocket
public MultiplexingDatagramSocket() public MultiplexingDatagramSocket()
throws SocketException throws SocketException
{ {
this(false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering and binds it to any available
* port on the local host machine. The socket will be bound to the wildcard
* address, an IP address chosen by the kernel.
*
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @see DatagramSocket#DatagramSocket()
*/
public MultiplexingDatagramSocket(boolean persistent)
throws SocketException
{
this.persistent = persistent;
} }
/** /**
@ -152,8 +181,30 @@ public class MultiplexingDatagramSocket
*/ */
public MultiplexingDatagramSocket(DatagramSocket delegate) public MultiplexingDatagramSocket(DatagramSocket delegate)
throws SocketException throws SocketException
{
this(delegate, false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering on a specific
* <tt>DatagramSocket</tt>.
*
* @param delegate the <tt>DatagramSocket</tt> on which
* <tt>DatagramPacket</tt> filtering is to be enabled by the new instance
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @throws SocketException if anything goes wrong while initializing the new
* instance
*/
public MultiplexingDatagramSocket(
DatagramSocket delegate,
boolean persistent)
throws SocketException
{ {
super(delegate); super(delegate);
this.persistent = persistent;
} }
/** /**
@ -169,8 +220,29 @@ public class MultiplexingDatagramSocket
*/ */
public MultiplexingDatagramSocket(int port) public MultiplexingDatagramSocket(int port)
throws SocketException throws SocketException
{
this(port, false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering and binds it to the specified
* port on the local host machine. The socket will be bound to the wildcard
* address, an IP address chosen by the kernel.
*
* @param port the port to bind the new socket to
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(int)
*/
public MultiplexingDatagramSocket(int port, boolean persistent)
throws SocketException
{ {
super(port); super(port);
this.persistent = persistent;
} }
/** /**
@ -188,8 +260,34 @@ public class MultiplexingDatagramSocket
*/ */
public MultiplexingDatagramSocket(int port, InetAddress laddr) public MultiplexingDatagramSocket(int port, InetAddress laddr)
throws SocketException throws SocketException
{
this(port, laddr, false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering, bound to the specified local
* address. The local port must be between 0 and 65535 inclusive. If the IP
* address is 0.0.0.0, the socket will be bound to the wildcard address, an
* IP address chosen by the kernel.
*
* @param port the local port to bind the new socket to
* @param laddr the local address to bind the new socket to
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(int, InetAddress)
*/
public MultiplexingDatagramSocket(
int port,
InetAddress laddr,
boolean persistent)
throws SocketException
{ {
super(port, laddr); super(port, laddr);
this.persistent = persistent;
} }
/** /**
@ -209,8 +307,35 @@ public class MultiplexingDatagramSocket
*/ */
public MultiplexingDatagramSocket(SocketAddress bindaddr) public MultiplexingDatagramSocket(SocketAddress bindaddr)
throws SocketException throws SocketException
{
this(bindaddr, false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering, bound to the specified local
* socket address.
* <p>
* If the specified local socket address is <tt>null</tt>, creates an
* unbound socket.
* </p>
*
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
* unbound socket
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(SocketAddress)
*/
public MultiplexingDatagramSocket(
SocketAddress bindaddr,
boolean persistent)
throws SocketException
{ {
super(bindaddr); super(bindaddr);
this.persistent = persistent;
} }
/** /**
@ -221,7 +346,11 @@ public class MultiplexingDatagramSocket
*/ */
void close(MultiplexedDatagramSocket multiplexed) void close(MultiplexedDatagramSocket multiplexed)
{ {
multiplexingXXXSocketSupport.close(multiplexed); if (!multiplexingXXXSocketSupport.close(multiplexed)
&& !persistent)
{
close();
}
} }
/** /**

View File

@ -279,12 +279,15 @@ abstract class MultiplexingXXXSocketSupport
* <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt>. * <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt>.
* *
* @param multiplexed the <tt>MultiplexedDatagramSocket</tt> to close * @param multiplexed the <tt>MultiplexedDatagramSocket</tt> to close
* @return {@code true} if there are remaining filtered sockets.
*/ */
void close(MultiplexedXXXSocketT multiplexed) boolean close(MultiplexedXXXSocketT multiplexed)
{ {
synchronized (sockets) synchronized (sockets)
{ {
sockets.remove(multiplexed); sockets.remove(multiplexed);
return !sockets.isEmpty();
} }
} }