1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* 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 |
|
//---PACKAGE DECLARATION--- |
22 |
< |
package uk.ac.ukc.iscream.filtermanager; |
22 |
> |
package uk.org.iscream.cms.server.filtermanager; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
< |
import uk.ac.ukc.iscream.core.*; |
26 |
< |
import uk.ac.ukc.iscream.filter.*; |
27 |
< |
import uk.ac.ukc.iscream.util.*; |
28 |
< |
import java.net.Socket; |
25 |
> |
import uk.org.iscream.cms.server.core.*; |
26 |
> |
import uk.org.iscream.cms.server.filter.*; |
27 |
> |
import uk.org.iscream.cms.util.*; |
28 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
29 |
> |
import java.net.*; |
30 |
|
import java.io.*; |
31 |
+ |
import java.util.*; |
32 |
|
|
33 |
|
/** |
34 |
|
* Handles setting up a host. |
51 |
|
|
52 |
|
//---CONSTRUCTORS--- |
53 |
|
|
54 |
+ |
/** |
55 |
+ |
* Construct a new HostInit. |
56 |
+ |
* |
57 |
+ |
* @param socket The socket to which the host is connected |
58 |
+ |
*/ |
59 |
|
public HostInit(Socket socket) throws IOException { |
60 |
+ |
// set the Thread name |
61 |
+ |
setName("filtermanager.HostInit"); |
62 |
+ |
|
63 |
|
_socket = socket; |
64 |
+ |
// setup reader & writer |
65 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
66 |
< |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
66 |
> |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
67 |
|
_logger.write(toString(), Logger.SYSINIT, "created"); |
68 |
|
} |
69 |
|
|
70 |
|
//---PUBLIC METHODS--- |
71 |
|
|
72 |
+ |
/** |
73 |
+ |
* Main method in this class, which handles communicating with |
74 |
+ |
* the host to determine it's setup. |
75 |
+ |
*/ |
76 |
|
public void run() { |
77 |
+ |
// get a hook on the config proxy |
78 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
79 |
+ |
// this is our config name |
80 |
+ |
String configName = "Host." + _socket.getInetAddress().getHostName().toLowerCase(); |
81 |
|
try { |
82 |
< |
String inBound = _socketIn.readLine(); |
83 |
< |
if (!inBound.equals("STARTCONFIG")) { |
84 |
< |
_socketOut.println("ERROR"); |
85 |
< |
_socketOut.flush(); |
86 |
< |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
87 |
< |
} |
88 |
< |
|
89 |
< |
Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase()); |
90 |
< |
if (myConfig == null) { |
91 |
< |
_socketOut.println("ERROR"); |
92 |
< |
throw new IOException("error in host configuration"); |
93 |
< |
} else { |
94 |
< |
_socketOut.println("OK"); |
95 |
< |
_socketOut.flush(); |
96 |
< |
|
97 |
< |
// get lastmodified |
98 |
< |
inBound = _socketIn.readLine(); |
99 |
< |
if(!inBound.equals("LASTMODIFIED")) { |
100 |
< |
// protocol error |
101 |
< |
_socketOut.println("ERROR"); |
102 |
< |
_socketOut.flush(); |
103 |
< |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
82 |
> |
// look for a command: |
83 |
> |
// STARTCONFIG - to get a configuration |
84 |
> |
// CHECKCONFIG - to check a configuration |
85 |
> |
// END - to finish |
86 |
> |
String cmd = getInBound(); |
87 |
> |
while(!cmd.equals("END")) { |
88 |
> |
if(cmd.equals("STARTCONFIG")) { |
89 |
> |
// respond to STARTCONFIG |
90 |
> |
_socketOut.println("OK"); |
91 |
> |
|
92 |
> |
// try for LASTMODIFIED |
93 |
> |
getInBound("LASTMODIFIED"); |
94 |
> |
_socketOut.println(cp.getLastModified(configName)); |
95 |
> |
|
96 |
> |
// try for FILELIST |
97 |
> |
getInBound("FILELIST"); |
98 |
> |
_socketOut.println(cp.getFileList(configName)); |
99 |
> |
|
100 |
> |
// try for FQDN |
101 |
> |
getInBound("FQDN"); |
102 |
> |
_socketOut.println(_socket.getInetAddress().getHostName().toLowerCase()); |
103 |
> |
|
104 |
> |
// get properties |
105 |
> |
String reqProperty = getInBound(); |
106 |
> |
while(!reqProperty.equals("ENDCONFIG")) { |
107 |
> |
// get the property |
108 |
> |
try { |
109 |
> |
String returnedProperty = cp.getProperty(configName, "Host."+reqProperty); |
110 |
> |
_socketOut.println(returnedProperty); |
111 |
> |
} catch (PropertyNotFoundException e) { |
112 |
> |
_socketOut.println("ERROR"); |
113 |
> |
} |
114 |
> |
// get the next request |
115 |
> |
reqProperty = _socketIn.readLine(); |
116 |
> |
} |
117 |
> |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
118 |
> |
_socketOut.println("OK"); |
119 |
> |
|
120 |
> |
// get filter reference |
121 |
> |
getInBound("FILTER"); |
122 |
> |
// send info |
123 |
> |
String filterList = cp.getProperty(configName, "Host.filter"); |
124 |
> |
FilterInfo filterInfoRef = null; |
125 |
> |
String filter = null; |
126 |
> |
StringTokenizer st = new StringTokenizer(filterList, ";"); |
127 |
> |
while (filterInfoRef==null && st.hasMoreTokens()) { |
128 |
> |
filter = st.nextToken(); |
129 |
> |
_logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter); |
130 |
> |
try { |
131 |
> |
filterInfoRef = FilterInfoHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.FilterInfo\\." + filter)); |
132 |
> |
} catch (ComponentCORBAException e) { |
133 |
> |
_logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter); |
134 |
> |
} |
135 |
> |
} |
136 |
> |
|
137 |
> |
// hopefully we found a filter |
138 |
> |
if(filterInfoRef != null) { |
139 |
> |
_logger.write(toString(), Logger.DEBUG, " found filter- " + filter); |
140 |
> |
try { |
141 |
> |
// tell the host about it... |
142 |
> |
_socketOut.println(filterInfoRef.getHostName() + ";" |
143 |
> |
+ filterInfoRef.getUDPPort()); |
144 |
> |
} |
145 |
> |
catch(org.omg.CORBA.COMM_FAILURE e) { |
146 |
> |
// failed to talk to filter, lets signal an error |
147 |
> |
_socketOut.println("ERROR"); |
148 |
> |
throw new IOException("error communicating with filter - " + e); |
149 |
> |
} |
150 |
> |
} |
151 |
> |
else { |
152 |
> |
// ...or throw a wobbly (and tell the host!) |
153 |
> |
_socketOut.println("ERROR"); |
154 |
> |
throw new IOException("unable to find filter for host"); |
155 |
> |
} |
156 |
> |
|
157 |
|
} |
158 |
< |
else { |
159 |
< |
// send info |
160 |
< |
_socketOut.println(myConfig.getLastModified()); |
161 |
< |
_socketOut.flush(); |
162 |
< |
} |
163 |
< |
|
164 |
< |
// get config fileList |
165 |
< |
inBound = _socketIn.readLine(); |
166 |
< |
if(!inBound.equals("FILELIST")) { |
167 |
< |
// protocol error |
168 |
< |
_socketOut.println("ERROR"); |
77 |
< |
_socketOut.flush(); |
78 |
< |
throw new IOException("protocol error - expected:FILELIST got:" + inBound); |
79 |
< |
} |
80 |
< |
else { |
81 |
< |
// send info |
82 |
< |
_socketOut.println(myConfig.getFileList()); |
83 |
< |
_socketOut.flush(); |
84 |
< |
} |
85 |
< |
|
86 |
< |
// get properties |
87 |
< |
inBound = _socketIn.readLine(); |
88 |
< |
while(!inBound.equals("ENDCONFIG")) { |
89 |
< |
|
90 |
< |
// get the property |
158 |
> |
else if(cmd.equals("CHECKCONFIG")) { |
159 |
> |
// respond to CHECKCONFIG |
160 |
> |
_socketOut.println("OK"); |
161 |
> |
|
162 |
> |
// try for {filelist} |
163 |
> |
String filelist = getInBound(); |
164 |
> |
_socketOut.println("OK"); |
165 |
> |
|
166 |
> |
// try for {lastModified} |
167 |
> |
String lastModified = getInBound(); |
168 |
> |
long lastmod; |
169 |
|
try { |
170 |
< |
String returnedProperty = myConfig.getProperty("Host."+inBound); |
171 |
< |
|
172 |
< |
_socketOut.println(returnedProperty); |
95 |
< |
_socketOut.flush(); |
96 |
< |
|
97 |
< |
} catch (org.omg.CORBA.MARSHAL e) { |
170 |
> |
lastmod = Long.parseLong(lastModified); |
171 |
> |
} |
172 |
> |
catch(NumberFormatException e) { |
173 |
|
_socketOut.println("ERROR"); |
174 |
< |
_socketOut.flush(); |
174 |
> |
throw new IOException("Last Modified invalid: " + e); |
175 |
|
} |
176 |
< |
inBound = _socketIn.readLine(); |
176 |
> |
|
177 |
> |
// check to see if a config update has happen |
178 |
> |
boolean newConfig = _configManager.isModified(filelist, lastmod); |
179 |
> |
if(newConfig) { |
180 |
> |
// new config ! |
181 |
> |
_socketOut.println("EXPIRED"); |
182 |
> |
} |
183 |
> |
else { |
184 |
> |
// nothing has changed |
185 |
> |
_socketOut.println("OK"); |
186 |
> |
} |
187 |
|
} |
103 |
– |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
104 |
– |
_socketOut.println("OK"); |
105 |
– |
_socketOut.flush(); |
106 |
– |
|
107 |
– |
// get filter reference |
108 |
– |
inBound = _socketIn.readLine(); |
109 |
– |
if(!inBound.equals("FILTER")) { |
110 |
– |
// protocol error |
111 |
– |
_socketOut.println("ERROR"); |
112 |
– |
_socketOut.flush(); |
113 |
– |
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
114 |
– |
} |
188 |
|
else { |
116 |
– |
// send info |
117 |
– |
String parentFilter = myConfig.getProperty("Host.filter"); |
118 |
– |
_logger.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter); |
119 |
– |
Filter filter = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + parentFilter)); |
120 |
– |
_socketOut.println(filter.getHostName() + ";" |
121 |
– |
+ filter.getUDPPort() + ";" |
122 |
– |
+ filter.getTCPPort()); |
123 |
– |
_socketOut.flush(); |
124 |
– |
} |
125 |
– |
|
126 |
– |
// confirm that all is ok |
127 |
– |
inBound = _socketIn.readLine(); |
128 |
– |
if(!inBound.equals("END")) { |
129 |
– |
// protocol error |
189 |
|
_socketOut.println("ERROR"); |
131 |
– |
_socketOut.flush(); |
132 |
– |
throw new IOException("protocol error - expected:END got:" + inBound); |
190 |
|
} |
191 |
< |
else { |
192 |
< |
// send ok |
136 |
< |
_socketOut.println("OK"); |
137 |
< |
_socketOut.flush(); |
138 |
< |
} |
139 |
< |
|
191 |
> |
// get the next command |
192 |
> |
cmd = getInBound(); |
193 |
|
} |
194 |
+ |
// respond to END |
195 |
+ |
_socketOut.println("OK"); |
196 |
|
|
197 |
|
} catch (Exception e) { |
198 |
< |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e.getMessage()); |
198 |
> |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
199 |
|
} |
200 |
|
|
146 |
– |
_socketOut.flush(); |
201 |
|
// Disconnect streams & socket |
202 |
|
try { |
203 |
|
_socketIn.close(); |
213 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
214 |
|
* method to provide clean logging (every class should have this). |
215 |
|
* |
216 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
216 |
> |
* This uses the uk.org.iscream.cms.util.NameFormat class |
217 |
|
* to format the toString() |
218 |
|
* |
219 |
|
* @return the name of this class and its CVS revision |
226 |
|
} |
227 |
|
|
228 |
|
//---PRIVATE METHODS--- |
229 |
+ |
|
230 |
+ |
private String getInBound(String expected) throws IOException { |
231 |
+ |
// grab the input |
232 |
+ |
String inBound = getInBound(); |
233 |
+ |
// check if it's what we're expecting |
234 |
+ |
if(!inBound.equals(expected)) { |
235 |
+ |
_socketOut.println("ERROR"); |
236 |
+ |
throw new IOException("protocol error - expected:"+expected+" got:" + inBound); |
237 |
+ |
} |
238 |
+ |
// it should be ok then |
239 |
+ |
return inBound; |
240 |
+ |
} |
241 |
+ |
|
242 |
+ |
private String getInBound() throws IOException { |
243 |
+ |
// grab the input |
244 |
+ |
String inBound = _socketIn.readLine(); |
245 |
+ |
// check for null's, likely disconnection |
246 |
+ |
if(inBound == null) { |
247 |
+ |
throw new IOException("got null from host, maybe it died"); |
248 |
+ |
} |
249 |
+ |
// it's a valid message it seems |
250 |
+ |
return inBound; |
251 |
+ |
} |
252 |
|
|
253 |
|
//---ACCESSOR/MUTATOR METHODS--- |
254 |
|
|