diff options
author | Draknyte1 <Draknyte1@hotmail.com> | 2016-11-07 22:11:04 +1000 |
---|---|---|
committer | Draknyte1 <Draknyte1@hotmail.com> | 2016-11-07 22:11:04 +1000 |
commit | 5209e2b9ea0741902be58e354d4bd54b5ad15af4 (patch) | |
tree | eb6789ef62c9f5376da48a02ef93bf9987d475dd /src/Java/gtPlusPlus/core/util | |
parent | b681c6a906c4d01129c8f624af68087bb1238dac (diff) | |
download | GT5-Unofficial-5209e2b9ea0741902be58e354d4bd54b5ad15af4.tar.gz GT5-Unofficial-5209e2b9ea0741902be58e354d4bd54b5ad15af4.tar.bz2 GT5-Unofficial-5209e2b9ea0741902be58e354d4bd54b5ad15af4.zip |
+ Added addition checks for availability of the internet.
> Should fix #34 and #27 forever, for all nationalities using this mod. (Maybe 4 checks isn't enough though?)
Diffstat (limited to 'src/Java/gtPlusPlus/core/util')
-rw-r--r-- | src/Java/gtPlusPlus/core/util/Utils.java | 4 | ||||
-rw-r--r-- | src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java | 98 |
2 files changed, 80 insertions, 22 deletions
diff --git a/src/Java/gtPlusPlus/core/util/Utils.java b/src/Java/gtPlusPlus/core/util/Utils.java index 2ee2de7bba..36a0b717aa 100644 --- a/src/Java/gtPlusPlus/core/util/Utils.java +++ b/src/Java/gtPlusPlus/core/util/Utils.java @@ -47,6 +47,10 @@ public class Utils { public static boolean isModUpToDate(){ + if (CORE.MASTER_VERSION.toLowerCase().equals("offline")){ + return false; + } + if (CORE.MASTER_VERSION.equals(CORE.VERSION.toLowerCase())){ return true; } diff --git a/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java b/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java index e2ec1f5d09..aa0f24f559 100644 --- a/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java +++ b/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java @@ -2,35 +2,38 @@ package gtPlusPlus.core.util.networking; import java.io.*; import java.net.*; +import java.util.Enumeration; public class NetworkUtils { public static String getContentFromURL(String args) { - if (netIsAvailable()){ - try { - URL url; - // get URL content - url = new URL(args); - URLConnection conn = url.openConnection(); - // open the stream and put it into BufferedReader - BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String inputLine; - String tempLine = null; - while ((inputLine = br.readLine()) != null) { - tempLine = inputLine; - } - br.close(); - return tempLine; - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + try { + if (hasValidNetworkInterface()){ + if (netIsAvailableGithub() || netIsAvailableOther() || netIsAvailableBaidu() || netIsAvailableGoogle()){ + try { + URL url; + // get URL content + url = new URL(args); + URLConnection conn = url.openConnection(); + // open the stream and put it into BufferedReader + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String inputLine; + String tempLine = null; + while ((inputLine = br.readLine()) != null) { + tempLine = inputLine; + } + br.close(); + return tempLine; + } + catch (MalformedURLException e) {} + catch (IOException e) {} + } } - } - return null; + } catch (SocketException e) {} + return "offline"; } - private static boolean netIsAvailable() { + private static boolean netIsAvailableGoogle() { try { final URL url = new URL("http://www.google.com"); final URLConnection conn = url.openConnection(); @@ -43,4 +46,55 @@ public class NetworkUtils { } } + private static boolean netIsAvailableBaidu() { + try { + final URL url = new URL("http://www.baidu.com"); + final URLConnection conn = url.openConnection(); + conn.connect(); + return true; + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + return false; + } + } + + private static boolean netIsAvailableGithub() { + try { + final URL url = new URL("https://github.com/draknyte1/GTplusplus"); + final URLConnection conn = url.openConnection(); + conn.connect(); + return true; + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + return false; + } + } + + private static boolean netIsAvailableOther() { + try { + int timeout = 2000; + InetAddress[] addresses = InetAddress.getAllByName("www.yahoo.com"); + for (InetAddress address : addresses) { + if (address.isReachable(timeout)) + return true; + return false; + } + } catch (Exception e) { + return false; + } + return false; + } + + private static boolean hasValidNetworkInterface() throws SocketException{ + Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + NetworkInterface interf = interfaces.nextElement(); + if (interf.isUp() && !interf.isLoopback()) + return true; + } + return false; + } + } |