Skip to content

Commit

Permalink
Implement INPUT
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpaljak committed Jan 27, 2016
1 parent 4134a57 commit 9f56520
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
33 changes: 25 additions & 8 deletions src/apdu4j/remote/CmdlineRemoteTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,27 @@ private void verify(Map<String, Object> msg) throws IOException {
private void dialog(Map<String, Object> msg) throws IOException {
System.out.println("# " + msg.get("text"));
Map< String, Object> m = JSONProtocol.ok(msg);
boolean yes = false;
boolean yes = get_yes_or_no_console("Decision");

// lanterna requires work as it screws up Console.readPassword()
yes = get_yes_or_no_console();
if (!yes) {
m.put("button", "red");
} else {
m.put("button", "green");
}
pipe.send(m);
}

private void input(Map<String, Object> msg) throws IOException {
System.out.println("# " + msg.get("text"));
Map< String, Object> m = JSONProtocol.ok(msg);

Console c = System.console();
String input = c.readLine(msg + "> ");
if (input == null)
input = "";
input = input.trim();
System.out.println("> \""+ input + " \"");
boolean yes = get_yes_or_no_console("Confirm");
if (!yes) {
m.put("button", "red");
} else {
Expand All @@ -125,6 +141,7 @@ private void dialog(Map<String, Object> msg) throws IOException {
pipe.send(m);
}


private void decrypt(Map<String, Object> msg) throws IOException {
String cmd = (String) msg.get("bytes");
if (cmd == null)
Expand All @@ -138,9 +155,7 @@ private void decrypt(Map<String, Object> msg) throws IOException {
System.out.println("# " + msg.get("text"));
System.out.println("# " + new String(r.getData(), "UTF-8"));
Map< String, Object> m = JSONProtocol.ok(msg);
boolean yes = false;
// lanterna requires work as it screws up Console.readPassword()
yes = get_yes_or_no_console();
boolean yes = get_yes_or_no_console("Confirm");

if (!yes) {
m.put("button", "red");
Expand Down Expand Up @@ -185,6 +200,8 @@ private boolean processMessage (Map<String, Object> msg) throws IOException {
verify(msg);
} else if (cmd.equals("DIALOG")) {
dialog(msg);
} else if (cmd.equals("INPUT")) {
input(msg);
} else if (cmd.equals("STOP")) {
stop(msg);
return false;
Expand All @@ -197,10 +214,10 @@ private boolean processMessage (Map<String, Object> msg) throws IOException {
return true;
}

private boolean get_yes_or_no_console() {
private boolean get_yes_or_no_console(String msg) {
Console c = System.console();
while (true) {
String response = c.readLine("y/n ? ");
String response = c.readLine(msg + " y/n ? ");
if (response == null)
continue;
response = response.trim();
Expand Down
25 changes: 21 additions & 4 deletions src/apdu4j/remote/RemoteTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void statusMessage(String text) throws IOException {
}

/**
* Shows a dialog message to the user and returnes the pressed button.
* Shows a dialog message to the user and returns the pressed button.
*
* @param message text to display to the user
* @return {@link Button} that was pressed by the user
Expand All @@ -91,11 +91,28 @@ public Button dialog(String message) throws IOException {
m.put("text", message);
pipe.send(m);
Map<String, Object> r = pipe.recv();
if (JSONProtocol.check(m, r)) {
return Button.valueOf(((String)r.get("button")).toUpperCase());
} else {
if (JSONProtocol.check(m, r) || !r.containsKey("button")) {
throw new IOException("Unknown button pressed");
}
return Button.valueOf(((String)r.get("button")).toUpperCase());
}

/**
* Asks for input from the user.
*
* @param message text to display to the user
* @return null or input
* @throws IOException when communication fails
*/
public String input(String message) throws IOException {
Map<String, Object> m = JSONProtocol.cmd("input");
m.put("text", message);
pipe.send(m);
Map<String, Object> r = pipe.recv();
if (!JSONProtocol.check(m, r) || !r.containsKey("value")) {
throw new IOException("No value");
}
return (String) r.get("value");
}

/**
Expand Down

0 comments on commit 9f56520

Please sign in to comment.