~extension-hackers/globalmenu-extension/3.5

« back to all changes in this revision

Viewing changes to build/mobile/sutagent/android/DataWorkerThread.java

  • Committer: Chris Coulson
  • Date: 2011-08-05 17:37:02 UTC
  • Revision ID: chrisccoulson@ubuntu.com-20110805173702-n11ykbt0tdp5u07q
Refresh build system from FIREFOX_6_0b5_BUILD1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ***** BEGIN LICENSE BLOCK *****
2
 
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3
 
 *
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/
8
 
 *
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
12
 
 * License.
13
 
 *
14
 
 * The Original Code is Android SUTAgent code.
15
 
 *
16
 
 * The Initial Developer of the Original Code is
17
 
 * Bob Moss.
18
 
 * Portions created by the Initial Developer are Copyright (C) 2010
19
 
 * the Initial Developer. All Rights Reserved.
20
 
 *
21
 
 * Contributor(s):
22
 
 *  Bob Moss <bmoss@mozilla.com>
23
 
 * 
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.
35
 
 *
36
 
 * ***** END LICENSE BLOCK ***** */
37
 
 
38
 
package com.mozilla.SUTAgentAndroid.service;
39
 
 
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;
49
 
 
50
 
// import com.mozilla.SUTAgentAndroid.DoCommand;
51
 
import com.mozilla.SUTAgentAndroid.SUTAgentAndroid;
52
 
 
53
 
public class DataWorkerThread extends Thread
54
 
{
55
 
        private RunDataThread theParent = null;
56
 
        private Socket socket   = null;
57
 
        boolean bListening      = true;
58
 
        PrintWriter out = null;
59
 
        SimpleDateFormat sdf = null;
60
 
 
61
 
        public DataWorkerThread(RunDataThread theParent, Socket workerSocket)
62
 
                {
63
 
                super("DataWorkerThread");
64
 
                this.theParent = theParent;
65
 
                this.socket = workerSocket;
66
 
                this.sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
67
 
                }
68
 
 
69
 
        public void StopListening()
70
 
                {
71
 
                bListening = false;
72
 
                }
73
 
        
74
 
        public void SendString(String strToSend)
75
 
                {
76
 
                if (this.out != null)
77
 
                        {
78
 
                        Calendar cal = Calendar.getInstance();
79
 
                        String strOut = sdf.format(cal.getTime());
80
 
                        strOut += " " + strToSend + "\r\n";
81
 
                
82
 
                        out.write(strOut);
83
 
                        out.flush();
84
 
                        }
85
 
                }
86
 
 
87
 
        private String readLine(BufferedInputStream in)
88
 
                {
89
 
                String sRet = "";
90
 
                int nByte = 0;
91
 
                char cChar = 0;
92
 
        
93
 
                try 
94
 
                        {
95
 
                        nByte = in.read();
96
 
                        while (nByte != -1)
97
 
                                {
98
 
                                cChar = ((char)(nByte & 0xFF));
99
 
                                if ((cChar != '\r') && (cChar != '\n'))
100
 
                                        sRet += cChar;
101
 
                                else
102
 
                                        break;
103
 
                                nByte = in.read();
104
 
                                }
105
 
                
106
 
                        if (in.available() > 0)
107
 
                                {
108
 
                                in.mark(1024);
109
 
                                nByte = in.read();
110
 
                
111
 
                                while (nByte != -1)
112
 
                                        {
113
 
                                        cChar = ((char)(nByte & 0xFF));
114
 
                                        if ((cChar == '\r') || (cChar == '\n'))
115
 
                                                {
116
 
                                                if (in.available() > 0)
117
 
                                                        {
118
 
                                                        in.mark(1024);
119
 
                                                        nByte = in.read();
120
 
                                                        }
121
 
                                                else
122
 
                                                        nByte = -1;
123
 
                                                }
124
 
                                        else
125
 
                                                {
126
 
                                                in.reset();
127
 
                                                break;
128
 
                                                }
129
 
                                        }
130
 
                                }
131
 
                        }
132
 
                catch (IOException e)
133
 
                        {
134
 
                        // TODO Auto-generated catch block
135
 
                        e.printStackTrace();
136
 
                        }
137
 
        
138
 
                if (sRet.length() == 0)
139
 
                        sRet = null;
140
 
        
141
 
                return(sRet);
142
 
                }
143
 
 
144
 
        public void run()
145
 
                {
146
 
                String  sRet = "";
147
 
                long lEndTime = System.currentTimeMillis() + 60000;
148
 
                
149
 
                try {
150
 
                        while(bListening)
151
 
                                {
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);
158
 
                                
159
 
                        Calendar cal = Calendar.getInstance();
160
 
                        sRet = sdf.format(cal.getTime());
161
 
                        sRet += " trace output";
162
 
 
163
 
                                out.println(sRet);
164
 
                                out.flush();
165
 
                                int nAvail = cmdIn.available();
166
 
                                cmdIn.skip(nAvail);
167
 
                                
168
 
                                while (bListening)
169
 
                                        {
170
 
                                        if (System.currentTimeMillis() > lEndTime)
171
 
                                                {
172
 
                                                cal = Calendar.getInstance();
173
 
                                        sRet = sdf.format(cal.getTime());
174
 
                                        sRet += " Thump thump - " + SUTAgentAndroid.sUniqueID + "\r\n";
175
 
 
176
 
                                        out.write(sRet);
177
 
                                        out.flush();
178
 
                                                
179
 
                                                lEndTime = System.currentTimeMillis() + 60000;
180
 
                                                }
181
 
                                        
182
 
                                        if (!(in.available() > 0))
183
 
                                                {
184
 
                                                socket.setSoTimeout(500);
185
 
                                                try {
186
 
                                                        int nRead = cmdIn.read();
187
 
                                                        if (nRead == -1)
188
 
                                                                {
189
 
                                                                bListening = false;
190
 
                                                                continue;
191
 
                                                                }
192
 
                                                        else
193
 
                                                                inputLine = (char)nRead + ""; 
194
 
                                                        }
195
 
                                                catch(SocketTimeoutException toe)
196
 
                                                        {
197
 
                                                        continue;
198
 
                                                        }
199
 
                                                }
200
 
                                        else
201
 
                                                inputLine = "";
202
 
                                
203
 
                                        if ((inputLine += readLine(in)) != null)
204
 
                                                {
205
 
                                                outputLine = dc.processCommand(inputLine, out, in, cmdOut);
206
 
                                                out.print(outputLine + "\n");
207
 
                                                out.flush();
208
 
                                                if (outputLine.equals("exit"))
209
 
                                                        {
210
 
                                                        theParent.StopListening();
211
 
                                                        bListening = false;
212
 
                                                        }
213
 
                                                if (outputLine.equals("quit"))
214
 
                                                        {
215
 
                                                        bListening = false;
216
 
                                                        }
217
 
                                                outputLine = null;
218
 
                                                System.gc();
219
 
                                                }
220
 
                                        else
221
 
                                                break;
222
 
                                        }
223
 
                                
224
 
                                out.close();
225
 
                                out = null;
226
 
                                in.close();
227
 
                                in = null;
228
 
                                socket.close();
229
 
                                }
230
 
                        }
231
 
                catch (IOException e)
232
 
                        {
233
 
                        // TODO Auto-generated catch block
234
 
                        e.printStackTrace();
235
 
                        }
236
 
                }
237
 
}