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