Merge branch 'master' of ssh://184.154.0.242:7999/min/Mineplex

This commit is contained in:
Jonathan Williams 2015-03-13 22:53:46 -07:00
commit a002769ac0
6 changed files with 201 additions and 2 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.8.0_31"/>
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/BungeeCord.jar"/>
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/commons-codec-1.6.jar"/>
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/commons-io-2.4.jar"/>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.8.0_31"/>
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/httpclient-4.2.jar"/>
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/httpcore-4.2.jar"/>
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/commons-codec-1.6.jar"/>

View File

@ -0,0 +1,28 @@
package mineplex.serverdata.servers;
/**
* ConnectionData stores information relevant for initiating a connection to a repository.
* @author MrTwiggy
*
*/
public class ConnectionData
{
private String _host; // The host URL to connect to repository
public String getHost() { return _host; }
private int _port; // The port to connect to repository
public int getPort() { return _port; }
/**
* Constructor
* @param host - the host URL defining the repository
* @param port - the port used for connection to repository
*/
public ConnectionData(String host, int port)
{
_host = host;
_port = port;
}
}

View File

@ -0,0 +1,20 @@
package mineplex.serverdata.servers;
import java.util.Comparator;
import mineplex.serverdata.data.DedicatedServer;
public class DedicatedServerSorter implements Comparator<DedicatedServer>
{
@Override
public int compare(DedicatedServer first, DedicatedServer second)
{
if (second.getAvailableRam() <= 1024) return -1;
else if (first.getAvailableRam() <= 1024) return 1;
else if (first.getAvailableRam() > second.getAvailableRam()) return -1;
else if (second.getAvailableRam() > first.getAvailableRam()) return 1;
else if (first.getAvailableCpu() > second.getAvailableCpu()) return -1;
else if (second.getAvailableCpu() > first.getAvailableCpu()) return 1;
else return 0;
}
}

View File

@ -0,0 +1,71 @@
package mineplex.serverdata.servers;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import mineplex.serverdata.Region;
import mineplex.serverdata.redis.RedisServerRepository;
/**
* ServerManager handles the creation/management of {@link ServerRepository}s for use.
* @author Ty
*
*/
public class ServerManager
{
// Connection host to server database
private static final String DATABASE_HOST = "10.33.53.16";
// Ports associated with slave redis instances
private static final int[] SLAVE_PORTS = {6377, 6378, 6380, 6381, 6382};
private static Random random = new Random();
// The cached repository instances
private static Map<Region, ServerRepository> repositories = new HashMap<Region, ServerRepository>();
/**
* @param host - the host url used to connect to the database
* @param port - the port to connect to the repository
* @param region - the geographical region of the {@link ServerRepository}.
* @return a newly instanced (or cached) {@link ServerRepository} for the specified {@code region}.
*/
public static ServerRepository getServerRepository(ConnectionData writeConn, ConnectionData readConn, Region region)
{
if (repositories.containsKey(region)) return repositories.get(region);
ServerRepository repository = new RedisServerRepository(writeConn, readConn, region);
repositories.put(region, repository);
return repository;
}
/**
* {@code host} defaults to {@value DEFAULT_REDIS_HOST} and
* {@code port} defaults to {@value DEFAULT_REDIS_PORT}.
*
* @see #getServerRepository(String, int, Region)
*/
public static ServerRepository getServerRepository(Region region)
{
return getServerRepository(getMasterConnection(), getSlaveConnection(), region);
}
/**
* @return the {@link ConnectionData} associated with the master instance connection.
*/
public static ConnectionData getMasterConnection()
{
return new ConnectionData(DATABASE_HOST, 6379);
}
/**
* Non-Deterministic: Generates random slave instance connection.
* @return the {@link ConnectionData} associated with a random slave connection.
*/
public static ConnectionData getSlaveConnection()
{
int port = SLAVE_PORTS[random.nextInt(SLAVE_PORTS.length)];
return new ConnectionData(DATABASE_HOST, port);
}
}

View File

@ -0,0 +1,80 @@
package mineplex.serverdata.servers;
import java.util.Collection;
import java.util.List;
import mineplex.serverdata.data.DedicatedServer;
import mineplex.serverdata.data.MinecraftServer;
import mineplex.serverdata.data.ServerGroup;
/**
* The ServerRepository is used for storing/retrieving active sessions
* for {@link MinecraftServer}s, {@link DedicatedServer}s, and {@link ServerGroup}s
* from a persistent database/repoistory.
* @author Ty
*
*/
public interface ServerRepository
{
/**
* @return a newly instanced snapshot {@link Collection} of all currently active
* {@link MinecraftServer}s in the repository.
*/
public Collection<MinecraftServer> getServerStatuses();
public Collection<MinecraftServer> getServerStatusesByPrefix(String prefix);
public Collection<MinecraftServer> getServersByGroup(String serverGroup);
/**
* @param serverName - the name of the {@link MinecraftServer} to be fetched.
* @return the currently active {@link MinecraftServer} with a matching {@code serverName},
* if an active one exists, null otherwise.
*/
public MinecraftServer getServerStatus(String serverName);
/**
* Update (or add, if it doesn't already exist) a {@link MinecraftServer}s data
* in the repository.
*
* A {@link MinecraftServer} must be updated within {@code timeout} milliseconds before
* it expires and is removed from the repository.
* @param serverData - the {@link MinecraftServer} to add/update in the repository.
* @param timeout - the timeout (in milliseconds) before the {@link MinecraftServer} session expires.
*/
public void updataServerStatus(MinecraftServer serverData, int timeout);
/**
* Remove an active {@link MinecraftServer} from the repository.
* @param serverData - the {@link MinecraftServer} to be removed.
*/
public void removeServerStatus(MinecraftServer serverData);
/**
* @param serverName - the name of the server whose existence is being checked.
* @return true, if there exists an active {@link MinecraftServer} session with a
* matching {@code serverName}, false otherwise.
*/
public boolean serverExists(String serverName);
/**
* @return a newly instanced snapshot {@link Collection} of all the
* currently active {@link DedicatedServer}s in the repository.
*/
public Collection<DedicatedServer> getDedicatedServers();
/**
* @return a newly instanced snapshot {@link Collection} of all the
* currently active {@link ServerGroup}s in the repository.
*/
public Collection<ServerGroup> getServerGroups(Collection<MinecraftServer> servers);
public ServerGroup getServerGroup(String serverGroup);
public Collection<MinecraftServer> getDeadServers();
void updateServerGroup(ServerGroup serverGroup);
public void removeServerGroup(ServerGroup serverGroup);
}