Improved behaviour if no valid config file is found.

It should show a message and deactivate the controller, but sometimes it
also disconnects.
I don't know if it's currently worth it to fix the disconnecting
problem, as you reconnect without any problems (and need to add/create
the config anyway).
This commit is contained in:
Maschell 2017-03-26 23:00:23 +02:00
parent 7914c8d633
commit 59c328c194
9 changed files with 57 additions and 21 deletions

View File

@ -48,6 +48,7 @@ public abstract class Controller implements Runnable {
private Object rumbleLock = new Object();
private boolean rumble = false;
private boolean hasConfig = true; // Let's be optimistic
public Controller(ControllerType type, String identifier) throws ControllerInitializationFailedException {
this.type = type;
@ -206,4 +207,12 @@ public abstract class Controller implements Runnable {
}
public abstract String getInfoText();
public boolean hasConfig() {
return this.hasConfig;
}
public void setHasConfig(boolean b) {
this.hasConfig = b;
}
}

View File

@ -33,6 +33,7 @@ import javax.swing.Timer;
import lombok.Getter;
import net.ash.HIDToVPADNetworkClient.controller.Controller;
import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
import net.ash.HIDToVPADNetworkClient.util.Utilities;
public class GuiControllerListItem extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
@ -40,6 +41,9 @@ public class GuiControllerListItem extends JPanel implements ActionListener {
@Getter private final Controller controller;
private JCheckBox checkbox;
private boolean clicked = false;
private boolean hasConfigCache = true;
public GuiControllerListItem(Controller data) {
super(new BorderLayout());
@ -57,6 +61,11 @@ public class GuiControllerListItem extends JPanel implements ActionListener {
int delay = 100; // milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (hasConfigCache != controller.hasConfig()) {
hasConfigCache = controller.hasConfig();
checkIfDisplayNoConfigMessage();
}
checkbox.setEnabled(NetworkManager.getInstance().isConnected());
checkbox.setSelected(controller.isActive());
}
@ -68,8 +77,15 @@ public class GuiControllerListItem extends JPanel implements ActionListener {
return controller.getInfoText();
}
private void checkIfDisplayNoConfigMessage() {
if (hasConfigCache == false) {
Utilities.messageBox("No configuration for this controller found on the console.");
}
}
@Override
public void actionPerformed(ActionEvent e) {
checkIfDisplayNoConfigMessage();
boolean selected = ((JCheckBox) e.getSource()).isSelected();
controller.setActive(selected);
checkbox.setSelected(controller.isActive());

View File

@ -103,7 +103,7 @@ public class ActiveControllerManager implements Runnable {
synchronized (activeControllers) {
for (Controller c : toRemove) {
NetworkManager.getInstance().removeHIDDevice(activeControllers.get(c));
//c.destroyDriver(); Removing it from the list doesn't require to close the connection.
// c.destroyDriver(); Removing it from the list doesn't require to close the connection.
activeControllers.remove(c);
}
}

View File

@ -29,6 +29,8 @@ import java.util.List;
import lombok.Getter;
import lombok.Synchronized;
import lombok.extern.java.Log;
import net.ash.HIDToVPADNetworkClient.controller.Controller;
import net.ash.HIDToVPADNetworkClient.manager.ActiveControllerManager;
import net.ash.HIDToVPADNetworkClient.network.commands.AttachCommand;
import net.ash.HIDToVPADNetworkClient.network.commands.DetachCommand;
import net.ash.HIDToVPADNetworkClient.network.commands.DeviceCommand;
@ -206,13 +208,20 @@ public class NetworkManager implements Runnable {
try {
configFound = recvTCPByte();
} catch (IOException e1) {
e1.printStackTrace();
log.info("Failed to get byte.");
disconnect();
return false;
}
if (configFound == Protocol.TCP_CMD_ATTACH_CONFIG_FOUND) {
// log.info("Config on the console found!");
} else if (configFound == Protocol.TCP_CMD_ATTACH_CONFIG_NOT_FOUND) {
log.info("NO CONFIG FOUND.");
return false;
Controller c = ActiveControllerManager.getInstance().getControllerByHIDHandle(command.getSender().getHidHandle());
if (c != null) {
c.setHasConfig(false);
c.setActive(false);
}
return true;
} else if (configFound == 0) {
log.info("Failed to get byte.");
disconnect();

View File

@ -58,7 +58,7 @@ public class Protocol {
}
public enum HandshakeReturnCode {
GOOD_HANDSHAKE,BAD_HANDSHAKE
GOOD_HANDSHAKE, BAD_HANDSHAKE
}
public static byte[] getRawAttachDataToSend(AttachCommand command) throws IOException {

View File

@ -57,13 +57,13 @@ public class TCPClient {
out = new DataOutputStream(sock.getOutputStream());
HandshakeReturnCode resultHandshake = HandshakeReturnCode.GOOD_HANDSHAKE;
if (recvByte() != Protocol.TCP_HANDSHAKE) resultHandshake = HandshakeReturnCode.BAD_HANDSHAKE;
if (recvByte() != Protocol.TCP_HANDSHAKE) resultHandshake = HandshakeReturnCode.BAD_HANDSHAKE;
if (resultHandshake == HandshakeReturnCode.GOOD_HANDSHAKE) {
ActiveControllerManager.getInstance().attachAllActiveControllers();
this.ip = ip;
shouldRetry = 0;
}else{
} else {
log.info("[TCP] Handshaking failed");
throw new Exception();
}
@ -104,12 +104,7 @@ public class TCPClient {
}
public synchronized byte recvByte() throws IOException {
try {
return in.readByte();
} catch (IOException e) {
log.info(e.getMessage());
throw e;
}
return in.readByte();
}
public synchronized short recvShort() throws IOException {

View File

@ -58,7 +58,7 @@ public class PureJavaHidApiManager {
if (Settings.isMacOSX()) real_path = real_path.substring(0, 13);
if (real_path.equals(expected_path)){
if (real_path.equals(expected_path)) {
return PureJavaHidApi.openDevice(info);
}

View File

@ -21,6 +21,9 @@
*******************************************************************************/
package net.ash.HIDToVPADNetworkClient.util;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Utilities {
private Utilities() {
@ -76,4 +79,8 @@ public class Utilities {
public static short signedShortToByte(short value) {
return signedShortToByte((int) value);
}
public static void messageBox(String string) {
JOptionPane.showMessageDialog(new JFrame(), string);
}
}