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.1
Committed: Mon Jan 22 01:42:32 2001 UTC (23 years, 4 months ago) by tdb
Branch: MAIN
Log Message:
The Control Handler for a TCP Client. Manages setting up the clinet, responding
to client requests, and keeping the client up to date.

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     * @author $Author$
22     * @version $Id$
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.1 $";
32    
33     public static final String PROTOVER = "PROTOCOL 1.0";
34    
35     //---STATIC METHODS---
36    
37     //---CONSTRUCTORS---
38    
39     public TCPControlHandler(Socket socket, ClientInterfaceServant cli) throws IOException {
40     _socket = socket;
41     _cli = cli;
42     _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     TCPDataHandler dh = new TCPDataHandler(s);
106     // start the data thread (need to be a thread ?) - NO
107     // register the data thread
108     _cli.register(dh);
109     // HOLD a ref to that data thread
110     _dataHandler = dh;
111     _socketOut.println("OK");
112     _socketOut.flush();
113     _logger.write(toString(), Logger.SYSMSG, "Data stream started at Clients Request on port: "+port);
114     }
115     else {
116     _socketOut.println("ERROR");
117     _socketOut.flush();
118     }
119     }
120     else if(cmd.equals("STOPDATA")) {
121     if(_dataHandler != null) {
122     // deregister the data thread
123     _cli.deregister(_dataHandler);
124     // SHUT DOWN THE DATA HANDLER ?
125     _dataHandler.shutdown();
126     // destroy the reference to it ?
127     _dataHandler = null;
128     _socketOut.println("OK");
129     _socketOut.flush();
130     _logger.write(toString(), Logger.SYSMSG, "Data stream stopped at Clients Request");
131     }
132     else {
133     _socketOut.println("ERROR");
134     _socketOut.flush();
135     }
136     }
137     else if(cmd.equals("END")) {
138     run=false;
139     _socketOut.println("OK");
140     _socketOut.flush();
141     _socketIn.close();
142     _socketOut.close();
143     _socket.close();
144     _logger.write(toString(), Logger.SYSMSG, "Closing at Clients Request");
145     }
146     else {
147     _socketOut.println("ERROR");
148     _socketOut.flush();
149     }
150     }
151     catch(IOException e) {
152     _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
153     }
154     }
155     }
156    
157     /**
158     * Overrides the {@link java.lang.Object#toString() Object.toString()}
159     * method to provide clean logging (every class should have this).
160     *
161     * This uses the uk.ac.ukc.iscream.util.NameFormat class
162     * to format the toString()
163     *
164     * @return the name of this class and its CVS revision
165     */
166     public String toString() {
167     return FormatName.getName(
168     _name,
169     getClass().getName(),
170     REVISION);
171     }
172    
173     //---PRIVATE METHODS---
174    
175     //---ACCESSOR/MUTATOR METHODS---
176    
177     //---ATTRIBUTES---
178    
179     /**
180     * This is the friendly identifier of the
181     * component this class is running in.
182     * eg, a Filter may be called "filter1",
183     * If this class does not have an owning
184     * component, a name from the configuration
185     * can be placed here. This name could also
186     * be changed to null for utility classes.
187     */
188     private String _name = ClientInterfaceMain.NAME;
189    
190     /**
191     * This holds a reference to the
192     * system logger that is being used.
193     */
194     private Logger _logger = ReferenceManager.getInstance().getLogger();
195    
196     /**
197     * A reference to the Configuration Manager the system is using
198     */
199     private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
200    
201    
202     /**
203     * The socket we are talking on
204     */
205     private Socket _socket;
206    
207     /**
208     * A hook to the inbound data from the socket
209     */
210     private BufferedReader _socketIn;
211    
212     /**
213     * A hook to the outbound stream for the socket
214     */
215     private PrintWriter _socketOut;
216    
217     /**
218     * A reference to the ClientInterfaceServant.
219     */
220     private ClientInterfaceServant _cli;
221    
222     private TCPDataHandler _dataHandler;
223    
224     private String _clientName;
225    
226     //---STATIC ATTRIBUTES---
227    
228     }