Skip to content

Commit

Permalink
Fixing connection reset using different keyboard layouts issue
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Feb 15, 2017
1 parent 22f0131 commit be8c5a8
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 43 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<target name="build" depends="compile">
<jar destfile="${dir.build}/dayon.jar">
<manifest>
<attribute name="Implementation-Version" value="1.4 #1" />
<attribute name="Implementation-Version" value="1.4 #2" />
<attribute name="Main-Class" value="${main.class}" />
<attribute name="Class-Path" value=". jetty-continuation-8.1.21.v20160908.jar jetty-http-8.1.21.v20160908.jar jetty-io-8.1.21.v20160908.jar jetty-server-8.1.21.v20160908.jar jetty-util-8.1.21.v20160908.jar servlet-api-3.0.jar" />
<attribute name="Permissions" value="all-permissions" />
Expand Down
16 changes: 6 additions & 10 deletions src/mpo/dayon/assistant/control/ControlEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,20 @@ protected void execute() throws Exception {
/**
* From AWT thread (!)
*/
public void onKeyPressed(final int keycode) {
public void onKeyPressed(final int keycode, final char keychar) {
executor.execute(new Executable(executor, null) {
protected void execute() throws Exception {
// System.out.println("PRESSED:" +
// KeyEvent.getKeyText(keycode));

pressedKeys.add(keycode);
network.sendKeyControl(new NetworkKeyControlMessage(NetworkKeyControlMessage.KeyState.PRESSED, keycode));
network.sendKeyControl(new NetworkKeyControlMessage(NetworkKeyControlMessage.KeyState.PRESSED, keycode, keychar));
}
});
}

/**
* From AWT thread (!)
*/
public void onKeyReleased(final int keycode) {
public void onKeyReleased(final int keycode, final char keychar) {
// -------------------------------------------------------------------------------------------------------------
// E.g., Windows + R : [Windows.PRESSED] and then the focus is LOST =>
// missing RELEASED events
Expand All @@ -129,23 +127,21 @@ public void onKeyReleased(final int keycode) {
if (pressedKeys.size() > 0) {
final Integer[] pkeys = pressedKeys.toArray(new Integer[pressedKeys.size()]);
for (Integer pkey : pkeys) {
onKeyReleased(pkey);
onKeyReleased(pkey, keychar);
}
}
return;
}

if (!pressedKeys.contains(keycode)) {
onKeyPressed(keycode);
onKeyPressed(keycode, keychar);
}

executor.execute(new Executable(executor, null) {
protected void execute() throws Exception {
// System.out.println("RELEASED:" +
// KeyEvent.getKeyText(keycode));

pressedKeys.remove(keycode);
network.sendKeyControl(new NetworkKeyControlMessage(NetworkKeyControlMessage.KeyState.RELEASED, keycode));
network.sendKeyControl(new NetworkKeyControlMessage(NetworkKeyControlMessage.KeyState.RELEASED, keycode, keychar));
}
});
}
Expand Down
14 changes: 7 additions & 7 deletions src/mpo/dayon/assistant/gui/AssistantFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ public void mouseWheelMoved(MouseWheelEvent ev) {
@Override
public void keyPressed(KeyEvent ev) {
if (controlActivated) {
fireOnKeyPressed(ev.getKeyCode());
fireOnKeyPressed(ev.getKeyCode(), ev.getKeyChar());
}
}

@Override
public void keyReleased(KeyEvent ev) {
if (controlActivated) {
fireOnKeyReleased(ev.getKeyCode());
fireOnKeyReleased(ev.getKeyCode(), ev.getKeyChar());
}
}
});
Expand All @@ -176,7 +176,7 @@ public void keyReleased(KeyEvent ev) {
@Override
public void focusLost(FocusEvent ev) {
if (controlActivated) {
fireOnKeyReleased(-1);
fireOnKeyReleased(-1, Character.MIN_VALUE);
}
}
});
Expand Down Expand Up @@ -427,27 +427,27 @@ private void fireOnMouseWheeled(int x, int y, int rotations) {
}
}

