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 |
> |
/* |
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.org.iscream.cms.server.clientinterface; |
23 |
> |
|
24 |
> |
//---IMPORTS--- |
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; |
31 |
> |
import java.net.InetAddress; |
32 |
> |
import java.net.UnknownHostException; |
33 |
> |
|
34 |
> |
/** |
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 TCPClientListener extends Thread { |
42 |
> |
|
43 |
> |
//---FINAL ATTRIBUTES--- |
44 |
> |
|
45 |
> |
/** |
46 |
> |
* The current CVS revision of this class |
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 TCPClientListener |
58 |
> |
* |
59 |
> |
* @param packetSorter A reference to the PacketSorter |
60 |
> |
*/ |
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 |
> |
|
69 |
> |
//---PUBLIC METHODS--- |
70 |
> |
|
71 |
> |
/** |
72 |
> |
* The run() method is the main loop for this thread, and we |
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 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 ACLServerSocket(acl, portNum); |
101 |
> |
} |
102 |
> |
catch(IOException e){ |
103 |
> |
} |
104 |
> |
// Log what machine/port we're listening on |
105 |
> |
try{ |
106 |
> |
_logger.write(toString(), Logger.SYSMSG, "ClientListener listening on " |
107 |
> |
+InetAddress.getLocalHost().getHostName() |
108 |
> |
+"/"+InetAddress.getLocalHost().getHostAddress() |
109 |
> |
+" port "+listenPort.getLocalPort()); |
110 |
> |
} |
111 |
> |
catch(UnknownHostException e){ |
112 |
> |
_logger.write(toString(), Logger.SYSMSG, "ClientListener listening on UnknownHost " |
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"); |
143 |
> |
// This will block until a host connects - at which point we get a Socket |
144 |
> |
hostSocket = listenPort.accept(); |
145 |
> |
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
146 |
> |
} |
147 |
> |
catch(IOException e){ |
148 |
> |
// Something went wrong with the ServerSocket, so we'll stop listening |
149 |
> |
run=false; |
150 |
> |
} |
151 |
> |
// If we've stopped on the line above we won't want to try this ! |
152 |
> |
if(run){ |
153 |
> |
try { |
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 |
> |
} |
160 |
> |
} |
161 |
> |
} |
162 |
> |
// Best log the fact that we're stopping |
163 |
> |
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
164 |
> |
} |
165 |
> |
|
166 |
> |
/** |
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.org.iscream.cms.util.NameFormat class |
171 |
> |
* to format the toString() |
172 |
> |
* |
173 |
> |
* @return the name of this class and its CVS revision |
174 |
> |
*/ |
175 |
> |
public String toString() { |
176 |
> |
return FormatName.getName( |
177 |
> |
_name, |
178 |
> |
getClass().getName(), |
179 |
> |
REVISION); |
180 |
> |
} |
181 |
> |
|
182 |
> |
//---PRIVATE METHODS--- |
183 |
> |
|
184 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
185 |
> |
|
186 |
> |
//---ATTRIBUTES--- |
187 |
> |
|
188 |
> |
/** |
189 |
> |
* This is the friendly identifier of the |
190 |
> |
* component this class is running in. |
191 |
> |
* eg, a Filter may be called "filter1", |
192 |
> |
* If this class does not have an owning |
193 |
> |
* component, a name from the configuration |
194 |
> |
* can be placed here. This name could also |
195 |
> |
* be changed to null for utility classes. |
196 |
> |
*/ |
197 |
> |
private String _name = ClientInterfaceMain.NAME; |
198 |
> |
|
199 |
> |
/** |
200 |
> |
* This holds a reference to the |
201 |
> |
* system logger that is being used. |
202 |
> |
*/ |
203 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
204 |
> |
|
205 |
> |
/** |
206 |
> |
* A reference to the PacketSorter. |
207 |
> |
*/ |
208 |
> |
private PacketSorter _packetSorter; |
209 |
> |
|
210 |
> |
//---STATIC ATTRIBUTES--- |
211 |
> |
|
212 |
> |
} |