X-Git-Url: https://git.ralfj.de/saartuer.git/blobdiff_plain/a89547dadb2928083b7bd859f99cca9c8c61f74b..5bceb6534d5f640dcdd4d5015cecfbad4e82a7b7:/AndTuer/src/de/hacksaar/andtuer/AsyncTyshell.java?ds=sidebyside diff --git a/AndTuer/src/de/hacksaar/andtuer/AsyncTyshell.java b/AndTuer/src/de/hacksaar/andtuer/AsyncTyshell.java new file mode 100644 index 0000000..4dcdcaf --- /dev/null +++ b/AndTuer/src/de/hacksaar/andtuer/AsyncTyshell.java @@ -0,0 +1,137 @@ +package de.hacksaar.andtuer; + +import android.os.AsyncTask; +import android.util.Log; +import de.hacksaar.javatuer.InteractiveLogin; +import de.hacksaar.javatuer.TyshellClient; + +import java.util.LinkedList; +import java.util.Queue; + +class AsyncTyshell extends AsyncTask { + + private static final String TAG = "AsyncTyshell"; + private final String hostname; + private final int port; + private final String username; + private final String keyFile; + private final Prompter prompter; + private final Queue messages = new LinkedList<>(); + private final Object promptWait = new Object(); + private String promptResultString; + private boolean promptResultBoolean; + private boolean disconnect = true; + + AsyncTyshell(String hostname, int port, String username, String keyFile, Prompter prompter) { + this.hostname = hostname; + this.port = port; + this.username = username; + this.keyFile = keyFile; + this.prompter = prompter; + } + + private void awaitPrompt() { + synchronized (promptWait) { + try { + promptWait.wait(); + } catch (InterruptedException e) { + Log.w(TAG, e); + } + } + } + + public void disconnect() { + disconnect = false; + synchronized (promptWait) { + promptWait.notify(); + } + synchronized (messages) { + messages.notify(); + } + } + + @Override + protected Void doInBackground(Void... voids) { + TyshellClient client = new TyshellClient(hostname, port, new AndroidLogging()); + client.connect(username, keyFile, new AsyncInteractiveLogin()); + while (disconnect) { + String msg = null; + synchronized (messages) { + while (disconnect && (msg = messages.poll()) == null) { + try { + messages.wait(); + } catch (InterruptedException e) { + return null; + } + } + } + if (msg == null) { + break; + } + client.sendCommand(msg); + } + client.disconnect(); + return null; + } + + public void promptResult(boolean result) { + promptResultBoolean = result; + synchronized (promptWait) { + promptWait.notify(); + } + } + + public void promptResult(String result) { + promptResultString = result; + synchronized (promptWait) { + promptWait.notify(); + } + } + + public void sendCommand(String string) { + synchronized (messages) { + messages.add(string); + messages.notify(); + } + } + + public interface Prompter { + void promptBoolean(String message); + + void promptString(String message); + + void sendMessage(String message); + } + + private class AsyncInteractiveLogin extends InteractiveLogin { + + AsyncInteractiveLogin() { + } + + @Override + public String promptKeyPassphrase(String question) { + prompter.promptString(question); + awaitPrompt(); + return promptResultString; + } + + @Override + public String promptUserPassword(String question) { + prompter.promptString(question); + awaitPrompt(); + return promptResultString; + } + + @Override + public boolean promptYesNo(String question) { + prompter.promptBoolean(question); + awaitPrompt(); + return promptResultBoolean; + } + + @Override + public void showMessage(String s) { + prompter.sendMessage(s); + } + } +}