Add fail-safe default connection in case of issue in proper redis-config.dat generation. Add documentation to clarify purposes related to redis configuration.

This commit is contained in:
Ty Sayers 2015-05-16 13:12:20 -04:00
parent a8b0dddf17
commit 672fc99a3e
2 changed files with 43 additions and 12 deletions

View File

@ -1,7 +1,5 @@
package mineplex.serverdata.redis;
import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@ -10,26 +8,51 @@ import mineplex.serverdata.servers.ConnectionData;
public class RedisConfig
{
private static Random random = new Random();
// Failsafe values in case configuration is not provided
private static final String DEFAULT_IP = "10.33.53.16";
private static final int DEFAULT_PORT = 6379;
private static Random random = new Random(); // Utility random
// The connections managed by this configuration
private ConnectionData _masterConnection;
private List<ConnectionData> _slaveConnections;
/**
* Class constructor
* @param master
* @param slaves
*/
public RedisConfig(ConnectionData master, List<ConnectionData> slaves)
{
_masterConnection = master;
_slaveConnections = slaves;
}
/**
* Class constructor
* Produces a default-value based RedisConfig.
*/
public RedisConfig()
{
this(new ConnectionData(DEFAULT_IP, DEFAULT_PORT), new ArrayList<ConnectionData>());
}
/**
* {@code writeable} defaults to {@literal true}.
* @see #getConnection(boolean)
*/
public ConnectionData getConnection()
{
return getConnection(true);
}
/**
* @param writeable - whether the returned connection reference can receive write-requests.
* @return a {@link ConnectionData} referencing a valid redis-connection from this configuration.
*/
public ConnectionData getConnection(boolean writeable)
{
if (writeable)
if (writeable || _slaveConnections.size() == 0)
{
return _masterConnection;
}

View File

@ -18,9 +18,9 @@ import mineplex.serverdata.redis.RedisServerRepository;
*
*/
public class ServerManager
{
{
// Configuration determining connection information
private static RedisConfig _config = getConfig();
private static RedisConfig _config;
// The cached repository instances
private static Map<Region, ServerRepository> repositories = new HashMap<Region, ServerRepository>();
@ -68,21 +68,24 @@ public class ServerManager
return getConnection(false);
}
/**
* @param writeable - whether the connection referenced in return can receive write-requests
* @return a newly generated {@code ConnectionData} pointing to a valid connection.
*/
public static ConnectionData getConnection(boolean writeable)
{
System.out.println("fETChing");
return getConfig().getConnection(writeable);
}
/**
* @return the {@link RedisConfig} associated with this manager, providing appropriate connections.
*/
public static RedisConfig getConfig()
{
System.out.println("LOADING CONFIG");
if (_config == null)
{
try
{
System.out.println("STARTING");
File configFile = new File("redis-config.dat");
if (configFile.exists())
@ -105,7 +108,8 @@ public class ServerManager
}
else
{
System.out.println("DOES NOT EXIST AT PATH " + configFile.toPath().toString());
System.out.println("redis-config.dat not found at " + configFile.toPath().toString());
_config = new RedisConfig();
}
}
catch (Exception exception)
@ -119,6 +123,10 @@ public class ServerManager
return _config;
}
/**
* @param line - the serialized line representing a valid {@link ConnectionData} object.
* @return a deserialized {@link ConnectionData} referenced by the {@code line} passed in.
*/
private static ConnectionData deserializeConnection(String line)
{
String[] args = line.split(" ");