Load settings from config file instead of hardcoded values

This commit is contained in:
Ash 2017-03-22 17:34:49 +11:00
parent 3392e44405
commit 7d00588ecf
10 changed files with 164 additions and 20 deletions

View File

@ -6,6 +6,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="resources"/>
<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"/>

View File

@ -17,6 +17,12 @@
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<!-- Band-aid over a knife wound, I know. Restructure is later. -->
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<defaultGoal>clean package</defaultGoal> <defaultGoal>clean package</defaultGoal>
<plugins> <plugins>
<plugin> <plugin>

View File

View File

@ -26,6 +26,7 @@ import javax.swing.SwingUtilities;
import net.ash.HIDToVPADNetworkClient.gui.GuiMain; import net.ash.HIDToVPADNetworkClient.gui.GuiMain;
import net.ash.HIDToVPADNetworkClient.manager.ActiveControllerManager; import net.ash.HIDToVPADNetworkClient.manager.ActiveControllerManager;
import net.ash.HIDToVPADNetworkClient.network.NetworkManager; import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
import net.ash.HIDToVPADNetworkClient.util.Settings;
/* Ash's todo list /* Ash's todo list
* TODO finish HidController * TODO finish HidController
@ -35,6 +36,7 @@ import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello World!"); System.out.println("Hello World!");
Settings.loadSettings();
try { try {
new Thread(ActiveControllerManager.getInstance()).start(); new Thread(ActiveControllerManager.getInstance()).start();
new Thread(NetworkManager.getInstance()).start(); new Thread(NetworkManager.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.DETECT_CONTROLLER_INTERVAL); Utilities.sleep(Settings.getDetectControllerInterval());
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.SLEEP_AFER_POLLING); Utilities.sleep(Settings.getSleepAfterPolling());
} }
@Synchronized("dataLock") @Synchronized("dataLock")

View File

@ -37,6 +37,7 @@ import javax.swing.SwingUtilities;
import javax.swing.Timer; import javax.swing.Timer;
import net.ash.HIDToVPADNetworkClient.network.NetworkManager; import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
import net.ash.HIDToVPADNetworkClient.util.Settings;
public class GuiInputControls extends JPanel implements ActionListener { public class GuiInputControls extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -68,7 +69,7 @@ public class GuiInputControls extends JPanel implements ActionListener {
ipTextBox = new JTextField(); ipTextBox = new JTextField();
ipTextBox.setColumns(15); ipTextBox.setColumns(15);
ipTextBox.setText("192.168.0.35"); ipTextBox.setText(Settings.getIpAddr());
ipTextBoxWrap = new JPanel(new FlowLayout()); ipTextBoxWrap = new JPanel(new FlowLayout());
ipTextBoxWrap.add(new JLabel("IP: ")); ipTextBoxWrap.add(new JLabel("IP: "));
ipTextBoxWrap.add(ipTextBox); ipTextBoxWrap.add(ipTextBox);

View File

@ -53,7 +53,7 @@ public class ActiveControllerManager implements Runnable{
while(true){ while(true){
updateControllerStates(); updateControllerStates();
ControllerManager.detectControllers(); ControllerManager.detectControllers();
Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL); Utilities.sleep(Settings.getDetectControllerInterval());
} }
} }
}).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.HANDLE_INPUTS_INTERVAL); Utilities.sleep(Settings.getHandleInputsInterval());
} }
} }
}).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.PROCESS_CMD_INTERVAL); Utilities.sleep(Settings.getProcessCmdInterval());
if(i++ > Settings.PING_INTERVAL/Settings.PROCESS_CMD_INTERVAL){ if(i++ > Settings.getPingInterval() / Settings.getProcessCmdInterval()){
ping(); ping();
i = 0; i = 0;
} }
@ -152,7 +152,7 @@ public class NetworkManager implements Runnable{
result = true; result = true;
} }
}else{ }else{
Utilities.sleep(Settings.SENDING_CMD_SLEEP_IF_NOT_CONNECTED); //TODO: move magic value to Settings Utilities.sleep(Settings.getSendingCmdSleepIfNotConnected());
} }
//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.MAXIMUM_TRIES_FOR_RECONNECTING; private int shouldRetry = Settings.getMaxTriesForReconnecting();
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.MAXIMUM_TRIES_FOR_RECONNECTING; shouldRetry = Settings.getMaxTriesForReconnecting();
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.MAXIMUM_TRIES_FOR_RECONNECTING){ if(shouldRetry++ < Settings.getMaxTriesForReconnecting()){
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.MAXIMUM_TRIES_FOR_RECONNECTING; return this.shouldRetry < Settings.getMaxTriesForReconnecting();
} }
} }

View File

@ -21,15 +21,149 @@
*******************************************************************************/ *******************************************************************************/
package net.ash.HIDToVPADNetworkClient.util; package net.ash.HIDToVPADNetworkClient.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import lombok.Getter;
import lombok.extern.java.Log;
//TODO autosave IP addr
@Log
public class Settings { public class Settings {
public static final int DETECT_CONTROLLER_INTERVAL = 1000; private static final String CONFIG_FILE_NAME = "config.properties";
public static final int HANDLE_INPUTS_INTERVAL = 15;
public static final int MAXIMUM_TRIES_FOR_RECONNECTING = 10; @Getter
public static final int SLEEP_AFER_POLLING = 10; private static int detectControllerInterval = 1000;
public static final int SENDING_CMD_SLEEP_IF_NOT_CONNECTED = 500; @Getter
public static final int PING_INTERVAL = 1000; private static int handleInputsInterval = 15;
public static final int PROCESS_CMD_INTERVAL = 10; @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
private static String ipAddr = "192.168.0.35"; //@Maschell, you're welcome
private Settings(){} private Settings() {}
public static void loadSettings() {
File configDir = new File(getConfigDir());
if (!configDir.exists()) {
log.info("Creating " + configDir.getAbsolutePath() + "...");
configDir.mkdirs();
}
File configFile = new File(getConfigDir() + CONFIG_FILE_NAME);
if (!configFile.exists()) {
log.info("Creating " + configFile.getAbsolutePath() + " with default values...");
try {
configFile.createNewFile();
} catch (IOException e) {
log.severe("Could not create config file!");
e.printStackTrace();
log.warning("Using default values");
}
saveSettings(configFile);
return;
}
log.info("Loading config from " + configFile.getAbsolutePath() + "...");
Properties prop = new Properties();
try {
prop.load(new FileInputStream(configFile));
} catch (IOException e) {
log.severe("Error while loading config file!");
e.printStackTrace();
log.warning("Using default values");
}
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;
log.info("Loaded config successfully!");
}
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 {
FileOutputStream outStream = new FileOutputStream(configFile);
prop.store(outStream, "HIDToVPADNetworkClient");
outStream.close();
} catch (FileNotFoundException e) {
log.severe("Could not open the new config file!");
e.printStackTrace();
log.warning("New file will not be written.");
return;
} catch (IOException e) {
log.severe("Could not write the new config file!");
e.printStackTrace();
log.warning("New file will not be written.");
return;
}
}
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/";
}
}
} }