From 694764e653333a1efd607dc64bb62f69560ae0ae Mon Sep 17 00:00:00 2001 From: Alexander Link <33052602+alxsap@users.noreply.github.com> Date: Sat, 28 Sep 2019 10:27:16 +0200 Subject: [PATCH] Introduce new hudson.TcpSlaveAgentListener.getAdvertisedHost() method (#4227) * Introduced hudson.TcpSlaveAgentListener.getAdvertisedHost() * review feedback * better error message --- .../main/java/hudson/TcpSlaveAgentListener.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/src/main/java/hudson/TcpSlaveAgentListener.java b/core/src/main/java/hudson/TcpSlaveAgentListener.java index 5ce689fcc8..28371d1456 100644 --- a/core/src/main/java/hudson/TcpSlaveAgentListener.java +++ b/core/src/main/java/hudson/TcpSlaveAgentListener.java @@ -43,6 +43,7 @@ import java.io.DataOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.SocketAddress; +import java.net.URL; import java.util.Arrays; import java.util.Base64; import jenkins.AgentProtocol; @@ -55,6 +56,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.BindException; import java.net.InetSocketAddress; +import java.net.MalformedURLException; import java.net.Socket; import java.nio.channels.ServerSocketChannel; import java.util.logging.Level; @@ -131,6 +133,21 @@ public final class TcpSlaveAgentListener extends Thread { return CLI_PORT != null ? CLI_PORT : getPort(); } + /** + * Gets the host name that we advertise protocol clients to connect to. + * @since TODO + */ + public String getAdvertisedHost() { + if (CLI_HOST_NAME != null) { + return CLI_HOST_NAME; + } + try { + return new URL(Jenkins.get().getRootUrl()).getHost(); + } catch (MalformedURLException | NullPointerException e) { + throw new IllegalStateException("Could not get TcpSlaveAgentListener host name", e); + } + } + /** * Gets the Base64 encoded public key that forms part of this instance's identity keypair. * @return the Base64 encoded public key -- GitLab