annotate src/com/five_ten_sg/connectbot/monitor/MonitorService.java @ 1:f12df02aa228

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