comparison src/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 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 ec74f347ab5f
comparison
equal deleted inserted replaced
401:977815f990ec 402:14aa0621aa7d
66 private IBinder bound = null; 66 private IBinder bound = null;
67 private Socket monitor_socket = null; 67 private Socket monitor_socket = null;
68 private InputStream monitor_in = null; 68 private InputStream monitor_in = null;
69 private OutputStream monitor_out = null; 69 private OutputStream monitor_out = null;
70 private MyReader monitor_reader = null; 70 private MyReader monitor_reader = null;
71 private MyWriter monitor_writer = null; 71 private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(100);
72 private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(10000);
73 private MyServiceConnection monitor_connection = new MyServiceConnection(); 72 private MyServiceConnection monitor_connection = new MyServiceConnection();
74
75 class MyWriter extends Thread {
76 private OutputStream monitor_out;
77 private boolean is_closing = false;
78
79 public MyWriter(OutputStream monitor_out) {
80 this.monitor_out = monitor_out;
81 }
82
83 public void closing() {
84 is_closing = true;
85 this.interrupt();
86 }
87
88 public void run() {
89 char [] c;
90 try {
91 while (!is_closing) {
92 c = pending_commands.take();
93 c[0] = (char)(c.length - 1); // number of chars following
94 c[1] = cmd;
95 //Log.i(TAG, String.format("sending %d command", (int)cmd));
96 monitor_out.write(charsToBytes(c));
97 }
98 }
99 catch (InterruptedException e) {
100 if (!is_closing) Log.e(TAG, "exception in monitorWrite()", e);
101 }
102 catch (IOException e) {
103 Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e);
104 }
105
106 try {
107 monitor_out.close();
108 }
109 catch (IOException ee) {
110 Log.e(TAG, "exception in monitorWrite() closing output stream", ee);
111 }
112 monitor_out = null;
113 }
114 }
115
116 73
117 class MyReader extends Thread { 74 class MyReader extends Thread {
118 private InputStream monitor_in; 75 private InputStream monitor_in;
119 private byte[] b; 76 private byte[] b;
120 private boolean is_closing = false; 77 private boolean is_closing = false;
221 monitor_in = monitor_socket.getInputStream(); 178 monitor_in = monitor_socket.getInputStream();
222 monitor_out = monitor_socket.getOutputStream(); 179 monitor_out = monitor_socket.getOutputStream();
223 Log.i(TAG, "connected to monitor socket, send init " + init); 180 Log.i(TAG, "connected to monitor socket, send init " + init);
224 monitor_reader = new MyReader(monitor_in); 181 monitor_reader = new MyReader(monitor_in);
225 monitor_reader.start(); 182 monitor_reader.start();
226 monitor_writer = new MyWriter(monitor_out);
227 monitor_writer.start();
228
229 String x = " " + init; 183 String x = " " + init;
230 monitorWrite(MONITOR_CMD_INIT, x.toCharArray()); 184 monitorWrite(MONITOR_CMD_INIT, x.toCharArray());
231 185 char [] c;
186
187 while (true) {
188 c = pending_commands.poll();
189
190 if (c == null) break;
191
192 monitorWrite(c[1], c);
193 }
232 } 194 }
233 catch (IOException e) { 195 catch (IOException e) {
234 Log.e(TAG, "exception in onServiceConnected()", e); 196 Log.e(TAG, "exception in onServiceConnected()", e);
235 } 197 }
236 } 198 }
295 } 257 }
296 258
297 259
298 public void Disconnect() { 260 public void Disconnect() {
299 if (monitor_reader != null) monitor_reader.closing(); 261 if (monitor_reader != null) monitor_reader.closing();
300 if (monitor_writer != null) monitor_writer.closing(); 262
301 try { 263 try {
302 if (monitor_out != null) monitor_out.close(); 264 if (monitor_out != null) monitor_out.close();
303 265
304 if (monitor_in != null) monitor_in.close(); 266 if (monitor_in != null) monitor_in.close();
305 267
347 309
348 return b; 310 return b;
349 } 311 }
350 312
351 313
352 public void monitorWrite(char cmd, char[] c) { 314 public synchronized void monitorWrite(char cmd, char[] c) {
353 c[1] = cmd; 315 try {
354 pending_commands.put(c); 316 if (monitor_out != null) {
317 c[0] = (char)(c.length - 1); // number of chars following
318 c[1] = cmd;
319 //Log.i(TAG, String.format("sending %d command", (int)cmd));
320 monitor_out.write(charsToBytes(c));
321 }
322 else {
323 c[1] = cmd;
324 pending_commands.put(c);
325 }
326 }
327 catch (InterruptedException e) {
328 Log.e(TAG, "exception in monitorWrite()", e);
329 }
330 catch (IOException e) {
331 Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e);
332
333 try {
334 monitor_out.close();
335 }
336 catch (IOException ee) {
337 Log.e(TAG, "exception in monitorWrite() closing output stream", ee);
338 }
339
340 monitor_out = null;
341 }
355 }; 342 };
356 343
357 public void resetWatch() { 344 public void resetWatch() {
358 start_line = 0; 345 start_line = 0;
359 end_line = 500; 346 end_line = 500;