diff src/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 15:1588e359a972

queue pending monitor commands until socket is open
author Carl Byington <carl@five-ten-sg.com>
date Thu, 29 May 2014 12:40:27 -0700
parents 0ce5cc452d02
children 48a8daea9221
line wrap: on
line diff
--- a/src/com/five_ten_sg/connectbot/service/TerminalMonitor.java	Thu May 22 10:46:17 2014 -0700
+++ b/src/com/five_ten_sg/connectbot/service/TerminalMonitor.java	Thu May 29 12:40:27 2014 -0700
@@ -16,6 +16,9 @@
 import java.net.Socket;
 import java.nio.charset.Charset;
 import java.util.HashMap;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ConcurrentHashMap;
 
 public class TerminalMonitor {
     public final static String TAG = "ConnectBot.TerminalMonitor";
@@ -50,6 +53,7 @@
     private InputStream         monitor_in     = null;
     private OutputStream        monitor_out    = null;
     private MyReader            monitor_reader = null;
+    private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(100);
     private MyServiceConnection monitor_connection = new MyServiceConnection();
 
     class MyReader extends Thread {
@@ -141,6 +145,12 @@
                 monitor_reader.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);
@@ -260,6 +270,13 @@
                 //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);
@@ -331,9 +348,11 @@
 
     public synchronized void setField(int l, int c, char[] data, int offset) {
         Log.i(TAG, "setField()");
-        byte[] d = charsToBytes(data);
-        byte[] b = new byte[d.length - offset];
-        System.arraycopy(d, offset, b, 0, d.length - offset);
+        byte[] b = new byte[data.length - offset];
+        int i;
+        for (i=0; i<b.length; i++) {
+            b[i] = (byte)(data[i+offset] & 0x00ff);
+        }
         buffer.write(b);
     }