Mercurial > 510ConnectbotMonitor
annotate src/com/five_ten_sg/connectbot/monitor/MonitorService.java @ 16:a481d8fb5571 stable-1.0.2
add switch session command to the TE
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 01 Jul 2014 19:08:47 -0700 |
parents | 5bf6d84cc5b8 |
children | a94ca5a89fe8 |
rev | line source |
---|---|
0 | 1 package com.five_ten_sg.connectbot.monitor; |
2 | |
3 import java.io.IOException; | |
4 import java.io.InputStream; | |
5 import java.io.OutputStream; | |
6 import java.net.ServerSocket; | |
7 import java.net.Socket; | |
5 | 8 import java.util.HashMap; |
0 | 9 import java.util.concurrent.ArrayBlockingQueue; |
10 import java.util.concurrent.BlockingQueue; | |
11 import java.util.concurrent.ConcurrentHashMap; | |
12 import java.util.Locale; | |
13 | |
14 import android.app.Activity; | |
15 import android.app.Service; | |
16 import android.content.Context; | |
17 import android.content.Intent; | |
18 import android.content.ServiceConnection; | |
19 import android.net.wifi.WifiManager.WifiLock; | |
20 import android.net.wifi.WifiManager; | |
21 import android.os.Binder; | |
22 import android.os.Bundle; | |
23 import android.os.Handler; | |
24 import android.os.IBinder; | |
25 import android.os.Message; | |
26 import android.os.PowerManager; | |
27 import android.speech.tts.TextToSpeech; | |
28 import android.speech.tts.TextToSpeech.OnInitListener; | |
29 import android.util.Log; | |
30 import android.widget.TextView; | |
31 | |
32 public class MonitorService extends Service implements OnInitListener { | |
33 public final static String TAG = "ConnectBot.MonitorService"; | |
34 | |
16
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
35 public static final char MONITOR_CMD_INIT = 0; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
36 public static final char MONITOR_CMD_ACTIVATE = 1; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
37 public static final char MONITOR_CMD_KEYSTATE = 2; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
38 public static final char MONITOR_CMD_CURSORMOVE = 3; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
39 public static final char MONITOR_CMD_SCREENCHANGE = 4; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
40 public static final char MONITOR_CMD_FIELDVALUE = 5; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
41 public static final char MONITOR_CMD_SETFIELD = 5; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
42 public static final char MONITOR_CMD_GETFIELD = 6; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
43 public static final char MONITOR_CMD_SCREENWATCH = 7; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
44 public static final char MONITOR_CMD_DEPRESS = 8; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
45 public static final char MONITOR_CMD_SHOWURL = 9; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
46 public static final char MONITOR_CMD_SWITCHSESSION = 10; |
0 | 47 |
48 public static final int MONITORPORT = 6000; | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
49 public static ConcurrentHashMap<Integer,CommunicationThread> clients = new ConcurrentHashMap<Integer,CommunicationThread>(); |
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
50 public static int currentConnection = -1; |
0 | 51 |
52 private boolean speech = false; | |
53 private TextToSpeech talker = null; | |
5 | 54 private BlockingQueue<String> talkerQueue = null; |
0 | 55 public Handler handler = null; |
56 private ServerSocket serverSocket; | |
57 private Thread serverThread = null; | |
58 private WifiManager.WifiLock wifiLock; | |
59 private PowerManager.WakeLock wakeLock; | |
60 final private IBinder binder = new MonitorBinder(); | |
61 | |
62 | |
63 public class MonitorBinder extends Binder { | |
64 public MonitorService getService() { | |
65 return MonitorService.this; | |
66 } | |
67 } | |
68 | |
69 @Override | |
70 public void onInit(int status) { | |
71 if (status == TextToSpeech.SUCCESS) { | |
72 talker.setLanguage(Locale.US); | |
73 speech = true; | |
74 } | |
75 } | |
76 | |
77 @Override | |
78 public void onCreate() { | |
79 WifiManager wMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE); | |
80 wifiLock = wMgr.createWifiLock(WifiManager.WIFI_MODE_FULL, "MyWifiLock"); | |
81 wifiLock.acquire(); | |
82 | |
83 PowerManager pMgr = (PowerManager) getSystemService(Context.POWER_SERVICE); | |
84 wakeLock = pMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock"); | |
85 wakeLock.acquire(); | |
86 | |
87 talker = new TextToSpeech(this, this); | |
88 this.serverThread = new Thread(new ServerThread()); | |
89 this.serverThread.start(); | |
90 } | |
91 | |
92 @Override | |
93 public IBinder onBind(Intent intent) { | |
94 startService(new Intent(this, MonitorService.class)); | |
95 return binder; | |
96 } | |
97 | |
98 public void printer(String msg) { | |
99 if (handler != null) handler.sendMessage(handler.obtainMessage(MonitorActivity.MESSAGE_CODE_PRINT, msg)); | |
100 } | |
101 | |
102 @Override | |
103 public int onStartCommand(Intent intent, int flags, int startId) { | |
104 Log.i(TAG, "service onStartCommand()"); | |
105 return START_STICKY; | |
106 } | |
107 | |
108 @Override | |
109 public void onDestroy() { | |
110 try { | |
111 Log.i(TAG, "service onDestroy()"); | |
112 talker.stop(); | |
113 talker.shutdown(); | |
114 wifiLock.release(); | |
115 wakeLock.release(); | |
116 serverSocket.close(); | |
117 } catch (IOException e) { | |
118 Log.e(TAG, "exception in onDestroy()", e); | |
119 } | |
120 super.onDestroy(); | |
121 } | |
122 | |
123 class ServerThread extends Thread { | |
124 public void run() { | |
125 Socket socket = null; | |
126 int connection = 0; | |
127 try { | |
128 serverSocket = new ServerSocket(MONITORPORT); | |
129 } catch (IOException e) { | |
130 Log.e(TAG, "exception in ServerThread.run(), cannot create listening socket", e); | |
131 return; | |
132 } | |
133 while (true) { | |
134 try{ | |
135 socket = serverSocket.accept(); | |
136 connection = connection + 1; | |
137 CommunicationThread commThread = new CommunicationThread(connection, socket); | |
138 clients.put(connection, commThread); | |
139 commThread.start(); | |
140 } catch (IOException e) { | |
141 Log.e(TAG, "exception in ServerThread.run(), listening socket closed", e); | |
142 break; | |
143 } | |
144 } | |
145 } | |
146 } | |
147 | |
148 class triple { | |
149 private int l, c; | |
150 private char[] b; | |
151 public triple(int l, int c, char[] b) { | |
152 this.l = l; | |
153 this.c = c; | |
154 this.b = b; | |
155 } | |
156 } | |
157 | |
158 class CommunicationThread extends Thread { | |
159 public int connection; | |
160 private Socket client_socket; | |
161 private InputStream client_in; | |
162 private OutputStream client_out; | |
163 private boolean is_closing = false; | |
164 private BlockingQueue<triple> queue = new ArrayBlockingQueue<triple>(1); | |
165 | |
166 public CommunicationThread(int handle, Socket socket) { | |
167 connection = handle; | |
168 client_socket = socket; | |
169 try { | |
170 client_in = client_socket.getInputStream(); | |
171 client_out = client_socket.getOutputStream(); | |
172 } catch (IOException e) { | |
173 Log.e(TAG, "exception in CommunicationThread() constructor, cannot get socket streams", e); | |
174 } | |
175 } | |
176 | |
4 | 177 public void speak(byte [] msg, boolean flush, boolean synchronous) { |
178 if (speech) { | |
179 String smsg = bytesToString(msg); | |
180 if (synchronous) { | |
181 HashMap<String, String> myHashParms = new HashMap(); | |
182 myHashParms.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.format("connection %d", connection)); | |
183 talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, myHashParms); | |
184 try { | |
185 String x = talkerQueue.take(); // wait for completion | |
186 } catch (InterruptedException e) { | |
187 Log.e(TAG, "exception in cm.speak()", e); | |
188 } | |
189 } | |
190 else { | |
191 talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, null); | |
192 } | |
193 } | |
194 } | |
195 | |
196 public String bytesToString(byte[] b) { | |
197 char[] c = new char[b.length]; | |
198 int bp = 0; | |
199 for(int i = 0; i < c.length; i++) { | |
200 byte b1 = 0; | |
201 byte b2 = b[bp++]; | |
202 c[i] = (char) (((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
203 } | |
204 return new String(c); | |
205 } | |
206 | |
0 | 207 public char[] bytesToChars(byte[] b, int len) { |
208 char[] c = new char[len >> 1]; | |
209 int bp = 0; | |
210 for(int i = 0; i < c.length; i++) { | |
211 byte b1 = b[bp++]; | |
212 byte b2 = b[bp++]; | |
213 c[i] = (char) (((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
214 } | |
215 return c; | |
216 } | |
217 | |
218 public byte[] charsToBytes(char[] c) { | |
219 byte[] b = new byte[c.length << 1]; | |
220 int bp = 0; | |
221 for (int i=0; i<c.length; i++) { | |
222 b[bp++] = (byte) ((c[i] & 0xff00) >> 8); | |
223 b[bp++] = (byte) (c[i] & 0x00ff); | |
224 } | |
225 return b; | |
226 } | |
227 | |
228 public synchronized void clientWrite(char cmd, char[] c) { | |
229 try { | |
230 if (client_out != null) { | |
231 c[0] = (char)(c.length - 1); // number of chars following | |
232 c[1] = cmd; | |
233 Log.i(TAG, String.format("sending %d command", (int)cmd)); | |
234 client_out.write(charsToBytes(c)); | |
235 } | |
236 } | |
237 catch (IOException e) { | |
238 Log.e(TAG, "exception in monitorWrite()", e); | |
239 try { | |
240 client_out.close(); | |
241 } | |
242 catch (IOException ee) { | |
243 Log.e(TAG, "exception in monitorWrite() closing socket", ee); | |
244 } | |
245 client_out = null; | |
246 } | |
247 }; | |
248 | |
249 private char[] forceRead(int len) throws IOException { | |
250 int len2 = len*2; | |
251 int off = 0; | |
252 byte[] b = new byte[len2]; | |
253 while (off < len2) { | |
254 int l = client_in.read(b, off, len2-off); | |
255 if (l < 0) { | |
256 is_closing = true; | |
257 throw new IOException("eof"); | |
258 } | |
259 off += l; | |
260 } | |
261 return bytesToChars(b, len2); | |
262 } | |
263 | |
264 public void run() { | |
265 Log.i(TAG, String.format("CommunicationThread.run() client %d connected", connection)); | |
266 while (true) { | |
267 try { | |
268 char[] len = forceRead(1); | |
269 char[] packet = forceRead(len[0]); | |
270 char[] buf; | |
271 char cmd = packet[0]; | |
272 int plen = packet.length; | |
273 //Log.i(TAG, String.format("received %d command length %d", (int)cmd, plen)); | |
274 switch (cmd) { | |
275 case MONITOR_CMD_INIT: | |
276 buf = new char[plen-1]; | |
277 System.arraycopy(packet, 1, buf, 0, plen-1); | |
278 abandonGetField(connection); | |
279 teInit(connection, buf); | |
280 break; | |
281 case MONITOR_CMD_ACTIVATE: | |
282 abandonGetField(connection); | |
283 buf = new char[plen-3]; | |
284 System.arraycopy(packet, 3, buf, 0, plen-3); | |
285 teActivate(connection, packet[1], packet[2], buf); | |
286 break; | |
2 | 287 case MONITOR_CMD_KEYSTATE: |
288 teKeyState(connection, (packet[1] == 1)); | |
0 | 289 break; |
290 case MONITOR_CMD_CURSORMOVE: | |
291 teCursorMove(connection, packet[1], packet[2]); | |
292 break; | |
293 case MONITOR_CMD_SCREENCHANGE: | |
294 buf = new char[plen-3]; | |
295 System.arraycopy(packet, 3, buf, 0, plen-3); | |
296 teScreenChange(connection, packet[1], packet[2], buf); | |
297 break; | |
298 case MONITOR_CMD_FIELDVALUE: | |
299 buf = new char[plen-3]; | |
300 System.arraycopy(packet, 3, buf, 0, plen-3); | |
301 Log.i(TAG, String.format("teFieldValue %d line %d column %d b.len %d", connection, packet[1], packet[2], buf.length)); | |
302 queue.put(new triple(packet[1], packet[2], buf)); | |
303 break; | |
304 default: | |
305 break; | |
306 } | |
307 } catch (IOException e) { | |
308 if (!is_closing) Log.e(TAG, "exception in CommunicationThread.run()", e); | |
309 break; | |
310 } catch (InterruptedException e) { | |
311 Log.e(TAG, "exception in CommunicationThread.run()", e); | |
312 break; | |
313 } | |
314 } | |
315 Log.i(TAG, String.format("shutting down connection %d", connection)); | |
316 try { | |
317 if (client_in != null) client_in.close(); | |
318 if (client_out != null) client_out.close(); | |
319 if (client_socket != null) client_socket.close(); | |
320 } catch (IOException e) { | |
321 Log.e(TAG, "exception in CommunicationThread.run() closing sockets", e); | |
322 } | |
323 client_in = null; | |
324 client_out = null; | |
325 client_socket = null; | |
326 } | |
327 } | |
328 | |
329 private void abandonGetField(int except) { | |
330 for (CommunicationThread cm : clients.values()) { | |
331 if (cm.connection != except) { | |
332 cm.queue.offer(new triple(0, 0, new char[0])); | |
333 } | |
334 } | |
335 } | |
336 | |
337 | |
338 //////////////////////////////////////// | |
339 //// these functions run on the reader thread here and call your monitoring code | |
340 | |
341 public void teInit(int connection, char[] buf) { | |
342 String fn = new String(buf); | |
343 Log.i(TAG, String.format("teInit %d file %s", connection, fn)); | |
2 | 344 //printer(String.format("init %d %s", connection, fn)); |
0 | 345 } |
346 | |
347 public void teActivate(int connection, int lines, int columns, char[] buf) { | |
348 Log.i(TAG, String.format("teActivate %d", connection)); | |
2 | 349 //printer(String.format("activate %d lines %d columns %d b.len %d", connection, lines, columns, buf.length)); |
0 | 350 } |
351 | |
2 | 352 public void teKeyState(int connection, boolean down) { |
353 String d = (down) ? "yes" : "no"; | |
354 Log.i(TAG, String.format("teKeyState %d isdown %s", connection, d)); | |
355 //printer(String.format("keystate %d isdown %s", connection, d)); | |
0 | 356 } |
357 | |
358 public void teCursorMove(int connection, int l, int c) { | |
359 //Log.i(TAG, String.format("teCursorMove %d line %d column %d", connection, l, c)); | |
360 } | |
361 | |
362 public void teScreenChange(int connection, int lines, int columns, char[] buf) { | |
363 Log.i(TAG, String.format("teScreenChange %d lines %d columns %d b.len %d", connection, lines, columns, buf.length)); | |
364 } | |
365 | |
366 | |
367 //////////////////////////////////////// | |
368 //// these functions are called from your monitoring code thread | |
369 | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
370 public static void teSetField(int connection, int l, int c, char[] buf) { |
0 | 371 int len = buf.length; |
372 Log.i(TAG, String.format("teSetField %d request line %d column %d len %d", connection, l, c, len)); | |
373 CommunicationThread cm = clients.get(connection); | |
374 if (cm != null) { | |
375 char[] arg2 = new char[4 + len]; | |
376 arg2[2] = (char) (l & 0x0000ffff); | |
377 arg2[3] = (char) (c & 0x0000ffff); | |
378 int base = 4; | |
379 System.arraycopy(buf, 0, arg2, base, len); | |
380 cm.clientWrite(MONITOR_CMD_SETFIELD, arg2); | |
381 } | |
382 } | |
383 | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
384 public static char[] teGetField(int connection, int l, int c, int len) { |
0 | 385 Log.i(TAG, String.format("teGetField %d request line %d column %d len %d", connection, l, c, len)); |
386 CommunicationThread cm = clients.get(connection); | |
387 if (cm != null) { | |
388 char[] arg = new char[5]; | |
389 arg[2] = (char) (l & 0x0000ffff); | |
390 arg[3] = (char) (c & 0x0000ffff); | |
391 arg[4] = (char) (len & 0x0000ffff); | |
392 cm.queue.clear(); // we never have more than one outstanding getfield request on the connection | |
393 cm.clientWrite(MONITOR_CMD_GETFIELD, arg); | |
394 try { | |
395 triple t = cm.queue.take(); // wait for response | |
396 Log.i(TAG, String.format("teGetField %d response line %d column %d len %d", connection, t.l, t.c, t.b.length)); | |
397 return t.b; | |
398 } catch (InterruptedException e) { | |
399 Log.e(TAG, "exception in teGetField(), return empty string", e); | |
400 } | |
401 } | |
402 return new char[0]; | |
403 } | |
404 | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
405 public static void teScreenWatch(int connection, int l, int c, int len) { |
0 | 406 Log.i(TAG, String.format("teScreenWatch %d request line %d column %d len %d", connection, l, c, len)); |
407 CommunicationThread cm = clients.get(connection); | |
408 if (cm != null) { | |
409 char[] arg = new char[5]; | |
410 arg[2] = (char) (l & 0x0000ffff); | |
411 arg[3] = (char) (c & 0x0000ffff); | |
412 arg[4] = (char) (len & 0x0000ffff); | |
413 cm.clientWrite(MONITOR_CMD_GETFIELD, arg); | |
414 } | |
415 } | |
416 | |
7 | 417 public static void teSpeak(int connection, byte [] msg, boolean flush, boolean synchronous) { |
4 | 418 CommunicationThread cm = clients.get(connection); |
419 if (cm != null) cm.speak(msg, flush, synchronous); | |
0 | 420 } |
2 | 421 |
422 public static void teDepress(int connection, int vk_key) { | |
423 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731 | |
424 Log.i(TAG, String.format("teDepress %d, %d", connection, vk_key)); | |
425 CommunicationThread cm = clients.get(connection); | |
426 if (cm != null) { | |
427 char[] arg = new char[3]; | |
428 arg[2] = (char) (vk_key & 0x0000ffff); | |
429 cm.clientWrite(MONITOR_CMD_DEPRESS, arg); | |
430 } | |
431 } | |
13 | 432 |
433 public static void teShowUrl(int connection, char [] url) { | |
434 int len = url.length; | |
435 CommunicationThread cm = clients.get(connection); | |
436 if (cm != null) { | |
437 char[] arg2 = new char[2 + len]; | |
438 int base = 2; | |
439 System.arraycopy(url, 0, arg2, base, len); | |
440 cm.clientWrite(MONITOR_CMD_SHOWURL, arg2); | |
441 } | |
442 } | |
443 | |
16
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
444 public static void teSwitchSession(int connection) { |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
445 CommunicationThread cm = clients.get(connection); |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
446 if (cm != null) { |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
447 char [] arg2 = new char[2]; |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
448 cm.clientWrite(MONITOR_CMD_SWITCHSESSION, arg2); |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
449 } |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
450 } |
a481d8fb5571
add switch session command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
451 |
0 | 452 } |