feat: Closes all socket containers when the the merging socket closes.

This commit is contained in:
Boris Grozev 2016-11-29 16:29:45 -06:00
parent 67f301eebc
commit ff6eeb343e

View File

@ -112,16 +112,22 @@ public class MergingDatagramSocket
{
return;
}
// TODO: close all containers
closed = true;
// XXX do we want to risk obtaining the lock here, or should we just
// let any thread in find out about the close after it's next timeout?
synchronized (receiveLock)
{
receiveLock.notifyAll();
}
synchronized (socketContainersSyncRoot)
{
for (SocketContainer container : socketContainers)
{
doRemove(container.getSocket());
}
}
}
/**
@ -334,10 +340,8 @@ public class MergingDatagramSocket
}
if (socketContainer != null)
{
// Stop the reading thread.
// TODO: do we want to interrupt the thread here? This will not
// help unless we set a soTimeout > 0
socketContainer.closed = true;
// We just removed it from the merging socket, so use remove=false
socketContainer.close(false);
}
}
@ -665,6 +669,10 @@ public class MergingDatagramSocket
}
}
// The receive thread is terminating, no reason to keep this
// container anymore.
close(true);
if (logger.isLoggable(Level.FINE))
{
logger.fine("Finished: " + toString());
@ -860,6 +868,35 @@ public class MergingDatagramSocket
}
}
/**
* @return the underlying socket of this {@link SocketContainer}.
*/
private Object getSocket()
{
return datagramSocket != null ? datagramSocket : delegatingSocket;
}
/**
* Closes this {@link SocketContainer}, stopping it's reading thread,
* and, if necessary removing it from the merging socket.
* @param remove whether to remove this container from the merging
* socket.
*/
private void close(boolean remove)
{
if (closed && !remove)
{
return;
}
closed = true;
thread.interrupt();
if (remove)
{
MergingDatagramSocket.this.doRemove(getSocket());
}
}
/**
* Represents a {@link DatagramPacket} for the purposes of {@link
* SocketContainer}.