aboutsummaryrefslogtreecommitdiffstats
path: root/jrt
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@yahoo-inc.com>2017-02-20 17:13:18 +0100
committerHåkon Hallingstad <hakon@yahoo-inc.com>2017-02-20 17:13:18 +0100
commitd27e86bcd396d64a92d4742083235ee6768dc6b9 (patch)
treef2daf9230f2575792e58f8abe76aeeeb8f8a0d86 /jrt
parent24eab4fe4b661526f327fcb32a045dfbc7f3c998 (diff)
Makes clustercontroller-core work on WiFi
Diffstat (limited to 'jrt')
-rw-r--r--jrt/src/com/yahoo/jrt/Acceptor.java2
-rw-r--r--jrt/src/com/yahoo/jrt/Connection.java2
-rw-r--r--jrt/src/com/yahoo/jrt/Spec.java42
-rw-r--r--jrt/tests/com/yahoo/jrt/SpecTest.java16
4 files changed, 45 insertions, 17 deletions
diff --git a/jrt/src/com/yahoo/jrt/Acceptor.java b/jrt/src/com/yahoo/jrt/Acceptor.java
index 7316f8c620b..0f40c6c8e55 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.address(), 500);
+ serverChannel.socket().bind(spec.listenAddress(), 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 52964726eb7..24efdf46087 100644
--- a/jrt/src/com/yahoo/jrt/Connection.java
+++ b/jrt/src/com/yahoo/jrt/Connection.java
@@ -163,7 +163,7 @@ class Connection extends Target {
return this;
}
try {
- channel = SocketChannel.open(spec.address());
+ channel = SocketChannel.open(spec.connectAddress());
} catch (Exception e) {
setLostReason(e);
}
diff --git a/jrt/src/com/yahoo/jrt/Spec.java b/jrt/src/com/yahoo/jrt/Spec.java
index 4c1f07b98a2..d40e773df68 100644
--- a/jrt/src/com/yahoo/jrt/Spec.java
+++ b/jrt/src/com/yahoo/jrt/Spec.java
@@ -4,8 +4,8 @@ package com.yahoo.jrt;
import com.yahoo.net.HostName;
-import java.net.SocketAddress;
import java.net.InetSocketAddress;
+import java.net.SocketAddress;
/**
@@ -70,6 +70,15 @@ public class Spec {
}
/**
+ * 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
*
* @return host name
@@ -98,25 +107,44 @@ public class Spec {
}
/**
- * Resolve the socket address for this Spec. If this Spec is
+ * Resolve the listening socket address for this Spec. If this Spec is
+ * malformed, this method will return null.
+ *
+ * @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.
*
- * @return socket address
+ * 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 address() {
+ SocketAddress connectAddress() {
+ return address(HostName.getLocalhost());
+ }
+
+ private SocketAddress address(String hostOverride) {
if (malformed) {
return null;
}
if (address == null) {
- if (host == null) {
+ if (hostOverride == null) {
address = new InetSocketAddress(port);
} else {
- address = new InetSocketAddress(host, port);
+ address = new InetSocketAddress(hostOverride, port);
}
}
return address;
}
-
+
/**
* Obtain a string representation of this address. The return
* value from this method may be used to create a new Spec.
diff --git a/jrt/tests/com/yahoo/jrt/SpecTest.java b/jrt/tests/com/yahoo/jrt/SpecTest.java
index d15b8f95d30..ec88f164a7a 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.address()));
+ assertTrue(addr.equals(spec.listenAddress()));
}
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.address()));
+ assertTrue(addr.equals(spec.listenAddress()));
}
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.address()));
+ assertTrue(addr.equals(spec.listenAddress()));
}
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.address()));
+ assertTrue(addr.equals(spec.listenAddress()));
}
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.address()));
+ assertTrue(addr.equals(spec.listenAddress()));
}
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.address());
+ assertNull(spec.listenAddress());
}
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.address());
+ assertNull(spec.listenAddress());
}
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.address());
+ assertNull(spec.listenAddress());
}
}