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.net.ServerSocket; |
10 |
import java.io.InputStream; |
11 |
import java.io.OutputStream; |
12 |
import java.io.IOException; |
13 |
import java.io.BufferedReader; |
14 |
import java.io.PrintWriter; |
15 |
import java.io.InputStreamReader; |
16 |
|
17 |
|
18 |
/** |
19 |
* Acts as a Control Handler to a TCP based client. |
20 |
* |
21 |
* @author $Author: tdb1 $ |
22 |
* @version $Id: TCPControlHandler.java,v 1.6 2001/01/23 18:23:03 tdb1 Exp $ |
23 |
*/ |
24 |
class TCPControlHandler extends Thread { |
25 |
|
26 |
//---FINAL ATTRIBUTES--- |
27 |
|
28 |
/** |
29 |
* The current CVS revision of this class |
30 |
*/ |
31 |
public final String REVISION = "$Revision: 1.6 $"; |
32 |
|
33 |
public static final String PROTOVER = "PROTOCOL 1.1"; |
34 |
|
35 |
//---STATIC METHODS--- |
36 |
|
37 |
//---CONSTRUCTORS--- |
38 |
|
39 |
public TCPControlHandler(Socket socket, PacketSorter ps) throws IOException { |
40 |
_socket = socket; |
41 |
_ps = ps; |
42 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
43 |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
44 |
_hostList = ""; |
45 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
46 |
} |
47 |
|
48 |
//---PUBLIC METHODS--- |
49 |
|
50 |
public void run() { |
51 |
boolean run = true; |
52 |
// lets init some communications with the client |
53 |
try { |
54 |
_socketOut.println(PROTOVER); |
55 |
_socketOut.flush(); |
56 |
_clientName = _socketIn.readLine(); |
57 |
_socketOut.println("OK"); |
58 |
_socketOut.flush(); |
59 |
} |
60 |
catch(IOException e) { |
61 |
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
62 |
run=false; |
63 |
} |
64 |
|
65 |
_logger.write(toString(), Logger.SYSMSG, "Client has connected: "+_clientName); |
66 |
|
67 |
while(run) { |
68 |
try { |
69 |
String cmd = _socketIn.readLine(); |
70 |
if(cmd.equals("STARTCONFIG")) { |
71 |
Configuration myConfig = _configManager.getConfiguration("Client."+_clientName); |
72 |
_socketOut.println("OK"); |
73 |
_socketOut.flush(); |
74 |
// get properties |
75 |
cmd = _socketIn.readLine(); |
76 |
while(!cmd.equals("ENDCONFIG")) { |
77 |
// get the property |
78 |
if(cmd.startsWith("Client.") || cmd.startsWith("Host.")) { |
79 |
try { |
80 |
String returnedProperty = myConfig.getProperty(cmd); |
81 |
_socketOut.println(returnedProperty); |
82 |
_socketOut.flush(); |
83 |
} |
84 |
catch (org.omg.CORBA.MARSHAL e) { |
85 |
_socketOut.println("ERROR"); |
86 |
_socketOut.flush(); |
87 |
} |
88 |
} |
89 |
else { |
90 |
_socketOut.println("ERROR"); |
91 |
_socketOut.flush(); |
92 |
} |
93 |
cmd = _socketIn.readLine(); |
94 |
} |
95 |
_socketOut.println("OK"); |
96 |
_socketOut.flush(); |
97 |
_logger.write(toString(), Logger.SYSMSG, "Client has been configured"); |
98 |
|
99 |
} |
100 |
else if(cmd.equals("STARTDATA")) { |
101 |
if(_dataHandler == null) { |
102 |
// create a serversocket |
103 |
ServerSocket ss = new ServerSocket(0); |
104 |
// get the port |
105 |
int port = ss.getLocalPort(); |
106 |
// tell the client |
107 |
_socketOut.println(port); |
108 |
_socketOut.flush(); |
109 |
// call accept() |
110 |
Socket s = ss.accept(); |
111 |
// when we get the Socket back, give it to the data thread |
112 |
TCPDataHandler dh = new TCPDataHandler(s); |
113 |
// register it ! |
114 |
_ps.register(dh.getQueue(), _hostList); |
115 |
// start it up |
116 |
dh.start(); |
117 |
// HOLD a ref to that data thread |
118 |
_dataHandler = dh; |
119 |
_socketOut.println("OK"); |
120 |
_socketOut.flush(); |
121 |
_logger.write(toString(), Logger.SYSMSG, "Data stream started at Clients Request on port: "+port); |
122 |
} |
123 |
else { |
124 |
_socketOut.println("ERROR"); |
125 |
_socketOut.flush(); |
126 |
} |
127 |
} |
128 |
else if(cmd.equals("STOPDATA")) { |
129 |
if(_dataHandler != null) { |
130 |
// Deregister |
131 |
_ps.deregister(_dataHandler.getQueue(), _hostList); |
132 |
// Shut down the data handler |
133 |
_dataHandler.shutdown(); |
134 |
// destroy the reference to it ? |
135 |
_dataHandler = null; |
136 |
_socketOut.println("OK"); |
137 |
_socketOut.flush(); |
138 |
_logger.write(toString(), Logger.SYSMSG, "Data stream stopped at Clients Request"); |
139 |
} |
140 |
else { |
141 |
_socketOut.println("ERROR"); |
142 |
_socketOut.flush(); |
143 |
} |
144 |
} |
145 |
else if(cmd.equals("SETHOSTLIST")) { |
146 |
if(_dataHandler == null) { |
147 |
_socketOut.println("OK"); |
148 |
_socketOut.flush(); |
149 |
cmd = _socketIn.readLine(); |
150 |
_hostList = cmd; |
151 |
_socketOut.println("OK"); |
152 |
_socketOut.flush(); |
153 |
} |
154 |
else { |
155 |
_socketOut.println("ERROR"); |
156 |
_socketOut.flush(); |
157 |
} |
158 |
} |
159 |
else if(cmd.equals("DISCONNECT")) { |
160 |
run=false; |
161 |
if(_dataHandler != null) { |
162 |
// Shut down the data handler |
163 |
_dataHandler.shutdown(); |
164 |
// destroy the reference to it ? |
165 |
_dataHandler = null; |
166 |
_logger.write(toString(), Logger.SYSMSG, "Data stream stopped at Clients Request"); |
167 |
} |
168 |
_socketOut.println("OK"); |
169 |
_socketOut.flush(); |
170 |
_socketIn.close(); |
171 |
_socketOut.close(); |
172 |
_socket.close(); |
173 |
_logger.write(toString(), Logger.SYSMSG, "Closing at Clients Request"); |
174 |
} |
175 |
else { |
176 |
_socketOut.println("ERROR"); |
177 |
_socketOut.flush(); |
178 |
} |
179 |
} |
180 |
catch(IOException e) { |
181 |
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
182 |
} |
183 |
} |
184 |
} |
185 |
|
186 |
/** |
187 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
188 |
* method to provide clean logging (every class should have this). |
189 |
* |
190 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
191 |
* to format the toString() |
192 |
* |
193 |
* @return the name of this class and its CVS revision |
194 |
*/ |
195 |
public String toString() { |
196 |
return FormatName.getName( |
197 |
_name, |
198 |
getClass().getName(), |
199 |
REVISION); |
200 |
} |
201 |
|
202 |
//---PRIVATE METHODS--- |
203 |
|
204 |
//---ACCESSOR/MUTATOR METHODS--- |
205 |
|
206 |
//---ATTRIBUTES--- |
207 |
|
208 |
/** |
209 |
* This is the friendly identifier of the |
210 |
* component this class is running in. |
211 |
* eg, a Filter may be called "filter1", |
212 |
* If this class does not have an owning |
213 |
* component, a name from the configuration |
214 |
* can be placed here. This name could also |
215 |
* be changed to null for utility classes. |
216 |
*/ |
217 |
private String _name = ClientInterfaceMain.NAME; |
218 |
|
219 |
/** |
220 |
* This holds a reference to the |
221 |
* system logger that is being used. |
222 |
*/ |
223 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
224 |
|
225 |
/** |
226 |
* A reference to the Configuration Manager the system is using |
227 |
*/ |
228 |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
229 |
|
230 |
|
231 |
/** |
232 |
* The socket we are talking on |
233 |
*/ |
234 |
private Socket _socket; |
235 |
|
236 |
/** |
237 |
* A hook to the inbound data from the socket |
238 |
*/ |
239 |
private BufferedReader _socketIn; |
240 |
|
241 |
/** |
242 |
* A hook to the outbound stream for the socket |
243 |
*/ |
244 |
private PrintWriter _socketOut; |
245 |
|
246 |
/** |
247 |
* A reference to the PacketSorter. |
248 |
*/ |
249 |
private PacketSorter _ps; |
250 |
|
251 |
private TCPDataHandler _dataHandler; |
252 |
|
253 |
private String _clientName; |
254 |
private String _hostList; |
255 |
|
256 |
//---STATIC ATTRIBUTES--- |
257 |
|
258 |
} |