1 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
ajm |
1.11 |
package uk.org.iscream.conient; |
3 |
ajm |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import java.io.*; |
6 |
ajm |
1.11 |
import uk.org.iscream.util.*; |
7 |
ajm |
1.1 |
|
8 |
|
|
/** |
9 |
ajm |
1.7 |
* The class reads in data from a BufferedReader, |
10 |
|
|
* it then converts it to an XMLPacket and adds |
11 |
|
|
* it to its Queue for anything that wants it. |
12 |
ajm |
1.1 |
* |
13 |
ajm |
1.11 |
* @author $Author: ajm4 $ |
14 |
ajm |
1.12 |
* @version $Id: DataReader.java,v 1.11 2001/03/15 01:05:46 ajm4 Exp $ |
15 |
ajm |
1.1 |
*/ |
16 |
|
|
public class DataReader extends Thread { |
17 |
|
|
|
18 |
|
|
//---FINAL ATTRIBUTES--- |
19 |
|
|
|
20 |
|
|
/** |
21 |
|
|
* The current CVS revision of this class |
22 |
|
|
*/ |
23 |
ajm |
1.12 |
public final String REVISION = "$Revision: 1.11 $"; |
24 |
ajm |
1.1 |
|
25 |
|
|
//---STATIC METHODS--- |
26 |
|
|
|
27 |
|
|
//---CONSTRUCTORS--- |
28 |
|
|
|
29 |
ajm |
1.7 |
/** |
30 |
|
|
* Constructs a new data reader, giving it its BufferedReader |
31 |
|
|
* and Queue. |
32 |
|
|
* |
33 |
|
|
* @param inBound the BufferedReader this class should use |
34 |
|
|
* @param dataQueue the queue new data should be placed on |
35 |
ajm |
1.12 |
* @param ch the connection handler in use |
36 |
ajm |
1.7 |
*/ |
37 |
ajm |
1.12 |
public DataReader(BufferedReader inBound, Queue dataQueue, ConnectionHandler ch) { |
38 |
|
|
_ch = ch; |
39 |
ajm |
1.1 |
_inBound = inBound; |
40 |
ajm |
1.5 |
_dataQueue = dataQueue; |
41 |
ajm |
1.1 |
} |
42 |
|
|
|
43 |
|
|
//---PUBLIC METHODS--- |
44 |
|
|
|
45 |
ajm |
1.7 |
/** |
46 |
|
|
* This thread reads data from the BufferedReader. |
47 |
|
|
* It does this until either there is a problem |
48 |
|
|
* or it is told to stop. |
49 |
|
|
* |
50 |
ajm |
1.12 |
* If there is a problem it calls shutdownLinks in the |
51 |
|
|
* ConnectionHandler. |
52 |
|
|
* |
53 |
ajm |
1.7 |
* Any data it reads it converts to XML and then |
54 |
|
|
* adds to its queue. |
55 |
|
|
*/ |
56 |
ajm |
1.1 |
public void run() { |
57 |
ajm |
1.3 |
try { |
58 |
ajm |
1.7 |
String line; |
59 |
|
|
|
60 |
|
|
// continue until we are told to stop |
61 |
ajm |
1.6 |
while (_running) { |
62 |
ajm |
1.7 |
line = _inBound.readLine(); |
63 |
tdb |
1.9 |
if (line == null) { |
64 |
|
|
throw new IOException("unexpected loss of connection to server"); |
65 |
|
|
} |
66 |
ajm |
1.7 |
_dataQueue.add(line); |
67 |
ajm |
1.6 |
} |
68 |
|
|
|
69 |
ajm |
1.7 |
// close the BufferedReader |
70 |
ajm |
1.6 |
_inBound.close(); |
71 |
|
|
|
72 |
ajm |
1.3 |
} catch (IOException e) { |
73 |
ajm |
1.10 |
Conient.addMessage("WARNING{data reader}: inbound data stopped - "+e); |
74 |
ajm |
1.7 |
_running = false; |
75 |
ajm |
1.12 |
// tell the ConnectionHandler to shut down the links |
76 |
|
|
_ch.shutdownLinks(); |
77 |
ajm |
1.1 |
} |
78 |
|
|
} |
79 |
|
|
|
80 |
ajm |
1.7 |
/** |
81 |
|
|
* This method allows other classes |
82 |
|
|
* to shutdown this data reader. |
83 |
|
|
*/ |
84 |
|
|
public void shutdown() { |
85 |
|
|
_running = false; |
86 |
|
|
} |
87 |
|
|
|
88 |
ajm |
1.1 |
//---PRIVATE METHODS--- |
89 |
|
|
|
90 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
91 |
|
|
|
92 |
|
|
//---ATTRIBUTES--- |
93 |
|
|
|
94 |
ajm |
1.7 |
/** |
95 |
|
|
* The reader we are reading from. |
96 |
|
|
*/ |
97 |
ajm |
1.12 |
private BufferedReader _inBound; |
98 |
ajm |
1.7 |
|
99 |
|
|
/** |
100 |
|
|
* The Queue we place data on. |
101 |
|
|
*/ |
102 |
ajm |
1.12 |
private Queue _dataQueue; |
103 |
ajm |
1.7 |
|
104 |
|
|
/** |
105 |
|
|
* The state of this thread. |
106 |
|
|
*/ |
107 |
ajm |
1.12 |
private boolean _running = true; |
108 |
|
|
|
109 |
|
|
/** |
110 |
|
|
* A reference to the ConnectionHandler in use |
111 |
|
|
*/ |
112 |
|
|
private ConnectionHandler _ch; |
113 |
ajm |
1.7 |
|
114 |
ajm |
1.1 |
//---STATIC ATTRIBUTES--- |
115 |
|
|
|
116 |
|
|
} |