changeset 402:14aa0621aa7d stable-1.9.0

Backed out changeset 2f2b5a244a4d
author Carl Byington <carl@five-ten-sg.com>
date Mon, 20 Oct 2014 19:14:39 -0700
parents 977815f990ec
children fa6c9ab5a5e2
files src/com/five_ten_sg/connectbot/service/TerminalMonitor.java
diffstat 1 files changed, 38 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- 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<char[]> pending_commands = new ArrayBlockingQueue<char[]>(10000);
+    private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(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() {