Starting work on dynamic server.

This commit is contained in:
Jonathan Williams 2013-10-15 14:19:14 -07:00
parent 1a5f6a9b74
commit 9569c3906e
3 changed files with 145 additions and 35 deletions

Binary file not shown.

View File

@ -15,7 +15,8 @@ public class Repository
private String _password = "y2D4atu3Pene2asw";
private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ServerStatus (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), serverGroup VARCHAR(256), address VARCHAR(256), updated LONG, motd VARCHAR(256), players INT, maxPlayers INT, tps INT, ram INT, maxRam INT, PRIMARY KEY (id));";
private static String RETRIEVE_SERVER_STATUSES = "SELECT serverName, address, motd, players, maxPlayers FROM ServerStatus WHERE TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.updated)) > 10;";
private static String RETRIEVE_OLD_SERVER_STATUSES = "SELECT serverName, address, motd, players, maxPlayers FROM ServerStatus WHERE TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.updated)) > 10;";
private static String RETRIEVE_SERVER_STATUSES = "SELECT serverName, address, motd, players, maxPlayers FROM ServerStatus WHERE TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.updated)) <= 10;";
public void initialize()
{
@ -52,12 +53,75 @@ public class Repository
}
}
public List<ServerStatusData> retrieveServerStatuses()
public List<ServerStatusData> retrieveOldServerStatuses()
{
Connection connection = null;
ResultSet resultSet = null;
List<ServerStatusData> serverData = new ArrayList<ServerStatusData>();
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
PreparedStatement preparedStatement = connection.prepareStatement(RETRIEVE_OLD_SERVER_STATUSES);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
ServerStatusData serverStatusData = new ServerStatusData();
serverStatusData.Name = resultSet.getString(1);
String addressPortString = resultSet.getString(2);
serverStatusData.Address = addressPortString.split(":")[0];
serverStatusData.Port = Integer.parseInt(addressPortString.split(":")[1]);
serverStatusData.Motd = resultSet.getString(3);
serverStatusData.Players = resultSet.getInt(4);
serverStatusData.MaxPlayers = resultSet.getInt(5);
serverData.add(serverStatusData);
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return serverData;
}
public List<GroupStatusData> retrieveGroupStatusData()
{
Connection connection = null;
ResultSet resultSet = null;
List<GroupStatusData> groupData = new ArrayList<GroupStatusData>();
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);

View File

@ -6,6 +6,7 @@ import java.io.InputStreamReader;
public class ServerMonitor
{
private static Repository _repository = new Repository();
private static int _count = 0;
public static void main (String args[])
{
@ -13,49 +14,94 @@ public class ServerMonitor
while (true)
{
for (ServerStatusData statusData : _repository.retrieveServerStatuses())
if (_count % 20 == 0)
{
String key = statusData.Address + " " + statusData.Name;
String cmd = "/home/mineplex/restartServer.sh";
Process process = null;
try
{
process = new ProcessBuilder(new String[] {"/bin/sh", "-x", cmd, statusData.Address, statusData.Name}).start();
process.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while(line != null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (process != null)
{
process.destroy();
}
}
System.out.println("Sent restart command to " + key + "");
for (ServerStatusData statusData : _repository.retrieveOldServerStatuses())
{
String key = statusData.Address + " " + statusData.Name;
String cmd = "/home/mineplex/restartServer.sh";
Process process = null;
try
{
process = new ProcessBuilder(new String[] {"/bin/sh", "-x", cmd, statusData.Address, statusData.Name}).start();
process.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while(line != null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (process != null)
{
process.destroy();
}
}
System.out.println("Sent restart command to " + key + "");
}
}
if (_count % 10 == 0)
{
HashMap<String, GroupStatusData>
for (ServerStatusData statusData : _repository.retrieveGroupStatusData())
{
String key = statusData.Address + " " + statusData.Name;
String cmd = "/home/mineplex/restartServer.sh";
Process process = null;
try
{
process = new ProcessBuilder(new String[] {"/bin/sh", "-x", cmd, statusData.Address, statusData.Name}).start();
process.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while(line != null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (process != null)
{
process.destroy();
}
}
System.out.println("Sent restart command to " + key + "");
}
}
try
{
Thread.sleep(15000);
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
_count++;
_count %= 20;
}
}
}