diff --git a/.classpath b/.classpath index 298eba2..2fe095b 100644 --- a/.classpath +++ b/.classpath @@ -6,6 +6,7 @@ + diff --git a/pom.xml b/pom.xml index b086242..6e62ae6 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,12 @@ src + + + + resources + + clean package diff --git a/resources/config.properties b/resources/config.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/net/ash/HIDToVPADNetworkClient/Main.java b/src/net/ash/HIDToVPADNetworkClient/Main.java index bdffeea..f250a17 100644 --- a/src/net/ash/HIDToVPADNetworkClient/Main.java +++ b/src/net/ash/HIDToVPADNetworkClient/Main.java @@ -26,6 +26,7 @@ import javax.swing.SwingUtilities; import net.ash.HIDToVPADNetworkClient.gui.GuiMain; import net.ash.HIDToVPADNetworkClient.manager.ActiveControllerManager; import net.ash.HIDToVPADNetworkClient.network.NetworkManager; +import net.ash.HIDToVPADNetworkClient.util.Settings; /* Ash's todo list * TODO finish HidController @@ -35,6 +36,7 @@ import net.ash.HIDToVPADNetworkClient.network.NetworkManager; public class Main { public static void main(String[] args) { System.out.println("Hello World!"); + Settings.loadSettings(); try { new Thread(ActiveControllerManager.getInstance()).start(); new Thread(NetworkManager.getInstance()).start(); diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java index 3f007c4..3ae301e 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java @@ -60,7 +60,7 @@ public abstract class Controller implements Runnable{ public void run() { boolean shutdownState = shutdown; while(!shutdownState){ - Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL); + Utilities.sleep(Settings.getDetectControllerInterval()); while(isActive()) { byte[] newData = pollLatestData(); if(newData != null){ @@ -78,7 +78,7 @@ public abstract class Controller implements Runnable{ } protected void doSleepAfterPollingData() { - Utilities.sleep(Settings.SLEEP_AFER_POLLING); + Utilities.sleep(Settings.getSleepAfterPolling()); } @Synchronized("dataLock") diff --git a/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java b/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java index 47a0da1..fe04016 100644 --- a/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java +++ b/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java @@ -37,6 +37,7 @@ import javax.swing.SwingUtilities; import javax.swing.Timer; import net.ash.HIDToVPADNetworkClient.network.NetworkManager; +import net.ash.HIDToVPADNetworkClient.util.Settings; public class GuiInputControls extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; @@ -68,7 +69,7 @@ public class GuiInputControls extends JPanel implements ActionListener { ipTextBox = new JTextField(); ipTextBox.setColumns(15); - ipTextBox.setText("192.168.0.35"); + ipTextBox.setText(Settings.getIpAddr()); ipTextBoxWrap = new JPanel(new FlowLayout()); ipTextBoxWrap.add(new JLabel("IP: ")); ipTextBoxWrap.add(ipTextBox); diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java index e739cf9..be51784 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java @@ -53,7 +53,7 @@ public class ActiveControllerManager implements Runnable{ while(true){ updateControllerStates(); ControllerManager.detectControllers(); - Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL); + Utilities.sleep(Settings.getDetectControllerInterval()); } } }).start(); @@ -63,7 +63,7 @@ public class ActiveControllerManager implements Runnable{ public void run() { while(true){ handleControllerInputs(); - Utilities.sleep(Settings.HANDLE_INPUTS_INTERVAL); + Utilities.sleep(Settings.getHandleInputsInterval()); } } }).start(); diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java index d7ef15d..bca7627 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java @@ -81,8 +81,8 @@ public class NetworkManager implements Runnable{ int i = 0; while(true){ proccessCommands(); - Utilities.sleep(Settings.PROCESS_CMD_INTERVAL); - if(i++ > Settings.PING_INTERVAL/Settings.PROCESS_CMD_INTERVAL){ + Utilities.sleep(Settings.getProcessCmdInterval()); + if(i++ > Settings.getPingInterval() / Settings.getProcessCmdInterval()){ ping(); i = 0; } @@ -152,7 +152,7 @@ public class NetworkManager implements Runnable{ result = true; } }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 diff --git a/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java b/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java index d17a3f4..e18cc31 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java @@ -45,7 +45,7 @@ public class TCPClient { @Getter private int clientID = HandleFoundry.next(); @Getter @Setter(AccessLevel.PRIVATE) - private int shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING; + private int shouldRetry = Settings.getMaxTriesForReconnecting(); private String ip; @@ -86,7 +86,7 @@ public class TCPClient { public synchronized boolean abort(){ try { - shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING; + shouldRetry = Settings.getMaxTriesForReconnecting(); sock.close(); clientID = HandleFoundry.next(); } catch (IOException e) { @@ -102,7 +102,7 @@ public class TCPClient { out.flush(); }catch(IOException e){ try { - if(shouldRetry++ < Settings.MAXIMUM_TRIES_FOR_RECONNECTING){ + if(shouldRetry++ < Settings.getMaxTriesForReconnecting()){ 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.MAXIMUM_TRIES_FOR_RECONNECTING; + return this.shouldRetry < Settings.getMaxTriesForReconnecting(); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java index 919fc1c..d1110e8 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java @@ -21,15 +21,149 @@ *******************************************************************************/ 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 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; + private static final String CONFIG_FILE_NAME = "config.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 + 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/"; + } + } }