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