Mineplex2018-withcommit/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/status/InternetStatus.java

79 lines
1.5 KiB
Java
Raw Normal View History

package mineplex.bungee.status;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
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;
public InternetStatus(Plugin plugin)
{
_plugin = plugin;
ListenerInfo listenerInfo = _plugin.getProxy().getConfigurationAdapter().getListeners().iterator().next();
boolean us = !new File("eu.dat").exists();
String address = listenerInfo.getHost().getAddress().getHostAddress() + ":" + listenerInfo.getHost().getPort();
_repository = new StatusRepository(address, us);
_repository.initialize();
_plugin.getProxy().getScheduler().schedule(_plugin, this, 5L, 5L, TimeUnit.MINUTES);
}
@Override
public void run()
{
_repository.updateOnlineStatus(isOnline());
}
private boolean isOnline()
{
if (testUrl("www.google.com"))
return true;
else if (testUrl("www.espn.com"))
return true;
else if (testUrl("www.bing.com"))
return true;
return false;
}
private boolean testUrl(String url)
{
Socket socket = null;
boolean reachable = false;
try
{
socket = new Socket(url, 80);
reachable = true;
}
catch (Exception e)
{
// Meh i don't care
}
finally
{
if (socket != null)
{
try
{
socket.close();
}
catch (IOException e)
{
}
}
}
return reachable;
}
}