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/TCPClientListener.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/clientinterface/TCPClientListener.java (file contents):
Revision 1.1 by tdb, Mon Dec 4 18:16:45 2000 UTC vs.
Revision 1.15 by tdb, Thu Mar 21 13:01:21 2002 UTC

# Line 1 | Line 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.core.*;
7 < import java.io.IOException;
8 < import java.net.Socket;
9 < import java.net.ServerSocket;
10 < import java.net.InetAddress;
11 < import java.net.UnknownHostException;
12 <
13 < /**
14 < * Listener for incoming clients.
15 < * Creates a local implementation of the ClientHandler interface
16 < * and registers this with the ClientInterfaceServant. It then
17 < * returns a "reference" to this ClientHandler back to the client.
18 < *
19 < * @author  $Author$
20 < * @version $Id$
21 < */
22 < class ClientListener extends Thread {
23 <
24 < //---FINAL ATTRIBUTES---
25 <
26 <    /**
27 <     * The current CVS revision of this class
28 <     */
29 <    public final String REVISION = "$Revision$";
30 <    
31 < //---STATIC METHODS---
32 <
33 < //---CONSTRUCTORS---
34 <
35 <        /**
36 <     * Constructs a new ClientListener
37 <     *
38 <         * @param port The port that the server will listen on.
39 <         * @param clit A reference to the ClientInterfaceServant.
40 <         */
41 <    public ClientListener(int port, ClientInterfaceServant cli) {
42 <        _port = port;
43 <        _cli = cli;
44 <        _logger.write(toString(), Logger.SYSINIT, "started");
45 <    }
46 <
47 < //---PUBLIC METHODS---
48 <
49 <        /**
50 <         * The run() method is the main loop for this thread, and we
51 <         * will remain in here until such a point as something goes
52 <         * wrong with the listening. After initially setting up the
53 <         * ServerSocket we go round a while loop receiving connections
54 <         * and then passing them off to other processes to deal with.
55 <         */
56 <    public void run(){
57 <        ServerSocket listenPort=null;
58 <                // We use this boolean so we can break out of the while loop if we want
59 <        boolean run = true;
60 <        try{
61 <                        // Setup the ServerSocket so that clients can connect
62 <            listenPort = new ServerSocket(_port);
63 <        }
64 <        catch(IOException e){
65 <        }
66 <                // Log what machine/port we're listening on
67 <                try{
68 <                        _logger.write(toString(), Logger.SYSMSG, "ClientListener listening on "
69 <                                                                                +InetAddress.getLocalHost().getHostName()
70 <                                                                                +"/"+InetAddress.getLocalHost().getHostAddress()
71 <                                                                                +" port "+listenPort.getLocalPort());
72 <                }
73 <                catch(UnknownHostException e){
74 <                        _logger.write(toString(), Logger.SYSMSG, "ClientListener listening on UnknownHost "
75 <                                                                                +"port "+listenPort.getLocalPort());
76 <                }
77 <                // Loop round constantly until we decide to stop
78 <        while(run){
79 <            Socket hostSocket=null;
80 <            try{
81 <                _logger.write(toString(), Logger.SYSMSG, "Waiting for Connection");
82 <                                // This will block until a host connects - at which point we get a Socket
83 <                hostSocket = listenPort.accept();
84 <                _logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString());
85 <            }
86 <            catch(IOException e){
87 <                                // Something went wrong with the ServerSocket, so we'll stop listening
88 <                run=false;
89 <            }
90 <                        // If we've stopped on the line above we won't want to try this !
91 <            if(run){
92 <                                try {
93 <                                    // Setup the HostInit so it can carry on communications with the host
94 <                                    ClientHandler handler = new TCPClientHandler(hostSocket, _cli);
95 <                                    // register it
96 <                                    _cli.register(handler);
97 <                                    // then we can leave here.... I trust handler won't go out of scope !
98 <                } catch (IOException e) {
99 <                    _logger.write(toString(), Logger.ERROR, e.toString());
100 <                }
101 <            }
102 <        }
103 <                // Best log the fact that we're stopping
104 <        _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
105 <    }
106 <
107 <    /**
108 <     * Overrides the {@link java.lang.Object#toString() Object.toString()}
109 <     * method to provide clean logging (every class should have this).
110 <     *
111 <     * @return the name of this class and its CVS revision
112 <     */
113 <    public String toString() {
114 <        return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")";
115 <    }
116 <
117 < //---PRIVATE METHODS---
118 <
119 < //---ACCESSOR/MUTATOR METHODS---
120 <
121 < //---ATTRIBUTES---
122 <
123 <    /**
124 <     * A reference to the logger the system is using
125 <     */
126 <    Logger _logger = ReferenceManager.getInstance().getLogger();
127 <
128 <    /**
129 <         * The port on which the server should listen.
130 <         */
131 <    private int _port;
132 <    
133 <    /**
134 <         * A reference to the ClientInterfaceServant.
135 <         */
136 <    private ClientInterfaceServant _cli;
137 <
138 < //---STATIC ATTRIBUTES---
139 <
140 < }
1 > //---PACKAGE DECLARATION---
2 > package uk.org.iscream.cms.server.clientinterface;
3 >
4 > //---IMPORTS---
5 > import uk.org.iscream.cms.server.util.*;
6 > import uk.org.iscream.cms.server.core.*;
7 > import uk.org.iscream.cms.server.componentmanager.*;
8 > import java.io.IOException;
9 > import java.net.Socket;
10 > import java.net.ServerSocket;
11 > import java.net.InetAddress;
12 > import java.net.UnknownHostException;
13 >
14 > /**
15 > * Listener for incoming TCP clients, and startup a Handler
16 > * a connection is received.
17 > *
18 > * @author  $Author$
19 > * @version $Id$
20 > */
21 > class TCPClientListener extends Thread {
22 >
23 > //---FINAL ATTRIBUTES---
24 >
25 >    /**
26 >     * The current CVS revision of this class
27 >     */
28 >    public final String REVISION = "$Revision$";
29 >    
30 >    public static final int DEFPORT = 4510;
31 >    
32 > //---STATIC METHODS---
33 >
34 > //---CONSTRUCTORS---
35 >
36 >        /**
37 >     * Constructs a new TCPClientListener
38 >     *
39 >         * @param packetSorter A reference to the PacketSorter
40 >         */
41 >    public TCPClientListener(PacketSorter packetSorter) {
42 >        // set the Thread name
43 >        setName("clientinterface.TCPClientListener");
44 >        
45 >        _packetSorter = packetSorter;
46 >        _logger.write(toString(), Logger.SYSINIT, "started");
47 >    }
48 >
49 > //---PUBLIC METHODS---
50 >
51 >        /**
52 >         * The run() method is the main loop for this thread, and we
53 >         * will remain in here until such a point as something goes
54 >         * wrong with the listening. After initially setting up the
55 >         * ServerSocket we go round a while loop receiving connections
56 >         * and then passing them off to handler processes to deal with.
57 >         */
58 >    public void run(){
59 >        // setup an empty ACL defaulting to ALLOW
60 >        ACL acl = new ACL(ACL.ALLOW);
61 >        
62 >        // get our port
63 >        int portNum;
64 >        try {
65 >            String port = ConfigurationProxy.getInstance().getProperty("ClientInterface", "ClientInterface.listenPort");
66 >            portNum = Integer.parseInt(port);
67 >        } catch (PropertyNotFoundException e) {
68 >            portNum = DEFPORT;
69 >            _logger.write(toString(), Logger.WARNING, "Configuration not found, using default of "+portNum+" : "+e);
70 >        } catch (NumberFormatException e) {
71 >            portNum = DEFPORT;
72 >            _logger.write(toString(), Logger.WARNING, "Bad configuration found, using default of "+portNum+" : "+e);
73 >        }
74 >        
75 >        ServerSocket listenPort=null;
76 >                // We use this boolean so we can break out of the while loop if we want
77 >        boolean run = true;
78 >        try{
79 >                        // Setup the ServerSocket so that clients can connect
80 >                        listenPort = new ACLServerSocket(acl, portNum);
81 >        }
82 >        catch(IOException e){
83 >        }
84 >                // Log what machine/port we're listening on
85 >                try{
86 >                        _logger.write(toString(), Logger.SYSMSG, "ClientListener listening on "
87 >                                                                                +InetAddress.getLocalHost().getHostName()
88 >                                                                                +"/"+InetAddress.getLocalHost().getHostAddress()
89 >                                                                                +" port "+listenPort.getLocalPort());
90 >                }
91 >                catch(UnknownHostException e){
92 >                        _logger.write(toString(), Logger.SYSMSG, "ClientListener listening on UnknownHost "
93 >                                                                                +"port "+listenPort.getLocalPort());
94 >                }
95 >                // Loop round constantly until we decide to stop
96 >                ConfigurationProxy cp = ConfigurationProxy.getInstance();
97 >        String stringACL = "";
98 >        String newStringACL = "";
99 >        while(run){
100 >            // get hold of the ACL in the configuration
101 >            try {
102 >                newStringACL = cp.getProperty("ClientInterface", "ClientInterface.TCPControlChannelACL");
103 >            }
104 >            catch(PropertyNotFoundException e) {
105 >                // if we can't find it, we'll just use a null ACL
106 >                newStringACL = "";
107 >                _logger.write(toString(), Logger.WARNING, "No ACL found for ClientInterface (control channel listener), using empty ACL instead: " + e);
108 >            }
109 >            // check to see if the ACL has changed
110 >            if(!newStringACL.equals(stringACL)) {
111 >                _logger.write(toString(), Logger.SYSMSG, "Reloading Access Control List");
112 >                // clear the ACL
113 >                acl.clear();
114 >                // set the default to something sane
115 >                acl.setDefaultMode(ACL.ALLOW);
116 >                // add the new ACL (this may change the default)
117 >                acl.add(newStringACL);
118 >                stringACL = newStringACL;
119 >            }
120 >            Socket hostSocket=null;
121 >            try{
122 >                _logger.write(toString(), Logger.SYSMSG, "Waiting for Connection");
123 >                                // This will block until a host connects - at which point we get a Socket
124 >                hostSocket = listenPort.accept();
125 >                _logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString());
126 >            }
127 >            catch(IOException e){
128 >                                // Something went wrong with the ServerSocket, so we'll stop listening
129 >                run=false;
130 >            }
131 >                        // If we've stopped on the line above we won't want to try this !
132 >            if(run){
133 >                                try {
134 >                                    // start up a ControlHandler to chat to the new Client
135 >                                    TCPControlHandler ctl = new TCPControlHandler(hostSocket, _packetSorter);
136 >                                    ctl.start();
137 >                } catch (IOException e) {
138 >                    _logger.write(toString(), Logger.ERROR, e.toString());
139 >                }
140 >            }
141 >        }
142 >                // Best log the fact that we're stopping
143 >        _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
144 >    }
145 >
146 >    /**
147 >     * Overrides the {@link java.lang.Object#toString() Object.toString()}
148 >     * method to provide clean logging (every class should have this).
149 >     *
150 >     * This uses the uk.org.iscream.cms.server.util.NameFormat class
151 >     * to format the toString()
152 >     *
153 >     * @return the name of this class and its CVS revision
154 >     */
155 >    public String toString() {
156 >        return FormatName.getName(
157 >            _name,
158 >            getClass().getName(),
159 >            REVISION);
160 >    }
161 >
162 > //---PRIVATE METHODS---
163 >
164 > //---ACCESSOR/MUTATOR METHODS---
165 >
166 > //---ATTRIBUTES---
167 >
168 >    /**
169 >     * This is the friendly identifier of the
170 >     * component this class is running in.
171 >     * eg, a Filter may be called "filter1",
172 >     * If this class does not have an owning
173 >     * component,  a name from the configuration
174 >     * can be placed here.  This name could also
175 >     * be changed to null for utility classes.
176 >     */
177 >    private String _name = ClientInterfaceMain.NAME;
178 >
179 >    /**
180 >     * This holds a reference to the
181 >     * system logger that is being used.
182 >     */
183 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
184 >    
185 >    /**
186 >         * A reference to the PacketSorter.
187 >         */
188 >    private PacketSorter _packetSorter;
189 >    
190 > //---STATIC ATTRIBUTES---
191 >
192 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines