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 |
|
|
* Acts as a Data Handler to a TCP based client. |
18 |
|
|
* |
19 |
tdb |
1.2 |
* @author $Author: tdb1 $ |
20 |
tdb |
1.5 |
* @version $Id: TCPDataHandler.java,v 1.4 2001/01/23 16:56:31 tdb1 Exp $ |
21 |
tdb |
1.1 |
*/ |
22 |
tdb |
1.2 |
class TCPDataHandler extends Thread { |
23 |
tdb |
1.1 |
|
24 |
|
|
//---FINAL ATTRIBUTES--- |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* The current CVS revision of this class |
28 |
|
|
*/ |
29 |
tdb |
1.5 |
public final String REVISION = "$Revision: 1.4 $"; |
30 |
tdb |
1.1 |
|
31 |
|
|
//---STATIC METHODS--- |
32 |
|
|
|
33 |
|
|
//---CONSTRUCTORS--- |
34 |
|
|
|
35 |
tdb |
1.4 |
public TCPDataHandler(Socket socket) throws IOException { |
36 |
tdb |
1.1 |
_socket = socket; |
37 |
tdb |
1.4 |
_queue = new Queue(); |
38 |
tdb |
1.1 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
39 |
|
|
_socketOut = new PrintWriter(_socket.getOutputStream()); |
40 |
|
|
_logger.write(toString(), Logger.SYSINIT, "created"); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
//---PUBLIC METHODS--- |
44 |
|
|
|
45 |
tdb |
1.2 |
public void run() { |
46 |
|
|
run = true; |
47 |
tdb |
1.5 |
_queueID = _queue.getQueue(); |
48 |
tdb |
1.2 |
while(run) { |
49 |
|
|
try { |
50 |
tdb |
1.5 |
String xml = (String) _queue.get(_queueID); |
51 |
tdb |
1.2 |
_socketOut.println(xml); |
52 |
|
|
_socketOut.flush(); |
53 |
|
|
} |
54 |
|
|
catch(InvalidQueueException e) { |
55 |
|
|
run = false; |
56 |
|
|
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
57 |
|
|
} |
58 |
|
|
} |
59 |
tdb |
1.1 |
_logger.write(toString(), Logger.SYSMSG, "Shutting Down"); |
60 |
tdb |
1.2 |
try { |
61 |
|
|
_socketOut.close(); |
62 |
|
|
_socketIn.close(); |
63 |
|
|
_socket.close(); |
64 |
|
|
} |
65 |
|
|
catch(IOException e) { |
66 |
|
|
_logger.write(toString(), Logger.ERROR, "Exception whilst shutting down: "+e); |
67 |
|
|
} |
68 |
tdb |
1.5 |
_queue.removeQueue(_queueID); |
69 |
tdb |
1.2 |
} |
70 |
|
|
|
71 |
|
|
public void shutdown() { |
72 |
|
|
run=false; |
73 |
tdb |
1.5 |
_queue.releaseQueue(_queueID); |
74 |
tdb |
1.1 |
} |
75 |
|
|
|
76 |
|
|
/** |
77 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
78 |
|
|
* method to provide clean logging (every class should have this). |
79 |
|
|
* |
80 |
|
|
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
81 |
|
|
* to format the toString() |
82 |
|
|
* |
83 |
|
|
* @return the name of this class and its CVS revision |
84 |
|
|
*/ |
85 |
|
|
public String toString() { |
86 |
|
|
return FormatName.getName( |
87 |
|
|
_name, |
88 |
|
|
getClass().getName(), |
89 |
|
|
REVISION); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
//---PRIVATE METHODS--- |
93 |
|
|
|
94 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
95 |
|
|
|
96 |
tdb |
1.4 |
public Queue getQueue() { |
97 |
|
|
return _queue; |
98 |
|
|
} |
99 |
|
|
|
100 |
tdb |
1.1 |
//---ATTRIBUTES--- |
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* This is the friendly identifier of the |
104 |
|
|
* component this class is running in. |
105 |
|
|
* eg, a Filter may be called "filter1", |
106 |
|
|
* If this class does not have an owning |
107 |
|
|
* component, a name from the configuration |
108 |
|
|
* can be placed here. This name could also |
109 |
|
|
* be changed to null for utility classes. |
110 |
|
|
*/ |
111 |
|
|
private String _name = ClientInterfaceMain.NAME; |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* This holds a reference to the |
115 |
|
|
* system logger that is being used. |
116 |
|
|
*/ |
117 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* A hook to the inbound data from the socket |
121 |
|
|
*/ |
122 |
|
|
private BufferedReader _socketIn; |
123 |
|
|
|
124 |
|
|
/** |
125 |
|
|
* A hook to the outbound stream for the socket |
126 |
|
|
*/ |
127 |
|
|
private PrintWriter _socketOut; |
128 |
|
|
|
129 |
|
|
private Socket _socket; |
130 |
tdb |
1.2 |
|
131 |
|
|
private Queue _queue; |
132 |
|
|
private boolean run; |
133 |
tdb |
1.5 |
private int _queueID; |
134 |
tdb |
1.1 |
|
135 |
|
|
//---STATIC ATTRIBUTES--- |
136 |
|
|
|
137 |
|
|
} |