ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/clientinterface/TCPControlHandler.java
Revision: 1.8
Committed: Wed Jan 24 00:55:33 2001 UTC (23 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.7: +9 -3 lines
Log Message:
A bit of tightening up. I think this will have caught the NullPointer exception
that was been thrown when clients "uncleanly" disconnected.

File Contents

# User Rev Content
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 tdb 1.8 * @version $Id: TCPControlHandler.java,v 1.7 2001/01/24 00:43:19 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 tdb 1.8 public final String REVISION = "$Revision: 1.7 $";
32 tdb 1.1
33 tdb 1.4 public static final String PROTOVER = "PROTOCOL 1.1";
34 tdb 1.1
35     //---STATIC METHODS---
36    
37     //---CONSTRUCTORS---
38    
39 tdb 1.4 public TCPControlHandler(Socket socket, PacketSorter ps) throws IOException {
40 tdb 1.1 _socket = socket;
41 tdb 1.4 _ps = ps;
42 tdb 1.1 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
43     _socketOut = new PrintWriter(_socket.getOutputStream());
44 tdb 1.4 _hostList = "";
45 tdb 1.1 _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 tdb 1.7 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 tdb 1.1 _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 tdb 1.4 TCPDataHandler dh = new TCPDataHandler(s);
113     // register it !
114     _ps.register(dh.getQueue(), _hostList);
115 tdb 1.2 // start it up
116     dh.start();
117 tdb 1.1 // 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 tdb 1.6 // Deregister
131     _ps.deregister(_dataHandler.getQueue(), _hostList);
132 tdb 1.2 // Shut down the data handler
133 tdb 1.1 _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 tdb 1.4 else if(cmd.equals("SETHOSTLIST")) {
146 tdb 1.5 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 tdb 1.4 }
159 tdb 1.2 else if(cmd.equals("DISCONNECT")) {
160 tdb 1.1 run=false;
161 tdb 1.3 if(_dataHandler != null) {
162 tdb 1.8 // Deregister
163     _ps.deregister(_dataHandler.getQueue(), _hostList);
164 tdb 1.3 // Shut down the data handler
165     _dataHandler.shutdown();
166     // destroy the reference to it ?
167     _dataHandler = null;
168 tdb 1.8 _socketOut.println("OK");
169     _socketOut.flush();
170 tdb 1.3 _logger.write(toString(), Logger.SYSMSG, "Data stream stopped at Clients Request");
171     }
172 tdb 1.1 _socketOut.println("OK");
173     _socketOut.flush();
174     _socketIn.close();
175     _socketOut.close();
176     _socket.close();
177     _logger.write(toString(), Logger.SYSMSG, "Closing at Clients Request");
178     }
179     else {
180     _socketOut.println("ERROR");
181     _socketOut.flush();
182     }
183     }
184     catch(IOException e) {
185 tdb 1.8 run=false;
186     _logger.write(toString(), Logger.FATAL, "Fatal communication error, shutdown pending");
187 tdb 1.1 }
188     }
189 tdb 1.8 _logger.write(toString(), Logger.SYSMSG, "Shutting down Control Handler, client has gone.");
190 tdb 1.1 }
191    
192     /**
193     * Overrides the {@link java.lang.Object#toString() Object.toString()}
194     * method to provide clean logging (every class should have this).
195     *
196     * This uses the uk.ac.ukc.iscream.util.NameFormat class
197     * to format the toString()
198     *
199     * @return the name of this class and its CVS revision
200     */
201     public String toString() {
202     return FormatName.getName(
203     _name,
204     getClass().getName(),
205     REVISION);
206     }
207    
208     //---PRIVATE METHODS---
209    
210     //---ACCESSOR/MUTATOR METHODS---
211    
212     //---ATTRIBUTES---
213    
214     /**
215     * This is the friendly identifier of the
216     * component this class is running in.
217     * eg, a Filter may be called "filter1",
218     * If this class does not have an owning
219     * component, a name from the configuration
220     * can be placed here. This name could also
221     * be changed to null for utility classes.
222     */
223     private String _name = ClientInterfaceMain.NAME;
224    
225     /**
226     * This holds a reference to the
227     * system logger that is being used.
228     */
229     private Logger _logger = ReferenceManager.getInstance().getLogger();
230    
231     /**
232     * A reference to the Configuration Manager the system is using
233     */
234     private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
235    
236    
237     /**
238     * The socket we are talking on
239     */
240     private Socket _socket;
241    
242     /**
243     * A hook to the inbound data from the socket
244     */
245     private BufferedReader _socketIn;
246    
247     /**
248     * A hook to the outbound stream for the socket
249     */
250     private PrintWriter _socketOut;
251 tdb 1.4
252 tdb 1.1 /**
253 tdb 1.4 * A reference to the PacketSorter.
254     */
255     private PacketSorter _ps;
256 tdb 1.1
257     private TCPDataHandler _dataHandler;
258    
259     private String _clientName;
260 tdb 1.4 private String _hostList;
261 tdb 1.1
262     //---STATIC ATTRIBUTES---
263    
264     }