package main; import ip.AdIp; import java.net.InetAddress; import javax.swing.JTextArea; /** * * Classe de gestion IP (tests ping et récupération de l'adresse IP) * * @author Emerick MAHE * @version 1.0 March 2009 * */ public class GestIp { /** * Teste le ping sur une adresse donnée * * @return l'état du test de ping */ static int valPing; private static String temp = ""; public static boolean ping() { boolean ping = false; try { setDisplay("\nTest ping en cours sur "); final String hostName = parametrage.Parametres.getAdHttp(); setDisplay(hostName); final InetAddress target = InetAddress.getByName(hostName); if (target != null) { //Teste le retour de ping pour des délais variant de 2ms à 5ms par pas de 0,5ms for (int i = 2000; i < 5000; i += 100) { ping = target.isReachable(i); if (ping) setDisplay("\nPing OK < " + i + " micro secondes"); else setDisplay("\nPing NOK > " + i + " micro secondes"); if (ping == true){ setValPing(i); return ping; } } } else setDisplay("false target"); } catch (Exception e) { System.err.println("Erreur : " + e); e.printStackTrace(); } return ping; } /** * Détecte l'attribution d'une adresse IP * * @return l'état du test d'attribution d'adresse IP */ public static boolean testIp() { boolean res = false; String ip = ""; int i = 0; // On teste plusieurs fois que l'adresse IP locale est différente de l'adresse de départ try { while ((ip.contains("127.0.0.1") || ip.isEmpty()) && i < 10) { ip = AdIp.getIp(); setDisplay("\nAdresse IP: " + ip); Thread.sleep(10000); i++; } } catch (Exception e2) { e2.printStackTrace(); } if ((ip.contains("127.0.0.1") || ip.isEmpty()) && i < 10) return false; else return true; } public int getValPing() { return valPing; } public static void setValPing(int i) { valPing = i; } public static void setDisplay(String tempd) { temp += tempd; } public static String getDisplay() { return temp; } }