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.server.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: tdb $ |
39 |
* @version $Id: TCPClientListener.java,v 1.16 2002/05/18 18:16:01 tdb Exp $ |
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: 1.16 $"; |
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.server.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 |
} |