comparison src/com/five_ten_sg/connectbot/service/TerminalBridge.java @ 42:7ac846a07ed4 tn5250

start tn5250 integration
author Carl Byington <carl@five-ten-sg.com>
date Wed, 11 Jun 2014 09:14:31 -0700
parents ecdbff49182c
children 80dcebe51af2
comparison
equal deleted inserted replaced
41:9621ac4dd5eb 42:7ac846a07ed4
94 final Paint defaultPaint; 94 final Paint defaultPaint;
95 95
96 private Relay relay; 96 private Relay relay;
97 97
98 private final String emulation; 98 private final String emulation;
99 private final int scrollback;
100 99
101 public Bitmap bitmap = null; 100 public Bitmap bitmap = null;
102 public vt320 buffer = null; 101 public vt320 buffer = null;
103 102
104 private TerminalView parent = null; 103 private TerminalView parent = null;
118 private final SelectionArea selectionArea; 117 private final SelectionArea selectionArea;
119 118
120 // TODO add support for the new clipboard API 119 // TODO add support for the new clipboard API
121 private ClipboardManager clipboard; 120 private ClipboardManager clipboard;
122 121
123 public int charWidth = -1; 122 public int charWidth = -1;
124 public int charHeight = -1; 123 public int charHeight = -1;
125 private int charTop = -1; 124 private int charTop = -1;
126 125 private float fontSize = -1;
127 private float fontSize = -1;
128 126
129 private final List<FontSizeChangedListener> fontSizeChangedListeners; 127 private final List<FontSizeChangedListener> fontSizeChangedListeners;
130 128
131 private final List<String> localOutput; 129 private final List<String> localOutput;
132 130
158 }; 156 };
159 emulation = null; 157 emulation = null;
160 manager = null; 158 manager = null;
161 defaultPaint = new Paint(); 159 defaultPaint = new Paint();
162 selectionArea = new SelectionArea(); 160 selectionArea = new SelectionArea();
163 scrollback = 1;
164 localOutput = new LinkedList<String>(); 161 localOutput = new LinkedList<String>();
165 fontSizeChangedListeners = new LinkedList<FontSizeChangedListener>(); 162 fontSizeChangedListeners = new LinkedList<FontSizeChangedListener>();
166 transport = null; 163 transport = null;
167 keyListener = new TerminalKeyListener(manager, this, buffer, null); 164 keyListener = new TerminalKeyListener(manager, this, buffer, null);
168 monitor = null; 165 monitor = null;
169 } 166 }
170 167
171 /** 168 /**
172 * Create new terminal bridge with following parameters. We will immediately 169 * Create new terminal bridge with following parameters.
173 * launch thread to start SSH connection and handle any hostkey verification
174 * and password authentication.
175 */ 170 */
176 public TerminalBridge(final TerminalManager manager, final HostBean host) throws IOException { 171 public TerminalBridge(final TerminalManager manager, final HostBean host) throws IOException {
177 float hostFontSize; 172 float hostFontSize;
178 this.manager = manager; 173 this.manager = manager;
179 this.host = host; 174 this.host = host;
180 emulation = manager.getEmulation(); 175 emulation = manager.getEmulation();
181 scrollback = manager.getScrollback();
182 // create prompt helper to relay password and hostkey requests up to gui 176 // create prompt helper to relay password and hostkey requests up to gui
183 promptHelper = new PromptHelper(this); 177 promptHelper = new PromptHelper(this);
184 // create our default paint 178 // create our default paint
185 defaultPaint = new Paint(); 179 defaultPaint = new Paint();
186 defaultPaint.setAntiAlias(true); 180 defaultPaint.setAntiAlias(true);
189 localOutput = new LinkedList<String>(); 183 localOutput = new LinkedList<String>();
190 fontSizeChangedListeners = new LinkedList<FontSizeChangedListener>(); 184 fontSizeChangedListeners = new LinkedList<FontSizeChangedListener>();
191 Integer defaultFontSize = Integer.parseInt(manager.prefs.getString(PreferenceConstants.DEFAULT_FONT_SIZE, "-1")); 185 Integer defaultFontSize = Integer.parseInt(manager.prefs.getString(PreferenceConstants.DEFAULT_FONT_SIZE, "-1"));
192 Log.i(TAG, "fontSize: " + this.fontSize + ", defaultFontSize: " + defaultFontSize); 186 Log.i(TAG, "fontSize: " + this.fontSize + ", defaultFontSize: " + defaultFontSize);
193 187
194 if (this.fontSize == -1) { 188 if (fontSize == -1) {
195 if (defaultFontSize > 0 && host.getFontSize() == -1) 189 if (defaultFontSize > 0 && host.getFontSize() == -1)
196 hostFontSize = defaultFontSize; 190 hostFontSize = defaultFontSize;
197 else 191 else
198 hostFontSize = host.getFontSize(); 192 hostFontSize = host.getFontSize();
199 } 193 }
200 else 194 else
201 hostFontSize = this.fontSize; 195 hostFontSize = fontSize;
202 196
203 if (hostFontSize <= 0) 197 if (hostFontSize <= 0) hostFontSize = DEFAULT_FONT_SIZE;
204 hostFontSize = DEFAULT_FONT_SIZE;
205 198
206 setFontSize(hostFontSize); 199 setFontSize(hostFontSize);
207 // create terminal buffer and handle outgoing data
208 // this is probably status reply information
209 buffer = new vt320() {
210 @Override
211 public void debug(String s) {
212 Log.d(TAG, s);
213 }
214 @Override
215 public void write(byte[] b) {
216 try {
217 if (b != null && transport != null)
218 if (monitor != null) monitor.hostData(b);
219
220 transport.write(b);
221 }
222 catch (IOException e) {
223 Log.e(TAG, "Problem writing outgoing data in vt320() thread", e);
224 }
225 }
226 @Override
227 public void write(int b) {
228 try {
229 if (transport != null)
230 if (monitor != null) monitor.hostData(b);
231
232 transport.write(b);
233 }
234 catch (IOException e) {
235 Log.e(TAG, "Problem writing outgoing data in vt320() thread", e);
236 }
237 }
238 // We don't use telnet sequences.
239 @Override
240 public void sendTelnetCommand(byte cmd) {
241 }
242 // We don't want remote to resize our window.
243 @Override
244 public void setWindowSize(int c, int r) {
245 }
246 // test for changed screen contents
247 @Override
248 public void testChanged() {
249 if (monitor != null) monitor.testChanged();
250 }
251 // play beep noise
252 @Override
253 public void beep() {
254 if (parent.isShown())
255 manager.playBeep();
256 else
257 manager.sendActivityNotification(host);
258 }
259 // monitor placement of new characters
260 @Override
261 public void putChar(int c, int l, char ch, int attributes) {
262 if (monitor != null) monitor.screenChanged(l, c);
263
264 super.putChar(c, l, ch, attributes);
265 }
266 @Override
267 public void insertChar(int c, int l, char ch, int attributes) {
268 if (monitor != null) monitor.screenChanged(l, l, c, width - 1);
269
270 super.insertChar(c, l, ch, attributes);
271 }
272 @Override
273 public void insertLine(int l, int n, boolean scrollDown) {
274 if (monitor != null) {
275 if (scrollDown) monitor.screenChanged(l, height - 1, 0, width - 1);
276 else monitor.screenChanged(0, l, 0, width - 1);
277 }
278
279 super.insertLine(l, n, scrollDown);
280 }
281 @Override
282 public void deleteLine(int l) {
283 if (monitor != null) monitor.screenChanged(l, height - 1, 0, width - 1);
284
285 super.deleteLine(l);
286 }
287 @Override
288 public void deleteChar(int c, int l) {
289 if (monitor != null) monitor.screenChanged(l, l, c, width - 1);
290
291 super.deleteChar(c, l);
292 }
293 @Override
294 public void setCursorPosition(int c, int l) {
295 if (monitor != null) monitor.cursorMove(l, c);
296
297 super.setCursorPosition(c, l);
298 }
299 };
300
301 // Don't keep any scrollback if a session is not being opened.
302 if (host.getWantSession())
303 buffer.setBufferSize(scrollback);
304 else
305 buffer.setBufferSize(0);
306
307 resetColors(); 200 resetColors();
308 buffer.setDisplay(this);
309 selectionArea = new SelectionArea(); 201 selectionArea = new SelectionArea();
310 } 202 }
311 203
312 public PromptHelper getPromptHelper() { 204 public PromptHelper getPromptHelper() {
313 return promptHelper; 205 return promptHelper;
316 /** 208 /**
317 * Spawn thread to open connection and start login process. 209 * Spawn thread to open connection and start login process.
318 */ 210 */
319 protected void startConnection() { 211 protected void startConnection() {
320 transport = TransportFactory.getTransport(host.getProtocol()); 212 transport = TransportFactory.getTransport(host.getProtocol());
321 transport.setLinks(manager, this, host, buffer, emulation); 213 transport.setLinks(manager, this, host, emulation);
322 214 buffer = transport.getTransportBuffer();
323 keyListener = transport.getTerminalKeyListener(); 215 keyListener = transport.getTerminalKeyListener();
324 216
325 String monitor_init = host.getMonitor(); 217 String monitor_init = host.getMonitor();
326 if ((monitor_init != null) && (monitor_init.length() > 0)) { 218 if ((monitor_init != null) && (monitor_init.length() > 0)) {
327 monitor = new TerminalMonitor(manager, buffer, keyListener, parent, monitor_init); 219 monitor = new TerminalMonitor(manager, buffer, keyListener, parent, monitor_init);
363 255
364 /** 256 /**
365 * @return charset in use by bridge 257 * @return charset in use by bridge
366 */ 258 */
367 public Charset getCharset() { 259 public Charset getCharset() {
368 return relay.getCharset(); 260 if (relay != null) return relay.getCharset();
261
369 } 262 }
370 263
371 /** 264 /**
372 * Sets the encoding used by the terminal. If the connection is live, 265 * Sets the encoding used by the terminal. If the connection is live,
373 * then the character set is changed for the next read. 266 * then the character set is changed for the next read.
374 * @param encoding the canonical name of the character encoding 267 * @param encoding the canonical name of the character encoding
375 */ 268 */
376 public void setCharset(String encoding) { 269 public void setCharset(String encoding) {
377 if (relay != null) 270 if (relay != null) relay.setCharset(encoding);
378 relay.setCharset(encoding);
379 271
380 keyListener.setCharset(encoding); 272 keyListener.setCharset(encoding);
381 } 273 }
382 274
383 /** 275 /**
425 * authentication. If called before authenticated, it will just fail. 317 * authentication. If called before authenticated, it will just fail.
426 */ 318 */
427 public void onConnected() { 319 public void onConnected() {
428 disconnected = false; 320 disconnected = false;
429 buffer.reset(); 321 buffer.reset();
430 // We no longer need our local output.
431 localOutput.clear();
432 buffer.setAnswerBack(emulation); 322 buffer.setAnswerBack(emulation);
323 localOutput.clear(); // We no longer need our local output.
433 324
434 if (HostDatabase.DELKEY_BACKSPACE.equals(host.getDelKey())) 325 if (HostDatabase.DELKEY_BACKSPACE.equals(host.getDelKey()))
435 buffer.setBackspace(vt320.DELETE_IS_BACKSPACE); 326 buffer.setBackspace(vt320.DELETE_IS_BACKSPACE);
436 else 327 else
437 buffer.setBackspace(vt320.DELETE_IS_DEL); 328 buffer.setBackspace(vt320.DELETE_IS_DEL);
662 canvas.drawLine(0, borderY, borderX + 1, borderY, defaultPaint); 553 canvas.drawLine(0, borderY, borderX + 1, borderY, defaultPaint);
663 } 554 }
664 555
665 try { 556 try {
666 // request a terminal pty resize 557 // request a terminal pty resize
667 synchronized (buffer) { 558 if (buffer != null) {
668 buffer.setScreenSize(columns, rows, true); 559 synchronized (buffer) {
560 buffer.setScreenSize(columns, rows, true);
561 }
669 } 562 }
670 563
671 if (transport != null) 564 if (transport != null)
672 transport.setDimensions(columns, rows, width, height); 565 transport.setDimensions(columns, rows, width, height);
673 } 566 }