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: tdb $ |
19 |
* @version $Id: TCPClientListener.java,v 1.13 2001/05/29 17:02:34 tdb Exp $ |
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: 1.13 $"; |
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 |
// get our ACL from the configuration |
60 |
ACL acl = null; |
61 |
try { |
62 |
String stringACL = ConfigurationProxy.getInstance().getProperty("ClientInterface", "ClientInterface.TCPControlChannelACL"); |
63 |
acl = new ACL(stringACL); |
64 |
} |
65 |
catch(PropertyNotFoundException e) { |
66 |
_logger.write(toString(), Logger.WARNING, "No ACL found for ClientInterface (control channel listener): " + e); |
67 |
} |
68 |
|
69 |
// get our port |
70 |
int portNum; |
71 |
try { |
72 |
String port = ConfigurationProxy.getInstance().getProperty("ClientInterface", "ClientInterface.listenPort"); |
73 |
portNum = Integer.parseInt(port); |
74 |
} catch (PropertyNotFoundException e) { |
75 |
portNum = DEFPORT; |
76 |
_logger.write(toString(), Logger.WARNING, "Configuration not found, using default of "+portNum+" : "+e); |
77 |
} catch (NumberFormatException e) { |
78 |
portNum = DEFPORT; |
79 |
_logger.write(toString(), Logger.WARNING, "Bad configuration found, using default of "+portNum+" : "+e); |
80 |
} |
81 |
|
82 |
ServerSocket listenPort=null; |
83 |
// We use this boolean so we can break out of the while loop if we want |
84 |
boolean run = true; |
85 |
try{ |
86 |
// Setup the ServerSocket so that clients can connect |
87 |
// use an ACLServerSocket if we have an ACL |
88 |
if(acl != null) { |
89 |
listenPort = new ACLServerSocket(acl, portNum); |
90 |
} |
91 |
else { |
92 |
listenPort = new ServerSocket(portNum); |
93 |
} |
94 |
} |
95 |
catch(IOException e){ |
96 |
} |
97 |
// Log what machine/port we're listening on |
98 |
try{ |
99 |
_logger.write(toString(), Logger.SYSMSG, "ClientListener listening on " |
100 |
+InetAddress.getLocalHost().getHostName() |
101 |
+"/"+InetAddress.getLocalHost().getHostAddress() |
102 |
+" port "+listenPort.getLocalPort()); |
103 |
} |
104 |
catch(UnknownHostException e){ |
105 |
_logger.write(toString(), Logger.SYSMSG, "ClientListener listening on UnknownHost " |
106 |
+"port "+listenPort.getLocalPort()); |
107 |
} |
108 |
// Loop round constantly until we decide to stop |
109 |
while(run){ |
110 |
Socket hostSocket=null; |
111 |
try{ |
112 |
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
113 |
// This will block until a host connects - at which point we get a Socket |
114 |
hostSocket = listenPort.accept(); |
115 |
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
116 |
} |
117 |
catch(IOException e){ |
118 |
// Something went wrong with the ServerSocket, so we'll stop listening |
119 |
run=false; |
120 |
} |
121 |
// If we've stopped on the line above we won't want to try this ! |
122 |
if(run){ |
123 |
try { |
124 |
// start up a ControlHandler to chat to the new Client |
125 |
TCPControlHandler ctl = new TCPControlHandler(hostSocket, _packetSorter); |
126 |
ctl.start(); |
127 |
} catch (IOException e) { |
128 |
_logger.write(toString(), Logger.ERROR, e.toString()); |
129 |
} |
130 |
} |
131 |
} |
132 |
// Best log the fact that we're stopping |
133 |
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
134 |
} |
135 |
|
136 |
/** |
137 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
138 |
* method to provide clean logging (every class should have this). |
139 |
* |
140 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
141 |
* to format the toString() |
142 |
* |
143 |
* @return the name of this class and its CVS revision |
144 |
*/ |
145 |
public String toString() { |
146 |
return FormatName.getName( |
147 |
_name, |
148 |
getClass().getName(), |
149 |
REVISION); |
150 |
} |
151 |
|
152 |
//---PRIVATE METHODS--- |
153 |
|
154 |
//---ACCESSOR/MUTATOR METHODS--- |
155 |
|
156 |
//---ATTRIBUTES--- |
157 |
|
158 |
/** |
159 |
* This is the friendly identifier of the |
160 |
* component this class is running in. |
161 |
* eg, a Filter may be called "filter1", |
162 |
* If this class does not have an owning |
163 |
* component, a name from the configuration |
164 |
* can be placed here. This name could also |
165 |
* be changed to null for utility classes. |
166 |
*/ |
167 |
private String _name = ClientInterfaceMain.NAME; |
168 |
|
169 |
/** |
170 |
* This holds a reference to the |
171 |
* system logger that is being used. |
172 |
*/ |
173 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
174 |
|
175 |
/** |
176 |
* A reference to the PacketSorter. |
177 |
*/ |
178 |
private PacketSorter _packetSorter; |
179 |
|
180 |
//---STATIC ATTRIBUTES--- |
181 |
|
182 |
} |