1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.filter; |
2 |
> |
package uk.org.iscream.cms.server.filter; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.core.*; |
6 |
< |
import uk.ac.ukc.iscream.filter.*; |
5 |
> |
import uk.org.iscream.cms.server.core.*; |
6 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
7 |
> |
import uk.org.iscream.cms.server.filter.*; |
8 |
|
import java.net.Socket; |
9 |
|
import java.net.ServerSocket; |
10 |
|
import java.io.OutputStream; |
11 |
|
import java.io.IOException; |
12 |
|
import java.net.InetAddress; |
13 |
|
import java.net.UnknownHostException; |
14 |
< |
import org.omg.CORBA.*; |
14 |
< |
import org.omg.CosNaming.*; |
15 |
< |
import uk.ac.ukc.iscream.util.*; |
14 |
> |
import uk.org.iscream.cms.server.util.*; |
15 |
|
|
16 |
|
/** |
17 |
< |
* A socket listener to listen for new hosts registering with the system. |
19 |
< |
* When a host makes a connection, the connecton is past to an instance |
20 |
< |
* of the HostInit class, which handles further communication. |
17 |
> |
* Reads TCP Heartbeats from the host applications. |
18 |
|
* |
19 |
|
* @author $Author$ |
20 |
|
* @version $Id$ |
32 |
|
|
33 |
|
//---CONSTRUCTORS--- |
34 |
|
|
35 |
< |
/** |
36 |
< |
* Constructs a new listener |
35 |
> |
/** |
36 |
> |
* Constructs a new TCPReader |
37 |
|
* |
38 |
< |
* @param logger a reference to the logger we are using |
39 |
< |
* @param configManager a reference to the ConfigurationManager we are using |
40 |
< |
* @param port The port that the server will listen on. |
41 |
< |
*/ |
42 |
< |
public TCPReader(int port, Filter parent) { |
38 |
> |
* @param queue A reference to our Queue |
39 |
> |
* @param port The port that the TCPReader will listen on |
40 |
> |
*/ |
41 |
> |
public TCPReader(int port, Queue queue) { |
42 |
> |
// set the Thread name |
43 |
> |
setName("filter.TCPReader"); |
44 |
> |
|
45 |
|
_port = port; |
46 |
< |
_parent = parent; |
46 |
> |
_queue = queue; |
47 |
|
_logger.write(toString(), Logger.SYSINIT, "started"); |
48 |
|
} |
49 |
|
|
51 |
|
|
52 |
|
//---PUBLIC METHODS--- |
53 |
|
|
54 |
< |
/** |
55 |
< |
* The run() method is the main loop for this thread, and we |
56 |
< |
* will remain in here until such a point as something goes |
57 |
< |
* wrong with the listening. After initially setting up the |
58 |
< |
* ServerSocket we go round a while loop receiving connections |
59 |
< |
* and then passing them off to other processes to deal with. |
60 |
< |
*/ |
54 |
> |
/** |
55 |
> |
* The run() method is the main loop for this thread, and we |
56 |
> |
* will remain in here until such a point as something goes |
57 |
> |
* wrong with the listening. After initially setting up the |
58 |
> |
* ServerSocket we go round a while loop receiving connections |
59 |
> |
* and then passing them off to other processes to deal with. |
60 |
> |
*/ |
61 |
|
public void run(){ |
62 |
+ |
// setup an empty ACL defaulting to ALLOW |
63 |
+ |
ACL acl = new ACL(ACL.ALLOW); |
64 |
+ |
|
65 |
+ |
// Setup the ServerSocket so that clients can connect |
66 |
|
ServerSocket listenPort=null; |
64 |
– |
// We use this boolean so we can break out of the while loop if we want |
65 |
– |
boolean run = true; |
67 |
|
try{ |
68 |
< |
// Setup the ServerSocket so that clients can connect |
68 |
< |
listenPort = new ServerSocket(_port); |
68 |
> |
listenPort = new ACLServerSocket(acl, _port); |
69 |
|
} |
70 |
|
catch(IOException e){ |
71 |
+ |
_logger.write(toString(), Logger.FATAL, "Failed to setup ServerSocket: " + e); |
72 |
+ |
return; |
73 |
|
} |
74 |
< |
// Log what machine/port we're listening on |
75 |
< |
try{ |
76 |
< |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
77 |
< |
+InetAddress.getLocalHost().getHostName() |
78 |
< |
+"/"+InetAddress.getLocalHost().getHostAddress() |
79 |
< |
+" port "+listenPort.getLocalPort()); |
80 |
< |
} |
81 |
< |
catch(UnknownHostException e){ |
82 |
< |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
83 |
< |
+"port "+listenPort.getLocalPort()); |
84 |
< |
} |
85 |
< |
// Loop round constantly until we decide to stop |
74 |
> |
|
75 |
> |
// Log what machine/port we're listening on |
76 |
> |
try{ |
77 |
> |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
78 |
> |
+InetAddress.getLocalHost().getHostName() |
79 |
> |
+"/"+InetAddress.getLocalHost().getHostAddress() |
80 |
> |
+" port "+listenPort.getLocalPort()); |
81 |
> |
} |
82 |
> |
catch(UnknownHostException e){ |
83 |
> |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
84 |
> |
+"port "+listenPort.getLocalPort()); |
85 |
> |
} |
86 |
> |
|
87 |
> |
// We use this boolean so we can break out of the while loop if we want |
88 |
> |
boolean run = true; |
89 |
> |
|
90 |
> |
// Loop round constantly until we decide to stop |
91 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
92 |
> |
String stringACL = ""; |
93 |
> |
String newStringACL = ""; |
94 |
|
while(run){ |
95 |
< |
Socket hostSocket=null; |
95 |
> |
// get hold of the ACL in the configuration |
96 |
> |
try { |
97 |
> |
newStringACL = cp.getProperty("Filter." + FilterMain.NAME, "Filter.TCPACL"); |
98 |
> |
} |
99 |
> |
catch(PropertyNotFoundException e) { |
100 |
> |
// if we can't find it, we'll just use a null ACL |
101 |
> |
newStringACL = ""; |
102 |
> |
_logger.write(toString(), Logger.WARNING, "No ACL found for TCPReader, using empty ACL instead: " + e); |
103 |
> |
} |
104 |
> |
// check to see if the ACL has changed |
105 |
> |
if(!newStringACL.equals(stringACL)) { |
106 |
> |
_logger.write(toString(), Logger.SYSMSG, "Reloading Access Control List"); |
107 |
> |
// clear the ACL |
108 |
> |
acl.clear(); |
109 |
> |
// set the default to something sane |
110 |
> |
acl.setDefaultMode(ACL.ALLOW); |
111 |
> |
// add the new ACL (this may change the default) |
112 |
> |
acl.add(newStringACL); |
113 |
> |
stringACL = newStringACL; |
114 |
> |
} |
115 |
> |
Socket hostSocket=null; |
116 |
|
try{ |
117 |
< |
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
118 |
< |
// This will block until a host connects - at which point we get a Socket |
117 |
> |
_logger.write(toString(), Logger.DEBUG, "Waiting for Connection"); |
118 |
> |
// This will block until a host connects - at which point we get a Socket |
119 |
|
hostSocket = listenPort.accept(); |
120 |
< |
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
120 |
> |
_logger.write(toString(), Logger.DEBUG, "Connection accepted from: " + hostSocket.toString()); |
121 |
|
} |
122 |
|
catch(IOException e){ |
123 |
< |
// Something went wrong with the ServerSocket, so we'll stop listening |
123 |
> |
// Something went wrong with the ServerSocket, so we'll stop listening |
124 |
|
run=false; |
125 |
|
} |
126 |
< |
// If we've stopped on the line above we won't want to try this ! |
126 |
> |
// If we've stopped on the line above we won't want to try this ! |
127 |
|
if(run){ |
128 |
< |
try { |
129 |
< |
// Setup the HostInit so it can carry on communications with the host |
130 |
< |
TCPReaderInit init = new TCPReaderInit(hostSocket, _parent); |
131 |
< |
// and start it |
128 |
> |
try { |
129 |
> |
// Setup the TCPReaderInit and start it |
130 |
> |
TCPReaderInit init = new TCPReaderInit(hostSocket, _queue); |
131 |
> |
// and start it |
132 |
|
init.start(); |
133 |
|
} catch (IOException e) { |
134 |
|
_logger.write(toString(), Logger.ERROR, e.toString()); |
135 |
|
} |
136 |
|
} |
137 |
|
} |
138 |
< |
// Best log the fact that we're stopping |
138 |
> |
// Best log the fact that we're stopping |
139 |
|
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
140 |
|
} |
141 |
|
|
143 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
144 |
|
* method to provide clean logging (every class should have this). |
145 |
|
* |
146 |
+ |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
147 |
+ |
* to format the toString() |
148 |
+ |
* |
149 |
|
* @return the name of this class and its CVS revision |
150 |
|
*/ |
151 |
|
public String toString() { |
152 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
152 |
> |
return FormatName.getName( |
153 |
> |
_name, |
154 |
> |
getClass().getName(), |
155 |
> |
REVISION); |
156 |
|
} |
157 |
|
|
158 |
|
//---PRIVATE METHODS--- |
162 |
|
//---ATTRIBUTES--- |
163 |
|
|
164 |
|
/** |
165 |
< |
* A reference to the logger the system is using |
165 |
> |
* This is the friendly identifier of the |
166 |
> |
* component this class is running in. |
167 |
> |
* eg, a Filter may be called "filter1", |
168 |
> |
* If this class does not have an owning |
169 |
> |
* component, a name from the configuration |
170 |
> |
* can be placed here. This name could also |
171 |
> |
* be changed to null for utility classes. |
172 |
|
*/ |
173 |
< |
Logger _logger = ReferenceManager.getInstance().getLogger(); |
173 |
> |
private String _name = FilterMain.NAME; |
174 |
> |
|
175 |
> |
/** |
176 |
> |
* This holds a reference to the |
177 |
> |
* system logger that is being used. |
178 |
> |
*/ |
179 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
180 |
|
|
181 |
|
/** |
182 |
< |
* The port on which the server should listen. |
183 |
< |
*/ |
182 |
> |
* The port on which the server should listen. |
183 |
> |
*/ |
184 |
|
private int _port; |
185 |
|
|
186 |
< |
private Filter _parent; |
186 |
> |
/** |
187 |
> |
* A reference to our Queue |
188 |
> |
*/ |
189 |
> |
private Queue _queue; |
190 |
|
|
191 |
|
//---STATIC ATTRIBUTES--- |
192 |
|
|