1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.12 |
package uk.org.iscream.cms.server.filter; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
tdb |
1.12 |
import uk.org.iscream.cms.server.core.*; |
6 |
|
|
import uk.org.iscream.cms.server.componentmanager.*; |
7 |
|
|
import uk.org.iscream.cms.server.filter.*; |
8 |
tdb |
1.1 |
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 |
tdb |
1.12 |
import uk.org.iscream.cms.server.util.*; |
15 |
tdb |
1.1 |
|
16 |
|
|
/** |
17 |
tdb |
1.8 |
* Reads TCP Heartbeats from the host applications. |
18 |
tdb |
1.1 |
* |
19 |
tdb |
1.13 |
* @author $Author: tdb $ |
20 |
tdb |
1.15 |
* @version $Id: TCPReader.java,v 1.14 2002/03/20 12:56:37 tdb Exp $ |
21 |
tdb |
1.1 |
*/ |
22 |
|
|
class TCPReader extends Thread { |
23 |
|
|
|
24 |
|
|
//---FINAL ATTRIBUTES--- |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* The current CVS revision of this class |
28 |
|
|
*/ |
29 |
tdb |
1.15 |
public final String REVISION = "$Revision: 1.14 $"; |
30 |
tdb |
1.1 |
|
31 |
|
|
//---STATIC METHODS--- |
32 |
|
|
|
33 |
|
|
//---CONSTRUCTORS--- |
34 |
|
|
|
35 |
|
|
/** |
36 |
tdb |
1.8 |
* Constructs a new TCPReader |
37 |
tdb |
1.1 |
* |
38 |
tdb |
1.8 |
* @param queue A reference to our Queue |
39 |
|
|
* @param port The port that the TCPReader will listen on |
40 |
tdb |
1.1 |
*/ |
41 |
tdb |
1.6 |
public TCPReader(int port, Queue queue) { |
42 |
tdb |
1.10 |
// set the Thread name |
43 |
|
|
setName("filter.TCPReader"); |
44 |
|
|
|
45 |
tdb |
1.1 |
_port = port; |
46 |
tdb |
1.6 |
_queue = queue; |
47 |
tdb |
1.1 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
|
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 |
|
|
*/ |
61 |
|
|
public void run(){ |
62 |
tdb |
1.13 |
// get our ACL from the configuration |
63 |
tdb |
1.14 |
ACL acl = null; |
64 |
tdb |
1.13 |
try { |
65 |
tdb |
1.15 |
String stringACL = ConfigurationProxy.getInstance().getProperty("Filter." + FilterMain.NAME, "Filter.TCPACL"); |
66 |
tdb |
1.14 |
acl = new ACL(stringACL); |
67 |
tdb |
1.13 |
} |
68 |
|
|
catch(PropertyNotFoundException e) { |
69 |
|
|
_logger.write(toString(), Logger.WARNING, "No ACL found for TCPReader: " + e); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
ServerSocket listenPort=null; |
73 |
tdb |
1.1 |
try{ |
74 |
|
|
// Setup the ServerSocket so that clients can connect |
75 |
tdb |
1.13 |
// use an ACLServerSocket if we have an ACL |
76 |
tdb |
1.14 |
if(acl != null) { |
77 |
|
|
listenPort = new ACLServerSocket(acl, _port); |
78 |
tdb |
1.13 |
} |
79 |
|
|
else { |
80 |
|
|
listenPort = new ServerSocket(_port); |
81 |
|
|
} |
82 |
tdb |
1.1 |
} |
83 |
|
|
catch(IOException e){ |
84 |
|
|
} |
85 |
tdb |
1.13 |
|
86 |
tdb |
1.1 |
// Log what machine/port we're listening on |
87 |
|
|
try{ |
88 |
|
|
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
89 |
|
|
+InetAddress.getLocalHost().getHostName() |
90 |
|
|
+"/"+InetAddress.getLocalHost().getHostAddress() |
91 |
|
|
+" port "+listenPort.getLocalPort()); |
92 |
|
|
} |
93 |
|
|
catch(UnknownHostException e){ |
94 |
|
|
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
95 |
|
|
+"port "+listenPort.getLocalPort()); |
96 |
|
|
} |
97 |
tdb |
1.13 |
|
98 |
|
|
// We use this boolean so we can break out of the while loop if we want |
99 |
|
|
boolean run = true; |
100 |
|
|
|
101 |
tdb |
1.1 |
// Loop round constantly until we decide to stop |
102 |
|
|
while(run){ |
103 |
|
|
Socket hostSocket=null; |
104 |
|
|
try{ |
105 |
tdb |
1.9 |
_logger.write(toString(), Logger.DEBUG, "Waiting for Connection"); |
106 |
tdb |
1.1 |
// This will block until a host connects - at which point we get a Socket |
107 |
|
|
hostSocket = listenPort.accept(); |
108 |
tdb |
1.9 |
_logger.write(toString(), Logger.DEBUG, "Connection accepted from: " + hostSocket.toString()); |
109 |
tdb |
1.1 |
} |
110 |
|
|
catch(IOException e){ |
111 |
|
|
// Something went wrong with the ServerSocket, so we'll stop listening |
112 |
|
|
run=false; |
113 |
|
|
} |
114 |
|
|
// If we've stopped on the line above we won't want to try this ! |
115 |
|
|
if(run){ |
116 |
|
|
try { |
117 |
tdb |
1.8 |
// Setup the TCPReaderInit and start it |
118 |
tdb |
1.6 |
TCPReaderInit init = new TCPReaderInit(hostSocket, _queue); |
119 |
tdb |
1.1 |
// and start it |
120 |
|
|
init.start(); |
121 |
|
|
} catch (IOException e) { |
122 |
|
|
_logger.write(toString(), Logger.ERROR, e.toString()); |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
// Best log the fact that we're stopping |
127 |
|
|
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/** |
131 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
132 |
|
|
* method to provide clean logging (every class should have this). |
133 |
|
|
* |
134 |
tdb |
1.12 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
135 |
ajm |
1.5 |
* to format the toString() |
136 |
|
|
* |
137 |
tdb |
1.1 |
* @return the name of this class and its CVS revision |
138 |
|
|
*/ |
139 |
|
|
public String toString() { |
140 |
ajm |
1.5 |
return FormatName.getName( |
141 |
|
|
_name, |
142 |
|
|
getClass().getName(), |
143 |
|
|
REVISION); |
144 |
tdb |
1.1 |
} |
145 |
|
|
|
146 |
|
|
//---PRIVATE METHODS--- |
147 |
|
|
|
148 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
149 |
|
|
|
150 |
|
|
//---ATTRIBUTES--- |
151 |
|
|
|
152 |
|
|
/** |
153 |
ajm |
1.5 |
* This is the friendly identifier of the |
154 |
|
|
* component this class is running in. |
155 |
|
|
* eg, a Filter may be called "filter1", |
156 |
|
|
* If this class does not have an owning |
157 |
|
|
* component, a name from the configuration |
158 |
|
|
* can be placed here. This name could also |
159 |
|
|
* be changed to null for utility classes. |
160 |
|
|
*/ |
161 |
|
|
private String _name = FilterMain.NAME; |
162 |
|
|
|
163 |
|
|
/** |
164 |
|
|
* This holds a reference to the |
165 |
|
|
* system logger that is being used. |
166 |
tdb |
1.1 |
*/ |
167 |
ajm |
1.5 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
168 |
tdb |
1.1 |
|
169 |
|
|
/** |
170 |
|
|
* The port on which the server should listen. |
171 |
|
|
*/ |
172 |
|
|
private int _port; |
173 |
|
|
|
174 |
tdb |
1.8 |
/** |
175 |
|
|
* A reference to our Queue |
176 |
|
|
*/ |
177 |
tdb |
1.6 |
private Queue _queue; |
178 |
tdb |
1.1 |
|
179 |
|
|
//---STATIC ATTRIBUTES--- |
180 |
|
|
|
181 |
|
|
} |