Ralfs Makefileänderung
[saartuer.git] / AndTuer / src / de / hacksaar / andtuer / AsyncTyshell.java
1 package de.hacksaar.andtuer;
2
3 import android.os.AsyncTask;
4 import android.util.Log;
5 import de.hacksaar.javatuer.InteractiveLogin;
6 import de.hacksaar.javatuer.TyshellClient;
7
8 import java.util.LinkedList;
9 import java.util.Queue;
10
11 class AsyncTyshell extends AsyncTask<Void, Void, Void> {
12
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;
24
25         AsyncTyshell(String hostname, int port, String username, String keyFile, Prompter prompter) {
26                 this.hostname = hostname;
27                 this.port = port;
28                 this.username = username;
29                 this.keyFile = keyFile;
30                 this.prompter = prompter;
31         }
32
33         private void awaitPrompt() {
34                 synchronized (promptWait) {
35                         try {
36                                 promptWait.wait();
37                         } catch (InterruptedException e) {
38                                 Log.w(TAG, e);
39                         }
40                 }
41         }
42
43         public void disconnect() {
44                 disconnect = false;
45                 synchronized (promptWait) {
46                         promptWait.notify();
47                 }
48                 synchronized (messages) {
49                         messages.notify();
50                 }
51         }
52
53         @Override
54         protected Void doInBackground(Void... voids) {
55                 TyshellClient client = new TyshellClient(hostname, port, new AndroidLogging());
56                 client.connect(username, keyFile, new AsyncInteractiveLogin());
57                 while (disconnect) {
58                         String msg = null;
59                         synchronized (messages) {
60                                 while (disconnect && (msg = messages.poll()) == null) {
61                                         try {
62                                                 messages.wait();
63                                         } catch (InterruptedException e) {
64                                                 return null;
65                                         }
66                                 }
67                         }
68                         if (msg == null) {
69                                 break;
70                         }
71                         client.sendCommand(msg);
72                 }
73                 client.disconnect();
74                 return null;
75         }
76
77         public void promptResult(boolean result) {
78                 promptResultBoolean = result;
79                 synchronized (promptWait) {
80                         promptWait.notify();
81                 }
82         }
83
84         public void promptResult(String result) {
85                 promptResultString = result;
86                 synchronized (promptWait) {
87                         promptWait.notify();
88                 }
89         }
90
91         public void sendCommand(String string) {
92                 synchronized (messages) {
93                         messages.add(string);
94                         messages.notify();
95                 }
96         }
97
98         public interface Prompter {
99                 void promptBoolean(String message);
100
101                 void promptString(String message);
102
103                 void sendMessage(String message);
104         }
105
106         private class AsyncInteractiveLogin extends InteractiveLogin {
107
108                 AsyncInteractiveLogin() {
109                 }
110
111                 @Override
112                 public String promptKeyPassphrase(String question) {
113                         prompter.promptString(question);
114                         awaitPrompt();
115                         return promptResultString;
116                 }
117
118                 @Override
119                 public String promptUserPassword(String question) {
120                         prompter.promptString(question);
121                         awaitPrompt();
122                         return promptResultString;
123                 }
124
125                 @Override
126                 public boolean promptYesNo(String question) {
127                         prompter.promptBoolean(question);
128                         awaitPrompt();
129                         return promptResultBoolean;
130                 }
131
132                 @Override
133                 public void showMessage(String s) {
134                         prompter.sendMessage(s);
135                 }
136         }
137 }