Even more refactoring (and fixing the build..)

This commit is contained in:
Maschell 2017-03-31 16:01:52 +02:00
parent 4c814a2aaf
commit 1c98eaffbb
15 changed files with 64 additions and 63 deletions

View File

@ -33,7 +33,7 @@ import net.ash.HIDToVPADNetworkClient.util.Settings;
* TODO finish HidController * TODO finish HidController
* TODO locale * TODO locale
*/ */
public class Main { public final class Main {
public static void main(String[] args) { public static void main(String[] args) {
Settings.loadSettings(); Settings.loadSettings();
try { try {

View File

@ -90,12 +90,12 @@ public abstract class Controller implements Runnable {
@Synchronized("dataLock") @Synchronized("dataLock")
public byte[] getLatestData() { public byte[] getLatestData() {
if (latestData != null) { if (latestData == null) {
return new byte[0];
}else{
byte[] data = this.latestData.clone(); byte[] data = this.latestData.clone();
this.latestData = null; this.latestData = null;
return data; return data;
} else {
return null;
} }
} }
@ -183,8 +183,7 @@ public abstract class Controller implements Runnable {
if (identifier == null) { if (identifier == null) {
if (other.identifier != null) return false; if (other.identifier != null) return false;
} else if (!identifier.equals(other.identifier)) return false; } else if (!identifier.equals(other.identifier)) return false;
if (type != other.type) return false; return (type != other.type);
return true;
} }
@Synchronized("rumbleLock") @Synchronized("rumbleLock")

View File

@ -97,12 +97,12 @@ public class LinuxDevInputController extends Controller implements Runnable {
return null; return null;
} }
if (value != 0) { if (value == 0) {
// Set bit with button number
buttonState |= (1 << number);
} else {
// Clear bit with button number // Clear bit with button number
buttonState &= ~(1 << number); buttonState &= ~(1 << number);
}else{
// Set bit with button number
buttonState |= (1 << number);
} }
} else if (type == JS_EVENT_AXIS) { } else if (type == JS_EVENT_AXIS) {
if (number >= NUM_SUPPORTED_AXIS) { if (number >= NUM_SUPPORTED_AXIS) {

View File

@ -34,7 +34,7 @@ import purejavahidapi.HidDevice;
import purejavahidapi.InputReportListener; import purejavahidapi.InputReportListener;
public class PureJavaHidController extends Controller implements 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 byte[] currentData = new byte[1];
protected int PACKET_LENGTH = 64; protected int PACKET_LENGTH = 64;

View File

@ -41,7 +41,7 @@ import lombok.Getter;
import net.ash.HIDToVPADNetworkClient.network.NetworkManager; import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
import net.ash.HIDToVPADNetworkClient.util.Settings; 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; private static final long serialVersionUID = 1L;
@Getter private static GuiInputControls instance = new GuiInputControls(); @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)); setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setPreferredSize(new Dimension(220, 150)); setPreferredSize(new Dimension(220, 150));
JButton connectButton = new JButton(CONNECT); final JButton connectButton = new JButton(CONNECT);
connectButton.setAlignmentX(Component.CENTER_ALIGNMENT); connectButton.setAlignmentX(Component.CENTER_ALIGNMENT);
ipTextBox = new JTextField(); ipTextBox = new JTextField();
@ -70,7 +70,7 @@ public class GuiInputControls extends JPanel implements ActionListener {
JLabel statusLabel = new JLabel("Ready."); JLabel statusLabel = new JLabel("Ready.");
statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT); statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JCheckBox cbautoActivateController = new JCheckBox(); final JCheckBox cbautoActivateController = new JCheckBox();
cbautoActivateController.setSelected(Settings.AUTO_ACTIVATE_CONTROLLER); cbautoActivateController.setSelected(Settings.AUTO_ACTIVATE_CONTROLLER);
cbautoActivateController.addActionListener(new ActionListener() { cbautoActivateController.addActionListener(new ActionListener() {

View File

@ -124,7 +124,7 @@ public final class ActiveControllerManager implements Runnable {
synchronized (activeControllers) { synchronized (activeControllers) {
for (Entry<Controller, NetworkHIDDevice> entry : activeControllers.entrySet()) { for (Entry<Controller, NetworkHIDDevice> entry : activeControllers.entrySet()) {
byte[] data = entry.getKey().getLatestData(); byte[] data = entry.getKey().getLatestData();
if (data != null) { if (data != null && data.length > 0) {
NetworkHIDDevice device = entry.getValue(); NetworkHIDDevice device = entry.getValue();
device.sendRead(data); device.sendRead(data);
} }

View File

@ -54,6 +54,8 @@ import purejavahidapi.HidDeviceInfo;
public final class ControllerManager { public final class ControllerManager {
private static Map<String, Controller> attachedControllers = new HashMap<String, Controller>(); private static Map<String, Controller> attachedControllers = new HashMap<String, Controller>();
private static boolean threwUnsatisfiedLinkError = false;
private ControllerManager() { private ControllerManager() {
// Utility Class // Utility Class
} }
@ -153,8 +155,6 @@ public final class ControllerManager {
return connectedDevices; return connectedDevices;
} }
private static boolean threwUnsatisfiedLinkError = false;
private static Map<String, ControllerType> detectWindowsControllers() { private static Map<String, ControllerType> detectWindowsControllers() {
Map<String, ControllerType> result = new HashMap<String, ControllerType>(); Map<String, ControllerType> result = new HashMap<String, ControllerType>();
ControllerType type = ControllerType.XINPUT13; ControllerType type = ControllerType.XINPUT13;

View File

@ -24,7 +24,7 @@ package net.ash.HIDToVPADNetworkClient.network;
import lombok.Data; import lombok.Data;
@Data @Data
abstract class DeviceCommand { class DeviceCommand {
private final int handle; private final int handle;
private final NetworkHIDDevice sender; private final NetworkHIDDevice sender;
private final Class<? extends DeviceCommand> type; private final Class<? extends DeviceCommand> type;

View File

@ -47,7 +47,7 @@ public class NetworkHIDDevice {
@Getter private final int hidHandle = HandleFoundry.next(); @Getter private final int hidHandle = HandleFoundry.next();
private Object readCommandLock = new Object(); private Object readCommandLock = new Object();
@Getter(AccessLevel.PRIVATE) private List<DeviceCommand> commands = new ArrayList<DeviceCommand>(); @Getter(AccessLevel.PRIVATE) private final List<DeviceCommand> commands = new ArrayList<DeviceCommand>();
@Getter(AccessLevel.PRIVATE) @Setter(AccessLevel.PRIVATE) private ReadCommand latestRead; @Getter(AccessLevel.PRIVATE) @Setter(AccessLevel.PRIVATE) private ReadCommand latestRead;
public NetworkHIDDevice(short vid, short pid) { public NetworkHIDDevice(short vid, short pid) {
@ -113,7 +113,6 @@ public class NetworkHIDDevice {
if (obj == null) return false; if (obj == null) return false;
if (getClass() != obj.getClass()) return false; if (getClass() != obj.getClass()) return false;
NetworkHIDDevice other = (NetworkHIDDevice) obj; NetworkHIDDevice other = (NetworkHIDDevice) obj;
if (hidHandle != other.hidHandle) return false; return (hidHandle != other.hidHandle);
return true;
} }
} }

View File

@ -38,7 +38,7 @@ import net.ash.HIDToVPADNetworkClient.util.Utilities;
@Log @Log
public final class NetworkManager implements Runnable { public final class NetworkManager implements Runnable {
private final TCPClient tcpClient = new TCPClient(); private final TCPClient tcpClient = TCPClient.getInstance();
private UDPClient udpClient = null; private UDPClient udpClient = null;
@Getter private static NetworkManager instance = new NetworkManager(); @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. // Let's save them for later and demand the first data packet.
NetworkHIDDevice sender = command.getSender(); NetworkHIDDevice sender = command.getSender();
if (sender != null) { if (sender == null) {
sender.setDeviceslot(deviceslot);
sender.setPadslot(padslot);
sender.setNeedFirstData(true); // Please send data after connecting.
} else {
log.info("Something really went wrong. Got an attach event with out an " + NetworkHIDDevice.class.getSimpleName()); log.info("Something really went wrong. Got an attach event with out an " + NetworkHIDDevice.class.getSimpleName());
return false; return false;
} }
sender.setDeviceslot(deviceslot);
sender.setPadslot(padslot);
sender.setNeedFirstData(true); // Please send data after connecting.
log.info("Attaching done!"); log.info("Attaching done!");
return true; return true;
} else { } else {
@ -274,7 +275,7 @@ public final class NetworkManager implements Runnable {
byte[] rawCommand; byte[] rawCommand;
try { try {
rawCommand = Protocol.getRawReadDataToSend(readCommands); 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)); System.out.println("UDP Packet sent: " + Utilities.ByteArrayToString(rawCommand));
} }
} catch (IOException e) { } catch (IOException e) {
@ -312,6 +313,7 @@ public final class NetworkManager implements Runnable {
public void disconnect() { public void disconnect() {
// ControllerManager.deactivateAllAttachedControllers(); // ControllerManager.deactivateAllAttachedControllers();
tcpClient.abort(); tcpClient.abort();
udpClient = null;
} }
private short recvTCPShort() throws IOException { private short recvTCPShort() throws IOException {

View File

@ -30,45 +30,45 @@ import java.util.List;
import lombok.extern.java.Log; import lombok.extern.java.Log;
@Log @Log
class Protocol { final class Protocol {
protected static final int TCP_PORT = 8112; static final int TCP_PORT = 8112;
protected static final int UDP_PORT = 8113; static final int UDP_PORT = 8113;
protected static final byte TCP_HANDSHAKE = 0x12; static final byte TCP_HANDSHAKE = 0x12;
protected static final byte TCP_SAME_CLIENT = 0x20; static final byte TCP_SAME_CLIENT = 0x20;
protected static final byte TCP_NEW_CLIENT = 0x21; static final byte TCP_NEW_CLIENT = 0x21;
protected static final byte TCP_CMD_ATTACH = 0x01; static final byte TCP_CMD_ATTACH = 0x01;
protected static final byte TCP_CMD_DETACH = 0x02; static final byte TCP_CMD_DETACH = 0x02;
protected static final byte TCP_CMD_PING = (byte) 0xF0; 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; static final byte TCP_CMD_ATTACH_CONFIG_FOUND = (byte) 0xE0;
protected static final byte TCP_CMD_ATTACH_CONFIG_NOT_FOUND = (byte) 0xE1; static final byte TCP_CMD_ATTACH_CONFIG_NOT_FOUND = (byte) 0xE1;
protected static final byte TCP_CMD_ATTACH_USERDATA_OKAY = (byte) 0xE8; 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_USERDATA_BAD = (byte) 0xE9;
private Protocol() { private Protocol() {
} }
protected enum HandshakeReturnCode { enum HandshakeReturnCode {
GOOD_HANDSHAKE, BAD_HANDSHAKE 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(); 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(); 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 }; return new byte[] { Protocol.TCP_CMD_PING };
} }
protected static byte[] getRawReadDataToSend(List<ReadCommand> readCommands) throws IOException { static byte[] getRawReadDataToSend(List<ReadCommand> readCommands) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos); DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(Protocol.UDP_CMD_DATA); dos.writeByte(Protocol.UDP_CMD_DATA);

View File

@ -38,8 +38,10 @@ import net.ash.HIDToVPADNetworkClient.network.Protocol.HandshakeReturnCode;
import net.ash.HIDToVPADNetworkClient.util.Settings; import net.ash.HIDToVPADNetworkClient.util.Settings;
@Log @Log
class TCPClient { final class TCPClient {
private final Object lock = new Object(); private final Object lock = new Object();
@Getter
private static TCPClient instance = new TCPClient();
private Socket sock; private Socket sock;
private DataInputStream in; private DataInputStream in;
private DataOutputStream out; private DataOutputStream out;
@ -48,11 +50,11 @@ class TCPClient {
private String ip; private String ip;
protected TCPClient() { private TCPClient() {
} }
@Synchronized("lock") @Synchronized("lock")
protected void connect(String ip) throws Exception { void connect(String ip) throws Exception {
sock = new Socket(); sock = new Socket();
sock.connect(new InetSocketAddress(ip, Protocol.TCP_PORT), 2000); sock.connect(new InetSocketAddress(ip, Protocol.TCP_PORT), 2000);
in = new DataInputStream(sock.getInputStream()); in = new DataInputStream(sock.getInputStream());
@ -72,7 +74,7 @@ class TCPClient {
} }
@Synchronized("lock") @Synchronized("lock")
protected boolean abort() { boolean abort() {
try { try {
shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING; shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
sock.close(); sock.close();
@ -84,7 +86,7 @@ class TCPClient {
} }
@Synchronized("lock") @Synchronized("lock")
protected void send(byte[] rawCommand) throws IOException { void send(byte[] rawCommand) throws IOException {
try { try {
out.write(rawCommand); out.write(rawCommand);
out.flush(); 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()); send(ByteBuffer.allocate(4).putInt(value).array());
} }
@Synchronized("lock") @Synchronized("lock")
protected byte recvByte() throws IOException { byte recvByte() throws IOException {
return in.readByte(); return in.readByte();
} }
@Synchronized("lock") @Synchronized("lock")
protected short recvShort() throws IOException { short recvShort() throws IOException {
try { try {
return in.readShort(); return in.readShort();
} catch (IOException e) { } catch (IOException e) {
@ -123,11 +125,11 @@ class TCPClient {
} }
@Synchronized("lock") @Synchronized("lock")
protected boolean isConnected() { boolean isConnected() {
return (sock != null && sock.isConnected() && !sock.isClosed()); return (sock != null && sock.isConnected() && !sock.isClosed());
} }
protected boolean isShouldRetry() { boolean isShouldRetry() {
return this.shouldRetry < Settings.MAXIMUM_TRIES_FOR_RECONNECTING; return this.shouldRetry < Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
} }
} }

View File

@ -27,8 +27,7 @@ import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.SocketException; import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
final class UDPClient {
class UDPClient {
private final DatagramSocket sock; private final DatagramSocket sock;
private final InetAddress host; private final InetAddress host;
@ -37,7 +36,7 @@ class UDPClient {
host = InetAddress.getByName(ip); host = InetAddress.getByName(ip);
} }
protected static UDPClient createUDPClient(String ip) { static UDPClient createUDPClient(String ip) {
UDPClient result = null; UDPClient result = null;
try { try {
result = new UDPClient(ip); result = new UDPClient(ip);
@ -47,7 +46,7 @@ class UDPClient {
return result; 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); DatagramPacket packet = new DatagramPacket(data, data.length, host, Protocol.UDP_PORT);
sock.send(packet); sock.send(packet);
} }

View File

@ -29,7 +29,7 @@ import purejavahidapi.HidDevice;
import purejavahidapi.HidDeviceInfo; import purejavahidapi.HidDeviceInfo;
import purejavahidapi.PureJavaHidApi; import purejavahidapi.PureJavaHidApi;
public class PureJavaHidApiManager { public final class PureJavaHidApiManager {
private PureJavaHidApiManager() { private PureJavaHidApiManager() {
} }

View File

@ -33,7 +33,7 @@ import lombok.Setter;
import lombok.extern.java.Log; import lombok.extern.java.Log;
@Log @Log
public class Settings { public final class Settings {
private static final String CONFIG_FILE_NAME = "hidtovpad.properties"; private static final String CONFIG_FILE_NAME = "hidtovpad.properties";
public static final int DETECT_CONTROLLER_INTERVAL = 1000; public static final int DETECT_CONTROLLER_INTERVAL = 1000;