From ffedc82785c47f1381fec01b522e065fbb5fa291 Mon Sep 17 00:00:00 2001 From: Ty Sayers Date: Sat, 14 Mar 2015 01:52:37 -0400 Subject: [PATCH] Commit leftover package that was not committed from previous re-organization. --- Plugins/Mineplex.Bungee.Mineplexer/.classpath | 2 +- Plugins/Mineplex.BungeeRotator/.classpath | 2 +- .../serverdata/servers/ConnectionData.java | 28 +++++++ .../servers/DedicatedServerSorter.java | 20 +++++ .../serverdata/servers/ServerManager.java | 71 ++++++++++++++++ .../serverdata/servers/ServerRepository.java | 80 +++++++++++++++++++ 6 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ConnectionData.java create mode 100644 Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/DedicatedServerSorter.java create mode 100644 Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerManager.java create mode 100644 Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerRepository.java diff --git a/Plugins/Mineplex.Bungee.Mineplexer/.classpath b/Plugins/Mineplex.Bungee.Mineplexer/.classpath index 1dd72efb3..cf5b770d7 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/.classpath +++ b/Plugins/Mineplex.Bungee.Mineplexer/.classpath @@ -1,7 +1,7 @@ - + diff --git a/Plugins/Mineplex.BungeeRotator/.classpath b/Plugins/Mineplex.BungeeRotator/.classpath index d664420d1..bacfcce13 100644 --- a/Plugins/Mineplex.BungeeRotator/.classpath +++ b/Plugins/Mineplex.BungeeRotator/.classpath @@ -1,7 +1,7 @@ - + diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ConnectionData.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ConnectionData.java new file mode 100644 index 000000000..efb265fc1 --- /dev/null +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ConnectionData.java @@ -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; + } +} + diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/DedicatedServerSorter.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/DedicatedServerSorter.java new file mode 100644 index 000000000..8f5b6853f --- /dev/null +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/DedicatedServerSorter.java @@ -0,0 +1,20 @@ +package mineplex.serverdata.servers; + +import java.util.Comparator; + +import mineplex.serverdata.data.DedicatedServer; + +public class DedicatedServerSorter implements Comparator +{ + @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; + } +} \ No newline at end of file diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerManager.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerManager.java new file mode 100644 index 000000000..81a87c6a4 --- /dev/null +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerManager.java @@ -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 repositories = new HashMap(); + + /** + * @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); + } +} diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerRepository.java new file mode 100644 index 000000000..37b119766 --- /dev/null +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerRepository.java @@ -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 getServerStatuses(); + + public Collection getServerStatusesByPrefix(String prefix); + + public Collection 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 getDedicatedServers(); + + /** + * @return a newly instanced snapshot {@link Collection} of all the + * currently active {@link ServerGroup}s in the repository. + */ + public Collection getServerGroups(Collection servers); + + public ServerGroup getServerGroup(String serverGroup); + + public Collection getDeadServers(); + + void updateServerGroup(ServerGroup serverGroup); + + public void removeServerGroup(ServerGroup serverGroup); +}