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.3 |
* @version $Id: TCPDataHandler.java,v 1.2 2001/01/22 02:57:38 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.3 |
public final String REVISION = "$Revision: 1.2 $"; |
30 |
tdb |
1.1 |
|
31 |
|
|
//---STATIC METHODS--- |
32 |
|
|
|
33 |
|
|
//---CONSTRUCTORS--- |
34 |
|
|
|
35 |
tdb |
1.2 |
public TCPDataHandler(Socket socket, Queue queue) throws IOException { |
36 |
tdb |
1.1 |
_socket = socket; |
37 |
tdb |
1.2 |
_queue = queue; |
38 |
|
|
_queueID = queue.getQueue(); |
39 |
tdb |
1.1 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
40 |
|
|
_socketOut = new PrintWriter(_socket.getOutputStream()); |
41 |
|
|
_logger.write(toString(), Logger.SYSINIT, "created"); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
//---PUBLIC METHODS--- |
45 |
|
|
|
46 |
tdb |
1.2 |
public void run() { |
47 |
|
|
run = true; |
48 |
|
|
while(run) { |
49 |
|
|
try { |
50 |
|
|
String xml = (String) _queue.get(_queueID); |
51 |
|
|
_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.3 |
_queue.removeQueue(_queueID); |
69 |
tdb |
1.2 |
} |
70 |
|
|
|
71 |
|
|
public void shutdown() { |
72 |
|
|
run=false; |
73 |
tdb |
1.1 |
} |
74 |
|
|
|
75 |
|
|
/** |
76 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
77 |
|
|
* method to provide clean logging (every class should have this). |
78 |
|
|
* |
79 |
|
|
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
80 |
|
|
* to format the toString() |
81 |
|
|
* |
82 |
|
|
* @return the name of this class and its CVS revision |
83 |
|
|
*/ |
84 |
|
|
public String toString() { |
85 |
|
|
return FormatName.getName( |
86 |
|
|
_name, |
87 |
|
|
getClass().getName(), |
88 |
|
|
REVISION); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
//---PRIVATE METHODS--- |
92 |
|
|
|
93 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
94 |
|
|
|
95 |
|
|
//---ATTRIBUTES--- |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* This is the friendly identifier of the |
99 |
|
|
* component this class is running in. |
100 |
|
|
* eg, a Filter may be called "filter1", |
101 |
|
|
* If this class does not have an owning |
102 |
|
|
* component, a name from the configuration |
103 |
|
|
* can be placed here. This name could also |
104 |
|
|
* be changed to null for utility classes. |
105 |
|
|
*/ |
106 |
|
|
private String _name = ClientInterfaceMain.NAME; |
107 |
|
|
|
108 |
|
|
/** |
109 |
|
|
* This holds a reference to the |
110 |
|
|
* system logger that is being used. |
111 |
|
|
*/ |
112 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* A hook to the inbound data from the socket |
116 |
|
|
*/ |
117 |
|
|
private BufferedReader _socketIn; |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* A hook to the outbound stream for the socket |
121 |
|
|
*/ |
122 |
|
|
private PrintWriter _socketOut; |
123 |
|
|
|
124 |
|
|
private Socket _socket; |
125 |
tdb |
1.2 |
|
126 |
|
|
private Queue _queue; |
127 |
|
|
private int _queueID; |
128 |
|
|
private boolean run; |
129 |
tdb |
1.1 |
|
130 |
|
|
//---STATIC ATTRIBUTES--- |
131 |
|
|
|
132 |
|
|
} |