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
|
|
|
|
{
|
|
|
|
private Plugin _plugin;
|
|
|
|
private StatusRepository _repository;
|
2014-07-08 22:18:27 +02:00
|
|
|
|
2014-07-08 21:20:19 +02:00
|
|
|
public InternetStatus(Plugin plugin)
|
|
|
|
{
|
|
|
|
_plugin = plugin;
|
2014-07-08 22:18:27 +02:00
|
|
|
|
2014-07-08 21:20:19 +02:00
|
|
|
ListenerInfo listenerInfo = _plugin.getProxy().getConfigurationAdapter().getListeners().iterator().next();
|
|
|
|
boolean us = !new File("eu.dat").exists();
|
|
|
|
String address = listenerInfo.getHost().getAddress().getHostAddress() + ":" + listenerInfo.getHost().getPort();
|
2014-07-08 22:18:27 +02:00
|
|
|
|
2014-07-08 21:20:19 +02:00
|
|
|
_repository = new StatusRepository(address, us);
|
|
|
|
_repository.initialize();
|
2014-07-08 22:18:27 +02:00
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
_repository.updateOnlineStatus(isOnline());
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isOnline()
|
|
|
|
{
|
2014-07-08 22:18:27 +02:00
|
|
|
if (testUrl("www.google.com"))
|
|
|
|
return true;
|
|
|
|
else if (testUrl("www.espn.com"))
|
|
|
|
return true;
|
|
|
|
else if (testUrl("www.bing.com"))
|
|
|
|
return true;
|
2014-07-09 03:01:03 +02:00
|
|
|
|
2014-07-08 22:18:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean testUrl(String url)
|
|
|
|
{
|
|
|
|
Socket socket = null;
|
|
|
|
boolean reachable = false;
|
|
|
|
|
2014-07-08 21:20:19 +02:00
|
|
|
try
|
|
|
|
{
|
2014-07-08 22:18:27 +02:00
|
|
|
socket = new Socket(url, 80);
|
|
|
|
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
|
|
|
finally
|
2014-07-08 21:20:19 +02:00
|
|
|
{
|
2014-07-08 22:18:27 +02:00
|
|
|
if (socket != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|