private void fireOnKeyPressed(int keycode) {
private void fireOnKeyPressed(int keycode, char keychar) {
final AssistantFrameListener[] xlisteners = listeners.getListeners();

if (xlisteners == null) {
return;
}

for (final AssistantFrameListener xlistener : xlisteners) {
xlistener.onKeyPressed(keycode);
xlistener.onKeyPressed(keycode, keychar);
}
}

private void fireOnKeyReleased(int keycode) {
private void fireOnKeyReleased(int keycode, char keychar) {
final AssistantFrameListener[] xlisteners = listeners.getListeners();

if (xlisteners == null) {
return;
}

for (final AssistantFrameListener xlistener : xlisteners) {
xlistener.onKeyReleased(keycode);
xlistener.onKeyReleased(keycode, keychar);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/mpo/dayon/assistant/gui/AssistantFrameListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface AssistantFrameListener extends Listener {

void onMouseWheeled(int x, int y, int rotations);

void onKeyPressed(int keycode);
void onKeyPressed(int keycode, char keychar);

void onKeyReleased(int keycode);
void onKeyReleased(int keycode, char keychar);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mpo.dayon.assisted.control;

import mpo.dayon.common.event.Subscriber;
import mpo.dayon.common.network.NetworkEngine;
import mpo.dayon.common.network.message.NetworkKeyControlMessage;
import mpo.dayon.common.network.message.NetworkMessageHandler;
Expand All @@ -15,5 +16,10 @@ public interface NetworkControlMessageHandler extends NetworkMessageHandler {
* Should not block as called from the network incoming message thread (!)
*/
void handleMessage(NetworkEngine engine, NetworkKeyControlMessage message);

/**
* Adds an event listener
*/
void subscribe(Subscriber listener);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mpo.dayon.assisted.control;

import mpo.dayon.common.event.Subscriber;
import mpo.dayon.common.network.NetworkEngine;
import mpo.dayon.common.network.message.NetworkKeyControlMessage;
import mpo.dayon.common.network.message.NetworkMouseControlMessage;
Expand All @@ -15,4 +16,7 @@ public void handleMessage(NetworkEngine engine, NetworkMouseControlMessage messa

public void handleMessage(NetworkEngine engine, NetworkKeyControlMessage message) {
}

public void subscribe(Subscriber listener) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.KeyStroke;

import mpo.dayon.common.event.Subscriber;
import mpo.dayon.common.log.Log;
import mpo.dayon.common.network.NetworkEngine;
import mpo.dayon.common.network.message.NetworkKeyControlMessage;
import mpo.dayon.common.network.message.NetworkMouseControlMessage;

public class RobotNetworkControlMessageHandler implements NetworkControlMessageHandler {
private final Robot robot;

private List<Subscriber> subscribers = new ArrayList<Subscriber>();

public void subscribe(Subscriber subscriber) {
subscribers.add(subscriber);
}

public void shout(char bogusChar) {
for (Subscriber subscriber : subscribers) {
subscriber.digest(String.valueOf(bogusChar));
}
}

public RobotNetworkControlMessageHandler() {
try {
Expand Down Expand Up @@ -51,9 +70,34 @@ public void handleMessage(NetworkEngine engine, NetworkMouseControlMessage messa
*/
public void handleMessage(NetworkEngine engine, NetworkKeyControlMessage message) {
if (message.isPressed()) {
robot.keyPress(message.getKeyCode());
try {
robot.keyPress(message.getKeyCode());
} catch (IllegalArgumentException ex) {
Log.warn(message.toString() +" contained an invalid keyCode for "+message.getKeyChar());
shout(message.getKeyChar());
if (message.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
KeyStroke key = KeyStroke.getKeyStroke(message.getKeyChar());
if (key.getKeyCode() != Character.MIN_VALUE) {
Log.warn("retrying with keyCode "+key.getKeyCode());
robot.keyPress(key.getKeyCode());
}
}
}
} else if (message.isReleased()) {
robot.keyRelease(message.getKeyCode());
try {
robot.keyRelease(message.getKeyCode());
} catch (IllegalArgumentException ex) {
Log.warn(message.toString() +" contained an invalid keyCode for "+message.getKeyChar());
shout(message.getKeyChar());
if (message.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
KeyStroke key = KeyStroke.getKeyStroke(message.getKeyChar());
if (key.getKeyCode() != Character.MIN_VALUE) {
Log.warn("retrying with keyCode "+key.getKeyCode());
robot.keyRelease(key.getKeyCode());
}
}
}

}
}
}
13 changes: 12 additions & 1 deletion src/mpo/dayon/assisted/gui/Assisted.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import mpo.dayon.assisted.network.NetworkAssistedEngineConfiguration;
import mpo.dayon.common.babylon.Babylon;
import mpo.dayon.common.error.FatalErrorHandler;
import mpo.dayon.common.error.SeriousErrorHandler;
import mpo.dayon.common.event.Subscriber;
import mpo.dayon.common.gui.common.DialogFactory;
import mpo.dayon.common.log.Log;
import mpo.dayon.common.network.NetworkEngine;
Expand All @@ -44,7 +46,7 @@
import mpo.dayon.common.security.CustomTrustManager;
import mpo.dayon.common.utils.SystemUtilities;

public class Assisted {
public class Assisted implements Subscriber {
private AssistedFrame frame;

private NetworkAssistedEngineConfiguration configuration;
Expand Down Expand Up @@ -155,6 +157,10 @@ public void handleConfiguration(NetworkEngine engine, NetworkCompressorConfigura
};

final NetworkControlMessageHandler controlHandler = new RobotNetworkControlMessageHandler();

SeriousErrorHandler.attachFrame(frame);

controlHandler.subscribe(this);

frame.onConnecting(configuration);

Expand Down Expand Up @@ -288,4 +294,9 @@ public boolean verify(String hostname, SSLSession session) {

}

@Override
public void digest(String message) {
SeriousErrorHandler.warn(String.valueOf(message));
}

}
19 changes: 5 additions & 14 deletions src/mpo/dayon/common/babylon/Babylon.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public static String translateEnum(Enum value) {
private static String formatValue(Locale locale, String tagValue, String tag, Object... arguments) {
String formattedTagValue;

if (tagValue != tag) // The identity equality is fine; that's what I
// want!
if (tagValue != tag) // The identity equality is fine; that's what I want!
{
try {
// The locale is required for example to convert a double into
Expand All @@ -78,20 +77,12 @@ private static String formatValue(Locale locale, String tagValue, String tag, Ob
formattedTagValue = String.format(locale, tagValue, arguments);
} catch (IllegalFormatException ex) {
Log.warn("Illegal format for tag [" + tag + "] - " + ex.getMessage(), ex);
formattedTagValue = tagValue + " " + Arrays.toString(arguments); // what
// else
// can
// I
// do
// here?
formattedTagValue = tagValue + " " + Arrays.toString(arguments);
// what else can I do here?
}
} else {
formattedTagValue = tagValue + " " + Arrays.toString(arguments); // what
// else
// can
// I
// do
// here?
formattedTagValue = tagValue + " " + Arrays.toString(arguments);
// what else can I do here?
}

return formattedTagValue;
Expand Down
5 changes: 5 additions & 0 deletions src/mpo/dayon/common/babylon/Babylon.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ fatal.error.msg1 = The program has encountered a fatal error.
fatal.error.msg2 = Details : %s
fatal.error.msg3 = not available.

serious.error = Warning
serious.error.msg1 = The program has encountered a keyboard error.
serious.error.msg2 = Details : %s
serious.error.msg3 = You should use the same input language on both computers.

comm.error = Communication Error
comm.error.msg1 = The program has encountered a communication error [%s].

Expand Down
5 changes: 5 additions & 0 deletions src/mpo/dayon/common/babylon/Babylon_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ fatal.error.msg1 = Es ist ein schwerwiegender Fehler aufgetreten.
fatal.error.msg2 = Details : %s
fatal.error.msg3 = nicht verf\u00fcgbar.

serious.error = Warnung
serious.error.msg1 = Es ist ein Tastaturfehler aufgetreten.
serious.error.msg2 = Details : %s
serious.error.msg3 = Sie sollten auf beiden Rechnern dieselbe Eingabesprache nutzen.

comm.error = Kommunikationsfehler
comm.error.msg1 = Es ist ein Kommunikationsfelher aufgetreten [%s].

Expand Down
5 changes: 5 additions & 0 deletions src/mpo/dayon/common/babylon/Babylon_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ fatal.error.msg1 = Une erreur fatale est survenue dans l'application.
fatal.error.msg2 = D\u00e9tails : %s
fatal.error.msg3 = non disponible.

serious.error = Attention
serious.error.msg1 = Il y avait une erreur de clavier.
serious.error.msg2 = D\u00e9tails : %s
serious.error.msg3 = Vous devez utiliser la m\u00eame langue de clavier sur les deux ordinateurs.

comm.error = \u00c9rreur de Communication
comm.error.msg1 = Une \u00e9rreur de communication est survenue dans l'application [%s].

Expand Down
29 changes: 29 additions & 0 deletions src/mpo/dayon/common/error/SeriousErrorHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mpo.dayon.common.error;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.jetbrains.annotations.Nullable;

import mpo.dayon.common.babylon.Babylon;
import mpo.dayon.common.log.Log;

public abstract class SeriousErrorHandler {
@Nullable
private static JFrame frame;

public static void warn(String message) {

if (frame != null) {
JOptionPane.showMessageDialog(frame, Babylon.translate("serious.error.msg1") + "\n" + Babylon.translate("serious.error.msg2", message) + "\n" + Babylon.translate("serious.error.msg3") ,
Babylon.translate("serious.error"), JOptionPane.ERROR_MESSAGE);
} else {
Log.error("Unable to display error message "+message);
}

}

public static void attachFrame(JFrame frame) {
SeriousErrorHandler.frame = frame;
}
}
6 changes: 6 additions & 0 deletions src/mpo/dayon/common/event/Subscriber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package mpo.dayon.common.event;

public interface Subscriber {
public void digest(String message);

}
Loading

0 comments on commit be8c5a8

Please sign in to comment.