Commit 745edbdc authored by Benedikt Geißler's avatar Benedikt Geißler 🐸

Merge branch 'import-config-from-client' into 'master'

Import config from client

The package gone.client.config now is gone.lib.config

See merge request !10
parents f7937e3f 015a09c1
......@@ -3,7 +3,7 @@ plugins {
id 'net.ltgt.errorprone' version '0.0.8'
}
version = '0.1.2'
version = '0.2.0'
group = 'gone.corewars.library'
apply plugin: 'java'
......
package gone.lib.common;
/**
* all compatible languages for the client/ui
*
* @author matthias
*/
public enum Language {
de,
en
}
package gone.lib.config;
import gone.lib.common.Language;
import gone.lib.common.RcStandard;
import java.net.InetAddress;
/**
* dataType which represents all configurable parameters for the client/ui
*
* @author matthias
*/
public class ClientConfig extends ConfigParameters {
private int resolutionWidth;
private int resolutionHeight;
private String warriorName;
private String relativePathToRcFile;
private InetAddress inetAddress;
private int portLastServer;
private RcStandard rcStandard;
private Language language;
/* constructor */
public ClientConfig(int width, int height, String warriorName, String pathToRcFile,
InetAddress inetAddress, int portLastServer, RcStandard rcStandard,
Language language) {
setResolutionWidth(width);
setResolutionHeight(height);
setWarriorName(warriorName);
setPathToRcFile(pathToRcFile);
setInetAddress(inetAddress);
setPortLastServer(portLastServer);
setRcStandard(rcStandard);
setLanguage(language);
}
/* setter */
private void setResolutionWidth(int value) throws IllegalArgumentException {
if (value < 640) {
throw new IllegalArgumentException("width cant be < 640");
}
this.resolutionWidth = value;
}
private void setResolutionHeight(int value) {
if (value < 400) {
throw new IllegalArgumentException("height cant be < 400");
}
this.resolutionHeight = value;
}
private void setWarriorName(String value) throws IllegalArgumentException {
if (value.isEmpty()) {
throw new IllegalArgumentException("name cant be empty");
}
this.warriorName = value;
}
private void setPathToRcFile(String value) throws IllegalArgumentException {
if (value.isEmpty()) {
throw new IllegalArgumentException("path cant be empty");
}
this.relativePathToRcFile = value;
}
private void setInetAddress(InetAddress value) throws IllegalArgumentException {
// TODO value check
this.inetAddress = value;
}
private void setPortLastServer(int value) throws IllegalArgumentException {
if (value < 0 || value > 65535) {
throw new IllegalArgumentException("port must be >= 0 && <= 65535");
}
this.portLastServer = value;
}
private void setRcStandard(RcStandard value) throws IllegalArgumentException {
if (value != RcStandard.ICWS88 && value != RcStandard.ICWS94) {
throw new IllegalArgumentException("rcStandard must be icws88 or icws94");
}
this.rcStandard = value;
}
private void setLanguage(Language value) {
if (!value.equals(Language.de) && !value.equals(Language.en)) {
throw new IllegalArgumentException("not a correct language input variable!");
}
this.language = value;
}
/* getter */
public int getPortLastServer() {
return portLastServer;
}
public int getResolutionWidth() {
return resolutionWidth;
}
public int getResolutionHeight() {
return resolutionHeight;
}
public String getWarriorName() {
return warriorName;
}
public String getPathToRcFile() {
return relativePathToRcFile;
}
public InetAddress getInetAddress() {
return inetAddress;
}
public RcStandard getRcStandard() {
return rcStandard;
}
public Language getLanguage() {
return language;
}
}
package gone.lib.config;
/**
* @author matthias
*/
public class ConfigParameters {
}
package gone.lib.config;
/**
* @author matthias
*/
public enum ConfigTypes {
CLIENT_CONFIG,
SERVER_CONFIG
}
package gone.lib.config;
import gone.lib.common.Language;
import gone.lib.common.RcStandard;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.Preferences;
/**
* loads the default / user config file
* the config file is stored as a preferences file (xml key=value, utf8)
*
* @author matthias
*/
public class LoadConfigFile {
private ConfigParameters configParameters;
/* constructor */
public LoadConfigFile() {
this.configParameters = new ConfigParameters();
}
/* getter */
public ConfigParameters getConfigParameters() throws IllegalAccessException {
if (configParameters == null) {
throw new IllegalAccessException("configParameters must be set");
}
return configParameters;
}
/* class methods */
/**
* * loads a config pref file, transfer it into a intern ClientConfig object
* @param defaultFilePath location of the default config file
* @param userFilePath location of the user config file
* @throws IOException if file not found
* @throws IllegalArgumentException
*/
public void loadClientPrefs(String defaultFilePath, String userFilePath, ConfigTypes configTypes)
throws IOException, IllegalArgumentException, InvalidPreferencesFormatException {
String nodeName = configTypes.toString();
// create and load default prefs
InputStream in1 = new BufferedInputStream(new FileInputStream(defaultFilePath));
Preferences.importPreferences(in1);
Preferences defaultPrefs = Preferences.userRoot().node(nodeName);
in1.close();
// initiate user prefs with default prefs, load prefs
Preferences userPrefs = defaultPrefs;
InputStream in2 = new BufferedInputStream(new FileInputStream(userFilePath));
Preferences.importPreferences(in2);
in2.close();
// get the pref values from userPrefs, set them to configParameters variable
// rc standard, language
RcStandard rcStd = (userPrefs.get("RC_STANDARD", RcStandard.ICWS88.toString()))
.equals("ICWS88") ? RcStandard.ICWS88 : RcStandard.ICWS94;
Language defLang = (userPrefs.get("LANGUAGE", Language.de.toString()))
.equals("de") ? Language.de : Language.en;
configParameters = new ClientConfig(userPrefs.getInt("WIDTH", 800),
userPrefs.getInt("HEIGHT", 600), userPrefs.get("WARRIOR_NAME", "unknown warrior"),
userPrefs.get("PATH_TO_RC_FILE", System.getProperty("user.home")),
InetAddress.getByName(userPrefs.get("SERVER_IP", "localhost")),
userPrefs.getInt("SERVER_PORT", 65500), rcStd, defLang);
}
public void loadServerPrefs(String path, ConfigTypes configTypes) {
// TODO: implement me
}
}
package gone.lib.config;
import gone.lib.common.Language;
import gone.lib.common.RcStandard;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
/**
* updates the user config file with new values
* the config file is stored as a key=value file, we use: java.util.Properties
*
* @author matthias
*/
public class SaveConfigFile {
/**
* simply using the private save method, catch exceptions
* @param actualParameters
* @param userFilePath
* @throws IOException
*/
public SaveConfigFile(ConfigParameters actualParameters, String userFilePath,
ConfigTypes configType) throws IOException {
if (configType.equals(ConfigTypes.CLIENT_CONFIG)) {
saveClientConfig((ClientConfig) actualParameters, userFilePath, configType.toString());
} else {
saveServerConfig((ServerConfig) actualParameters, userFilePath, configType.toString());
}
}
/**
* * transfer the config parameters to a property file and save this
* to our user config file
* @param params our object with the actual parameters
* @param userFilePath where the user config file is located (absolute path)
* @throws IllegalArgumentException
* @throws IOException
*/
private void saveClientConfig(ClientConfig params, String userFilePath, String nodeName)
throws IllegalArgumentException, IOException {
if (userFilePath.isEmpty()) {
throw new IllegalArgumentException("user file path should not be empty!");
}
// TODO: same as load config, use relative path to ~/.config
Preferences userPrefs = Preferences.userRoot().node(nodeName);
userPrefs.putInt("WIDTH", params.getResolutionWidth());
userPrefs.putInt("HEIGHT", params.getResolutionHeight());
userPrefs.put("WARRIOR_NAME", params.getWarriorName());
userPrefs.put("PATH_TO_RC_FILE", params.getPathToRcFile());
userPrefs.putInt("SERVER_PORT", params.getPortLastServer());
userPrefs.put("SERVER_IP", params.getInetAddress().getHostAddress());
userPrefs.put("SERVER_NAME", params.getInetAddress().getHostName());
String rcStd = (params.getRcStandard().toString()).equals(RcStandard.ICWS88.toString())
? RcStandard.ICWS88.toString() : RcStandard.ICWS94.toString();
userPrefs.put("RC_STANDARD", rcStd);
String lang = (params.getRcStandard().toString()).equals(Language.de.toString())
? Language.de.toString() : Language.en.toString();
userPrefs.put("LANGUAGE", lang);
FileOutputStream out = new FileOutputStream(userFilePath);
try {
userPrefs.exportNode(out);
} catch (IOException | BackingStoreException exc) {
exc.printStackTrace();
}
out.close();
}
private void saveServerConfig(ServerConfig params, String userFilePath, String nodeName) {
// TODO: implement me
}
}
package gone.lib.config;
/**
* @author matthias
*/
public class ServerConfig extends ConfigParameters {
// TODO: add necessary variables
}
package gone.lib.network.config;
import static org.junit.Assert.assertEquals;
import gone.lib.common.Language;
import gone.lib.common.RcStandard;
import gone.lib.config.ClientConfig;
import gone.lib.config.ConfigTypes;
import gone.lib.config.LoadConfigFile;
import gone.lib.config.SaveConfigFile;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.security.SecureRandom;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
/**
*
* @author matthias
*/
public class ConfigIoTest {
// TODO: help function for less code redundance
private ClientConfig defaultCfg;
private ClientConfig userCfg;
private ClientConfig berndCfg;
private String defaultCfgS;
private String userCfgS;
private String berndCfgS;
private SecureRandom random;
// help function to generate random strings (file name)
private String nextRandomString() {
return new BigInteger(130, random).toString(32);
}
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Before
public void saveFilesToHome() throws IOException {
random = new SecureRandom();
File defaultConfig = folder.newFile(nextRandomString() + ".xml");
defaultCfgS = defaultConfig.getAbsolutePath();
File userConfig = folder.newFile(nextRandomString() + ".xml");
userCfgS = userConfig.getAbsolutePath();
File berndConfig = folder.newFile(nextRandomString() + ".xml");
berndCfgS = berndConfig.getAbsolutePath();
// create config params objects
defaultCfg = new ClientConfig(1024, 800, "default", System.getProperty("user.home"),
InetAddress.getByName("222.111.222.1"), 65501, RcStandard.ICWS88, Language.en);
userCfg = new ClientConfig(1024, 800, "user", System.getProperty("user.home"),
InetAddress.getByName("212.121.212.121"), 64321, RcStandard.ICWS94, Language.de);
berndCfg = new ClientConfig(1920, 1080, "Bernd Wiesacher", System.getProperty("user.home"),
InetAddress.getByName("188.68.54.64"), 65432, RcStandard.ICWS88, Language.en);
// save 3 config files
new SaveConfigFile(defaultCfg, defaultCfgS, ConfigTypes.CLIENT_CONFIG);
new SaveConfigFile(userCfg, userCfgS, ConfigTypes.CLIENT_CONFIG);
new SaveConfigFile(berndCfg, berndCfgS, ConfigTypes.CLIENT_CONFIG);
}
@Test
public void testCfgFileLoader_defaultClientConfig() throws IOException,
InvalidPreferencesFormatException,
IllegalAccessException, BackingStoreException {
LoadConfigFile loadCfg = new LoadConfigFile();
loadCfg.loadClientPrefs(defaultCfgS, defaultCfgS, ConfigTypes.CLIENT_CONFIG);
ClientConfig loadCfgParams = (ClientConfig) loadCfg.getConfigParameters();
assertEquals(1024, loadCfgParams.getResolutionWidth());
assertEquals(800, loadCfgParams.getResolutionHeight());
assertEquals("default", loadCfgParams.getWarriorName());
assertEquals(System.getProperty("user.home"), loadCfgParams.getPathToRcFile());
assertEquals("222.111.222.1", loadCfgParams.getInetAddress().getHostAddress());
assertEquals(65501, loadCfgParams.getPortLastServer());
assertEquals("ICWS88", loadCfgParams.getRcStandard().toString());
assertEquals("en", loadCfgParams.getLanguage().toString());
}
@Test
public void testCfgFileLoader_userClientConfig() throws IOException,
InvalidPreferencesFormatException,
IllegalAccessException, BackingStoreException {
LoadConfigFile loadCfg = new LoadConfigFile();
loadCfg.loadClientPrefs(defaultCfgS, userCfgS, ConfigTypes.CLIENT_CONFIG);
ClientConfig loadCfgParams = (ClientConfig) loadCfg.getConfigParameters();
assertEquals(1024, loadCfgParams.getResolutionWidth());
assertEquals(800, loadCfgParams.getResolutionHeight());
assertEquals("user", loadCfgParams.getWarriorName());
assertEquals(System.getProperty("user.home"), loadCfgParams.getPathToRcFile());
assertEquals("212.121.212.121", loadCfgParams.getInetAddress().getHostAddress());
assertEquals(64321, loadCfgParams.getPortLastServer());
assertEquals("ICWS94", loadCfgParams.getRcStandard().toString());
assertEquals("en", loadCfgParams.getLanguage().toString());
}
@Test
public void testCfgFileSaver_userClientConfig() throws IOException, BackingStoreException,
InvalidPreferencesFormatException, IllegalAccessException {
// save user config
ClientConfig saveParams = new ClientConfig(1920, 1080, "Bernd Wiesacher",
"/home/bernd", InetAddress.getByName("mail.g5r.eu"), 65432, RcStandard.ICWS88,
Language.de);
new SaveConfigFile(saveParams, berndCfgS, ConfigTypes.CLIENT_CONFIG);
// read that file again
LoadConfigFile loadCfg = new LoadConfigFile();
loadCfg.loadClientPrefs(defaultCfgS, berndCfgS, ConfigTypes.CLIENT_CONFIG);
ClientConfig loadCfgParams = (ClientConfig) loadCfg.getConfigParameters();
assertEquals(1920, loadCfgParams.getResolutionWidth());
assertEquals(1080, loadCfgParams.getResolutionHeight());
assertEquals("Bernd Wiesacher", loadCfgParams.getWarriorName());
assertEquals("/home/bernd", loadCfgParams.getPathToRcFile());
assertEquals("188.68.54.64", loadCfgParams.getInetAddress().getHostAddress());
assertEquals("mail.g5r.eu", loadCfgParams.getInetAddress().getHostName());
assertEquals(65432, loadCfgParams.getPortLastServer());
assertEquals("ICWS88", loadCfgParams.getRcStandard().toString());
assertEquals("en", loadCfgParams.getLanguage().toString());
}
}
\ No newline at end of file
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