From d0e7928ed20c4badaba39734df12e5c6aad0421c Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 31 Mar 2017 16:47:06 +0200 Subject: [PATCH] Fixed the equals methods + more refactoring --- .../controller/Controller.java | 4 +-- .../controller/LinuxDevInputController.java | 2 +- .../network/NetworkHIDDevice.java | 25 +++++++++++++------ .../network/NetworkManager.java | 8 +++--- .../network/TCPClient.java | 3 +-- .../network/UDPClient.java | 1 + 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java index afba52c..4792971 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java @@ -92,7 +92,7 @@ public abstract class Controller implements Runnable { public byte[] getLatestData() { if (latestData == null) { return new byte[0]; - }else{ + } else { byte[] data = this.latestData.clone(); this.latestData = null; return data; @@ -183,7 +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; - return (type != other.type); + 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 810c62c..af9d08a 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java @@ -100,7 +100,7 @@ public class LinuxDevInputController extends Controller implements Runnable { if (value == 0) { // Clear bit with button number buttonState &= ~(1 << number); - }else{ + } else { // Set bit with button number buttonState |= (1 << number); } diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java index 7214836..5d3b8b6 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java @@ -30,6 +30,7 @@ import java.util.List; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; +import lombok.Synchronized; import net.ash.HIDToVPADNetworkClient.util.HandleFoundry; import net.ash.HIDToVPADNetworkClient.util.Settings; @@ -46,8 +47,9 @@ public class NetworkHIDDevice { @Getter private final int hidHandle = HandleFoundry.next(); - private Object readCommandLock = new Object(); - @Getter(AccessLevel.PRIVATE) private final List commands = new ArrayList(); + private final Object readCommandLock = new Object(); + private final Object pullCommandsLock = new Object(); + private final List commands = new ArrayList(); @Getter(AccessLevel.PRIVATE) @Setter(AccessLevel.PRIVATE) private ReadCommand latestRead; public NetworkHIDDevice(short vid, short pid) { @@ -55,10 +57,12 @@ public class NetworkHIDDevice { this.pid = pid; } + @Synchronized("commands") private void addCommand(DeviceCommand command) { this.commands.add(command); } + @Synchronized("commands") private void clearCommands() { this.commands.clear(); } @@ -85,17 +89,24 @@ public class NetworkHIDDevice { protected Collection getCommandList() { List commands = new ArrayList(); - commands.addAll(getCommands()); - DeviceCommand lastRead; + synchronized (pullCommandsLock) { + commands.addAll(getCommands()); + clearCommands(); + } synchronized (readCommandLock) { - if ((lastRead = getLatestRead()) != null) { + DeviceCommand lastRead = getLatestRead(); + if (lastRead != null) { commands.add(lastRead); setLatestRead(null); } } - clearCommands(); + return commands; + } + + @Synchronized("commands") + private List getCommands() { return commands; } @@ -113,6 +124,6 @@ public class NetworkHIDDevice { if (obj == null) return false; if (getClass() != obj.getClass()) return false; NetworkHIDDevice other = (NetworkHIDDevice) obj; - return (hidHandle != other.hidHandle); + return (hidHandle == other.hidHandle); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java index a995c9f..3ada071 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java @@ -57,9 +57,9 @@ public final class NetworkManager implements Runnable { } public void addHIDDevice(NetworkHIDDevice device) { - if (!getDevices().contains(device)) { + if (!devices.contains(device)) { synchronized (devices) { - getDevices().add(device); + devices.add(device); } } } @@ -258,11 +258,11 @@ public final class NetworkManager implements Runnable { 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 { diff --git a/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java b/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java index 06ba3f8..70c6db7 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java @@ -40,8 +40,7 @@ import net.ash.HIDToVPADNetworkClient.util.Settings; @Log final class TCPClient { private final Object lock = new Object(); - @Getter - private static TCPClient instance = new TCPClient(); + @Getter private static TCPClient instance = new TCPClient(); private Socket sock; private DataInputStream in; private DataOutputStream out; diff --git a/src/net/ash/HIDToVPADNetworkClient/network/UDPClient.java b/src/net/ash/HIDToVPADNetworkClient/network/UDPClient.java index 3794f35..485d146 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/UDPClient.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/UDPClient.java @@ -27,6 +27,7 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; + final class UDPClient { private final DatagramSocket sock; private final InetAddress host;