diff --git a/src/net/ash/HIDToVPADNetworkClient/Main.java b/src/net/ash/HIDToVPADNetworkClient/Main.java index 52e7e90..5b5cf7c 100644 --- a/src/net/ash/HIDToVPADNetworkClient/Main.java +++ b/src/net/ash/HIDToVPADNetworkClient/Main.java @@ -33,7 +33,7 @@ import net.ash.HIDToVPADNetworkClient.util.Settings; * TODO finish HidController * TODO locale */ -public class Main { +public final class Main { public static void main(String[] args) { Settings.loadSettings(); try { diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java index 97b3798..afba52c 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java @@ -90,12 +90,12 @@ public abstract class Controller implements Runnable { @Synchronized("dataLock") public byte[] getLatestData() { - if (latestData != null) { + if (latestData == null) { + return new byte[0]; + }else{ byte[] data = this.latestData.clone(); this.latestData = null; return data; - } else { - return null; } } @@ -183,8 +183,7 @@ public abstract class Controller implements Runnable { if (identifier == null) { if (other.identifier != null) return false; } else if (!identifier.equals(other.identifier)) return false; - if (type != other.type) return false; - return true; + return (type != other.type); } @Synchronized("rumbleLock") diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java b/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java index 21ecc4f..810c62c 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java @@ -97,12 +97,12 @@ public class LinuxDevInputController extends Controller implements Runnable { return null; } - if (value != 0) { - // Set bit with button number - buttonState |= (1 << number); - } else { + if (value == 0) { // Clear bit with button number buttonState &= ~(1 << number); + }else{ + // Set bit with button number + buttonState |= (1 << number); } } else if (type == JS_EVENT_AXIS) { if (number >= NUM_SUPPORTED_AXIS) { diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java b/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java index a559bc4..6dd8f74 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java @@ -34,7 +34,7 @@ import purejavahidapi.HidDevice; import purejavahidapi.InputReportListener; public class PureJavaHidController extends Controller implements InputReportListener { - private Object dataLock = new Object(); + private final Object dataLock = new Object(); protected byte[] currentData = new byte[1]; protected int PACKET_LENGTH = 64; diff --git a/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java b/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java index 5594966..2608830 100644 --- a/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java +++ b/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java @@ -41,7 +41,7 @@ import lombok.Getter; import net.ash.HIDToVPADNetworkClient.network.NetworkManager; import net.ash.HIDToVPADNetworkClient.util.Settings; -public class GuiInputControls extends JPanel implements ActionListener { +public final class GuiInputControls extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; @Getter private static GuiInputControls instance = new GuiInputControls(); @@ -56,7 +56,7 @@ public class GuiInputControls extends JPanel implements ActionListener { setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); setPreferredSize(new Dimension(220, 150)); - JButton connectButton = new JButton(CONNECT); + final JButton connectButton = new JButton(CONNECT); connectButton.setAlignmentX(Component.CENTER_ALIGNMENT); ipTextBox = new JTextField(); @@ -70,7 +70,7 @@ public class GuiInputControls extends JPanel implements ActionListener { JLabel statusLabel = new JLabel("Ready."); statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT); - JCheckBox cbautoActivateController = new JCheckBox(); + final JCheckBox cbautoActivateController = new JCheckBox(); cbautoActivateController.setSelected(Settings.AUTO_ACTIVATE_CONTROLLER); cbautoActivateController.addActionListener(new ActionListener() { diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java index 6adebcf..8dc173f 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java @@ -124,7 +124,7 @@ public final class ActiveControllerManager implements Runnable { synchronized (activeControllers) { for (Entry entry : activeControllers.entrySet()) { byte[] data = entry.getKey().getLatestData(); - if (data != null) { + if (data != null && data.length > 0) { NetworkHIDDevice device = entry.getValue(); device.sendRead(data); } diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java index e394bcd..e3f6c8d 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java @@ -54,6 +54,8 @@ import purejavahidapi.HidDeviceInfo; public final class ControllerManager { private static Map attachedControllers = new HashMap(); + private static boolean threwUnsatisfiedLinkError = false; + private ControllerManager() { // Utility Class } @@ -153,8 +155,6 @@ public final class ControllerManager { return connectedDevices; } - private static boolean threwUnsatisfiedLinkError = false; - private static Map detectWindowsControllers() { Map result = new HashMap(); ControllerType type = ControllerType.XINPUT13; diff --git a/src/net/ash/HIDToVPADNetworkClient/network/DeviceCommand.java b/src/net/ash/HIDToVPADNetworkClient/network/DeviceCommand.java index 5a20598..6c7ec03 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/DeviceCommand.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/DeviceCommand.java @@ -24,7 +24,7 @@ package net.ash.HIDToVPADNetworkClient.network; import lombok.Data; @Data -abstract class DeviceCommand { +class DeviceCommand { private final int handle; private final NetworkHIDDevice sender; private final Class type; diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java index 67060fe..7214836 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java @@ -47,7 +47,7 @@ public class NetworkHIDDevice { @Getter private final int hidHandle = HandleFoundry.next(); private Object readCommandLock = new Object(); - @Getter(AccessLevel.PRIVATE) private List commands = new ArrayList(); + @Getter(AccessLevel.PRIVATE) private final List commands = new ArrayList(); @Getter(AccessLevel.PRIVATE) @Setter(AccessLevel.PRIVATE) private ReadCommand latestRead; public NetworkHIDDevice(short vid, short pid) { @@ -113,7 +113,6 @@ public class NetworkHIDDevice { if (obj == null) return false; if (getClass() != obj.getClass()) return false; NetworkHIDDevice other = (NetworkHIDDevice) obj; - if (hidHandle != other.hidHandle) return false; - return true; + return (hidHandle != other.hidHandle); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java index 93ffc52..a995c9f 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java @@ -38,7 +38,7 @@ import net.ash.HIDToVPADNetworkClient.util.Utilities; @Log public final class NetworkManager implements Runnable { - private final TCPClient tcpClient = new TCPClient(); + private final TCPClient tcpClient = TCPClient.getInstance(); private UDPClient udpClient = null; @Getter private static NetworkManager instance = new NetworkManager(); @@ -254,14 +254,15 @@ public final class NetworkManager implements Runnable { // Let's save them for later and demand the first data packet. NetworkHIDDevice sender = command.getSender(); - if (sender != null) { - sender.setDeviceslot(deviceslot); - sender.setPadslot(padslot); - sender.setNeedFirstData(true); // Please send data after connecting. - } else { + if (sender == null) { log.info("Something really went wrong. Got an attach event with out an " + NetworkHIDDevice.class.getSimpleName()); return false; } + + sender.setDeviceslot(deviceslot); + sender.setPadslot(padslot); + sender.setNeedFirstData(true); // Please send data after connecting. + log.info("Attaching done!"); return true; } else { @@ -274,7 +275,7 @@ public final class NetworkManager implements Runnable { byte[] rawCommand; try { rawCommand = Protocol.getRawReadDataToSend(readCommands); - if (sendUDP(rawCommand) == true && Settings.DEBUG_UDP_OUTPUT) { + if (sendUDP(rawCommand) && Settings.DEBUG_UDP_OUTPUT) { System.out.println("UDP Packet sent: " + Utilities.ByteArrayToString(rawCommand)); } } catch (IOException e) { @@ -312,6 +313,7 @@ public final class NetworkManager implements Runnable { public void disconnect() { // ControllerManager.deactivateAllAttachedControllers(); tcpClient.abort(); + udpClient = null; } private short recvTCPShort() throws IOException { diff --git a/src/net/ash/HIDToVPADNetworkClient/network/Protocol.java b/src/net/ash/HIDToVPADNetworkClient/network/Protocol.java index 882756c..6077dd1 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/Protocol.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/Protocol.java @@ -30,45 +30,45 @@ import java.util.List; import lombok.extern.java.Log; @Log -class Protocol { - protected static final int TCP_PORT = 8112; - protected static final int UDP_PORT = 8113; +final class Protocol { + static final int TCP_PORT = 8112; + static final int UDP_PORT = 8113; - protected static final byte TCP_HANDSHAKE = 0x12; - protected static final byte TCP_SAME_CLIENT = 0x20; - protected static final byte TCP_NEW_CLIENT = 0x21; + static final byte TCP_HANDSHAKE = 0x12; + static final byte TCP_SAME_CLIENT = 0x20; + static final byte TCP_NEW_CLIENT = 0x21; - protected static final byte TCP_CMD_ATTACH = 0x01; - protected static final byte TCP_CMD_DETACH = 0x02; - protected static final byte TCP_CMD_PING = (byte) 0xF0; + static final byte TCP_CMD_ATTACH = 0x01; + static final byte TCP_CMD_DETACH = 0x02; + static final byte TCP_CMD_PING = (byte) 0xF0; - protected static final byte UDP_CMD_DATA = 0x03; + static final byte UDP_CMD_DATA = 0x03; - protected static final byte TCP_CMD_ATTACH_CONFIG_FOUND = (byte) 0xE0; - protected static final byte TCP_CMD_ATTACH_CONFIG_NOT_FOUND = (byte) 0xE1; - protected static final byte TCP_CMD_ATTACH_USERDATA_OKAY = (byte) 0xE8; - protected static final byte TCP_CMD_ATTACH_USERDATA_BAD = (byte) 0xE9; + static final byte TCP_CMD_ATTACH_CONFIG_FOUND = (byte) 0xE0; + static final byte TCP_CMD_ATTACH_CONFIG_NOT_FOUND = (byte) 0xE1; + static final byte TCP_CMD_ATTACH_USERDATA_OKAY = (byte) 0xE8; + static final byte TCP_CMD_ATTACH_USERDATA_BAD = (byte) 0xE9; private Protocol() { } - protected enum HandshakeReturnCode { + enum HandshakeReturnCode { GOOD_HANDSHAKE, BAD_HANDSHAKE } - protected static byte[] getRawAttachDataToSend(AttachCommand command) throws IOException { + static byte[] getRawAttachDataToSend(AttachCommand command) throws IOException { return ByteBuffer.allocate(9).put(Protocol.TCP_CMD_ATTACH).putInt(command.getHandle()).putShort(command.getVid()).putShort(command.getPid()).array(); } - protected static byte[] getRawDetachDataToSend(DetachCommand command) throws IOException { + static byte[] getRawDetachDataToSend(DetachCommand command) throws IOException { return ByteBuffer.allocate(5).put(Protocol.TCP_CMD_DETACH).putInt(command.getHandle()).array(); } - protected static byte[] getRawPingDataToSend(PingCommand command) { + static byte[] getRawPingDataToSend(PingCommand command) { return new byte[] { Protocol.TCP_CMD_PING }; } - protected static byte[] getRawReadDataToSend(List readCommands) throws IOException { + static byte[] getRawReadDataToSend(List readCommands) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeByte(Protocol.UDP_CMD_DATA); diff --git a/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java b/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java index a63cfb8..06ba3f8 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java @@ -38,8 +38,10 @@ import net.ash.HIDToVPADNetworkClient.network.Protocol.HandshakeReturnCode; import net.ash.HIDToVPADNetworkClient.util.Settings; @Log -class TCPClient { +final class TCPClient { private final Object lock = new Object(); + @Getter + private static TCPClient instance = new TCPClient(); private Socket sock; private DataInputStream in; private DataOutputStream out; @@ -48,11 +50,11 @@ class TCPClient { private String ip; - protected TCPClient() { + private TCPClient() { } @Synchronized("lock") - protected void connect(String ip) throws Exception { + void connect(String ip) throws Exception { sock = new Socket(); sock.connect(new InetSocketAddress(ip, Protocol.TCP_PORT), 2000); in = new DataInputStream(sock.getInputStream()); @@ -72,7 +74,7 @@ class TCPClient { } @Synchronized("lock") - protected boolean abort() { + boolean abort() { try { shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING; sock.close(); @@ -84,7 +86,7 @@ class TCPClient { } @Synchronized("lock") - protected void send(byte[] rawCommand) throws IOException { + void send(byte[] rawCommand) throws IOException { try { out.write(rawCommand); out.flush(); @@ -103,17 +105,17 @@ class TCPClient { } } - protected void send(int value) throws IOException { + void send(int value) throws IOException { send(ByteBuffer.allocate(4).putInt(value).array()); } @Synchronized("lock") - protected byte recvByte() throws IOException { + byte recvByte() throws IOException { return in.readByte(); } @Synchronized("lock") - protected short recvShort() throws IOException { + short recvShort() throws IOException { try { return in.readShort(); } catch (IOException e) { @@ -123,11 +125,11 @@ class TCPClient { } @Synchronized("lock") - protected boolean isConnected() { + boolean isConnected() { return (sock != null && sock.isConnected() && !sock.isClosed()); } - protected boolean isShouldRetry() { + boolean isShouldRetry() { return this.shouldRetry < Settings.MAXIMUM_TRIES_FOR_RECONNECTING; } } diff --git a/src/net/ash/HIDToVPADNetworkClient/network/UDPClient.java b/src/net/ash/HIDToVPADNetworkClient/network/UDPClient.java index 7718a23..3794f35 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/UDPClient.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/UDPClient.java @@ -27,8 +27,7 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; - -class UDPClient { +final class UDPClient { private final DatagramSocket sock; private final InetAddress host; @@ -37,7 +36,7 @@ class UDPClient { host = InetAddress.getByName(ip); } - protected static UDPClient createUDPClient(String ip) { + static UDPClient createUDPClient(String ip) { UDPClient result = null; try { result = new UDPClient(ip); @@ -47,7 +46,7 @@ class UDPClient { return result; } - protected void send(byte[] data) throws IOException { + void send(byte[] data) throws IOException { DatagramPacket packet = new DatagramPacket(data, data.length, host, Protocol.UDP_PORT); sock.send(packet); } diff --git a/src/net/ash/HIDToVPADNetworkClient/util/PureJavaHidApiManager.java b/src/net/ash/HIDToVPADNetworkClient/util/PureJavaHidApiManager.java index f92dad9..3c2416b 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/PureJavaHidApiManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/PureJavaHidApiManager.java @@ -29,7 +29,7 @@ import purejavahidapi.HidDevice; import purejavahidapi.HidDeviceInfo; import purejavahidapi.PureJavaHidApi; -public class PureJavaHidApiManager { +public final class PureJavaHidApiManager { private PureJavaHidApiManager() { } diff --git a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java index 11c6862..17d879a 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java @@ -33,7 +33,7 @@ import lombok.Setter; import lombok.extern.java.Log; @Log -public class Settings { +public final class Settings { private static final String CONFIG_FILE_NAME = "hidtovpad.properties"; public static final int DETECT_CONTROLLER_INTERVAL = 1000;