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.3
Committed: Mon Jan 22 23:08:15 2001 UTC (23 years, 4 months ago) by tdb
Branch: MAIN
CVS Tags: CLI_PROTOCOL_1_0
Changes since 1.2: +9 -2 lines
Log Message:
Fixed a bug with the DISCONNECT command not closing the data channel.

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.3 * @version $Id: TCPControlHandler.java,v 1.2 2001/01/22 02:57:38 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.3 public final String REVISION = "$Revision: 1.2 $";
32 tdb 1.1
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 tdb 1.3 if(_dataHandler != null) {
137     // Shut down the data handler
138     _dataHandler.shutdown();
139     // destroy the reference to it ?
140     _dataHandler = null;
141     _logger.write(toString(), Logger.SYSMSG, "Data stream stopped at Clients Request");
142     }
143 tdb 1.1 _socketOut.println("OK");
144     _socketOut.flush();
145     _socketIn.close();
146     _socketOut.close();
147     _socket.close();
148     _logger.write(toString(), Logger.SYSMSG, "Closing at Clients Request");
149     }
150     else {
151     _socketOut.println("ERROR");
152     _socketOut.flush();
153     }
154     }
155     catch(IOException e) {
156     _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
157     }
158     }
159     }
160    
161     /**
162     * Overrides the {@link java.lang.Object#toString() Object.toString()}
163     * method to provide clean logging (every class should have this).
164     *
165     * This uses the uk.ac.ukc.iscream.util.NameFormat class
166     * to format the toString()
167     *
168     * @return the name of this class and its CVS revision
169     */
170     public String toString() {
171     return FormatName.getName(
172     _name,
173     getClass().getName(),
174     REVISION);
175     }
176    
177     //---PRIVATE METHODS---
178    
179     //---ACCESSOR/MUTATOR METHODS---
180    
181     //---ATTRIBUTES---
182    
183     /**
184     * This is the friendly identifier of the
185     * component this class is running in.
186     * eg, a Filter may be called "filter1",
187     * If this class does not have an owning
188     * component, a name from the configuration
189     * can be placed here. This name could also
190     * be changed to null for utility classes.
191     */
192     private String _name = ClientInterfaceMain.NAME;
193    
194     /**
195     * This holds a reference to the
196     * system logger that is being used.
197     */
198     private Logger _logger = ReferenceManager.getInstance().getLogger();
199    
200     /**
201     * A reference to the Configuration Manager the system is using
202     */
203     private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
204    
205    
206     /**
207     * The socket we are talking on
208     */
209     private Socket _socket;
210    
211     /**
212     * A hook to the inbound data from the socket
213     */
214     private BufferedReader _socketIn;
215    
216     /**
217     * A hook to the outbound stream for the socket
218     */
219     private PrintWriter _socketOut;
220    
221     /**
222 tdb 1.2 * A reference to the Queue
223 tdb 1.1 */
224 tdb 1.2 private Queue _queue;
225 tdb 1.1
226     private TCPDataHandler _dataHandler;
227    
228     private String _clientName;
229    
230     //---STATIC ATTRIBUTES---
231    
232     }