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.4
Committed: Tue Jan 23 16:56:31 2001 UTC (23 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.3: +24 -10 lines
Log Message:
Changed to support the new Protocol 1.1. This protocol supports selecting the
hosts a client wishes to monitor at connect time.

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