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