1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.org.iscream.conient; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import java.io.*; |
6 |
+ |
import uk.org.iscream.util.*; |
7 |
|
|
8 |
|
/** |
9 |
< |
* Reads in bound data and presents it as it comes |
10 |
< |
* in for anything that wants it. |
9 |
> |
* 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 |
|
* |
13 |
|
* @author $Author$ |
14 |
|
* @version $Id$ |
26 |
|
|
27 |
|
//---CONSTRUCTORS--- |
28 |
|
|
29 |
< |
public DataReader(BufferedReader inBound) { |
29 |
> |
/** |
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 |
> |
* @param ch the connection handler in use |
36 |
> |
*/ |
37 |
> |
public DataReader(BufferedReader inBound, Queue dataQueue, ConnectionHandler ch) { |
38 |
> |
_ch = ch; |
39 |
|
_inBound = inBound; |
40 |
+ |
_dataQueue = dataQueue; |
41 |
|
} |
42 |
|
|
43 |
|
//---PUBLIC METHODS--- |
44 |
|
|
45 |
+ |
/** |
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 |
+ |
* If there is a problem it calls shutdownLinks in the |
51 |
+ |
* ConnectionHandler. |
52 |
+ |
* |
53 |
+ |
* Any data it reads it converts to XML and then |
54 |
+ |
* adds to its queue. |
55 |
+ |
*/ |
56 |
|
public void run() { |
33 |
– |
boolean running = true; |
57 |
|
try { |
58 |
< |
while (true) { |
59 |
< |
_xml = _inBound.readLine(); |
60 |
< |
synchronized (this) { |
61 |
< |
notifyAll(); |
58 |
> |
String line; |
59 |
> |
|
60 |
> |
// continue until we are told to stop |
61 |
> |
while (_running) { |
62 |
> |
line = _inBound.readLine(); |
63 |
> |
if (line == null) { |
64 |
> |
throw new IOException("unexpected loss of connection to server"); |
65 |
|
} |
66 |
+ |
_dataQueue.add(line); |
67 |
|
} |
68 |
+ |
|
69 |
+ |
// close the BufferedReader |
70 |
+ |
_inBound.close(); |
71 |
+ |
|
72 |
|
} catch (IOException e) { |
73 |
< |
System.err.println("This DataReader thread has been shut down as an exception occured: "+e); |
74 |
< |
running = false; |
75 |
< |
return; |
73 |
> |
Conient.addMessage("WARNING{data reader}: inbound data stopped - "+e); |
74 |
> |
_running = false; |
75 |
> |
// tell the ConnectionHandler to shut down the links |
76 |
> |
_ch.shutdownDataLink(); |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
+ |
/** |
81 |
+ |
* This method allows other classes |
82 |
+ |
* to shutdown this data reader. |
83 |
+ |
*/ |
84 |
+ |
public void shutdown() { |
85 |
+ |
_running = false; |
86 |
+ |
} |
87 |
+ |
|
88 |
|
//---PRIVATE METHODS--- |
89 |
|
|
90 |
|
//---ACCESSOR/MUTATOR METHODS--- |
91 |
|
|
52 |
– |
public String getXML() { |
53 |
– |
return _xml; |
54 |
– |
} |
55 |
– |
|
92 |
|
//---ATTRIBUTES--- |
93 |
|
|
94 |
< |
BufferedReader _inBound; |
95 |
< |
String _xml; |
96 |
< |
|
94 |
> |
/** |
95 |
> |
* The reader we are reading from. |
96 |
> |
*/ |
97 |
> |
private BufferedReader _inBound; |
98 |
> |
|
99 |
> |
/** |
100 |
> |
* The Queue we place data on. |
101 |
> |
*/ |
102 |
> |
private Queue _dataQueue; |
103 |
> |
|
104 |
> |
/** |
105 |
> |
* The state of this thread. |
106 |
> |
*/ |
107 |
> |
private boolean _running = true; |
108 |
> |
|
109 |
> |
/** |
110 |
> |
* A reference to the ConnectionHandler in use |
111 |
> |
*/ |
112 |
> |
private ConnectionHandler _ch; |
113 |
> |
|
114 |
|
//---STATIC ATTRIBUTES--- |
115 |
|
|
116 |
|
} |