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