/** * Copyright (c) 2014-2017 by the respective copyright holders. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html */ package org.eclipse.smarthome.model.script.actions; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.SocketTimeoutException; /** * This Action checks the vitality of the given host. * * @author Thomas.Eichstaedt-Engelen * @author Kai Kreuzer */ public class Ping { /** * Checks the vitality of <code>host</code>. If <code>port</code> '0' * is specified (which is the default when configuring just the host), a * regular ping is issued. If other ports are specified we try open a new * Socket with the given <code>timeout</code>. * * @param host * @param port * @param timeout * * @return <code>true</code> when <code>host</code> is reachable on <code>port</code> within the given * <code>timeout</code> and <code>false</code> in all other * cases. * * @throws IOException * @throws SocketTimeoutException */ public static boolean checkVitality(String host, int port, int timeout) throws IOException, SocketTimeoutException { boolean success = false; if (host != null && timeout > 0) { if (port == 0) { success = InetAddress.getByName(host).isReachable(timeout); } else { SocketAddress socketAddress = new InetSocketAddress(host, port); Socket socket = new Socket(); socket.connect(socketAddress, timeout); success = true; socket.close(); } } return success; } }