comparison app/src/main/java/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/com/five_ten_sg/connectbot/service/TerminalMonitor.java@b284b8f9e535
children 105815cce146
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
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.net.Uri;
8 import android.os.IBinder;
9 import android.util.Log;
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.Arrays;
19 import java.util.HashMap;
20 import java.util.concurrent.ArrayBlockingQueue;
21 import java.util.concurrent.BlockingQueue;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import com.five_ten_sg.connectbot.ConsoleActivity;
25 import com.five_ten_sg.connectbot.bean.HostBean;
26
27
28 public class TerminalMonitor {
29 public final static String TAG = "ConnectBot.TerminalMonitor";
30
31 public static final char MONITOR_CMD_INIT = 0;
32 public static final char MONITOR_CMD_ACTIVATE = 1;
33 public static final char MONITOR_CMD_KEYSTATE = 2;
34 public static final char MONITOR_CMD_CURSORMOVE = 3;
35 public static final char MONITOR_CMD_SCREENCHANGE = 4;
36 public static final char MONITOR_CMD_FIELDVALUE = 5;
37 public static final char MONITOR_CMD_SETFIELD = 5;
38 public static final char MONITOR_CMD_GETFIELD = 6;
39 public static final char MONITOR_CMD_SCREENWATCH = 7;
40 public static final char MONITOR_CMD_DEPRESS = 8;
41 public static final char MONITOR_CMD_SHOWURL = 9;
42 public static final char MONITOR_CMD_SWITCHSESSION = 10;
43 public static final char MONITOR_CMD_CURSORREQUEST = 11;
44
45 public static final String[] commands = {
46 "cmd_init",
47 "cmd_activate",
48 "cmd_keystate",
49 "cmd_cursormove",
50 "cmd_screenchange",
51 "cmd_fieldvalue/setfield",
52 "cmd_getfield",
53 "cmd_screenwatch",
54 "cmd_depress",
55 "cmd_showurl",
56 "cmd_switchsession",
57 "cmd_cursorrequest"
58 };
59
60 public static final char CURSOR_REQUESTED = 0;
61 public static final char CURSOR_SCREEN_CHANGE = 1;
62 public static final char CURSOR_USER_KEY = 2;
63
64 private static final int MONITORPORT = 6000;
65 private static final String LOCALHOST = "127.0.0.1";
66
67 private Context parent = null;
68 private vt320 buffer = null;
69 private View view = null;
70 private HostBean host = null;
71 private String init = null;
72 private int start_line = 0; // monitor part of the screen for changes
73 private int end_line = 500; // ""
74 private int start_column = 0; // ""
75 private int end_column = 500; // ""
76 private boolean modified = false; // used to delay screen change notifications
77 private boolean moved = false; // used to delay cursor moved notifications
78 private int to_line = 0; // ""
79 private int to_column = 0; // ""
80 private HashMap<Integer, Integer> keymap = null; // map MS VK_ keys to vt320 virtual keys
81 private IBinder bound = null;
82 private Socket monitor_socket = null;
83 private InputStream monitor_in = null;
84 private OutputStream monitor_out = null;
85 private MyReader monitor_reader = null;
86 private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(100);
87 private MyServiceConnection monitor_connection = new MyServiceConnection();
88
89 class MyReader extends Thread {
90 private InputStream monitor_in;
91 private byte[] b;
92 private boolean is_closing = false;
93
94 public MyReader(InputStream monitor_in) {
95 this.monitor_in = monitor_in;
96 b = new byte[100];
97 Log.i(TAG, "MyReader constructor");
98 }
99
100 public void closing() {
101 is_closing = true;
102 }
103
104 private char[] forceRead(int len) throws IOException {
105 int len2 = len * 2;
106 int off = 0;
107
108 if (b.length < len2) b = new byte[len2];
109
110 while (off < len2) {
111 int l = monitor_in.read(b, off, len2 - off);
112
113 if (l < 0) throw new IOException("eof");
114
115 off += l;
116 }
117
118 return bytesToChars(b, len2);
119 }
120
121 public void run() {
122 while (true) {
123 try {
124 char[] len = forceRead(1);
125 char[] packet = forceRead(len[0]);
126 char cmd = packet[0];
127 Log.i(TAG, String.format("received %s", commands[cmd]));
128
129 switch (cmd) {
130 case MONITOR_CMD_SETFIELD:
131 if (packet.length >= 3)
132 setField(packet[1], packet[2], packet, 3);
133
134 break;
135
136 case MONITOR_CMD_GETFIELD:
137 if (packet.length == 4)
138 getField(packet[1], packet[2], packet[3]);
139
140 break;
141
142 case MONITOR_CMD_SCREENWATCH:
143 if (packet.length == 4)
144 screenWatch(packet[1], packet[2], packet[3]);
145
146 break;
147
148 case MONITOR_CMD_DEPRESS:
149 if (packet.length == 2)
150 depress(packet[1]);
151
152 break;
153
154 case MONITOR_CMD_SHOWURL:
155 if (packet.length > 1)
156 showUrl(packet, 1);
157
158 break;
159
160 case MONITOR_CMD_SWITCHSESSION:
161 if (packet.length == 1)
162 switchSession();
163
164 break;
165
166 case MONITOR_CMD_CURSORREQUEST:
167 if (packet.length == 1)
168 cursorRequest();
169
170 break;
171
172 default:
173 break;
174 }
175 }
176 catch (IOException e) {
177 if (!is_closing) Log.e(TAG, "exception in MyReader.run()", e);
178
179 break;
180 }
181 }
182 }
183 }
184
185 class MyServiceConnection implements ServiceConnection {
186 public void onServiceConnected(ComponentName className, IBinder service) {
187 bound = service;
188 Log.i(TAG, "bound to service");
189
190 try {
191 InetAddress serverAddr = InetAddress.getByName(LOCALHOST);
192 int tries = 0;
193 while (tries < 10) {
194 try {
195 Thread.sleep(100);
196 monitor_socket = new Socket(serverAddr, MONITORPORT);
197 break;
198 }
199 catch (Exception e) {
200 monitor_socket = null;
201 Log.e(TAG, "exception connecting to monitor socket", e);
202 tries = tries + 1;
203 }
204 }
205 if (monitor_socket != null) {
206 Log.i(TAG, "connected to monitor socket, send init " + init);
207 monitor_in = monitor_socket.getInputStream();
208 monitor_out = monitor_socket.getOutputStream();
209 monitor_reader = new MyReader(monitor_in);
210 monitor_reader.start();
211 String x = " " + init;
212 monitorWrite(MONITOR_CMD_INIT, x.toCharArray());
213 char [] c;
214
215 while (true) {
216 c = pending_commands.poll();
217
218 if (c == null) break;
219
220 monitorWrite(c[1], c);
221 }
222 }
223 }
224 catch (IOException e) {
225 Log.e(TAG, "exception in onServiceConnected()", e);
226 }
227 }
228 public void onServiceDisconnected(ComponentName classNam) {
229 bound = null;
230 Log.i(TAG, "unbound from service");
231 }
232 };
233
234
235 public TerminalMonitor(Context parent, vt320 buffer, View view, HostBean host, String init) {
236 this.parent = parent;
237 this.buffer = buffer;
238 this.view = view;
239 this.host = host;
240 this.init = init;
241 // setup the windows->android keymapping
242 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731
243 keymap = new HashMap<Integer, Integer>();
244 keymap.put(0x08, vt320.KEY_BACK_SPACE); // vk_back
245 keymap.put(0x09, vt320.KEY_TAB); // vk_tab
246 keymap.put(0x0d, vt320.KEY_ENTER); // vk_return
247 keymap.put(0x1b, vt320.KEY_ESCAPE); // vk_escape
248 keymap.put(0x21, vt320.KEY_PAGE_UP); // vk_prior
249 keymap.put(0x22, vt320.KEY_PAGE_DOWN); // vk_next
250 keymap.put(0x23, vt320.KEY_END); // vk_end
251 keymap.put(0x24, vt320.KEY_HOME); // vk_home
252 keymap.put(0x25, vt320.KEY_LEFT); // vk_left
253 keymap.put(0x26, vt320.KEY_UP); // vk_up
254 keymap.put(0x27, vt320.KEY_RIGHT); // vk_right
255 keymap.put(0x28, vt320.KEY_DOWN); // vk_down
256 keymap.put(0x2d, vt320.KEY_INSERT); // vk_insert
257 keymap.put(0x2e, vt320.KEY_DELETE); // vk_delete
258 keymap.put(0x70, vt320.KEY_F1); // vk_f1
259 keymap.put(0x71, vt320.KEY_F2); // vk_f2
260 keymap.put(0x72, vt320.KEY_F3); // vk_f3
261 keymap.put(0x73, vt320.KEY_F4); // vk_f4
262 keymap.put(0x74, vt320.KEY_F5); // vk_f5
263 keymap.put(0x75, vt320.KEY_F6); // vk_f6
264 keymap.put(0x76, vt320.KEY_F7); // vk_f7
265 keymap.put(0x77, vt320.KEY_F8); // vk_f8
266 keymap.put(0x78, vt320.KEY_F9); // vk_f9
267 keymap.put(0x79, vt320.KEY_F10); // vk_f10
268 keymap.put(0x7a, vt320.KEY_F11); // vk_f11
269 keymap.put(0x7b, vt320.KEY_F12); // vk_f12
270 keymap.put(0x7c, vt320.KEY_F13); // vk_f13
271 keymap.put(0x7d, vt320.KEY_F14); // vk_f14
272 keymap.put(0x7e, vt320.KEY_F15); // vk_f15
273 keymap.put(0x7f, vt320.KEY_F16); // vk_f16
274 keymap.put(0x80, vt320.KEY_F17); // vk_f17
275 keymap.put(0x81, vt320.KEY_F18); // vk_f18
276 keymap.put(0x82, vt320.KEY_F19); // vk_f19
277 keymap.put(0x83, vt320.KEY_F20); // vk_f20
278 keymap.put(0x84, vt320.KEY_F21); // vk_f21
279 keymap.put(0x85, vt320.KEY_F22); // vk_f22
280 keymap.put(0x86, vt320.KEY_F23); // vk_f23
281 keymap.put(0x87, vt320.KEY_F24); // vk_f24
282 // bind to the monitor service
283 Intent intent = new Intent("com.five_ten_sg.connectbot.monitor.MonitorService");
284 parent.bindService(intent, monitor_connection, Context.BIND_AUTO_CREATE);
285 Log.i(TAG, "constructor");
286 }
287
288
289 public void Disconnect() {
290 if (monitor_reader != null) monitor_reader.closing();
291
292 try {
293 if (monitor_out != null) monitor_out.close();
294
295 if (monitor_in != null) monitor_in.close();
296
297 if (monitor_socket != null) monitor_socket.close();
298
299 Log.i(TAG, "disconnected from monitor socket");
300 }
301 catch (IOException e) {
302 Log.e(TAG, "exception in Disconnect() closing sockets", e);
303 }
304
305 monitor_reader = null;
306 monitor_out = null;
307 monitor_in = null;
308 monitor_socket = null;
309
310 if (bound != null) parent.unbindService(monitor_connection);
311
312 monitor_connection = null;
313 }
314
315
316 public char[] bytesToChars(byte[] b, int len) {
317 char[] c = new char[len >> 1];
318 int bp = 0;
319
320 for (int i = 0; i < c.length; i++) {
321 byte b1 = b[bp++];
322 byte b2 = b[bp++];
323 c[i] = (char)(((b1 & 0x00FF) << 8) + (b2 & 0x00FF));
324 }
325
326 return c;
327 }
328
329
330 public byte[] charsToBytes(char[] c) {
331 byte[] b = new byte[c.length << 1];
332 int bp = 0;
333
334 for (int i = 0; i < c.length; i++) {
335 b[bp++] = (byte)((c[i] & 0xff00) >> 8);
336 b[bp++] = (byte)(c[i] & 0x00ff);
337 }
338
339 return b;
340 }
341
342
343 public synchronized void monitorWrite(char cmd, char[] c) {
344 try {
345 if (monitor_out != null) {
346 c[0] = (char)(c.length - 1); // number of chars following
347 c[1] = cmd;
348 Log.i(TAG, String.format("sending %s", commands[cmd]));
349 monitor_out.write(charsToBytes(c));
350 monitor_out.flush();
351 }
352 else {
353 c[1] = cmd;
354 pending_commands.offer(c);
355 }
356 }
357 catch (IOException e) {
358 Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e);
359
360 try {
361 monitor_out.close();
362 }
363 catch (IOException ee) {
364 Log.e(TAG, "exception in monitorWrite() closing output stream", ee);
365 }
366
367 monitor_out = null;
368 }
369 };
370
371 public void resetWatch() {
372 start_line = 0;
373 end_line = 500;
374 start_column = 0;
375 end_column = 500;
376 };
377
378 public void sendScreen(char cmd) {
379 char lines = (char)(buffer.height & 0x0000ffff);
380 char columns = (char)(buffer.width & 0x0000ffff);
381 char[] arg = new char[4 + lines * columns];
382 arg[2] = lines;
383 arg[3] = columns;
384 int base = 4;
385
386 for (int i = 0; i < lines; i++) {
387 System.arraycopy(buffer.charArray[buffer.screenBase + i], 0, arg, base, columns);
388 base += columns;
389 }
390
391 monitorWrite(cmd, arg);
392 resetWatch();
393 }
394
395 public synchronized void activate() {
396 sendScreen(MONITOR_CMD_ACTIVATE);
397 cursorMoved(CURSOR_SCREEN_CHANGE);
398 }
399
400 public synchronized void keyState(boolean down) {
401 char[] arg = new char[3];
402 arg[2] = (char)((down) ? 1 : 0);
403 monitorWrite(MONITOR_CMD_KEYSTATE, arg);
404 }
405
406 public synchronized void cursorMove(int l, int c) {
407 if ((to_line != l) || (to_column != c)) moved = true;
408
409 to_line = l;
410 to_column = c;
411 }
412
413 public void cursorMoved(char why) {
414 char[] arg = new char[5];
415 arg[2] = (char)(to_line & 0x0000ffff);
416 arg[3] = (char)(to_column & 0x0000ffff);
417 arg[4] = why;
418 monitorWrite(MONITOR_CMD_CURSORMOVE, arg);
419 moved = false;
420 }
421
422 public void testMoved() {
423 if (moved) cursorMoved(CURSOR_USER_KEY);
424 }
425
426 public synchronized void testChanged() {
427 if (modified) {
428 modified = false;
429 sendScreen(MONITOR_CMD_SCREENCHANGE);
430 cursorMoved(CURSOR_SCREEN_CHANGE);
431 }
432 else {
433 if (moved) cursorMoved(CURSOR_SCREEN_CHANGE);
434 }
435 }
436
437 public synchronized void screenChanged(int llow, int lhigh, int clow, int chigh) {
438 if ((start_line <= lhigh) && (llow <= end_line) && (start_column <= chigh) && (clow <= end_column)) {
439 modified = true;
440 }
441 }
442
443 public synchronized void screenChanged(int l, int c) {
444 screenChanged(l, l, c, c);
445 }
446
447 public synchronized void setField(int l, int c, char[] data, int offset) {
448 int len = data.length - offset;
449 char[] da = new char[len];
450 System.arraycopy(data, offset, da, 0, len);
451 Log.i(TAG, String.format("setField(line %d, col %d, value %s)", l, c, new String(da)));
452
453 if ((l > 60000) || (c > 60000)) {
454 l = -1;
455 c = -1;
456 }
457 else {
458 // ignore setfield outside screen boundaries
459 if ((l >= buffer.height) || (c + len > buffer.width)) return;
460 }
461
462 buffer.setField(l, c, da);
463 }
464
465 public synchronized void showUrl(char [] data, int offset) {
466 char[] da = new char[data.length - offset];
467 System.arraycopy(data, offset, da, 0, data.length - offset);
468 String url = new String(da);
469 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
470 parent.startActivity(intent);
471 }
472
473 public synchronized void getField(int l, int c, int len) {
474 Log.i(TAG, String.format("getField(line %d, col %d, len %d)", l, c, len));
475 char[] arg2 = new char[4 + len];
476 arg2[2] = (char)(l & 0x0000ffff);
477 arg2[3] = (char)(c & 0x0000ffff);
478 int base = 4;
479
480 if ((l >= buffer.height) || (c + len > buffer.width)) {
481 Arrays.fill(arg2, base, base + len, ' ');
482 }
483 else {
484 System.arraycopy(buffer.charArray[buffer.screenBase + l], c, arg2, base, len);
485 }
486
487 char[] da = new char[len];
488 System.arraycopy(arg2, base, da, 0, len);
489 Log.i(TAG, String.format("getField value %s", new String(da)));
490
491 monitorWrite(MONITOR_CMD_FIELDVALUE, arg2);
492 }
493
494 public synchronized void screenWatch(int l, int c, int len) {
495 Log.i(TAG, String.format("screenWatch(line %d, col %d, len %d)", l, c, len));
496 start_line = l;
497 end_line = l;
498 start_column = c;
499 end_column = c + len - 1;
500 }
501
502 public synchronized void depress(int vk_key) {
503 Log.i(TAG, String.format("depress(%d)", vk_key));
504 Integer x = keymap.get(new Integer(vk_key));
505
506 if (x != null) buffer.keyDepressed(x, ' ', 0);
507 }
508
509 public synchronized void switchSession() {
510 Log.i(TAG, "switchSession()");
511 Intent intent = new Intent(parent, ConsoleActivity.class);
512 intent.setAction(Intent.ACTION_VIEW);
513 intent.setData(host.getUri());
514 parent.startActivity(intent);
515 }
516
517
518 public synchronized void cursorRequest() {
519 cursorMoved(CURSOR_REQUESTED);
520 }
521
522 }