diff options
author | Draknyte1 <Draknyte1@hotmail.com> | 2017-04-13 16:51:45 +1000 |
---|---|---|
committer | Draknyte1 <Draknyte1@hotmail.com> | 2017-04-13 16:51:45 +1000 |
commit | 0184ca54f686c8e8972f702ff0b37a7ae8990f5b (patch) | |
tree | 6ac7e0045a4ec2347e1aaa42b272ab7a469df1c9 /src/Java/gtPlusPlus/core | |
parent | 6f9b2f221ca631ff2f28aa4b3e538ded9e51ca71 (diff) | |
download | GT5-Unofficial-0184ca54f686c8e8972f702ff0b37a7ae8990f5b.tar.gz GT5-Unofficial-0184ca54f686c8e8972f702ff0b37a7ae8990f5b.tar.bz2 GT5-Unofficial-0184ca54f686c8e8972f702ff0b37a7ae8990f5b.zip |
% Attempted more recipe work on the Industrial Sifter.
% Changed network availability testing to be more robust.
Diffstat (limited to 'src/Java/gtPlusPlus/core')
-rw-r--r-- | src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java b/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java index c25c5d05be..77f7d74928 100644 --- a/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java +++ b/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java @@ -7,36 +7,32 @@ import java.util.Enumeration; public class NetworkUtils { public static String getContentFromURL(final String args) { - try { - if (hasValidNetworkInterface()){ - if (netIsAvailableGithub() || netIsAvailableOther() || netIsAvailableBaidu() || netIsAvailableGoogle()){ - try { - URL url; - // get URL content - url = new URL(args); - final URLConnection conn = url.openConnection(); - // open the stream and put it into BufferedReader - final BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String inputLine; - String tempLine = null; - while ((inputLine = br.readLine()) != null) { - tempLine = inputLine; - } - br.close(); - return tempLine; + if (checkNetworkIsAvailableWithValidInterface()){ + try { + URL url; + // get URL content + url = new URL(args); + final URLConnection conn = url.openConnection(); + // open the stream and put it into BufferedReader + final BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String inputLine; + String tempLine = null; + while ((inputLine = br.readLine()) != null) { + tempLine = inputLine; } - catch (final MalformedURLException e) {} - catch (final IOException e) {} + br.close(); + return tempLine; } - } - } catch (final SocketException e) {} + catch (final MalformedURLException e) {} + catch (final IOException e) {} + } return "offline"; } public static boolean checkNetworkIsAvailableWithValidInterface(){ try { if (hasValidNetworkInterface()){ - if (netIsAvailableGithub() || netIsAvailableOther() || netIsAvailableBaidu() || netIsAvailableGoogle()){ + if (checkAddressWithTimeout("http://www.google.com", 10) || checkAddressWithTimeout("http://www.baidu.com", 10) || checkAddressWithTimeout("https://github.com/draknyte1/GTplusplus", 10) || checkAddressWithTimeout("www.yahoo.com", 10)){ return true; } } @@ -99,6 +95,21 @@ public class NetworkUtils { } return false; } + + private static boolean checkAddressWithTimeout(String URL, int timeout) { + try { + final InetAddress[] addresses = InetAddress.getAllByName(URL); + for (final InetAddress address : addresses) { + if (address.isReachable(timeout)) { + return true; + } + return false; + } + } catch (final Exception e) { + return false; + } + return false; + } private static boolean hasValidNetworkInterface() throws SocketException{ final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |