changeset 1055:9d3ce623fb33

7901123: Manual override for binary link server/client addresses. Summary: introduce "jmh.link.address" and "jmh.link.port" override options.
author shade
date Tue, 09 Dec 2014 23:29:42 +0300
parents 448ef82f0360
children 73451bc77ee0
files jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkServer.java
diffstat 1 files changed, 22 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkServer.java	Fri Dec 05 01:10:22 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkServer.java	Tue Dec 09 23:29:42 2014 +0300
@@ -146,8 +146,19 @@
         this.plan.set(actionPlan);
     }
 
-    private InetAddress getLoopback() {
-        // Try to use JDK 7+ method first, it is more reliable.
+    private InetAddress getListenAddress() {
+        // Try to use user-provided override first.
+        String addr = System.getProperty("jmh.link.address");
+        if (addr != null) {
+            try {
+                return InetAddress.getByName(addr);
+            } catch (UnknownHostException e) {
+                // override failed, notify user
+                throw new IllegalStateException("Can not initialize binary link.", e);
+            }
+        }
+
+        // Auto-detection should try to use JDK 7+ method first, it is more reliable.
         try {
             Method m = InetAddress.class.getMethod("getLoopbackAddress");
             return (InetAddress) m.invoke(null);
@@ -177,12 +188,18 @@
         }
     }
 
+    private int getListenPort() {
+        return Integer.getInteger("jmh.link.port", 0);
+    }
+
     private final class Acceptor extends Thread {
 
         private final ServerSocket server;
+        private final InetAddress listenAddress;
 
         public Acceptor() throws IOException {
-            server = new ServerSocket(0, 50, getLoopback());
+            listenAddress = getListenAddress();
+            server = new ServerSocket(getListenPort(), 50, listenAddress);
         }
 
         @Override
@@ -206,10 +223,11 @@
         }
 
         public String getHost() {
-            return getLoopback().getHostAddress();
+            return listenAddress.getHostAddress();
         }
 
         public int getPort() {
+            // Poll the actual listen port, in case it is ephemeral
             return server.getLocalPort();
         }