Auto saving the config on exiting, changing the to config file

- Only save the IP-Address (the end-user shouldn't worry about any of
the other values)
-change the config path to a local path.
This commit is contained in:
Maschell 2017-03-22 21:31:20 +01:00
parent 7d00588ecf
commit abb3b23b44
10 changed files with 51 additions and 85 deletions

View File

@ -6,7 +6,11 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="resources"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ hs_err_pid*
bin/
target/
.settings/
config/hidtovpad.properties

View File

@ -32,10 +32,8 @@ import net.ash.HIDToVPADNetworkClient.util.Settings;
* TODO finish HidController
* TODO locale
*/
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
public static void main(String[] args){
Settings.loadSettings();
try {
new Thread(ActiveControllerManager.getInstance()).start();
@ -48,7 +46,7 @@ public class Main {
public void run() {
GuiMain.createGUI();
}
});
});
}
public static void fatal() {

View File

@ -60,7 +60,7 @@ public abstract class Controller implements Runnable{
public void run() {
boolean shutdownState = shutdown;
while(!shutdownState){
Utilities.sleep(Settings.getDetectControllerInterval());
Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL);
while(isActive()) {
byte[] newData = pollLatestData();
if(newData != null){
@ -78,7 +78,7 @@ public abstract class Controller implements Runnable{
}
protected void doSleepAfterPollingData() {
Utilities.sleep(Settings.getSleepAfterPolling());
Utilities.sleep(Settings.SLEEP_AFER_POLLING);
}
@Synchronized("dataLock")

View File

@ -25,10 +25,13 @@ import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import net.ash.HIDToVPADNetworkClient.Main;
import net.ash.HIDToVPADNetworkClient.util.Settings;
public class GuiCloseListener implements WindowListener {
@Override
public void windowClosing(WindowEvent arg0) {
Settings.saveSettings();
Main.initiateShutdown();
}

View File

@ -134,6 +134,7 @@ public class GuiInputControls extends JPanel implements ActionListener {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Settings.setIpAddr(ipTextBox.getText());
if(NetworkManager.getInstance().isReconnecting()){
}else{

View File

@ -53,7 +53,7 @@ public class ActiveControllerManager implements Runnable{
while(true){
updateControllerStates();
ControllerManager.detectControllers();
Utilities.sleep(Settings.getDetectControllerInterval());
Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL);
}
}
}).start();
@ -63,7 +63,7 @@ public class ActiveControllerManager implements Runnable{
public void run() {
while(true){
handleControllerInputs();
Utilities.sleep(Settings.getHandleInputsInterval());
Utilities.sleep(Settings.HANDLE_INPUTS_INTERVAL);
}
}
}).start();

View File

@ -81,8 +81,8 @@ public class NetworkManager implements Runnable{
int i = 0;
while(true){
proccessCommands();
Utilities.sleep(Settings.getProcessCmdInterval());
if(i++ > Settings.getPingInterval() / Settings.getProcessCmdInterval()){
Utilities.sleep(Settings.PROCESS_CMD_INTERVAL);
if(i++ > Settings.PING_INTERVAL / Settings.PROCESS_CMD_INTERVAL){
ping();
i = 0;
}
@ -152,7 +152,7 @@ public class NetworkManager implements Runnable{
result = true;
}
}else{
Utilities.sleep(Settings.getSendingCmdSleepIfNotConnected());
Utilities.sleep(Settings.SENDING_CMD_SLEEP_IF_NOT_CONNECTED);
}
//Add the command again on errors

View File

@ -45,7 +45,7 @@ public class TCPClient {
@Getter private int clientID = HandleFoundry.next();
@Getter @Setter(AccessLevel.PRIVATE)
private int shouldRetry = Settings.getMaxTriesForReconnecting();
private int shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
private String ip;
@ -86,7 +86,7 @@ public class TCPClient {
public synchronized boolean abort(){
try {
shouldRetry = Settings.getMaxTriesForReconnecting();
shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
sock.close();
clientID = HandleFoundry.next();
} catch (IOException e) {
@ -102,7 +102,7 @@ public class TCPClient {
out.flush();
}catch(IOException e){
try {
if(shouldRetry++ < Settings.getMaxTriesForReconnecting()){
if(shouldRetry++ < Settings.MAXIMUM_TRIES_FOR_RECONNECTING){
System.out.println("Trying again to connect! Attempt number " + shouldRetry);
connect(ip); //TODO: this is for reconnecting when the WiiU switches the application. But this breaks disconnecting, woops.
}else{
@ -142,6 +142,6 @@ public class TCPClient {
}
public boolean isShouldRetry() {
return this.shouldRetry < Settings.getMaxTriesForReconnecting();
return this.shouldRetry < Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
}
}

View File

@ -29,32 +29,24 @@ import java.io.IOException;
import java.util.Properties;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.java.Log;
//TODO autosave IP addr
@Log
public class Settings {
private static final String CONFIG_FILE_NAME = "config.properties";
private static final String CONFIG_FILE_NAME = "hidtovpad.properties";
@Getter
private static int detectControllerInterval = 1000;
@Getter
private static int handleInputsInterval = 15;
@Getter
private static int maxTriesForReconnecting = 10;
@Getter
private static int sleepAfterPolling = 10;
/**
* What does this even mean?
*/
@Getter
private static int sendingCmdSleepIfNotConnected = 500;
@Getter
private static int pingInterval = 1000;
@Getter
private static int processCmdInterval = 10;
@Getter
public static final int DETECT_CONTROLLER_INTERVAL = 1000;
public static final int HANDLE_INPUTS_INTERVAL = 15;
public static final int MAXIMUM_TRIES_FOR_RECONNECTING = 10;
public static final int SLEEP_AFER_POLLING = 10;
public static final int SENDING_CMD_SLEEP_IF_NOT_CONNECTED = 500;
public static final int PING_INTERVAL = 1000;
public static final int PROCESS_CMD_INTERVAL = 10;
@Getter @Setter
private static String ipAddr = "192.168.0.35"; //@Maschell, you're welcome
private Settings() {}
@ -65,7 +57,7 @@ public class Settings {
log.info("Creating " + configDir.getAbsolutePath() + "...");
configDir.mkdirs();
}
File configFile = new File(getConfigDir() + CONFIG_FILE_NAME);
File configFile = getConfigFile();
if (!configFile.exists()) {
log.info("Creating " + configFile.getAbsolutePath() + " with default values...");
try {
@ -88,55 +80,29 @@ public class Settings {
log.severe("Error while loading config file!");
e.printStackTrace();
log.warning("Using default values");
return;
}
String s_detectControllerInterval = prop.getProperty("detectControllerInterval");
String s_handleInputsInterval = prop.getProperty("handleInputsInterval");
String s_maxTriesForReconnecting = prop.getProperty("maxTriesForReconnecting");
String s_sleepAfterPolling = prop.getProperty("sleepAfterPolling");
String s_sendingCmdSleepIfNotConnected = prop.getProperty("sendingCmdSleepIfNotConnected");
String s_pingInterval = prop.getProperty("pingInterval");
String s_processCmdInterval = prop.getProperty("processCmdInterval");
String s_ipAddr = prop.getProperty("ipAddr");
int detectControllerInterval, handleInputsInterval, maxTriesForReconnecting, sleepAfterPolling, sendingCmdSleepIfNotConnected, pingInterval, processCmdInterval;
try {
detectControllerInterval = Integer.parseInt(s_detectControllerInterval);
handleInputsInterval = Integer.parseInt(s_handleInputsInterval);
maxTriesForReconnecting = Integer.parseInt(s_maxTriesForReconnecting);
sleepAfterPolling = Integer.parseInt(s_sleepAfterPolling);
sendingCmdSleepIfNotConnected = Integer.parseInt(s_sendingCmdSleepIfNotConnected);
pingInterval = Integer.parseInt(s_pingInterval);
processCmdInterval = Integer.parseInt(s_processCmdInterval);
} catch (NumberFormatException e) {
log.warning("Config file contains invalid values!");
log.warning("Reconstructing...");
saveSettings(configFile);
return;
}
Settings.detectControllerInterval = detectControllerInterval;
Settings.handleInputsInterval = handleInputsInterval;
Settings.maxTriesForReconnecting = maxTriesForReconnecting;
Settings.sleepAfterPolling = sleepAfterPolling;
Settings.sendingCmdSleepIfNotConnected = sendingCmdSleepIfNotConnected;
Settings.pingInterval = pingInterval;
Settings.processCmdInterval = processCmdInterval;
Settings.ipAddr = s_ipAddr;
Settings.ipAddr = prop.getProperty("ipAddr");
log.info("Loaded config successfully!");
}
private static File getConfigFile() {
return new File(getConfigDir() + CONFIG_FILE_NAME);
}
public static void saveSettings() {
File configFile = getConfigFile();
if (configFile.exists()) {
log.info("Settings saved.");
saveSettings(configFile);
}
}
private static void saveSettings(File configFile) {
Properties prop = new Properties();
prop.setProperty("detectControllerInterval", Integer.toString(Settings.detectControllerInterval));
prop.setProperty("handleInputsInterval", Integer.toString(Settings.handleInputsInterval));
prop.setProperty("maxTriesForReconnecting", Integer.toString(Settings.maxTriesForReconnecting));
prop.setProperty("sleepAfterPolling", Integer.toString(Settings.sleepAfterPolling));
prop.setProperty("sendingCmdSleepIfNotConnected", Integer.toString(Settings.sendingCmdSleepIfNotConnected));
prop.setProperty("pingInterval", Integer.toString(Settings.pingInterval));
prop.setProperty("processCmdInterval", Integer.toString(Settings.processCmdInterval));
prop.setProperty("ipAddr", Settings.ipAddr);
try {
@ -157,13 +123,6 @@ public class Settings {
}
private static String getConfigDir() {
String os = System.getProperty("os.name");
if (os.contains("Windows")) {
return System.getenv("APPDATA") + "/HIDToVPADNetworkClient/";
} else if (os.contains("Mac OS X")) {
return System.getProperty("user.home") + "/Library/Application Support/HIDToVPADNetworkClient/";
} else { //Linux
return System.getProperty("user.home") + "/.config/HIDToVPADNetworkClient/";
}
return "config/";
}
}