# HG changeset patch # User Carl Byington # Date 1413857679 25200 # Node ID 14aa0621aa7d88ff867474d414e653233db73f1e # Parent 977815f990ecbdd58b6907640911512e312cde61 Backed out changeset 2f2b5a244a4d diff -r 977815f990ec -r 14aa0621aa7d src/com/five_ten_sg/connectbot/service/TerminalMonitor.java --- a/src/com/five_ten_sg/connectbot/service/TerminalMonitor.java Mon Oct 20 19:14:10 2014 -0700 +++ b/src/com/five_ten_sg/connectbot/service/TerminalMonitor.java Mon Oct 20 19:14:39 2014 -0700 @@ -68,52 +68,9 @@ private InputStream monitor_in = null; private OutputStream monitor_out = null; private MyReader monitor_reader = null; - private MyWriter monitor_writer = null; - private BlockingQueue pending_commands = new ArrayBlockingQueue(10000); + private BlockingQueue pending_commands = new ArrayBlockingQueue(100); private MyServiceConnection monitor_connection = new MyServiceConnection(); - class MyWriter extends Thread { - private OutputStream monitor_out; - private boolean is_closing = false; - - public MyWriter(OutputStream monitor_out) { - this.monitor_out = monitor_out; - } - - public void closing() { - is_closing = true; - this.interrupt(); - } - - public void run() { - char [] c; - try { - while (!is_closing) { - c = pending_commands.take(); - c[0] = (char)(c.length - 1); // number of chars following - c[1] = cmd; - //Log.i(TAG, String.format("sending %d command", (int)cmd)); - monitor_out.write(charsToBytes(c)); - } - } - catch (InterruptedException e) { - if (!is_closing) Log.e(TAG, "exception in monitorWrite()", e); - } - catch (IOException e) { - Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e); - } - - try { - monitor_out.close(); - } - catch (IOException ee) { - Log.e(TAG, "exception in monitorWrite() closing output stream", ee); - } - monitor_out = null; - } - } - - class MyReader extends Thread { private InputStream monitor_in; private byte[] b; @@ -223,12 +180,17 @@ Log.i(TAG, "connected to monitor socket, send init " + init); monitor_reader = new MyReader(monitor_in); monitor_reader.start(); - monitor_writer = new MyWriter(monitor_out); - monitor_writer.start(); - String x = " " + init; monitorWrite(MONITOR_CMD_INIT, x.toCharArray()); + char [] c; + while (true) { + c = pending_commands.poll(); + + if (c == null) break; + + monitorWrite(c[1], c); + } } catch (IOException e) { Log.e(TAG, "exception in onServiceConnected()", e); @@ -297,7 +259,7 @@ public void Disconnect() { if (monitor_reader != null) monitor_reader.closing(); - if (monitor_writer != null) monitor_writer.closing(); + try { if (monitor_out != null) monitor_out.close(); @@ -349,9 +311,34 @@ } - public void monitorWrite(char cmd, char[] c) { - c[1] = cmd; - pending_commands.put(c); + public synchronized void monitorWrite(char cmd, char[] c) { + try { + if (monitor_out != null) { + c[0] = (char)(c.length - 1); // number of chars following + c[1] = cmd; + //Log.i(TAG, String.format("sending %d command", (int)cmd)); + monitor_out.write(charsToBytes(c)); + } + else { + c[1] = cmd; + pending_commands.put(c); + } + } + catch (InterruptedException e) { + Log.e(TAG, "exception in monitorWrite()", e); + } + catch (IOException e) { + Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e); + + try { + monitor_out.close(); + } + catch (IOException ee) { + Log.e(TAG, "exception in monitorWrite() closing output stream", ee); + } + + monitor_out = null; + } }; public void resetWatch() {