diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java b/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java index 4318517..bd6d2de 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java @@ -36,8 +36,7 @@ import purejavahidapi.InputReportListener; public class PureJavaHidController extends Controller implements InputReportListener { public static Controller getInstance(String deviceIdentifier) throws IOException, ControllerInitializationFailedException { HidDevice device = PureJavaHidApiManager.getDeviceByPath(deviceIdentifier); - // We use a special version to optimize the data for the switch pro - // controller + // We use a special version to optimize the data for the switch pro controller if (device.getHidDeviceInfo().getVendorId() == SwitchProController.SWITCH_PRO_CONTROLLER_VID && device.getHidDeviceInfo().getProductId() == SwitchProController.SWITCH_PRO_CONTROLLER_PID) { return new SwitchProController(deviceIdentifier); diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java index c42e521..6cd4703 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java @@ -103,7 +103,7 @@ public class ActiveControllerManager implements Runnable { synchronized (activeControllers) { for (Controller c : toRemove) { NetworkManager.getInstance().removeHIDDevice(activeControllers.get(c)); - c.destroyDriver(); + //c.destroyDriver(); Removing it from the list doesn't require to close the connection. activeControllers.remove(c); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java index e6897ad..35ddb20 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java @@ -121,8 +121,7 @@ public class ControllerManager { default: break; } - if (c != null) { // I don't like that starting the Thread - // happens here =/ + if (c != null) { // I don't like that starting the Thread happens here =/ new Thread(c).start(); attachedControllers.put(deviceIdentifier, c); } @@ -134,7 +133,7 @@ public class ControllerManager { public static List getAttachedControllers() { return new ArrayList(attachedControllers.values()); } - + private static Map detectHIDDevices() { Map connectedDevices = new HashMap(); diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java index 3161a4f..4318f5d 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java @@ -278,7 +278,7 @@ public class NetworkManager implements Runnable { System.out.println("UDP Packet sent: " + Utilities.ByteArrayToString(rawCommand)); } } catch (IOException e) { - System.out.println("Sending read data failed."); + log.info("Sending read data failed."); } } @@ -331,13 +331,13 @@ public class NetworkManager implements Runnable { log.info("Trying to connect to: " + ip); try { tcpClient.connect(ip); - System.out.println("TCP Connected!"); + log.info("TCP Connected!"); udpClient = UDPClient.createUDPClient(ip); if (udpClient != null) { result = true; } } catch (Exception e) { - System.out.println("Error while connecting: " + e.getMessage()); + log.info("Error while connecting: " + e.getMessage()); } return result; } diff --git a/src/net/ash/HIDToVPADNetworkClient/network/Protocol.java b/src/net/ash/HIDToVPADNetworkClient/network/Protocol.java index 04ca75c..0d6ff50 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/Protocol.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/Protocol.java @@ -28,6 +28,7 @@ import java.nio.ByteBuffer; import java.util.List; import lombok.extern.java.Log; +import net.ash.HIDToVPADNetworkClient.network.Protocol.HandshakeReturnCode; import net.ash.HIDToVPADNetworkClient.network.commands.AttachCommand; import net.ash.HIDToVPADNetworkClient.network.commands.DetachCommand; import net.ash.HIDToVPADNetworkClient.network.commands.PingCommand; @@ -57,9 +58,9 @@ public class Protocol { } public enum HandshakeReturnCode { - BAD_HANDSHAKE, SAME_CLIENT, NEW_CLIENT + GOOD_HANDSHAKE,BAD_HANDSHAKE } - + public 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(); } @@ -98,4 +99,5 @@ public class Protocol { return bos.toByteArray(); } + } diff --git a/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java b/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java index 1bbfd99..8430c3a 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/TCPClient.java @@ -42,7 +42,6 @@ public class TCPClient { private Socket sock; private DataInputStream in; private DataOutputStream out; - @Getter private int clientID = HandleFoundry.next(); @Getter @Setter(AccessLevel.PRIVATE) private int shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING; @@ -52,46 +51,30 @@ public class TCPClient { } public synchronized void connect(String ip) throws Exception { - sock = new Socket(); sock.connect(new InetSocketAddress(ip, Protocol.TCP_PORT), 2000); in = new DataInputStream(sock.getInputStream()); out = new DataOutputStream(sock.getOutputStream()); - - HandshakeReturnCode resultHandshake = doHandshake(); - if (resultHandshake == HandshakeReturnCode.BAD_HANDSHAKE) { - log.info("[TCP] Handshaking failed"); - throw new Exception(); - } else { - if (resultHandshake == HandshakeReturnCode.NEW_CLIENT && this.ip != null) { - // We check the IP to be sure it's the first time we connect to - // a WiiU. //TODO: Sending a ID from the WiiU which will be - // compared? - // we are new to the client. - ActiveControllerManager.getInstance().attachAllActiveControllers(); - } else if (resultHandshake == HandshakeReturnCode.SAME_CLIENT) { - - } + + HandshakeReturnCode resultHandshake = HandshakeReturnCode.GOOD_HANDSHAKE; + if (recvByte() != Protocol.TCP_HANDSHAKE) resultHandshake = HandshakeReturnCode.BAD_HANDSHAKE; + + if (resultHandshake == HandshakeReturnCode.GOOD_HANDSHAKE) { + ActiveControllerManager.getInstance().attachAllActiveControllers(); this.ip = ip; shouldRetry = 0; + }else{ + log.info("[TCP] Handshaking failed"); + throw new Exception(); } } - - private synchronized HandshakeReturnCode doHandshake() throws Exception { - if (recvByte() != Protocol.TCP_HANDSHAKE) return HandshakeReturnCode.BAD_HANDSHAKE; - send(clientID); - log.info("[TCP] Handshaking..."); - HandshakeReturnCode test = (recvByte() == Protocol.TCP_NEW_CLIENT) ? HandshakeReturnCode.NEW_CLIENT : HandshakeReturnCode.SAME_CLIENT; - return test; - } - + public synchronized boolean abort() { try { shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING; sock.close(); - clientID = HandleFoundry.next(); } catch (IOException e) { - System.out.println(e.getMessage()); // TODO: handle + log.info(e.getMessage()); // TODO: handle return false; } return true; @@ -104,10 +87,8 @@ public class TCPClient { } catch (IOException e) { try { 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. + log.info("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 { abort(); } @@ -126,7 +107,7 @@ public class TCPClient { try { return in.readByte(); } catch (IOException e) { - System.out.println(e.getMessage()); + log.info(e.getMessage()); throw e; } } @@ -135,7 +116,7 @@ public class TCPClient { try { return in.readShort(); } catch (IOException e) { - System.out.println(e.getMessage()); + log.info(e.getMessage()); throw e; } }