comparison 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
comparison
equal deleted inserted replaced
1:ab7313512aba 15:1588e359a972
14 import java.io.OutputStream; 14 import java.io.OutputStream;
15 import java.net.InetAddress; 15 import java.net.InetAddress;
16 import java.net.Socket; 16 import java.net.Socket;
17 import java.nio.charset.Charset; 17 import java.nio.charset.Charset;
18 import java.util.HashMap; 18 import java.util.HashMap;
19 import java.util.concurrent.ArrayBlockingQueue;
20 import java.util.concurrent.BlockingQueue;
21 import java.util.concurrent.ConcurrentHashMap;
19 22
20 public class TerminalMonitor { 23 public class TerminalMonitor {
21 public final static String TAG = "ConnectBot.TerminalMonitor"; 24 public final static String TAG = "ConnectBot.TerminalMonitor";
22 25
23 public static final char MONITOR_CMD_INIT = 0; 26 public static final char MONITOR_CMD_INIT = 0;
48 private IBinder bound = null; 51 private IBinder bound = null;
49 private Socket monitor_socket = null; 52 private Socket monitor_socket = null;
50 private InputStream monitor_in = null; 53 private InputStream monitor_in = null;
51 private OutputStream monitor_out = null; 54 private OutputStream monitor_out = null;
52 private MyReader monitor_reader = null; 55 private MyReader monitor_reader = null;
56 private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(100);
53 private MyServiceConnection monitor_connection = new MyServiceConnection(); 57 private MyServiceConnection monitor_connection = new MyServiceConnection();
54 58
55 class MyReader extends Thread { 59 class MyReader extends Thread {
56 private InputStream monitor_in; 60 private InputStream monitor_in;
57 private byte[] b; 61 private byte[] b;
139 Log.i(TAG, "connected to monitor socket, send init " + init); 143 Log.i(TAG, "connected to monitor socket, send init " + init);
140 monitor_reader = new MyReader(monitor_in); 144 monitor_reader = new MyReader(monitor_in);
141 monitor_reader.start(); 145 monitor_reader.start();
142 String x = " " + init; 146 String x = " " + init;
143 monitorWrite(MONITOR_CMD_INIT, x.toCharArray()); 147 monitorWrite(MONITOR_CMD_INIT, x.toCharArray());
148 char [] c;
149 while (true) {
150 c = pending_commands.poll();
151 if (c == null) break;
152 monitorWrite(c[1], c);
153 }
144 } 154 }
145 catch (IOException e) { 155 catch (IOException e) {
146 Log.e(TAG, "exception in onServiceConnected()", e); 156 Log.e(TAG, "exception in onServiceConnected()", e);
147 } 157 }
148 } 158 }
258 c[0] = (char)(c.length - 1); // number of chars following 268 c[0] = (char)(c.length - 1); // number of chars following
259 c[1] = cmd; 269 c[1] = cmd;
260 //Log.i(TAG, String.format("sending %d command", (int)cmd)); 270 //Log.i(TAG, String.format("sending %d command", (int)cmd));
261 monitor_out.write(charsToBytes(c)); 271 monitor_out.write(charsToBytes(c));
262 } 272 }
273 else {
274 c[1] = cmd;
275 pending_commands.put(c);
276 }
277 }
278 catch (InterruptedException e) {
279 Log.e(TAG, "exception in monitorWrite()", e);
263 } 280 }
264 catch (IOException e) { 281 catch (IOException e) {
265 Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e); 282 Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e);
266 283
267 try { 284 try {
329 screenChanged(l, l, c, c); 346 screenChanged(l, l, c, c);
330 } 347 }
331 348
332 public synchronized void setField(int l, int c, char[] data, int offset) { 349 public synchronized void setField(int l, int c, char[] data, int offset) {
333 Log.i(TAG, "setField()"); 350 Log.i(TAG, "setField()");
334 byte[] d = charsToBytes(data); 351 byte[] b = new byte[data.length - offset];
335 byte[] b = new byte[d.length - offset]; 352 int i;
336 System.arraycopy(d, offset, b, 0, d.length - offset); 353 for (i=0; i<b.length; i++) {
354 b[i] = (byte)(data[i+offset] & 0x00ff);
355 }
337 buffer.write(b); 356 buffer.write(b);
338 } 357 }
339 358
340 public synchronized void getField(int l, int c, int len) { 359 public synchronized void getField(int l, int c, int len) {
341 Log.i(TAG, "getField()"); 360 Log.i(TAG, "getField()");