1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.iscream.clientinterface; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import uk.ac.ukc.iscream.util.*; |
6 |
|
|
import uk.ac.ukc.iscream.componentmanager.*; |
7 |
|
|
import uk.ac.ukc.iscream.core.*; |
8 |
|
|
import java.net.Socket; |
9 |
|
|
import java.io.InputStream; |
10 |
|
|
import java.io.OutputStream; |
11 |
|
|
import java.io.IOException; |
12 |
|
|
import java.io.BufferedReader; |
13 |
|
|
import java.io.PrintWriter; |
14 |
|
|
import java.io.InputStreamReader; |
15 |
|
|
|
16 |
|
|
/** |
17 |
tdb |
1.6 |
* 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 |
tdb |
1.1 |
* |
21 |
tdb |
1.2 |
* @author $Author: tdb1 $ |
22 |
tdb |
1.9 |
* @version $Id: TCPDataHandler.java,v 1.8 2001/02/07 13:47:33 tdb1 Exp $ |
23 |
tdb |
1.1 |
*/ |
24 |
tdb |
1.2 |
class TCPDataHandler extends Thread { |
25 |
tdb |
1.1 |
|
26 |
|
|
//---FINAL ATTRIBUTES--- |
27 |
|
|
|
28 |
|
|
/** |
29 |
|
|
* The current CVS revision of this class |
30 |
|
|
*/ |
31 |
tdb |
1.9 |
public final String REVISION = "$Revision: 1.8 $"; |
32 |
tdb |
1.1 |
|
33 |
|
|
//---STATIC METHODS--- |
34 |
|
|
|
35 |
|
|
//---CONSTRUCTORS--- |
36 |
tdb |
1.6 |
|
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 |
tdb |
1.4 |
public TCPDataHandler(Socket socket) throws IOException { |
45 |
tdb |
1.1 |
_socket = socket; |
46 |
tdb |
1.4 |
_queue = new Queue(); |
47 |
tdb |
1.6 |
// setup the reader and writer |
48 |
tdb |
1.1 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
49 |
tdb |
1.8 |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
50 |
tdb |
1.1 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
//---PUBLIC METHODS--- |
54 |
tdb |
1.6 |
/** |
55 |
|
|
* Main loop for the Data Handler, keeps sending data from |
56 |
|
|
* it's local Queue. |
57 |
|
|
*/ |
58 |
tdb |
1.2 |
public void run() { |
59 |
|
|
run = true; |
60 |
tdb |
1.5 |
_queueID = _queue.getQueue(); |
61 |
tdb |
1.6 |
// run until we get told to stop |
62 |
tdb |
1.2 |
while(run) { |
63 |
tdb |
1.6 |
// get some data, and send some data |
64 |
tdb |
1.2 |
try { |
65 |
tdb |
1.5 |
String xml = (String) _queue.get(_queueID); |
66 |
tdb |
1.7 |
// if we're shutting down and we've been "bumped" |
67 |
|
|
// it's likely we'll get a null back, and we don't |
68 |
|
|
// want to send that on to the Client |
69 |
|
|
if(xml != null) { |
70 |
|
|
_socketOut.println(xml); |
71 |
|
|
} |
72 |
tdb |
1.2 |
} |
73 |
|
|
catch(InvalidQueueException e) { |
74 |
|
|
run = false; |
75 |
|
|
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
76 |
|
|
} |
77 |
|
|
} |
78 |
tdb |
1.6 |
// if we get here we've been told to stop |
79 |
tdb |
1.1 |
_logger.write(toString(), Logger.SYSMSG, "Shutting Down"); |
80 |
tdb |
1.6 |
// shut down the reader, writer and Socket |
81 |
tdb |
1.2 |
try { |
82 |
|
|
_socketOut.close(); |
83 |
|
|
_socketIn.close(); |
84 |
|
|
_socket.close(); |
85 |
|
|
} |
86 |
|
|
catch(IOException e) { |
87 |
|
|
_logger.write(toString(), Logger.ERROR, "Exception whilst shutting down: "+e); |
88 |
|
|
} |
89 |
tdb |
1.6 |
// remove ourselves from the queue |
90 |
tdb |
1.5 |
_queue.removeQueue(_queueID); |
91 |
tdb |
1.9 |
_queue.stopMonitor(); |
92 |
tdb |
1.2 |
} |
93 |
tdb |
1.6 |
|
94 |
|
|
/** |
95 |
|
|
* Method to shutdown this Data Handler. All this actually does |
96 |
|
|
* is set a flag which the main loop will see and commence shutting |
97 |
|
|
* down. This method will return before the main loop stops. |
98 |
|
|
*/ |
99 |
tdb |
1.2 |
public void shutdown() { |
100 |
tdb |
1.6 |
// set the main loop to stop |
101 |
tdb |
1.2 |
run=false; |
102 |
tdb |
1.6 |
// if the main loop is waiting for data it won't notice the |
103 |
|
|
// above flag to stop. This bumps it out of the blocked get. |
104 |
tdb |
1.5 |
_queue.releaseQueue(_queueID); |
105 |
tdb |
1.1 |
} |
106 |
|
|
|
107 |
|
|
/** |
108 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
109 |
|
|
* method to provide clean logging (every class should have this). |
110 |
|
|
* |
111 |
|
|
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
112 |
|
|
* to format the toString() |
113 |
|
|
* |
114 |
|
|
* @return the name of this class and its CVS revision |
115 |
|
|
*/ |
116 |
|
|
public String toString() { |
117 |
|
|
return FormatName.getName( |
118 |
|
|
_name, |
119 |
|
|
getClass().getName(), |
120 |
|
|
REVISION); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
//---PRIVATE METHODS--- |
124 |
|
|
|
125 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
126 |
tdb |
1.6 |
|
127 |
|
|
/** |
128 |
|
|
* Accessor to our Queue. The Control Handler needs to |
129 |
|
|
* get this reference to register us. |
130 |
|
|
* |
131 |
|
|
* @return a reference to our Queue |
132 |
|
|
*/ |
133 |
tdb |
1.4 |
public Queue getQueue() { |
134 |
|
|
return _queue; |
135 |
|
|
} |
136 |
|
|
|
137 |
tdb |
1.1 |
//---ATTRIBUTES--- |
138 |
|
|
|
139 |
|
|
/** |
140 |
|
|
* This is the friendly identifier of the |
141 |
|
|
* component this class is running in. |
142 |
|
|
* eg, a Filter may be called "filter1", |
143 |
|
|
* If this class does not have an owning |
144 |
|
|
* component, a name from the configuration |
145 |
|
|
* can be placed here. This name could also |
146 |
|
|
* be changed to null for utility classes. |
147 |
|
|
*/ |
148 |
|
|
private String _name = ClientInterfaceMain.NAME; |
149 |
|
|
|
150 |
|
|
/** |
151 |
|
|
* This holds a reference to the |
152 |
|
|
* system logger that is being used. |
153 |
|
|
*/ |
154 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
155 |
|
|
|
156 |
|
|
/** |
157 |
|
|
* A hook to the inbound data from the socket |
158 |
|
|
*/ |
159 |
|
|
private BufferedReader _socketIn; |
160 |
|
|
|
161 |
|
|
/** |
162 |
|
|
* A hook to the outbound stream for the socket |
163 |
|
|
*/ |
164 |
|
|
private PrintWriter _socketOut; |
165 |
|
|
|
166 |
tdb |
1.6 |
/** |
167 |
|
|
* A reference to the Socket connected to the client |
168 |
|
|
*/ |
169 |
tdb |
1.1 |
private Socket _socket; |
170 |
tdb |
1.2 |
|
171 |
tdb |
1.6 |
/** |
172 |
|
|
* A reference to our Queue |
173 |
|
|
*/ |
174 |
tdb |
1.2 |
private Queue _queue; |
175 |
tdb |
1.6 |
|
176 |
|
|
/** |
177 |
|
|
* The flag that dictates whether we should be running |
178 |
|
|
*/ |
179 |
tdb |
1.2 |
private boolean run; |
180 |
tdb |
1.6 |
|
181 |
|
|
/** |
182 |
|
|
* Our queue number within our Queue |
183 |
|
|
*/ |
184 |
tdb |
1.5 |
private int _queueID; |
185 |
tdb |
1.1 |
|
186 |
|
|
//---STATIC ATTRIBUTES--- |
187 |
|
|
|
188 |
|
|
} |