From eff395487187f040dc660038b12f3c4d225f23b9 Mon Sep 17 00:00:00 2001 From: HÃ¥kon Hallingstad Date: Wed, 22 Feb 2017 11:45:34 +0100 Subject: Improve Spec API - Removes Spec.getLocalHostName - Removes distinction between listening- and connect- address for Spec - Makes all usage of connect w/Spec specify hostname --- jrt/src/com/yahoo/jrt/Acceptor.java | 2 +- jrt/src/com/yahoo/jrt/Connection.java | 3 ++- jrt/src/com/yahoo/jrt/Spec.java | 49 ++++++++++------------------------- jrt/tests/com/yahoo/jrt/SpecTest.java | 16 ++++++------ 4 files changed, 24 insertions(+), 46 deletions(-) (limited to 'jrt') diff --git a/jrt/src/com/yahoo/jrt/Acceptor.java b/jrt/src/com/yahoo/jrt/Acceptor.java index 0f40c6c8e55..7316f8c620b 100644 --- a/jrt/src/com/yahoo/jrt/Acceptor.java +++ b/jrt/src/com/yahoo/jrt/Acceptor.java @@ -47,7 +47,7 @@ public class Acceptor { if (spec.port() != 0) { serverChannel.socket().setReuseAddress(true); } - serverChannel.socket().bind(spec.listenAddress(), 500); + serverChannel.socket().bind(spec.address(), 500); } catch (Exception e) { if (serverChannel != null) { try { serverChannel.socket().close(); } catch (Exception x) {} diff --git a/jrt/src/com/yahoo/jrt/Connection.java b/jrt/src/com/yahoo/jrt/Connection.java index 24efdf46087..3e28b5d1d6c 100644 --- a/jrt/src/com/yahoo/jrt/Connection.java +++ b/jrt/src/com/yahoo/jrt/Connection.java @@ -162,8 +162,9 @@ class Connection extends Target { setLostReason(new IllegalArgumentException("jrt: malformed or missing spec")); return this; } + try { - channel = SocketChannel.open(spec.connectAddress()); + channel = SocketChannel.open(spec.address()); } catch (Exception e) { setLostReason(e); } diff --git a/jrt/src/com/yahoo/jrt/Spec.java b/jrt/src/com/yahoo/jrt/Spec.java index d40e773df68..e20bb9109dc 100644 --- a/jrt/src/com/yahoo/jrt/Spec.java +++ b/jrt/src/com/yahoo/jrt/Spec.java @@ -2,8 +2,6 @@ package com.yahoo.jrt; -import com.yahoo.net.HostName; - import java.net.InetSocketAddress; import java.net.SocketAddress; @@ -61,23 +59,21 @@ public class Spec { } /** - * Create a Spec from a port number. + * Create a Spec that will produce a wildcard address with address, and the + * hostname of the localhost with connectAddress. * - * @param port port number + * WARNING: Do not use omit host if connecting (to localhost) - use Spec("localhost", port) instead. + * Why? Because the SocketAddress obtained from spec is independent on whether to use the address + * for listening socket or connecting socket. A listening socket without host means the wildcard address + * is used. But when using the wildcard address for connecting, Java ends up getting the canonical + * hostname for localhost, which may not be reachable if on WiFi-only, see HostName.getLocalhost for details. + * + * @param port */ public Spec(int port) { this.port = port; } - /** - * Creates a Spec with the hostname of the current/local host and given port - * - * @param port port number - */ - public static Spec fromLocalHostName(int port) { - return new Spec(HostName.getLocalhost(), port); - } - /** * Obtain the host name of this address * @@ -112,34 +108,15 @@ public class Spec { * * @return listening socket address */ - SocketAddress listenAddress() { - return address(host); - } - - /** - * Resolve the connect socket address for this Spec. If this Spec is - * malformed, this method will return null. - * - * Why is this different than listenAddress()? If no host is given, a SocketAddress will be bound - * to the wildcard address (INADDR_ANY or 0.0.0.0 assuming IPv4) by InetSocketAddress. A wildcard - * address used to connect (at least SocketChannel::open) is interpreted as InetAddress::getLocalHost, - * which is wrong for the reasons stated in HostName::getLocalhost. - * - * @return connect socket address - */ - SocketAddress connectAddress() { - return address(HostName.getLocalhost()); - } - - private SocketAddress address(String hostOverride) { + SocketAddress address() { if (malformed) { return null; } if (address == null) { - if (hostOverride == null) { - address = new InetSocketAddress(port); + if (host == null) { + return new InetSocketAddress(port); } else { - address = new InetSocketAddress(hostOverride, port); + return new InetSocketAddress(host, port); } } return address; diff --git a/jrt/tests/com/yahoo/jrt/SpecTest.java b/jrt/tests/com/yahoo/jrt/SpecTest.java index ec88f164a7a..d15b8f95d30 100644 --- a/jrt/tests/com/yahoo/jrt/SpecTest.java +++ b/jrt/tests/com/yahoo/jrt/SpecTest.java @@ -17,7 +17,7 @@ public class SpecTest extends junit.framework.TestCase { assertFalse(spec.malformed()); assertEquals(457, spec.port()); assertNull(spec.host()); - assertTrue(addr.equals(spec.listenAddress())); + assertTrue(addr.equals(spec.address())); } public void testHostPort() { @@ -29,7 +29,7 @@ public class SpecTest extends junit.framework.TestCase { assertFalse(spec.malformed()); assertEquals(457, spec.port()); assertEquals(host, spec.host()); - assertTrue(addr.equals(spec.listenAddress())); + assertTrue(addr.equals(spec.address())); } public void testBogusHostPort() { @@ -41,7 +41,7 @@ public class SpecTest extends junit.framework.TestCase { assertFalse(spec.malformed()); assertEquals(457, spec.port()); assertEquals(host, spec.host()); - assertTrue(addr.equals(spec.listenAddress())); + assertTrue(addr.equals(spec.address())); } public void testSpec1() { @@ -52,7 +52,7 @@ public class SpecTest extends junit.framework.TestCase { assertFalse(spec.malformed()); assertEquals(8080, spec.port()); assertEquals("localhost", spec.host()); - assertTrue(addr.equals(spec.listenAddress())); + assertTrue(addr.equals(spec.address())); } public void testSpec2() { @@ -63,7 +63,7 @@ public class SpecTest extends junit.framework.TestCase { assertFalse(spec.malformed()); assertEquals(8080, spec.port()); assertNull(spec.host()); - assertTrue(addr.equals(spec.listenAddress())); + assertTrue(addr.equals(spec.address())); } public void testBogusSpec1() { @@ -73,7 +73,7 @@ public class SpecTest extends junit.framework.TestCase { assertTrue(spec.malformed()); assertEquals(0, spec.port()); assertNull(spec.host()); - assertNull(spec.listenAddress()); + assertNull(spec.address()); } public void testBogusSpec2() { @@ -83,7 +83,7 @@ public class SpecTest extends junit.framework.TestCase { assertTrue(spec.malformed()); assertEquals(0, spec.port()); assertNull(spec.host()); - assertNull(spec.listenAddress()); + assertNull(spec.address()); } public void testBogusSpec3() { @@ -93,6 +93,6 @@ public class SpecTest extends junit.framework.TestCase { assertTrue(spec.malformed()); assertEquals(0, spec.port()); assertNull(spec.host()); - assertNull(spec.listenAddress()); + assertNull(spec.address()); } } -- cgit v1.2.3