From bb8bf3c4986ac3e30eeaee6d96a89e06fb496e1f Mon Sep 17 00:00:00 2001 From: kohsuke Date: Mon, 20 Sep 2010 01:12:44 +0000 Subject: [PATCH] [FIXED HUDSON-5249] Made the ping behaviour customizable. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@35023 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/model/Computer.java | 2 +- .../java/hudson/model/ComputerPinger.java | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/hudson/model/ComputerPinger.java diff --git a/core/src/main/java/hudson/model/Computer.java b/core/src/main/java/hudson/model/Computer.java index ce39e9a1c8..cb32a629a6 100644 --- a/core/src/main/java/hudson/model/Computer.java +++ b/core/src/main/java/hudson/model/Computer.java @@ -792,7 +792,7 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces LOGGER.fine(address+" is not an IPv4 address"); continue; } - if(!ia.isReachable(3000)) { + if(!ComputerPinger.checkIsReachable(ia, 3)) { LOGGER.fine(address+" didn't respond to ping"); continue; } diff --git a/core/src/main/java/hudson/model/ComputerPinger.java b/core/src/main/java/hudson/model/ComputerPinger.java new file mode 100644 index 0000000000..57ea2385a1 --- /dev/null +++ b/core/src/main/java/hudson/model/ComputerPinger.java @@ -0,0 +1,65 @@ +package hudson.model; + +import hudson.Extension; +import hudson.ExtensionList; +import hudson.ExtensionPoint; + +import java.io.IOException; +import java.net.InetAddress; +import java.util.logging.Logger; + +/** + * A way to see if a computer is reachable. + * + * @since 1.378 + */ +public abstract class ComputerPinger implements ExtensionPoint { + /** + * Is the specified address reachable? + * + * @param ia The address to check. + * @param timeout Timeout in seconds. + */ + public abstract boolean isReachable(InetAddress ia, int timeout) throws IOException; + + /** + * Get all registered instances. + */ + public static ExtensionList all() { + return Hudson.getInstance().getExtensionList(ComputerPinger.class); + } + + /** + * Is this computer reachable via the given address? + * + * @param ia The address to check. + * @param timeout Timeout in seconds. + */ + public static boolean checkIsReachable(InetAddress ia, int timeout) throws IOException { + for (ComputerPinger pinger : ComputerPinger.all()) { + try { + if (pinger.isReachable(ia, timeout)) { + return true; + } + } catch (IOException e) { + LOGGER.fine("Error checking reachability with " + pinger + ": " + e.getMessage()); + } + } + + return false; + } + + /** + * Default pinger - use Java built-in functionality. This doesn't always work, + * a host may be reachable even if this returns false. + */ + @Extension + public static class BuiltInComputerPinger extends ComputerPinger { + @Override + public boolean isReachable(InetAddress ia, int timeout) throws IOException { + return ia.isReachable(timeout * 1000); + } + } + + private static final Logger LOGGER = Logger.getLogger(ComputerPinger.class.getName()); +}