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.6
Committed: Tue Jan 23 18:23:03 2001 UTC (23 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.5: +4 -4 lines
Log Message:
General bug fixing. It now works for the features it should.

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.6 * @version $Id: TCPControlHandler.java,v 1.5 2001/01/23 17:22:01 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.6 public final String REVISION = "$Revision: 1.5 $";
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     try {
79     String returnedProperty = myConfig.getProperty("Client."+cmd);
80     _socketOut.println(returnedProperty);
81     _socketOut.flush();
82    
83     } catch (org.omg.CORBA.MARSHAL e) {
84     _socketOut.println("ERROR");
85     _socketOut.flush();
86     }
87     cmd = _socketIn.readLine();
88     }
89     _socketOut.println("OK");
90     _socketOut.flush();
91     _logger.write(toString(), Logger.SYSMSG, "Client has been configured");
92    
93     }
94     else if(cmd.equals("STARTDATA")) {
95     if(_dataHandler == null) {
96     // create a serversocket
97     ServerSocket ss = new ServerSocket(0);
98     // get the port
99     int port = ss.getLocalPort();
100     // tell the client
101     _socketOut.println(port);
102     _socketOut.flush();
103     // call accept()
104     Socket s = ss.accept();
105     // when we get the Socket back, give it to the data thread
106 tdb 1.4 TCPDataHandler dh = new TCPDataHandler(s);
107     // register it !
108     _ps.register(dh.getQueue(), _hostList);
109 tdb 1.2 // start it up
110     dh.start();
111 tdb 1.1 // HOLD a ref to that data thread
112     _dataHandler = dh;
113     _socketOut.println("OK");
114     _socketOut.flush();
115     _logger.write(toString(), Logger.SYSMSG, "Data stream started at Clients Request on port: "+port);
116     }
117     else {
118     _socketOut.println("ERROR");
119     _socketOut.flush();
120     }
121     }
122     else if(cmd.equals("STOPDATA")) {
123     if(_dataHandler != null) {
124 tdb 1.6 // Deregister
125     _ps.deregister(_dataHandler.getQueue(), _hostList);
126 tdb 1.2 // Shut down the data handler
127 tdb 1.1 _dataHandler.shutdown();
128     // destroy the reference to it ?
129     _dataHandler = null;
130     _socketOut.println("OK");
131     _socketOut.flush();
132     _logger.write(toString(), Logger.SYSMSG, "Data stream stopped at Clients Request");
133     }
134     else {
135     _socketOut.println("ERROR");
136     _socketOut.flush();
137     }
138     }
139 tdb 1.4 else if(cmd.equals("SETHOSTLIST")) {
140 tdb 1.5 if(_dataHandler == null) {
141     _socketOut.println("OK");
142     _socketOut.flush();
143     cmd = _socketIn.readLine();
144     _hostList = cmd;
145     _socketOut.println("OK");
146     _socketOut.flush();
147     }
148     else {
149     _socketOut.println("ERROR");
150     _socketOut.flush();
151     }
152 tdb 1.4 }
153 tdb 1.2 else if(cmd.equals("DISCONNECT")) {
154 tdb 1.1 run=false;
155 tdb 1.3 if(_dataHandler != null) {
156     // Shut down the data handler
157     _dataHandler.shutdown();
158     // destroy the reference to it ?
159     _dataHandler = null;
160     _logger.write(toString(), Logger.SYSMSG, "Data stream stopped at Clients Request");
161     }
162 tdb 1.1 _socketOut.println("OK");
163     _socketOut.flush();
164     _socketIn.close();
165     _socketOut.close();
166     _socket.close();
167     _logger.write(toString(), Logger.SYSMSG, "Closing at Clients Request");
168     }
169     else {
170     _socketOut.println("ERROR");
171     _socketOut.flush();
172     }
173     }
174     catch(IOException e) {
175     _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
176     }
177     }
178     }
179    
180     /**
181     * Overrides the {@link java.lang.Object#toString() Object.toString()}
182     * method to provide clean logging (every class should have this).
183     *
184     * This uses the uk.ac.ukc.iscream.util.NameFormat class
185     * to format the toString()
186     *
187     * @return the name of this class and its CVS revision
188     */
189     public String toString() {
190     return FormatName.getName(
191     _name,
192     getClass().getName(),
193     REVISION);
194     }
195    
196     //---PRIVATE METHODS---
197    
198     //---ACCESSOR/MUTATOR METHODS---
199    
200     //---ATTRIBUTES---
201    
202     /**
203     * This is the friendly identifier of the
204     * component this class is running in.
205     * eg, a Filter may be called "filter1",
206     * If this class does not have an owning
207     * component, a name from the configuration
208     * can be placed here. This name could also
209     * be changed to null for utility classes.
210     */
211     private String _name = ClientInterfaceMain.NAME;
212    
213     /**
214     * This holds a reference to the
215     * system logger that is being used.
216     */
217     private Logger _logger = ReferenceManager.getInstance().getLogger();
218    
219     /**
220     * A reference to the Configuration Manager the system is using
221     */
222     private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
223    
224    
225     /**
226     * The socket we are talking on
227     */
228     private Socket _socket;
229    
230     /**
231     * A hook to the inbound data from the socket
232     */
233     private BufferedReader _socketIn;
234    
235     /**
236     * A hook to the outbound stream for the socket
237     */
238     private PrintWriter _socketOut;
239 tdb 1.4
240 tdb 1.1 /**
241 tdb 1.4 * A reference to the PacketSorter.
242     */
243     private PacketSorter _ps;
244 tdb 1.1
245     private TCPDataHandler _dataHandler;
246    
247     private String _clientName;
248 tdb 1.4 private String _hostList;
249 tdb 1.1
250     //---STATIC ATTRIBUTES---
251    
252     }