1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public License Version
5
* 1.1 (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
* http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* The Original Code is Android SUTAgent code.
16
* The Initial Developer of the Original Code is
18
* Portions created by the Initial Developer are Copyright (C) 2010
19
* the Initial Developer. All Rights Reserved.
22
* Bob Moss <bmoss@mozilla.com>
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
38
package com.mozilla.SUTAgentAndroid.service;
40
import java.io.BufferedInputStream;
41
import java.io.IOException;
42
import java.io.InputStream;
43
import java.io.OutputStream;
44
import java.io.PrintWriter;
45
import java.net.Socket;
46
import java.net.SocketTimeoutException;
47
import java.text.SimpleDateFormat;
48
import java.util.Calendar;
50
// import com.mozilla.SUTAgentAndroid.DoCommand;
51
import com.mozilla.SUTAgentAndroid.SUTAgentAndroid;
53
public class DataWorkerThread extends Thread
55
private RunDataThread theParent = null;
56
private Socket socket = null;
57
boolean bListening = true;
58
PrintWriter out = null;
59
SimpleDateFormat sdf = null;
61
public DataWorkerThread(RunDataThread theParent, Socket workerSocket)
63
super("DataWorkerThread");
64
this.theParent = theParent;
65
this.socket = workerSocket;
66
this.sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
69
public void StopListening()
74
public void SendString(String strToSend)
78
Calendar cal = Calendar.getInstance();
79
String strOut = sdf.format(cal.getTime());
80
strOut += " " + strToSend + "\r\n";
87
private String readLine(BufferedInputStream in)
98
cChar = ((char)(nByte & 0xFF));
99
if ((cChar != '\r') && (cChar != '\n'))
106
if (in.available() > 0)
113
cChar = ((char)(nByte & 0xFF));
114
if ((cChar == '\r') || (cChar == '\n'))
116
if (in.available() > 0)
132
catch (IOException e)
134
// TODO Auto-generated catch block
138
if (sRet.length() == 0)
147
long lEndTime = System.currentTimeMillis() + 60000;
152
OutputStream cmdOut = socket.getOutputStream();
153
InputStream cmdIn = socket.getInputStream();
154
this.out = new PrintWriter(cmdOut, true);
155
BufferedInputStream in = new BufferedInputStream(cmdIn);
156
String inputLine, outputLine;
157
DoCommand dc = new DoCommand(theParent.svc);
159
Calendar cal = Calendar.getInstance();
160
sRet = sdf.format(cal.getTime());
161
sRet += " trace output";
165
int nAvail = cmdIn.available();
170
if (System.currentTimeMillis() > lEndTime)
172
cal = Calendar.getInstance();
173
sRet = sdf.format(cal.getTime());
174
sRet += " Thump thump - " + SUTAgentAndroid.sUniqueID + "\r\n";
179
lEndTime = System.currentTimeMillis() + 60000;
182
if (!(in.available() > 0))
184
socket.setSoTimeout(500);
186
int nRead = cmdIn.read();
193
inputLine = (char)nRead + "";
195
catch(SocketTimeoutException toe)
203
if ((inputLine += readLine(in)) != null)
205
outputLine = dc.processCommand(inputLine, out, in, cmdOut);
206
out.print(outputLine + "\n");
208
if (outputLine.equals("exit"))
210
theParent.StopListening();
213
if (outputLine.equals("quit"))
231
catch (IOException e)
233
// TODO Auto-generated catch block