2014-07-08 21:20:19 +02:00
|
|
|
package mineplex.bungee.status;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2014-07-08 22:18:27 +02:00
|
|
|
import java.net.Socket;
|
2014-07-08 21:20:19 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import net.md_5.bungee.api.config.ListenerInfo;
|
|
|
|
import net.md_5.bungee.api.plugin.Plugin;
|
|
|
|
|
|
|
|
public class InternetStatus implements Runnable
|
|
|
|
{
|
2015-03-14 06:03:22 +01:00
|
|
|
// Current internet connectivity status
|
|
|
|
private static boolean _connected = true;
|
|
|
|
public static boolean isConnected() { return _connected; }
|
|
|
|
|
2014-07-08 21:20:19 +02:00
|
|
|
private Plugin _plugin;
|
2014-07-08 22:18:27 +02:00
|
|
|
|
2014-07-08 21:20:19 +02:00
|
|
|
public InternetStatus(Plugin plugin)
|
|
|
|
{
|
|
|
|
_plugin = plugin;
|
2014-07-09 03:01:03 +02:00
|
|
|
_plugin.getProxy().getScheduler().schedule(_plugin, this, 1L, 1L, TimeUnit.MINUTES);
|
2014-11-28 23:06:41 +01:00
|
|
|
|
|
|
|
System.out.println("Initialized InternetStatus.");
|
2014-07-08 21:20:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2015-03-14 06:03:22 +01:00
|
|
|
_connected = isOnline(); // Update _connected flag.
|
2014-07-08 21:20:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isOnline()
|
|
|
|
{
|
2015-03-14 06:03:22 +01:00
|
|
|
return testUrl("www.google.com")
|
|
|
|
|| testUrl("www.espn.com")
|
|
|
|
|| testUrl("www.bing.com");
|
2014-07-08 22:18:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean testUrl(String url)
|
|
|
|
{
|
|
|
|
boolean reachable = false;
|
|
|
|
|
2015-03-14 06:03:22 +01:00
|
|
|
try (Socket socket = new Socket(url, 80))
|
2014-07-08 21:20:19 +02:00
|
|
|
{
|
2014-07-08 22:18:27 +02:00
|
|
|
reachable = true;
|
2014-07-08 21:20:19 +02:00
|
|
|
}
|
2014-07-08 22:18:27 +02:00
|
|
|
catch (Exception e)
|
2014-07-08 21:20:19 +02:00
|
|
|
{
|
2014-07-08 22:18:27 +02:00
|
|
|
// Meh i don't care
|
2014-07-08 21:20:19 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 22:18:27 +02:00
|
|
|
return reachable;
|
2014-07-08 21:20:19 +02:00
|
|
|
}
|
|
|
|
}
|