Commit cfc8613c authored by Benedikt Geißler's avatar Benedikt Geißler 🐸

Merge branch 'server-config' into 'master'

Server config

- adapt load/save config to server config files

See merge request !12
parents 4df6aff9 da431153
......@@ -74,7 +74,22 @@ public class LoadConfigFile {
userPrefs.getInt("SERVER_PORT", 65500), rcStd, defLang);
}
public void loadServerPrefs(String path, ConfigTypes configTypes) {
// TODO: implement me
public void loadServerPrefs(String path, ConfigTypes configTypes)
throws IOException, InvalidPreferencesFormatException {
String nodeName = configTypes.toString();
// create and load default prefs
InputStream in = new BufferedInputStream(new FileInputStream(path));
Preferences.importPreferences(in);
Preferences defaultPrefs = Preferences.userRoot().node(nodeName);
in.close();
configParameters = new ServerConfig(
defaultPrefs.getInt("MAX_START_INSTRUCTIONS_COUNT", 1000),
defaultPrefs.getInt("MAX_PLAYER_COUNT", 2),
defaultPrefs.getInt("MAX_CYCLES", 100000),
defaultPrefs.getInt("CORE_SIZE", 10000),
defaultPrefs.getInt("PORT", 123));
}
}
......@@ -73,7 +73,25 @@ public class SaveConfigFile {
out.close();
}
private void saveServerConfig(ServerConfig params, String userFilePath, String nodeName) {
// TODO: implement me
private void saveServerConfig(ServerConfig params, String userFilePath, String nodeName)
throws IOException {
if (userFilePath.isEmpty()) {
throw new IllegalArgumentException("user file path should not be empty!");
}
Preferences userPrefs = Preferences.userRoot().node(nodeName);
userPrefs.putInt("MAX_START_INSTRUCTIONS_COUNT", params.getMaxStartInstructions());
userPrefs.putInt("MAX_PLAYER_COUNT", params.getMaxPlayersCount());
userPrefs.putInt("MAX_CYCLES", params.getCoreSize());
userPrefs.putInt("CORE_SIZE", params.getCoreSize());
userPrefs.putInt("PORT", params.getPort());
FileOutputStream out = new FileOutputStream(userFilePath);
try {
userPrefs.exportNode(out);
} catch (IOException | BackingStoreException exc) {
exc.printStackTrace();
}
out.close();
}
}
package gone.lib.config;
/**
* simple data type to store server config variables
*
* @author matthias
*/
public class ServerConfig extends ConfigParameters {
// TODO: add necessary variables
private int maxCycles;
private int maxStartInstructions;
private int port;
private int maxPlayersCount;
private int coreSize;
/* constructor */
public ServerConfig(int maxCycles, int maxStartInstructions, int port, int maxPlayersCount,
int coreSize) {
setMaxCycles(maxCycles);
setMaxStartInstructions(maxStartInstructions);
setPort(port);
setMaxPlayersCount(maxPlayersCount);
setCoreSize(coreSize);
}
/* setter */
private void setMaxCycles(int maxCycles) {
if (maxCycles <= 0) {
throw new IllegalArgumentException("max cycles must be > 0");
}
this.maxCycles = maxCycles;
}
private void setMaxStartInstructions(int maxStartInstructions) {
if (maxStartInstructions <= 0 || maxStartInstructions > 2000) {
throw new IllegalArgumentException("maxStartInstructions must be > 0 && <= 2000");
}
this.maxStartInstructions = maxStartInstructions;
}
private void setPort(int port) {
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("port must be >= 0 && <= 65535");
}
this.port = port;
}
private void setMaxPlayersCount(int maxPlayersCount) {
if (maxPlayersCount != 1 && maxPlayersCount != 2) {
throw new IllegalArgumentException("maxPlayersCount must be 1 or 2");
}
this.maxPlayersCount = maxPlayersCount;
}
private void setCoreSize(int coreSize) {
if (coreSize <= 0) {
throw new IllegalArgumentException("max cycles must be > 0");
}
this.coreSize = coreSize;
}
/* getter */
public int getMaxCycles() {
return maxCycles;
}
public int getMaxStartInstructions() {
return maxStartInstructions;
}
public int getPort() {
return port;
}
public int getMaxPlayersCount() {
return maxPlayersCount;
}
public int getCoreSize() {
return coreSize;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment