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 |
* @author $Author: tdb1 $ |
20 |
* @version $Id: TCPDataHandler.java,v 1.3 2001/01/23 00:01:39 tdb1 Exp $ |
21 |
*/ |
22 |
class TCPDataHandler extends Thread { |
23 |
|
24 |
//---FINAL ATTRIBUTES--- |
25 |
|
26 |
/** |
27 |
* The current CVS revision of this class |
28 |
*/ |
29 |
public final String REVISION = "$Revision: 1.3 $"; |
30 |
|
31 |
//---STATIC METHODS--- |
32 |
|
33 |
//---CONSTRUCTORS--- |
34 |
|
35 |
public TCPDataHandler(Socket socket) throws IOException { |
36 |
_socket = socket; |
37 |
_queue = new Queue(); |
38 |
_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 |
public void run() { |
46 |
run = true; |
47 |
int qID = _queue.getQueue(); |
48 |
while(run) { |
49 |
try { |
50 |
String xml = (String) _queue.get(qID); |
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 |
_logger.write(toString(), Logger.SYSMSG, "Shutting Down"); |
60 |
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 |
_queue.removeQueue(qID); |
69 |
} |
70 |
|
71 |
public void shutdown() { |
72 |
run=false; |
73 |
} |
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 |
public Queue getQueue() { |
96 |
return _queue; |
97 |
} |
98 |
|
99 |
//---ATTRIBUTES--- |
100 |
|
101 |
/** |
102 |
* This is the friendly identifier of the |
103 |
* component this class is running in. |
104 |
* eg, a Filter may be called "filter1", |
105 |
* If this class does not have an owning |
106 |
* component, a name from the configuration |
107 |
* can be placed here. This name could also |
108 |
* be changed to null for utility classes. |
109 |
*/ |
110 |
private String _name = ClientInterfaceMain.NAME; |
111 |
|
112 |
/** |
113 |
* This holds a reference to the |
114 |
* system logger that is being used. |
115 |
*/ |
116 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
117 |
|
118 |
/** |
119 |
* A hook to the inbound data from the socket |
120 |
*/ |
121 |
private BufferedReader _socketIn; |
122 |
|
123 |
/** |
124 |
* A hook to the outbound stream for the socket |
125 |
*/ |
126 |
private PrintWriter _socketOut; |
127 |
|
128 |
private Socket _socket; |
129 |
|
130 |
private Queue _queue; |
131 |
private boolean run; |
132 |
|
133 |
//---STATIC ATTRIBUTES--- |
134 |
|
135 |
} |