Mineplex2018-withcommit/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/status/InternetStatus.java
Jonathan Williams 92ea94c3be Fixed creating clan delay in clans/ClientClan.java
Added logging for Mineplexer startup.
Fixed return generated keys in RepoBase
2014-11-28 14:06:41 -08:00

81 lines
1.5 KiB
Java

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, 1L, 1L, TimeUnit.MINUTES);
System.out.println("Initialized InternetStatus.");
}
@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;
}
}