1 |
|
/* |
2 |
|
* i-scream central monitoring system |
3 |
< |
* http://www.i-scream.org.uk |
3 |
> |
* http://www.i-scream.org |
4 |
|
* Copyright (C) 2000-2002 i-scream |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or |
24 |
|
//---IMPORTS--- |
25 |
|
import uk.org.iscream.cms.server.core.*; |
26 |
|
import uk.org.iscream.cms.server.filter.*; |
27 |
< |
import uk.org.iscream.cms.server.util.*; |
27 |
> |
import uk.org.iscream.cms.util.*; |
28 |
|
import uk.org.iscream.cms.server.componentmanager.*; |
29 |
|
import java.net.*; |
30 |
|
import java.io.*; |
79 |
|
// this is our config name |
80 |
|
String configName = "Host." + _socket.getInetAddress().getHostName().toLowerCase(); |
81 |
|
try { |
82 |
< |
// try for STARTCONFIG |
83 |
< |
getInBound("STARTCONFIG"); |
84 |
< |
_socketOut.println("OK"); |
85 |
< |
|
86 |
< |
// try for LASTMODIFIED |
87 |
< |
getInBound("LASTMODIFIED"); |
88 |
< |
_socketOut.println(cp.getLastModified(configName)); |
89 |
< |
|
90 |
< |
// try for FILELIST |
91 |
< |
getInBound("FILELIST"); |
92 |
< |
_socketOut.println(cp.getFileList(configName)); |
93 |
< |
|
94 |
< |
// try for FQDN |
95 |
< |
getInBound("FQDN"); |
96 |
< |
_socketOut.println(_socket.getInetAddress().getHostName().toLowerCase()); |
97 |
< |
|
98 |
< |
// get properties |
99 |
< |
String reqProperty = getInBound(); |
100 |
< |
while(!reqProperty.equals("ENDCONFIG")) { |
101 |
< |
// get the property |
102 |
< |
try { |
103 |
< |
String returnedProperty = cp.getProperty(configName, "Host."+reqProperty); |
104 |
< |
_socketOut.println(returnedProperty); |
105 |
< |
} catch (PropertyNotFoundException e) { |
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 |
> |
// try for IP |
105 |
> |
getInBound("IP"); |
106 |
> |
_socketOut.println(_socket.getInetAddress().getHostAddress()); |
107 |
> |
|
108 |
> |
// get properties |
109 |
> |
String reqProperty = getInBound(); |
110 |
> |
while(!reqProperty.equals("ENDCONFIG")) { |
111 |
> |
// get the property |
112 |
> |
try { |
113 |
> |
String returnedProperty = cp.getProperty(configName, "Host."+reqProperty); |
114 |
> |
_socketOut.println(returnedProperty); |
115 |
> |
} catch (PropertyNotFoundException e) { |
116 |
> |
_socketOut.println("ERROR"); |
117 |
> |
} |
118 |
> |
// get the next request |
119 |
> |
reqProperty = _socketIn.readLine(); |
120 |
> |
} |
121 |
> |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
122 |
> |
_socketOut.println("OK"); |
123 |
> |
|
124 |
> |
// get filter reference |
125 |
> |
getInBound("FILTER"); |
126 |
> |
// send info |
127 |
> |
String filterList = cp.getProperty(configName, "Host.filter"); |
128 |
> |
FilterInfo filterInfoRef = null; |
129 |
> |
String filter = null; |
130 |
> |
StringTokenizer st = new StringTokenizer(filterList, ";"); |
131 |
> |
while (filterInfoRef==null && st.hasMoreTokens()) { |
132 |
> |
filter = st.nextToken(); |
133 |
> |
_logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter); |
134 |
> |
try { |
135 |
> |
filterInfoRef = FilterInfoHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.FilterInfo\\." + filter)); |
136 |
> |
} catch (ComponentCORBAException e) { |
137 |
> |
_logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter); |
138 |
> |
} |
139 |
> |
} |
140 |
> |
|
141 |
> |
// hopefully we found a filter |
142 |
> |
if(filterInfoRef != null) { |
143 |
> |
_logger.write(toString(), Logger.DEBUG, " found filter- " + filter); |
144 |
> |
try { |
145 |
> |
// tell the host about it... |
146 |
> |
_socketOut.println(filterInfoRef.getHostName() + ";" |
147 |
> |
+ filterInfoRef.getUDPPort()); |
148 |
> |
} |
149 |
> |
catch(org.omg.CORBA.COMM_FAILURE e) { |
150 |
> |
// failed to talk to filter, lets signal an error |
151 |
> |
_socketOut.println("ERROR"); |
152 |
> |
throw new IOException("error communicating with filter - " + e); |
153 |
> |
} |
154 |
> |
} |
155 |
> |
else { |
156 |
> |
// ...or throw a wobbly (and tell the host!) |
157 |
> |
_socketOut.println("ERROR"); |
158 |
> |
throw new IOException("unable to find filter for host"); |
159 |
> |
} |
160 |
> |
|
161 |
> |
} |
162 |
> |
else if(cmd.equals("CHECKCONFIG")) { |
163 |
> |
// respond to CHECKCONFIG |
164 |
> |
_socketOut.println("OK"); |
165 |
> |
|
166 |
> |
// try for {filelist} |
167 |
> |
String filelist = getInBound(); |
168 |
> |
_socketOut.println("OK"); |
169 |
> |
|
170 |
> |
// try for {lastModified} |
171 |
> |
String lastModified = getInBound(); |
172 |
> |
long lastmod; |
173 |
> |
try { |
174 |
> |
lastmod = Long.parseLong(lastModified); |
175 |
> |
} |
176 |
> |
catch(NumberFormatException e) { |
177 |
> |
_socketOut.println("ERROR"); |
178 |
> |
throw new IOException("Last Modified invalid: " + e); |
179 |
> |
} |
180 |
> |
|
181 |
> |
// check to see if a config update has happen |
182 |
> |
boolean newConfig = _configManager.isModified(filelist, lastmod); |
183 |
> |
if(newConfig) { |
184 |
> |
// new config ! |
185 |
> |
_socketOut.println("EXPIRED"); |
186 |
> |
} |
187 |
> |
else { |
188 |
> |
// nothing has changed |
189 |
> |
_socketOut.println("OK"); |
190 |
> |
} |
191 |
> |
} |
192 |
> |
else { |
193 |
|
_socketOut.println("ERROR"); |
194 |
|
} |
195 |
< |
// get the next request |
196 |
< |
reqProperty = _socketIn.readLine(); |
195 |
> |
// get the next command |
196 |
> |
cmd = getInBound(); |
197 |
|
} |
198 |
< |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
198 |
> |
// respond to END |
199 |
|
_socketOut.println("OK"); |
200 |
|
|
114 |
– |
// get filter reference |
115 |
– |
getInBound("FILTER"); |
116 |
– |
// send info |
117 |
– |
String filterList = cp.getProperty(configName, "Host.filter"); |
118 |
– |
FilterInfo filterInfoRef = null; |
119 |
– |
String filter = null; |
120 |
– |
StringTokenizer st = new StringTokenizer(filterList, ";"); |
121 |
– |
while (filterInfoRef==null && st.hasMoreTokens()) { |
122 |
– |
filter = st.nextToken(); |
123 |
– |
_logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter); |
124 |
– |
try { |
125 |
– |
filterInfoRef = FilterInfoHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.FilterInfo\\." + filter)); |
126 |
– |
} catch (ComponentCORBAException e) { |
127 |
– |
_logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter); |
128 |
– |
} |
129 |
– |
} |
130 |
– |
|
131 |
– |
// hopefully we found a filter |
132 |
– |
if(filterInfoRef != null) { |
133 |
– |
_logger.write(toString(), Logger.DEBUG, " found filter- " + filter); |
134 |
– |
try { |
135 |
– |
// tell the host about it... |
136 |
– |
_socketOut.println(filterInfoRef.getHostName() + ";" |
137 |
– |
+ filterInfoRef.getUDPPort() + ";" |
138 |
– |
+ filterInfoRef.getTCPPort()); |
139 |
– |
} |
140 |
– |
catch(org.omg.CORBA.COMM_FAILURE e) { |
141 |
– |
// failed to talk to filter, lets signal an error |
142 |
– |
_socketOut.println("ERROR"); |
143 |
– |
throw new IOException("error communicating with filter - " + e); |
144 |
– |
} |
145 |
– |
} |
146 |
– |
else { |
147 |
– |
// ...or throw a wobbly (and tell the host!) |
148 |
– |
_socketOut.println("ERROR"); |
149 |
– |
throw new IOException("unable to find filter for host"); |
150 |
– |
} |
151 |
– |
|
152 |
– |
// confirm ok with END |
153 |
– |
getInBound("END"); |
154 |
– |
_socketOut.println("OK"); |
155 |
– |
|
201 |
|
} catch (Exception e) { |
202 |
|
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
203 |
|
} |
217 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
218 |
|
* method to provide clean logging (every class should have this). |
219 |
|
* |
220 |
< |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
220 |
> |
* This uses the uk.org.iscream.cms.util.NameFormat class |
221 |
|
* to format the toString() |
222 |
|
* |
223 |
|
* @return the name of this class and its CVS revision |
236 |
|
String inBound = getInBound(); |
237 |
|
// check if it's what we're expecting |
238 |
|
if(!inBound.equals(expected)) { |
239 |
+ |
_socketOut.println("ERROR"); |
240 |
|
throw new IOException("protocol error - expected:"+expected+" got:" + inBound); |
241 |
|
} |
242 |
|
// it should be ok then |