1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package com
.mozilla
.SUTAgentAndroid
.service
;
7 import java
.io
.BufferedInputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStream
;
10 import java
.io
.OutputStream
;
11 import java
.io
.PrintWriter
;
12 import java
.net
.Socket
;
13 import java
.net
.SocketTimeoutException
;
15 import com
.mozilla
.SUTAgentAndroid
.SUTAgentAndroid
;
17 import android
.util
.Log
;
19 // import com.mozilla.SUTAgentAndroid.DoCommand;
21 public class CmdWorkerThread
extends Thread
23 private RunCmdThread theParent
= null;
24 private Socket socket
= null;
25 private String prompt
= null;
26 boolean bListening
= true;
28 public CmdWorkerThread(RunCmdThread theParent
, Socket workerSocket
)
30 super("CmdWorkerThread");
31 this.theParent
= theParent
;
32 this.socket
= workerSocket
;
33 byte pr
[] = new byte [3];
37 prompt
= new String(pr
,0,3);
40 public void StopListening()
45 private String
readLine(BufferedInputStream in
)
56 cChar
= ((char)(nByte
& 0xFF));
57 if ((cChar
!= '\r') && (cChar
!= '\n'))
64 if ((in
.available() > 0) && (cChar
!= '\n'))
71 cChar
= ((char)(nByte
& 0xFF));
81 // TODO Auto-generated catch block
85 if (sRet
.length() == 0)
94 OutputStream cmdOut
= socket
.getOutputStream();
95 InputStream cmdIn
= socket
.getInputStream();
96 PrintWriter out
= new PrintWriter(cmdOut
, true);
97 BufferedInputStream in
= new BufferedInputStream(cmdIn
);
98 String inputLine
, outputLine
;
99 DoCommand dc
= new DoCommand(theParent
.svc
);
101 SUTAgentAndroid
.log(dc
, "CmdWorkerThread starts: "+getId());
103 int nAvail
= cmdIn
.available();
111 if (!(in
.available() > 0))
113 socket
.setSoTimeout(500);
115 int nRead
= cmdIn
.read();
123 inputLine
= ((char)nRead
) + "";
124 socket
.setSoTimeout(120000);
127 catch(SocketTimeoutException toe
)
135 if ((inputLine
+= readLine(in
)) != null)
137 String message
= String
.format("%s : %s",
138 socket
.getInetAddress().getHostAddress(), inputLine
);
139 SUTAgentAndroid
.log(dc
, message
);
141 outputLine
= dc
.processCommand(inputLine
, out
, in
, cmdOut
);
142 if (outputLine
== null)
146 if (outputLine
.length() > 0)
148 out
.print(outputLine
+ "\n" + prompt
);
153 if (outputLine
.equals("exit"))
155 theParent
.StopListening();
158 if (outputLine
.equals("quit"))
173 SUTAgentAndroid
.log(dc
, "CmdWorkerThread ends: "+getId());
175 catch (IOException e
)
177 // TODO Auto-generated catch block