1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* Copyright (C) 2000-2002 i-scream |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
//---PACKAGE DECLARATION--- |
21 |
package uk.org.iscream.cms.conient; |
22 |
|
23 |
//---IMPORTS--- |
24 |
import java.io.*; |
25 |
import uk.org.iscream.cms.server.util.*; |
26 |
|
27 |
/** |
28 |
* The class reads in data from a BufferedReader, |
29 |
* it then converts it to an XMLPacket and adds |
30 |
* it to its Queue for anything that wants it. |
31 |
* |
32 |
* @author $Author: tdb $ |
33 |
* @version $Id: DataReader.java,v 1.14 2001/05/29 17:41:32 tdb Exp $ |
34 |
*/ |
35 |
public class DataReader extends Thread { |
36 |
|
37 |
//---FINAL ATTRIBUTES--- |
38 |
|
39 |
/** |
40 |
* The current CVS revision of this class |
41 |
*/ |
42 |
public final String REVISION = "$Revision: 1.14 $"; |
43 |
|
44 |
//---STATIC METHODS--- |
45 |
|
46 |
//---CONSTRUCTORS--- |
47 |
|
48 |
/** |
49 |
* Constructs a new data reader, giving it its BufferedReader |
50 |
* and Queue. |
51 |
* |
52 |
* @param inBound the BufferedReader this class should use |
53 |
* @param dataQueue the queue new data should be placed on |
54 |
* @param ch the connection handler in use |
55 |
*/ |
56 |
public DataReader(BufferedReader inBound, Queue dataQueue, ConnectionHandler ch) { |
57 |
_ch = ch; |
58 |
_inBound = inBound; |
59 |
_dataQueue = dataQueue; |
60 |
} |
61 |
|
62 |
//---PUBLIC METHODS--- |
63 |
|
64 |
/** |
65 |
* This thread reads data from the BufferedReader. |
66 |
* It does this until either there is a problem |
67 |
* or it is told to stop. |
68 |
* |
69 |
* If there is a problem it calls shutdownLinks in the |
70 |
* ConnectionHandler. |
71 |
* |
72 |
* Any data it reads it converts to XML and then |
73 |
* adds to its queue. |
74 |
*/ |
75 |
public void run() { |
76 |
try { |
77 |
String line; |
78 |
|
79 |
// continue until we are told to stop |
80 |
while (_running) { |
81 |
line = _inBound.readLine(); |
82 |
if (line == null) { |
83 |
throw new IOException("unexpected loss of connection to server"); |
84 |
} |
85 |
_dataQueue.add(line); |
86 |
} |
87 |
|
88 |
// close the BufferedReader |
89 |
_inBound.close(); |
90 |
|
91 |
} catch (IOException e) { |
92 |
Conient.addMessage("WARNING{data reader}: inbound data stopped - "+e); |
93 |
_running = false; |
94 |
// tell the ConnectionHandler to shut down the links |
95 |
_ch.shutdownDataLink(); |
96 |
} |
97 |
} |
98 |
|
99 |
/** |
100 |
* This method allows other classes |
101 |
* to shutdown this data reader. |
102 |
*/ |
103 |
public void shutdown() { |
104 |
_running = false; |
105 |
} |
106 |
|
107 |
//---PRIVATE METHODS--- |
108 |
|
109 |
//---ACCESSOR/MUTATOR METHODS--- |
110 |
|
111 |
//---ATTRIBUTES--- |
112 |
|
113 |
/** |
114 |
* The reader we are reading from. |
115 |
*/ |
116 |
private BufferedReader _inBound; |
117 |
|
118 |
/** |
119 |
* The Queue we place data on. |
120 |
*/ |
121 |
private Queue _dataQueue; |
122 |
|
123 |
/** |
124 |
* The state of this thread. |
125 |
*/ |
126 |
private boolean _running = true; |
127 |
|
128 |
/** |
129 |
* A reference to the ConnectionHandler in use |
130 |
*/ |
131 |
private ConnectionHandler _ch; |
132 |
|
133 |
//---STATIC ATTRIBUTES--- |
134 |
|
135 |
} |