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
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/clientinterface/TCPControlHandler.java (file contents):
Revision 1.8 by tdb, Wed Jan 24 00:55:33 2001 UTC vs.
Revision 1.9 by tdb, Sat Jan 27 23:30:40 2001 UTC

# Line 30 | Line 30 | class TCPControlHandler extends Thread {
30       */
31      public final String REVISION = "$Revision$";
32      
33 +    /**
34 +     * This is our protocol version. It is hardcoded to ensure
35 +     * that it doesn't get accidently changed. A change to the
36 +     * protocol would also require changing this classes code !
37 +     */
38      public static final String PROTOVER = "PROTOCOL 1.1";
39      
40   //---STATIC METHODS---
41  
42   //---CONSTRUCTORS---
43 <
44 <    public TCPControlHandler(Socket socket, PacketSorter ps) throws IOException {
43 >    
44 >    /**
45 >     * Construct a new TCPControlHandler, and setup the reader
46 >     * and writer for the new Socket.
47 >     *
48 >     * @param socket The Socket connected to the new Client
49 >     * @param packetSorter A reference to the PacketSorter in the component
50 >     */
51 >    public TCPControlHandler(Socket socket, PacketSorter packetSorter) throws IOException {
52          _socket = socket;
53 <        _ps = ps;
53 >        _packetSorter = packetSorter;
54 >        // setup the reader & writer
55          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
56          _socketOut = new PrintWriter(_socket.getOutputStream());
57 +        // set the default hostlist to "all host", for backward compatibility
58          _hostList = "";
59          _logger.write(toString(), Logger.SYSINIT, "created");
60      }
61      
62   //---PUBLIC METHODS---
63 <
63 >    
64 >    /**
65 >     * This method initiates the thread, setting things up, and then
66 >     * reading commands from the Client. It handles setting up of the
67 >     * DataHandler, and clean shutting down.
68 >     */
69      public void run() {
70          boolean run = true;
71 <        // lets init some communications with the client
71 >        // Tell the client our protocol, and ask it for it's name
72          try {
73              _socketOut.println(PROTOVER);
74              _socketOut.flush();
# Line 64 | Line 83 | class TCPControlHandler extends Thread {
83          
84          _logger.write(toString(), Logger.SYSMSG, "Client has connected: "+_clientName);
85          
86 +        // loop until we decide to shutdown (run=false)
87          while(run) {
88              try {
89                  String cmd = _socketIn.readLine();
90 +                // make a decision about what to do
91                  if(cmd.equals("STARTCONFIG")) {
92 +                    // get the configuration for this client
93                      Configuration myConfig = _configManager.getConfiguration("Client."+_clientName);
94                      _socketOut.println("OK");
95                      _socketOut.flush();
96                      // get properties
97                      cmd = _socketIn.readLine();
98 +                    // provide all the requested properties
99                      while(!cmd.equals("ENDCONFIG")) {
100 <                        // get the property
100 >                        // client is restricted to this properties
101                          if(cmd.startsWith("Client.") || cmd.startsWith("Host.")) {
102                              try {
103                                  String returnedProperty = myConfig.getProperty(cmd);    
# Line 98 | Line 121 | class TCPControlHandler extends Thread {
121                      
122                  }
123                  else if(cmd.equals("STARTDATA")) {
124 +                    // if we don't have a DataHandler, set one up
125                      if(_dataHandler == null) {
126                          // create a serversocket
127                          ServerSocket ss = new ServerSocket(0);
128                          // get the port
129                          int port = ss.getLocalPort();
130 <                        // tell the client
130 >                        // tell the client the port
131                          _socketOut.println(port);
132                          _socketOut.flush();
133 <                        // call accept()
133 >                        // wait for the client to connect
134                          Socket s = ss.accept();
135                          // when we get the Socket back, give it to the data thread
136                          TCPDataHandler dh = new TCPDataHandler(s);
137 <                        // register it !
138 <                        _ps.register(dh.getQueue(), _hostList);
139 <                        // start it up
137 >                        // register the DataHandler's queue, giving the host list
138 >                        _packetSorter.register(dh.getQueue(), _hostList);
139 >                        // start up the DataHandler
140                          dh.start();
141 <                        // HOLD a ref to that data thread
141 >                        // Hold a reference to the DataHandler, so we can stop it later
142                          _dataHandler = dh;
143                          _socketOut.println("OK");
144                          _socketOut.flush();
# Line 126 | Line 150 | class TCPControlHandler extends Thread {
150                      }
151                  }
152                  else if(cmd.equals("STOPDATA")) {
153 +                    // if we have a DataHandler, shut it down
154                      if(_dataHandler != null) {
155 <                        // Deregister
156 <                        _ps.deregister(_dataHandler.getQueue(), _hostList);
155 >                        // Deregister the DataHandler, giving the host list
156 >                        _packetSorter.deregister(_dataHandler.getQueue(), _hostList);
157                          // Shut down the data handler
158                          _dataHandler.shutdown();
159 <                        // destroy the reference to it ?
159 >                        // Destroy our reference, leaving it for the GC
160                          _dataHandler = null;
161                          _socketOut.println("OK");
162                          _socketOut.flush();
# Line 143 | Line 168 | class TCPControlHandler extends Thread {
168                      }
169                  }
170                  else if(cmd.equals("SETHOSTLIST")) {
171 +                    // we can only set the host list when
172 +                    // the DataHandler is not connected
173                      if(_dataHandler == null) {
174                          _socketOut.println("OK");
175                          _socketOut.flush();
176 +                        // read and set the host list
177                          cmd = _socketIn.readLine();
178                          _hostList = cmd;
179                          _socketOut.println("OK");
# Line 157 | Line 185 | class TCPControlHandler extends Thread {
185                      }
186                  }
187                  else if(cmd.equals("DISCONNECT")) {
188 +                    // we going to disconnect, so lets stop this loop
189                      run=false;
190 +                    // if there is a DataHandler, we'd best shut it down
191                      if(_dataHandler != null) {
192 <                        // Deregister
193 <                        _ps.deregister(_dataHandler.getQueue(), _hostList);
192 >                        // Deregister the DataHandler, giving the host list
193 >                        _packetSorter.deregister(_dataHandler.getQueue(), _hostList);
194                          // Shut down the data handler
195                          _dataHandler.shutdown();
196 <                        // destroy the reference to it ?
196 >                        // Destroy our reference, leaving it for the GC
197                          _dataHandler = null;
198                          _socketOut.println("OK");
199                          _socketOut.flush();
# Line 171 | Line 201 | class TCPControlHandler extends Thread {
201                      }
202                      _socketOut.println("OK");
203                      _socketOut.flush();
204 +                    // close the reader, writer and Socket
205                      _socketIn.close();
206                      _socketOut.close();
207                      _socket.close();
# Line 182 | Line 213 | class TCPControlHandler extends Thread {
213                  }
214              }
215              catch(IOException e) {
216 +                // if we get an exception, the client has probably left, so we stop
217                  run=false;
218                  _logger.write(toString(), Logger.FATAL, "Fatal communication error, shutdown pending");
219              }
# Line 233 | Line 265 | class TCPControlHandler extends Thread {
265       */
266      private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
267      
236
268      /**
269       * The socket we are talking on
270       */
# Line 252 | Line 283 | class TCPControlHandler extends Thread {
283      /**
284           * A reference to the PacketSorter.
285           */
286 <    private PacketSorter _ps;
286 >    private PacketSorter _packetSorter;
287      
288 +    /**
289 +     * A reference to the DataHandler, if there is one
290 +     */
291      private TCPDataHandler _dataHandler;
292      
293 +    /**
294 +     * The name of the Client we're connected to
295 +     */
296      private String _clientName;
297 +    
298 +    /**
299 +     * The host list the Client has requested
300 +     */
301      private String _hostList;
302      
303   //---STATIC ATTRIBUTES---

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines