diff --git a/core/src/main/java/hudson/model/Computer.java b/core/src/main/java/hudson/model/Computer.java index b648d33653c48b5bffb61087b2b4ecd14d2cc333..eb95dd6868e4b26e82c43f96329dce5d594a97a1 100644 --- a/core/src/main/java/hudson/model/Computer.java +++ b/core/src/main/java/hudson/model/Computer.java @@ -57,12 +57,17 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Enumeration; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.logging.LogRecord; +import java.util.logging.Level; +import java.util.logging.Logger; import java.nio.charset.Charset; +import java.net.InetAddress; +import java.net.NetworkInterface; /** * Represents the running state of a remote computer that holds {@link Executor}s. @@ -552,6 +557,55 @@ public abstract class Computer extends AbstractModelObject implements AccessCont return RemotingDiagnostics.getThreadDump(getChannel()); } + /** + * This method tries to compute the name of the host that's reachable by all the other nodes. + * + *
+ * Since it's possible that the slave is not reachable from the master (it may be behind a firewall, + * connecting to master via JNLP), in which case this method returns null. + * + * It's surprisingly tricky for a machine to know a name that other systems can get to, + * especially between things like DNS search suffix, the hosts file, and YP. + * + *
+ * So the technique here is to compute possible interfaces and names on the slave,
+ * then try to ping them from the master, and pick the one that worked.
+ *
+ * @since 1.300
+ */
+ public String getHostName() throws IOException, InterruptedException {
+ for( String address : getChannel().call(new ListPossibleNames())) {
+ try {
+ InetAddress ia = InetAddress.getByName(address);
+ if(ia.isReachable(500))
+ return ia.getCanonicalHostName();
+ } catch (IOException e) {
+ // if a given name fails to parse on this host, we get this error
+ LOGGER.log(Level.FINE, "Failed to parse "+address,e);
+ }
+ }
+ return null;
+ }
+
+ private static class ListPossibleNames implements Callable,IOException> {
+ public List