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