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