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"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </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"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>

1
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -45,7 +45,7 @@ public class TCPClient {
@Getter private int clientID = HandleFoundry.next(); @Getter private int clientID = HandleFoundry.next();
@Getter @Setter(AccessLevel.PRIVATE) @Getter @Setter(AccessLevel.PRIVATE)
private int shouldRetry = Settings.getMaxTriesForReconnecting(); private int shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
private String ip; private String ip;
@ -86,7 +86,7 @@ public class TCPClient {
public synchronized boolean abort(){ public synchronized boolean abort(){
try { try {
shouldRetry = Settings.getMaxTriesForReconnecting(); shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
sock.close(); sock.close();
clientID = HandleFoundry.next(); clientID = HandleFoundry.next();
} catch (IOException e) { } catch (IOException e) {
@ -102,7 +102,7 @@ public class TCPClient {
out.flush(); out.flush();
}catch(IOException e){ }catch(IOException e){
try { try {
if(shouldRetry++ < Settings.getMaxTriesForReconnecting()){ if(shouldRetry++ < Settings.MAXIMUM_TRIES_FOR_RECONNECTING){
System.out.println("Trying again to connect! Attempt number " + shouldRetry); 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. connect(ip); //TODO: this is for reconnecting when the WiiU switches the application. But this breaks disconnecting, woops.
}else{ }else{
@ -142,6 +142,6 @@ public class TCPClient {
} }
public boolean isShouldRetry() { 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 java.util.Properties;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import lombok.extern.java.Log; import lombok.extern.java.Log;
//TODO autosave IP addr //TODO autosave IP addr
@Log @Log
public class Settings { public class Settings {
private static final String CONFIG_FILE_NAME = "config.properties"; private static final String CONFIG_FILE_NAME = "hidtovpad.properties";
@Getter public static final int DETECT_CONTROLLER_INTERVAL = 1000;
private static int detectControllerInterval = 1000; public static final int HANDLE_INPUTS_INTERVAL = 15;
@Getter public static final int MAXIMUM_TRIES_FOR_RECONNECTING = 10;
private static int handleInputsInterval = 15; public static final int SLEEP_AFER_POLLING = 10;
@Getter public static final int SENDING_CMD_SLEEP_IF_NOT_CONNECTED = 500;
private static int maxTriesForReconnecting = 10; public static final int PING_INTERVAL = 1000;
@Getter public static final int PROCESS_CMD_INTERVAL = 10;
private static int sleepAfterPolling = 10;
/** @Getter @Setter
* What does this even mean?
*/
@Getter
private static int sendingCmdSleepIfNotConnected = 500;
@Getter
private static int pingInterval = 1000;
@Getter
private static int processCmdInterval = 10;
@Getter
private static String ipAddr = "192.168.0.35"; //@Maschell, you're welcome private static String ipAddr = "192.168.0.35"; //@Maschell, you're welcome
private Settings() {} private Settings() {}
@ -65,7 +57,7 @@ public class Settings {
log.info("Creating " + configDir.getAbsolutePath() + "..."); log.info("Creating " + configDir.getAbsolutePath() + "...");
configDir.mkdirs(); configDir.mkdirs();
} }
File configFile = new File(getConfigDir() + CONFIG_FILE_NAME); File configFile = getConfigFile();
if (!configFile.exists()) { if (!configFile.exists()) {
log.info("Creating " + configFile.getAbsolutePath() + " with default values..."); log.info("Creating " + configFile.getAbsolutePath() + " with default values...");
try { try {
@ -88,55 +80,29 @@ public class Settings {
log.severe("Error while loading config file!"); log.severe("Error while loading config file!");
e.printStackTrace(); e.printStackTrace();
log.warning("Using default values"); log.warning("Using default values");
return;
} }
String s_detectControllerInterval = prop.getProperty("detectControllerInterval"); Settings.ipAddr = prop.getProperty("ipAddr");
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;
log.info("Loaded config successfully!"); 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) { private static void saveSettings(File configFile) {
Properties prop = new Properties(); 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); prop.setProperty("ipAddr", Settings.ipAddr);
try { try {
@ -157,13 +123,6 @@ public class Settings {
} }
private static String getConfigDir() { private static String getConfigDir() {
String os = System.getProperty("os.name"); return "config/";
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/";
}
} }
} }