14 |
|
import java.io.InputStreamReader; |
15 |
|
|
16 |
|
/** |
17 |
< |
* Acts as a Data Handler to a TCP based client. |
17 |
> |
* Acts as a Data Handler to a TCP based client, providing |
18 |
> |
* it with a constant stream of XML data for the hosts the |
19 |
> |
* client has requested. |
20 |
|
* |
21 |
|
* @author $Author$ |
22 |
|
* @version $Id$ |
33 |
|
//---STATIC METHODS--- |
34 |
|
|
35 |
|
//---CONSTRUCTORS--- |
36 |
< |
|
36 |
> |
|
37 |
> |
/** |
38 |
> |
* Construct a new TCPDataHandler with a Socket provided |
39 |
> |
* by the Control Handler. |
40 |
> |
* |
41 |
> |
* @param socket The socket to which the Client has connected |
42 |
> |
* @throws IOException if something goes wrong, the Control Handler can deal with it |
43 |
> |
*/ |
44 |
|
public TCPDataHandler(Socket socket) throws IOException { |
45 |
|
_socket = socket; |
46 |
|
_queue = new Queue(); |
47 |
+ |
// setup the reader and writer |
48 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
49 |
|
_socketOut = new PrintWriter(_socket.getOutputStream()); |
50 |
|
_logger.write(toString(), Logger.SYSINIT, "created"); |
51 |
|
} |
52 |
|
|
53 |
|
//---PUBLIC METHODS--- |
54 |
< |
|
54 |
> |
/** |
55 |
> |
* Main loop for the Data Handler, keeps sending data from |
56 |
> |
* it's local Queue. |
57 |
> |
*/ |
58 |
|
public void run() { |
59 |
|
run = true; |
60 |
|
_queueID = _queue.getQueue(); |
61 |
+ |
// run until we get told to stop |
62 |
|
while(run) { |
63 |
+ |
// get some data, and send some data |
64 |
|
try { |
65 |
|
String xml = (String) _queue.get(_queueID); |
66 |
|
_socketOut.println(xml); |
71 |
|
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
72 |
|
} |
73 |
|
} |
74 |
+ |
// if we get here we've been told to stop |
75 |
|
_logger.write(toString(), Logger.SYSMSG, "Shutting Down"); |
76 |
+ |
// shut down the reader, writer and Socket |
77 |
|
try { |
78 |
|
_socketOut.close(); |
79 |
|
_socketIn.close(); |
82 |
|
catch(IOException e) { |
83 |
|
_logger.write(toString(), Logger.ERROR, "Exception whilst shutting down: "+e); |
84 |
|
} |
85 |
+ |
// remove ourselves from the queue |
86 |
|
_queue.removeQueue(_queueID); |
87 |
|
} |
88 |
< |
|
88 |
> |
|
89 |
> |
/** |
90 |
> |
* Method to shutdown this Data Handler. All this actually does |
91 |
> |
* is set a flag which the main loop will see and commence shutting |
92 |
> |
* down. This method will return before the main loop stops. |
93 |
> |
*/ |
94 |
|
public void shutdown() { |
95 |
+ |
// set the main loop to stop |
96 |
|
run=false; |
97 |
+ |
// if the main loop is waiting for data it won't notice the |
98 |
+ |
// above flag to stop. This bumps it out of the blocked get. |
99 |
|
_queue.releaseQueue(_queueID); |
100 |
|
} |
101 |
|
|
118 |
|
//---PRIVATE METHODS--- |
119 |
|
|
120 |
|
//---ACCESSOR/MUTATOR METHODS--- |
121 |
< |
|
121 |
> |
|
122 |
> |
/** |
123 |
> |
* Accessor to our Queue. The Control Handler needs to |
124 |
> |
* get this reference to register us. |
125 |
> |
* |
126 |
> |
* @return a reference to our Queue |
127 |
> |
*/ |
128 |
|
public Queue getQueue() { |
129 |
|
return _queue; |
130 |
|
} |
158 |
|
*/ |
159 |
|
private PrintWriter _socketOut; |
160 |
|
|
161 |
+ |
/** |
162 |
+ |
* A reference to the Socket connected to the client |
163 |
+ |
*/ |
164 |
|
private Socket _socket; |
165 |
|
|
166 |
+ |
/** |
167 |
+ |
* A reference to our Queue |
168 |
+ |
*/ |
169 |
|
private Queue _queue; |
170 |
+ |
|
171 |
+ |
/** |
172 |
+ |
* The flag that dictates whether we should be running |
173 |
+ |
*/ |
174 |
|
private boolean run; |
175 |
+ |
|
176 |
+ |
/** |
177 |
+ |
* Our queue number within our Queue |
178 |
+ |
*/ |
179 |
|
private int _queueID; |
180 |
|
|
181 |
|
//---STATIC ATTRIBUTES--- |