Bumping manifests a=b2g-bump
[gecko.git] / build / mobile / sutagent / android / CmdWorkerThread.java
blobe8037aec018918f33f2b82ba8afaf5d4a259ec94
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];
34 pr[0] = '$';
35 pr[1] = '>';
36 pr[2] = 0;
37 prompt = new String(pr,0,3);
40 public void StopListening()
42 bListening = false;
45 private String readLine(BufferedInputStream in)
47 String sRet = "";
48 int nByte = 0;
49 char cChar = 0;
51 try
53 nByte = in.read();
54 while (nByte != -1)
56 cChar = ((char)(nByte & 0xFF));
57 if ((cChar != '\r') && (cChar != '\n'))
58 sRet += cChar;
59 else
60 break;
61 nByte = in.read();
64 if ((in.available() > 0) && (cChar != '\n'))
66 in.mark(1024);
67 nByte = in.read();
69 if (nByte != -1)
71 cChar = ((char)(nByte & 0xFF));
72 if (cChar != '\n')
74 in.reset();
79 catch (IOException e)
81 // TODO Auto-generated catch block
82 e.printStackTrace();
85 if (sRet.length() == 0)
86 sRet = null;
88 return(sRet);
91 public void run()
93 try {
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();
104 cmdIn.skip(nAvail);
106 out.print(prompt);
107 out.flush();
109 while (bListening)
111 if (!(in.available() > 0))
113 socket.setSoTimeout(500);
114 try {
115 int nRead = cmdIn.read();
116 if (nRead == -1)
118 bListening = false;
119 continue;
121 else
123 inputLine = ((char)nRead) + "";
124 socket.setSoTimeout(120000);
127 catch(SocketTimeoutException toe)
129 continue;
132 else
133 inputLine = "";
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)
144 outputLine = "";
146 if (outputLine.length() > 0)
148 out.print(outputLine + "\n" + prompt);
150 else
151 out.print(prompt);
152 out.flush();
153 if (outputLine.equals("exit"))
155 theParent.StopListening();
156 bListening = false;
158 if (outputLine.equals("quit"))
160 bListening = false;
162 outputLine = null;
163 System.gc();
165 else
166 break;
168 out.close();
169 out = null;
170 in.close();
171 in = null;
172 socket.close();
173 SUTAgentAndroid.log(dc, "CmdWorkerThread ends: "+getId());
175 catch (IOException e)
177 // TODO Auto-generated catch block
178 e.printStackTrace();