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 locale
*/
public class Main {
public final class Main {
public static void main(String[] args) {
Settings.loadSettings();
try {

View File

@ -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")

View File

@ -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) {

View File

@ -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;

View File

@ -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() {

View File

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

View File

@ -54,6 +54,8 @@ import purejavahidapi.HidDeviceInfo;
public final class ControllerManager {
private static Map<String, Controller> attachedControllers = new HashMap<String, Controller>();
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<String, ControllerType> detectWindowsControllers() {
Map<String, ControllerType> result = new HashMap<String, ControllerType>();
ControllerType type = ControllerType.XINPUT13;

View File

@ -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<? extends DeviceCommand> type;

View File

@ -47,7 +47,7 @@ public class NetworkHIDDevice {
@Getter private final int hidHandle = HandleFoundry.next();
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;
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);
}
}

View File

@ -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 {

View File

@ -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<ReadCommand> readCommands) throws IOException {
static byte[] getRawReadDataToSend(List<ReadCommand> readCommands) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
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;
@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;
}
}

View File

@ -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);
}

View File

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

View File

@ -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;