1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
16 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
|
//---PACKAGE DECLARATION--- |
22 |
< |
package uk.ac.ukc.iscream.clientinterface; |
22 |
> |
package uk.org.iscream.cms.server.clientinterface; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
< |
import uk.ac.ukc.iscream.util.*; |
26 |
< |
import uk.ac.ukc.iscream.core.*; |
25 |
> |
import uk.org.iscream.cms.util.*; |
26 |
> |
import uk.org.iscream.cms.server.core.*; |
27 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
28 |
|
import java.io.IOException; |
29 |
|
import java.net.Socket; |
30 |
|
import java.net.ServerSocket; |
32 |
|
import java.net.UnknownHostException; |
33 |
|
|
34 |
|
/** |
35 |
< |
* Listener for incoming clients. |
36 |
< |
* 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. |
35 |
> |
* Listener for incoming TCP clients, and startup a Handler |
36 |
> |
* a connection is received. |
37 |
|
* |
38 |
|
* @author $Author$ |
39 |
|
* @version $Id$ |
40 |
|
*/ |
41 |
< |
class ClientListener extends Thread { |
41 |
> |
class TCPClientListener extends Thread { |
42 |
|
|
43 |
|
//---FINAL ATTRIBUTES--- |
44 |
|
|
47 |
|
*/ |
48 |
|
public final String REVISION = "$Revision$"; |
49 |
|
|
50 |
+ |
public static final int DEFPORT = 4510; |
51 |
+ |
|
52 |
|
//---STATIC METHODS--- |
53 |
|
|
54 |
|
//---CONSTRUCTORS--- |
55 |
|
|
56 |
|
/** |
57 |
< |
* Constructs a new ClientListener |
57 |
> |
* Constructs a new TCPClientListener |
58 |
|
* |
59 |
< |
* @param port The port that the server will listen on. |
39 |
< |
* @param clit A reference to the ClientInterfaceServant. |
59 |
> |
* @param packetSorter A reference to the PacketSorter |
60 |
|
*/ |
61 |
< |
public ClientListener(int port, ClientInterfaceServant cli) { |
62 |
< |
_port = port; |
63 |
< |
_cli = cli; |
61 |
> |
public TCPClientListener(PacketSorter packetSorter) { |
62 |
> |
// set the Thread name |
63 |
> |
setName("clientinterface.TCPClientListener"); |
64 |
> |
|
65 |
> |
_packetSorter = packetSorter; |
66 |
|
_logger.write(toString(), Logger.SYSINIT, "started"); |
67 |
|
} |
68 |
|
|
73 |
|
* will remain in here until such a point as something goes |
74 |
|
* wrong with the listening. After initially setting up the |
75 |
|
* ServerSocket we go round a while loop receiving connections |
76 |
< |
* and then passing them off to other processes to deal with. |
76 |
> |
* and then passing them off to handler processes to deal with. |
77 |
|
*/ |
78 |
|
public void run(){ |
79 |
+ |
// setup an empty ACL defaulting to ALLOW |
80 |
+ |
ACL acl = new ACL(ACL.ALLOW); |
81 |
+ |
|
82 |
+ |
// get our port |
83 |
+ |
int portNum; |
84 |
+ |
try { |
85 |
+ |
String port = ConfigurationProxy.getInstance().getProperty("ClientInterface", "ClientInterface.listenPort"); |
86 |
+ |
portNum = Integer.parseInt(port); |
87 |
+ |
} catch (PropertyNotFoundException e) { |
88 |
+ |
portNum = DEFPORT; |
89 |
+ |
_logger.write(toString(), Logger.WARNING, "Configuration not found, using default of "+portNum+" : "+e); |
90 |
+ |
} catch (NumberFormatException e) { |
91 |
+ |
portNum = DEFPORT; |
92 |
+ |
_logger.write(toString(), Logger.WARNING, "Bad configuration found, using default of "+portNum+" : "+e); |
93 |
+ |
} |
94 |
+ |
|
95 |
|
ServerSocket listenPort=null; |
96 |
|
// We use this boolean so we can break out of the while loop if we want |
97 |
|
boolean run = true; |
98 |
|
try{ |
99 |
|
// Setup the ServerSocket so that clients can connect |
100 |
< |
listenPort = new ServerSocket(_port); |
100 |
> |
listenPort = new ACLServerSocket(acl, portNum); |
101 |
|
} |
102 |
|
catch(IOException e){ |
103 |
|
} |
113 |
|
+"port "+listenPort.getLocalPort()); |
114 |
|
} |
115 |
|
// Loop round constantly until we decide to stop |
116 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
117 |
+ |
String stringACL = ""; |
118 |
+ |
String newStringACL = ""; |
119 |
|
while(run){ |
120 |
+ |
// get hold of the ACL in the configuration |
121 |
+ |
try { |
122 |
+ |
newStringACL = cp.getProperty("ClientInterface", "ClientInterface.TCPControlChannelACL"); |
123 |
+ |
} |
124 |
+ |
catch(PropertyNotFoundException e) { |
125 |
+ |
// if we can't find it, we'll just use a null ACL |
126 |
+ |
newStringACL = ""; |
127 |
+ |
_logger.write(toString(), Logger.WARNING, "No ACL found for ClientInterface (control channel listener), using empty ACL instead: " + e); |
128 |
+ |
} |
129 |
+ |
// check to see if the ACL has changed |
130 |
+ |
if(!newStringACL.equals(stringACL)) { |
131 |
+ |
_logger.write(toString(), Logger.SYSMSG, "Reloading Access Control List"); |
132 |
+ |
// clear the ACL |
133 |
+ |
acl.clear(); |
134 |
+ |
// set the default to something sane |
135 |
+ |
acl.setDefaultMode(ACL.ALLOW); |
136 |
+ |
// add the new ACL (this may change the default) |
137 |
+ |
acl.add(newStringACL); |
138 |
+ |
stringACL = newStringACL; |
139 |
+ |
} |
140 |
|
Socket hostSocket=null; |
141 |
|
try{ |
142 |
|
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
151 |
|
// If we've stopped on the line above we won't want to try this ! |
152 |
|
if(run){ |
153 |
|
try { |
154 |
< |
// Setup the HostInit so it can carry on communications with the host |
155 |
< |
ClientHandler handler = new TCPClientHandler(hostSocket, _cli); |
156 |
< |
// register it |
96 |
< |
_cli.register(handler); |
97 |
< |
// then we can leave here.... I trust handler won't go out of scope ! |
154 |
> |
// start up a ControlHandler to chat to the new Client |
155 |
> |
TCPControlHandler ctl = new TCPControlHandler(hostSocket, _packetSorter); |
156 |
> |
ctl.start(); |
157 |
|
} catch (IOException e) { |
158 |
|
_logger.write(toString(), Logger.ERROR, e.toString()); |
159 |
|
} |
167 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
168 |
|
* method to provide clean logging (every class should have this). |
169 |
|
* |
170 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
170 |
> |
* This uses the uk.org.iscream.cms.util.NameFormat class |
171 |
|
* to format the toString() |
172 |
|
* |
173 |
|
* @return the name of this class and its CVS revision |
201 |
|
* system logger that is being used. |
202 |
|
*/ |
203 |
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
145 |
– |
|
146 |
– |
/** |
147 |
– |
* The port on which the server should listen. |
148 |
– |
*/ |
149 |
– |
private int _port; |
204 |
|
|
205 |
|
/** |
206 |
< |
* A reference to the ClientInterfaceServant. |
206 |
> |
* A reference to the PacketSorter. |
207 |
|
*/ |
208 |
< |
private ClientInterfaceServant _cli; |
209 |
< |
|
208 |
> |
private PacketSorter _packetSorter; |
209 |
> |
|
210 |
|
//---STATIC ATTRIBUTES--- |
211 |
|
|
212 |
|
} |