Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 15:1588e359a972
queue pending monitor commands until socket is open
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 29 May 2014 12:40:27 -0700 |
parents | 0ce5cc452d02 |
children | 48a8daea9221 |
rev | line source |
---|---|
0 | 1 package com.five_ten_sg.connectbot.service; |
2 | |
3 import android.content.ComponentName; | |
4 import android.content.Context; | |
5 import android.content.Intent; | |
6 import android.content.ServiceConnection; | |
7 import android.os.IBinder; | |
8 import android.util.Log; | |
9 import android.view.KeyEvent; | |
10 import android.view.View; | |
11 import de.mud.terminal.vt320; | |
12 import java.io.IOException; | |
13 import java.io.InputStream; | |
14 import java.io.OutputStream; | |
15 import java.net.InetAddress; | |
16 import java.net.Socket; | |
17 import java.nio.charset.Charset; | |
18 import java.util.HashMap; | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
19 import java.util.concurrent.ArrayBlockingQueue; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
20 import java.util.concurrent.BlockingQueue; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
21 import java.util.concurrent.ConcurrentHashMap; |
0 | 22 |
23 public class TerminalMonitor { | |
24 public final static String TAG = "ConnectBot.TerminalMonitor"; | |
25 | |
26 public static final char MONITOR_CMD_INIT = 0; | |
27 public static final char MONITOR_CMD_ACTIVATE = 1; | |
28 public static final char MONITOR_CMD_HOSTDATA = 2; | |
29 public static final char MONITOR_CMD_CURSORMOVE = 3; | |
30 public static final char MONITOR_CMD_SCREENCHANGE = 4; | |
31 public static final char MONITOR_CMD_FIELDVALUE = 5; | |
32 public static final char MONITOR_CMD_SETFIELD = 5; | |
33 public static final char MONITOR_CMD_GETFIELD = 6; | |
34 public static final char MONITOR_CMD_SCREENWATCH = 7; | |
35 public static final char MONITOR_CMD_DEPRESS = 8; | |
36 | |
37 private static final int MONITORPORT = 6000; | |
38 private static final String LOCALHOST = "127.0.0.1"; | |
39 | |
40 private Context parent = null; | |
41 private vt320 buffer = null; | |
42 private TerminalKeyListener keyListener = null; | |
43 private View view = null; | |
44 private String init = null; | |
45 private int start_line = 0; // monitor part of the screen for changes | |
46 private int end_line = 500; // "" | |
47 private int start_column = 0; // "" | |
48 private int end_column = 500; // "" | |
49 private boolean modified = false; // "" | |
50 private HashMap<Integer,Integer> keymap = null; | |
51 private IBinder bound = null; | |
52 private Socket monitor_socket = null; | |
53 private InputStream monitor_in = null; | |
54 private OutputStream monitor_out = null; | |
55 private MyReader monitor_reader = null; | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
56 private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(100); |
0 | 57 private MyServiceConnection monitor_connection = new MyServiceConnection(); |
58 | |
59 class MyReader extends Thread { | |
60 private InputStream monitor_in; | |
61 private byte[] b; | |
62 private boolean is_closing = false; | |
63 | |
64 public MyReader(InputStream monitor_in) { | |
65 this.monitor_in = monitor_in; | |
66 b = new byte[100]; | |
67 Log.i(TAG, "MyReader constructor"); | |
68 } | |
69 | |
70 public void closing() { | |
71 is_closing = true; | |
72 } | |
73 | |
74 private char[] forceRead(int len) throws IOException { | |
75 int len2 = len * 2; | |
76 int off = 0; | |
77 | |
78 if (b.length < len2) b = new byte[len2]; | |
79 | |
80 while (off < len2) { | |
81 int l = monitor_in.read(b, off, len2 - off); | |
82 | |
83 if (l < 0) throw new IOException("eof"); | |
84 | |
85 off += l; | |
86 } | |
87 | |
88 return bytesToChars(b, len2); | |
89 } | |
90 | |
91 public void run() { | |
92 while (true) { | |
93 try { | |
94 char[] len = forceRead(1); | |
95 char[] packet = forceRead(len[0]); | |
96 char cmd = packet[0]; | |
97 Log.i(TAG, String.format("received %d command", (int)cmd)); | |
98 | |
99 switch (cmd) { | |
100 case MONITOR_CMD_SETFIELD: | |
101 if (packet.length >= 4) | |
102 setField(packet[1], packet[2], packet, 3); | |
103 break; | |
104 | |
105 case MONITOR_CMD_GETFIELD: | |
106 if (packet.length == 4) | |
107 getField(packet[1], packet[2], packet[3]); | |
108 break; | |
109 | |
110 case MONITOR_CMD_SCREENWATCH: | |
111 if (packet.length == 4) | |
112 screenWatch(packet[1], packet[2], packet[3]); | |
113 break; | |
114 | |
115 case MONITOR_CMD_DEPRESS: | |
116 if (packet.length == 2) | |
117 depress(packet[1]); | |
118 break; | |
119 | |
120 default: | |
121 break; | |
122 } | |
123 } | |
124 catch (IOException e) { | |
125 if (!is_closing) Log.e(TAG, "exception in MyReader.run()", e); | |
126 | |
127 break; | |
128 } | |
129 } | |
130 } | |
131 } | |
132 | |
133 class MyServiceConnection implements ServiceConnection { | |
134 public void onServiceConnected(ComponentName className, IBinder service) { | |
135 bound = service; | |
136 Log.i(TAG, "bound to service"); | |
137 | |
138 try { | |
139 InetAddress serverAddr = InetAddress.getByName(LOCALHOST); | |
140 monitor_socket = new Socket(serverAddr, MONITORPORT); | |
141 monitor_in = monitor_socket.getInputStream(); | |
142 monitor_out = monitor_socket.getOutputStream(); | |
143 Log.i(TAG, "connected to monitor socket, send init " + init); | |
144 monitor_reader = new MyReader(monitor_in); | |
145 monitor_reader.start(); | |
146 String x = " " + init; | |
147 monitorWrite(MONITOR_CMD_INIT, x.toCharArray()); | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
148 char [] c; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
149 while (true) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
150 c = pending_commands.poll(); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
151 if (c == null) break; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
152 monitorWrite(c[1], c); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
153 } |
0 | 154 } |
155 catch (IOException e) { | |
156 Log.e(TAG, "exception in onServiceConnected()", e); | |
157 } | |
158 } | |
159 public void onServiceDisconnected(ComponentName classNam) { | |
160 bound = null; | |
161 Log.i(TAG, "unbound from service"); | |
162 } | |
163 }; | |
164 | |
165 | |
166 public TerminalMonitor(Context parent, vt320 buffer, TerminalKeyListener keyListener, View view, String init) { | |
167 this.parent = parent; | |
168 this.buffer = buffer; | |
169 this.keyListener = keyListener; | |
170 this.view = view; | |
171 this.init = init; | |
172 | |
173 // setup the windows->android keymapping | |
174 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx | |
175 // http://developer.android.com/reference/android/view/KeyEvent.html | |
176 keymap = new HashMap<Integer,Integer>(); | |
177 keymap.put(0x08, KeyEvent.KEYCODE_BACK); // vk_back | |
178 keymap.put(0x09, KeyEvent.KEYCODE_TAB); // vk_tab | |
179 keymap.put(0x0d, KeyEvent.KEYCODE_ENTER); // vk_return | |
180 keymap.put(0x1b, KeyEvent.KEYCODE_ESCAPE); // vk_escape | |
181 keymap.put(0x21, KeyEvent.KEYCODE_PAGE_UP); // vk_prior | |
182 keymap.put(0x22, KeyEvent.KEYCODE_PAGE_DOWN); // vk_next | |
183 keymap.put(0x23, KeyEvent.KEYCODE_MOVE_END); // vk_end | |
184 keymap.put(0x24, KeyEvent.KEYCODE_MOVE_HOME); // vk_home | |
185 keymap.put(0x25, KeyEvent.KEYCODE_DPAD_LEFT); // vk_left | |
186 keymap.put(0x26, KeyEvent.KEYCODE_DPAD_UP); // vk_up | |
187 keymap.put(0x27, KeyEvent.KEYCODE_DPAD_RIGHT); // vk_right | |
188 keymap.put(0x28, KeyEvent.KEYCODE_DPAD_DOWN); // vk_down | |
189 keymap.put(0x2d, KeyEvent.KEYCODE_INSERT); // vk_insert | |
190 keymap.put(0x2e, KeyEvent.KEYCODE_DEL); // vk_delete | |
191 keymap.put(0x70, KeyEvent.KEYCODE_F1); // vk_f1 | |
192 keymap.put(0x71, KeyEvent.KEYCODE_F2); // vk_f2 | |
193 keymap.put(0x72, KeyEvent.KEYCODE_F3); // vk_f3 | |
194 keymap.put(0x73, KeyEvent.KEYCODE_F4); // vk_f4 | |
195 keymap.put(0x74, KeyEvent.KEYCODE_F5); // vk_f5 | |
196 keymap.put(0x75, KeyEvent.KEYCODE_F6); // vk_f6 | |
197 keymap.put(0x76, KeyEvent.KEYCODE_F7); // vk_f7 | |
198 keymap.put(0x77, KeyEvent.KEYCODE_F8); // vk_f8 | |
199 keymap.put(0x78, KeyEvent.KEYCODE_F9); // vk_f9 | |
200 keymap.put(0x79, KeyEvent.KEYCODE_F10); // vk_f10 | |
201 keymap.put(0x7a, KeyEvent.KEYCODE_F11); // vk_f11 | |
202 keymap.put(0x7b, KeyEvent.KEYCODE_F12); // vk_f12 | |
203 | |
204 // bind to the monitor service | |
205 Intent intent = new Intent("com.five_ten_sg.connectbot.monitor.MonitorService"); | |
206 parent.bindService(intent, monitor_connection, Context.BIND_AUTO_CREATE); | |
207 Log.i(TAG, "constructor"); | |
208 } | |
209 | |
210 | |
211 public void Disconnect() { | |
212 if (monitor_reader != null) monitor_reader.closing(); | |
213 | |
214 try { | |
215 if (monitor_out != null) monitor_out.close(); | |
216 | |
217 if (monitor_in != null) monitor_in.close(); | |
218 | |
219 if (monitor_socket != null) monitor_socket.close(); | |
220 | |
221 Log.i(TAG, "disconnected from monitor socket"); | |
222 } | |
223 catch (IOException e) { | |
224 Log.e(TAG, "exception in Disconnect() closing sockets", e); | |
225 } | |
226 | |
227 monitor_reader = null; | |
228 monitor_out = null; | |
229 monitor_in = null; | |
230 monitor_socket = null; | |
231 | |
232 if (bound != null) parent.unbindService(monitor_connection); | |
233 | |
234 monitor_connection = null; | |
235 } | |
236 | |
237 | |
238 public char[] bytesToChars(byte[] b, int len) { | |
239 char[] c = new char[len >> 1]; | |
240 int bp = 0; | |
241 | |
242 for (int i = 0; i < c.length; i++) { | |
243 byte b1 = b[bp++]; | |
244 byte b2 = b[bp++]; | |
245 c[i] = (char)(((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
246 } | |
247 | |
248 return c; | |
249 } | |
250 | |
251 | |
252 public byte[] charsToBytes(char[] c) { | |
253 byte[] b = new byte[c.length << 1]; | |
254 int bp = 0; | |
255 | |
256 for (int i = 0; i < c.length; i++) { | |
257 b[bp++] = (byte)((c[i] & 0xff00) >> 8); | |
258 b[bp++] = (byte)(c[i] & 0x00ff); | |
259 } | |
260 | |
261 return b; | |
262 } | |
263 | |
264 | |
265 public synchronized void monitorWrite(char cmd, char[] c) { | |
266 try { | |
267 if (monitor_out != null) { | |
268 c[0] = (char)(c.length - 1); // number of chars following | |
269 c[1] = cmd; | |
270 //Log.i(TAG, String.format("sending %d command", (int)cmd)); | |
271 monitor_out.write(charsToBytes(c)); | |
272 } | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
273 else { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
274 c[1] = cmd; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
275 pending_commands.put(c); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
276 } |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
277 } |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
278 catch (InterruptedException e) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
279 Log.e(TAG, "exception in monitorWrite()", e); |
0 | 280 } |
281 catch (IOException e) { | |
282 Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e); | |
283 | |
284 try { | |
285 monitor_out.close(); | |
286 } | |
287 catch (IOException ee) { | |
288 Log.e(TAG, "exception in monitorWrite() closing output stream", ee); | |
289 } | |
290 | |
291 monitor_out = null; | |
292 } | |
293 }; | |
294 | |
295 public void sendScreen(char cmd) { | |
296 char lines = (char)(buffer.height & 0x0000ffff); | |
297 char columns = (char)(buffer.width & 0x0000ffff); | |
298 char[] arg = new char[4 + lines * columns]; | |
299 arg[2] = lines; | |
300 arg[3] = columns; | |
301 int base = 4; | |
302 for (int i = 0; i < lines; i++) { | |
303 System.arraycopy(buffer.charArray[buffer.screenBase + i], 0, arg, base, columns); | |
304 base += columns; | |
305 } | |
306 monitorWrite(cmd, arg); | |
307 } | |
308 | |
309 public synchronized void activate() { | |
310 sendScreen(MONITOR_CMD_ACTIVATE); | |
311 } | |
312 | |
313 public synchronized void hostData(byte[] b) { | |
314 for (int i = 0; i < b.length; i++) { | |
315 hostData((int)b[i] & 0xff); | |
316 } | |
317 } | |
318 | |
319 public synchronized void hostData(int b) { | |
320 char[] arg = new char[3]; | |
321 arg[2] = (char)(b & 0x0000ffff); | |
322 monitorWrite(MONITOR_CMD_HOSTDATA, arg); | |
323 } | |
324 | |
325 public synchronized void cursorMove(int l, int c) { | |
326 char[] arg = new char[4]; | |
327 arg[2] = (char)(l & 0x0000ffff); | |
328 arg[3] = (char)(c & 0x0000ffff); | |
329 monitorWrite(MONITOR_CMD_CURSORMOVE, arg); | |
330 } | |
331 | |
332 public synchronized void testChanged() { | |
333 if (modified) { | |
334 modified = false; | |
335 sendScreen(MONITOR_CMD_SCREENCHANGE); | |
336 } | |
337 } | |
338 | |
339 public synchronized void screenChanged(int llow, int lhigh, int clow, int chigh) { | |
340 if ((start_line <= lhigh) && (llow <= end_line) && (start_column <= chigh) && (clow <= end_column)) { | |
341 modified = true; | |
342 } | |
343 } | |
344 | |
345 public synchronized void screenChanged(int l, int c) { | |
346 screenChanged(l, l, c, c); | |
347 } | |
348 | |
349 public synchronized void setField(int l, int c, char[] data, int offset) { | |
350 Log.i(TAG, "setField()"); | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
351 byte[] b = new byte[data.length - offset]; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
352 int i; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
353 for (i=0; i<b.length; i++) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
354 b[i] = (byte)(data[i+offset] & 0x00ff); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
355 } |
0 | 356 buffer.write(b); |
357 } | |
358 | |
359 public synchronized void getField(int l, int c, int len) { | |
360 Log.i(TAG, "getField()"); | |
361 char[] arg2 = new char[4 + len]; | |
362 arg2[2] = (char)(l & 0x0000ffff); | |
363 arg2[3] = (char)(c & 0x0000ffff); | |
364 int base = 4; | |
365 System.arraycopy(buffer.charArray[buffer.screenBase + l], c, arg2, base, len); | |
366 monitorWrite(MONITOR_CMD_FIELDVALUE, arg2); | |
367 } | |
368 | |
369 public synchronized void screenWatch(int l, int c, int len) { | |
370 Log.i(TAG, "screenWatch()"); | |
371 start_line = l; | |
372 end_line = l; | |
373 start_column = c; | |
374 end_column = c + len - 1; | |
375 } | |
376 | |
377 public synchronized void depress(int vk_key) { | |
378 Log.i(TAG, String.format("depress() %d", vk_key)); | |
379 Integer x = keymap.get(new Integer(vk_key)); | |
380 if (x != null) { | |
381 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, x.intValue()); | |
382 keyListener.onKey(view, event.getKeyCode(), event); | |
383 } | |
384 } | |
385 } |