1 package de.hacksaar.andtuer;
3 import android.os.AsyncTask;
4 import android.util.Log;
5 import de.hacksaar.javatuer.InteractiveLogin;
6 import de.hacksaar.javatuer.TyshellClient;
8 import java.util.LinkedList;
9 import java.util.Queue;
11 class AsyncTyshell extends AsyncTask<Void, Void, Void> {
13 private static final String TAG = "AsyncTyshell";
14 private final String hostname;
15 private final int port;
16 private final String username;
17 private final String keyFile;
18 private final Prompter prompter;
19 private final Queue<String> messages = new LinkedList<>();
20 private final Object promptWait = new Object();
21 private String promptResultString;
22 private boolean promptResultBoolean;
23 private boolean disconnect = true;
25 AsyncTyshell(String hostname, int port, String username, String keyFile, Prompter prompter) {
26 this.hostname = hostname;
28 this.username = username;
29 this.keyFile = keyFile;
30 this.prompter = prompter;
33 private void awaitPrompt() {
34 synchronized (promptWait) {
37 } catch (InterruptedException e) {
43 public void disconnect() {
45 synchronized (promptWait) {
48 synchronized (messages) {
54 protected Void doInBackground(Void... voids) {
55 TyshellClient client = new TyshellClient(hostname, port, new AndroidLogging());
56 client.connect(username, keyFile, new AsyncInteractiveLogin());
59 synchronized (messages) {
60 while (disconnect && (msg = messages.poll()) == null) {
63 } catch (InterruptedException e) {
71 client.sendCommand(msg);
77 public void promptResult(boolean result) {
78 promptResultBoolean = result;
79 synchronized (promptWait) {
84 public void promptResult(String result) {
85 promptResultString = result;
86 synchronized (promptWait) {
91 public void sendCommand(String string) {
92 synchronized (messages) {
98 public interface Prompter {
99 void promptBoolean(String message);
101 void promptString(String message);
103 void sendMessage(String message);
106 private class AsyncInteractiveLogin extends InteractiveLogin {
108 AsyncInteractiveLogin() {
112 public String promptKeyPassphrase(String question) {
113 prompter.promptString(question);
115 return promptResultString;
119 public String promptUserPassword(String question) {
120 prompter.promptString(question);
122 return promptResultString;
126 public boolean promptYesNo(String question) {
127 prompter.promptBoolean(question);
129 return promptResultBoolean;
133 public void showMessage(String s) {
134 prompter.sendMessage(s);