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