diff --git a/.classpath b/.classpath index 2fe095b..c537c90 100644 --- a/.classpath +++ b/.classpath @@ -6,7 +6,11 @@ - + + + + + diff --git a/.gitignore b/.gitignore index 3adf196..1e5c983 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ hs_err_pid* bin/ target/ .settings/ +config/hidtovpad.properties diff --git a/src/net/ash/HIDToVPADNetworkClient/Main.java b/src/net/ash/HIDToVPADNetworkClient/Main.java index f250a17..7cafc60 100644 --- a/src/net/ash/HIDToVPADNetworkClient/Main.java +++ b/src/net/ash/HIDToVPADNetworkClient/Main.java @@ -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() { diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java index 3ae301e..3f007c4 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.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") diff --git a/src/net/ash/HIDToVPADNetworkClient/gui/GuiCloseListener.java b/src/net/ash/HIDToVPADNetworkClient/gui/GuiCloseListener.java index 7e42d36..11b5f68 100644 --- a/src/net/ash/HIDToVPADNetworkClient/gui/GuiCloseListener.java +++ b/src/net/ash/HIDToVPADNetworkClient/gui/GuiCloseListener.java @@ -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(); } diff --git a/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java b/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java index fe04016..143c67a 100644 --- a/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java +++ b/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java @@ -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{ diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java index be51784..e739cf9 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.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(); diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java index bca7627..0b42b0b 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.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 diff --git a/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java b/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java index e18cc31..d17a3f4 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.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; } } diff --git a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java index d1110e8..9618565 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java @@ -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/"; } }