- Adding logic for MuxServerSocketChannel to be aware of "anyLocalAddress" instances in cache and return matches when trying to build new ones to reuse the same underlying ServerSocketChannel

- Testing the new bit of functionality
This commit is contained in:
Roberto Andrade 2015-07-25 10:21:01 -04:00
parent 8443e7e914
commit b8ff856e29
2 changed files with 71 additions and 1 deletions

View File

@ -44,6 +44,18 @@ public class MuxServerSocketChannel
*/
private static final int SOCKET_CHANNEL_READ_TIMEOUT
= MuxServerSocketChannelFactory.SOCKET_CHANNEL_READ_TIMEOUT;
/**
* Reference to 0.0.0.0 IPv4 or 0::0 IPv6 address for "wildcard" matching purposes
*/
private static final InetAddress ANY_LOCAL_ADDRESS;
static {
try {
ANY_LOCAL_ADDRESS = InetAddress.getByName("::");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
/**
* Asserts that <tt>t</tt> is not <tt>null</tt> by throwing a
@ -492,7 +504,21 @@ public class MuxServerSocketChannel
{
aLocalAddr = null;
}
if (aLocalAddr != null && aLocalAddr.equals(localAddr))
boolean matches = aLocalAddr != null && aLocalAddr.equals(localAddr);
// if not the same address, let's see if the cached one is an "anyLocalAddress" and if so let's consider it a match
if (!matches && aLocalAddr instanceof InetSocketAddress && localAddr instanceof InetSocketAddress)
{
InetSocketAddress aLocalInetAddr = (InetSocketAddress) aLocalAddr;
InetSocketAddress localInetAddr = (InetSocketAddress) localAddr;
matches =
aLocalInetAddr.getAddress().equals(ANY_LOCAL_ADDRESS) &&
aLocalInetAddr.getPort() == localInetAddr.getPort();
}
if (matches)
{
channel = aChannel;
// The whole idea of using (1) a List for

View File

@ -0,0 +1,44 @@
package org.ice4j.socket.jdk8;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import org.ice4j.socket.DatagramPacketFilter;
import junit.framework.TestCase;
/**
* @author robertoandrade
*/
public class MuxServerSocketChannelTest extends TestCase {
public void testOpenAndBindReuseWhenBindingToAllInterfacesFirst() throws IOException
{
SocketAddress allInterfaces = new InetSocketAddress("::", 4443);
SocketAddress oneInterface = new InetSocketAddress("localhost", 4443);
MuxServerSocketChannel channelForAllInterfaces =
MuxServerSocketChannel.openAndBind(null, allInterfaces, 0, new DatagramPacketFilter()
{
@Override
public boolean accept(DatagramPacket p)
{
return false;
}
});
MuxServerSocketChannel channelForOneInterface =
MuxServerSocketChannel.openAndBind(null, oneInterface, 0, new DatagramPacketFilter()
{
@Override
public boolean accept(DatagramPacket p)
{
return false;
}
});
assertEquals(channelForAllInterfaces.getFDVal(), channelForOneInterface.getFDVal());
}
}