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 |
+ |
|
23 |
+ |
//---IMPORTS--- |
24 |
+ |
|
25 |
|
import java.net.*; |
26 |
|
import java.util.*; |
27 |
|
import java.io.*; |
28 |
|
|
29 |
+ |
/** |
30 |
+ |
* Configurator object for the JavaHost |
31 |
+ |
* Will connect to the filter manager and collect its specific |
32 |
+ |
* configuration |
33 |
+ |
* |
34 |
+ |
* @author $Author$ |
35 |
+ |
* @version $Id$ |
36 |
+ |
*/ |
37 |
|
class Config { |
38 |
|
|
39 |
< |
private String lastModified; |
8 |
< |
private int numProperties; |
9 |
< |
private Hashtable myProperties; |
10 |
< |
private String filterName; |
11 |
< |
private int filterUDPPort; |
12 |
< |
private int filterTCPPort; |
13 |
< |
private ArrayList aList; |
14 |
< |
private BufferedReader socketIn; |
15 |
< |
private PrintWriter socketOut; |
16 |
< |
|
39 |
> |
//---FINAL ATTRIBUTES--- |
40 |
|
|
41 |
< |
public Config( String serverName, int serverPort ){ |
19 |
< |
// takes in the master config settings and creates a connection with |
20 |
< |
// this computer, and downloads all the values listed in config.values.txt |
21 |
< |
// which should be in the local directory |
22 |
< |
|
23 |
< |
// read in from the file. |
24 |
< |
try { |
25 |
< |
BufferedReader inFile = new BufferedReader(new FileReader("config.values.txt")); |
26 |
< |
aList = new ArrayList(); |
27 |
< |
numProperties = 0; |
28 |
< |
String tmpIn = inFile.readLine(); |
29 |
< |
while ( tmpIn != null ){ |
30 |
< |
aList.add(tmpIn); |
31 |
< |
numProperties++; |
32 |
< |
tmpIn = inFile.readLine(); |
33 |
< |
} |
34 |
< |
} |
35 |
< |
catch ( FileNotFoundException e ){ |
36 |
< |
// do something |
37 |
< |
} |
38 |
< |
catch ( IOException e ){ |
39 |
< |
// do something |
40 |
< |
} |
41 |
< |
|
42 |
< |
// do the funky jibble |
43 |
< |
connect(serverName, serverPort); |
44 |
< |
|
45 |
< |
|
46 |
< |
} |
41 |
> |
//---STATIC METHODS--- |
42 |
|
|
43 |
< |
private void connect( String serverName, int serverPort ){ |
44 |
< |
Socket mySocket; |
45 |
< |
|
46 |
< |
// might throw a UnknownHostException |
47 |
< |
try { |
48 |
< |
mySocket = new Socket(serverName, serverPort ); |
49 |
< |
// ok we have our socket connected ok. grab their input and output streams |
50 |
< |
socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); |
51 |
< |
socketOut = new PrintWriter(mySocket.getOutputStream()); |
52 |
< |
|
53 |
< |
|
54 |
< |
if (sendCommand("STARTCONFIG") == "OK"){ |
55 |
< |
// everything is fine |
56 |
< |
sendCommand("LASTMODIFIED"); |
57 |
< |
sendCommand("FILELIST"); |
58 |
< |
|
59 |
< |
// get all the properties |
60 |
< |
for ( int i = 0; i < numProperties; i++ ){ |
61 |
< |
String property = (String) aList.get(i); |
62 |
< |
myProperties.put(property, sendCommand(property)); |
63 |
< |
} |
64 |
< |
|
65 |
< |
sendCommand("ENDCONFIG"); |
66 |
< |
String filter_data = sendCommand("FILTER"); |
67 |
< |
StringTokenizer tok = new StringTokenizer(filter_data,";"); |
68 |
< |
filterName = tok.nextToken(); |
69 |
< |
filterUDPPort = Integer.parseInt(tok.nextToken()); |
70 |
< |
filterTCPPort = Integer.parseInt(tok.nextToken()); |
71 |
< |
|
72 |
< |
sendCommand("END"); |
73 |
< |
|
79 |
< |
} |
43 |
> |
//---CONSTRUCTORS--- |
44 |
> |
|
45 |
> |
public Config( String serverName, int serverPort ){ |
46 |
> |
// takes in the master config settings and creates a connection with |
47 |
> |
// this computer, and downloads all the values listed in config.values.txt |
48 |
> |
// which should be in the local directory |
49 |
> |
|
50 |
> |
// read in from the file. |
51 |
> |
try { |
52 |
> |
System.out.println("Reading Config file"); |
53 |
> |
BufferedReader inFile = new BufferedReader(new FileReader("config.values.txt")); |
54 |
> |
aList = new ArrayList(); |
55 |
> |
numProperties = 0; |
56 |
> |
String tmpIn = inFile.readLine(); |
57 |
> |
while ( tmpIn != null ){ |
58 |
> |
aList.add(numProperties, tmpIn); |
59 |
> |
numProperties++; |
60 |
> |
tmpIn = inFile.readLine(); |
61 |
> |
} |
62 |
> |
System.out.println("Added "+numProperties+" properties"); |
63 |
> |
} |
64 |
> |
catch ( FileNotFoundException e ){ |
65 |
> |
// do something |
66 |
> |
System.out.println("Config file not found"); |
67 |
> |
} |
68 |
> |
catch ( IOException e ){ |
69 |
> |
// do something |
70 |
> |
} |
71 |
> |
|
72 |
> |
myProperties = new HashMap(); |
73 |
> |
configChanged = false; |
74 |
|
|
75 |
< |
// close the socket |
76 |
< |
mySocket.close(); |
75 |
> |
// time in seconds before first retry |
76 |
> |
filterManagerRetryTime = 10; |
77 |
> |
|
78 |
> |
// do the funky jibble |
79 |
> |
connect(serverName, serverPort); |
80 |
> |
} |
81 |
> |
|
82 |
> |
//---PUBLIC METHODS--- |
83 |
> |
|
84 |
> |
public InetAddress getFilterName(){ |
85 |
> |
// will return the most recent IP address (if it is dynamic for whatever reason) |
86 |
> |
try { |
87 |
> |
return InetAddress.getByName(filterName); |
88 |
> |
} |
89 |
> |
catch ( UnknownHostException e ){ |
90 |
> |
// do something |
91 |
> |
return null; |
92 |
> |
} |
93 |
> |
|
94 |
> |
} |
95 |
> |
|
96 |
> |
/** |
97 |
> |
* Used to retrieve the port to send UDP packets to on the filter |
98 |
> |
* |
99 |
> |
* @return an integer corresponding to the UDP port of the filter |
100 |
> |
*/ |
101 |
> |
public int getFilterUDPPort(){ |
102 |
> |
|
103 |
> |
return filterUDPPort; |
104 |
> |
} |
105 |
> |
|
106 |
> |
/** |
107 |
> |
* Used to retrieve the port to send TCP heartbeats to on the filter |
108 |
> |
* |
109 |
> |
* @return an integer corresponding to the TCP of the filter |
110 |
> |
*/ |
111 |
> |
public int getFilterTCPPort(){ |
112 |
> |
|
113 |
> |
return filterTCPPort; |
114 |
> |
} |
115 |
> |
|
116 |
> |
/** |
117 |
> |
* Used to get the hostname of the filter we are to talk to. |
118 |
> |
* |
119 |
> |
* @return a string representing the hostname of the filter |
120 |
> |
*/ |
121 |
> |
public String getProperty( String name ){ |
122 |
> |
|
123 |
> |
if ( myProperties.containsKey(name) ){ |
124 |
> |
// its in there, may return ERROR |
125 |
> |
return (String) myProperties.get(name); |
126 |
> |
} |
127 |
> |
else |
128 |
> |
{ |
129 |
> |
// can't have been in the config.values.txt file! |
130 |
> |
return "ERROR"; |
131 |
> |
} |
132 |
> |
} |
133 |
> |
|
134 |
> |
|
135 |
> |
//---PRIVATE METHODS--- |
136 |
> |
|
137 |
> |
private void connect( String serverName, int serverPort ){ |
138 |
> |
Socket mySocket; |
139 |
> |
configChanged = false; |
140 |
> |
|
141 |
> |
System.out.println("Establishing connection with filter manager"); |
142 |
> |
|
143 |
> |
// might throw a UnknownHostException |
144 |
> |
try { |
145 |
> |
mySocket = new Socket( serverName, serverPort ); |
146 |
> |
// ok we have our socket connected ok. grab their input and output streams |
147 |
> |
socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); |
148 |
> |
socketOut = new PrintWriter(mySocket.getOutputStream()); |
149 |
> |
|
150 |
> |
if (sendCommand("STARTCONFIG").equals("OK")){ |
151 |
> |
// everything is fine |
152 |
> |
// sendCommand("LASTMODIFIED"); |
153 |
> |
lastModified = sendCommand("LASTMODIFIED"); |
154 |
> |
|
155 |
> |
fileList = sendCommand("FILELIST"); |
156 |
> |
|
157 |
> |
fQDN = sendCommand("FQDN"); |
158 |
> |
// get all the properties |
159 |
> |
if ( numProperties > 0 ){ |
160 |
> |
// sendCommand("CONFIG"); |
161 |
> |
for ( int i = 0; i < numProperties; i++ ){ |
162 |
> |
String property = (String) aList.get(i); |
163 |
> |
myProperties.put(property, sendCommand(property)); |
164 |
> |
} |
165 |
> |
} |
166 |
> |
|
167 |
> |
sendCommand("ENDCONFIG"); |
168 |
> |
String filter_data = sendCommand("FILTER"); |
169 |
> |
StringTokenizer tok = new StringTokenizer(filter_data,";"); |
170 |
> |
filterName = tok.nextToken(); |
171 |
> |
filterUDPPort = Integer.parseInt(tok.nextToken()); |
172 |
> |
filterTCPPort = Integer.parseInt(tok.nextToken()); |
173 |
> |
|
174 |
> |
sendCommand("END"); |
175 |
> |
|
176 |
> |
} |
177 |
> |
|
178 |
> |
|
179 |
> |
// close the socket |
180 |
> |
mySocket.close(); |
181 |
> |
System.out.println("Completed communication with filter manager"); |
182 |
> |
|
183 |
> |
} |
184 |
> |
catch ( UnknownHostException e ){ |
185 |
> |
// what to do |
186 |
> |
System.out.println("Host not found"); |
187 |
> |
} |
188 |
> |
catch ( IOException e ){ |
189 |
> |
// what to do |
190 |
> |
System.out.println("Unable to read from socket, might not be open"); |
191 |
> |
System.out.println("Retrying in "+filterManagerRetryTime+" seconds"); |
192 |
> |
configChanged = true; |
193 |
> |
try { |
194 |
> |
Thread.sleep(filterManagerRetryTime*1000); |
195 |
> |
} |
196 |
> |
catch( InterruptedException f ){ |
197 |
> |
System.out.println("Sleep interrupted"); |
198 |
> |
} |
199 |
> |
filterManagerRetryTime = filterManagerRetryTime * 2; |
200 |
> |
// warning this WILL cause a stack overflow after a while.. |
201 |
> |
// need to fix it. |
202 |
> |
connect(serverName, serverPort); |
203 |
> |
} |
204 |
> |
|
205 |
> |
} // connect |
206 |
> |
|
207 |
> |
|
208 |
> |
public void sendHeartBeat(){ |
209 |
|
|
210 |
+ |
Socket mySocket; |
211 |
+ |
try { |
212 |
+ |
|
213 |
+ |
mySocket = new Socket( filterName, filterTCPPort ); |
214 |
+ |
// ok we have our socket connected ok. grab their input and output streams |
215 |
+ |
socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); |
216 |
+ |
socketOut = new PrintWriter(mySocket.getOutputStream()); |
217 |
+ |
|
218 |
+ |
|
219 |
+ |
if ( sendCommand("HEARTBEAT").equals("OK") ){ |
220 |
+ |
if ( sendCommand("CONFIG").equals("OK") ){ |
221 |
+ |
sendCommand(fileList); |
222 |
+ |
if ( sendCommand(lastModified).equals("ERROR") ){ |
223 |
+ |
// config has changed |
224 |
+ |
configChanged = true; |
225 |
+ |
} |
226 |
+ |
sendCommand("ENDHEARTBEAT"); |
227 |
+ |
} |
228 |
+ |
} |
229 |
|
} |
230 |
< |
catch ( UnknownHostException e ){ |
231 |
< |
// what to do |
232 |
< |
} |
233 |
< |
catch ( IOException e ){ |
234 |
< |
// what to do |
235 |
< |
} |
236 |
< |
|
237 |
< |
} // connect |
230 |
> |
catch ( UnknownHostException e ){ |
231 |
> |
// what to do |
232 |
> |
System.out.println("Host not found"); |
233 |
> |
} |
234 |
> |
catch ( IOException e ){ |
235 |
> |
// what to do |
236 |
> |
System.out.println("Unable to read from socket, might not be open"); |
237 |
> |
System.out.println("Re-establishing contact with filter manager"); |
238 |
> |
configChanged = true; |
239 |
> |
} |
240 |
> |
} |
241 |
|
|
242 |
< |
public InetAddress getFilterName(){ |
243 |
< |
// will return the most recient IP address (if it is dynamic for whatever reason |
244 |
< |
try { |
245 |
< |
return InetAddress.getByName(filterName); |
246 |
< |
} |
247 |
< |
catch ( UnknownHostException e ){ |
248 |
< |
// do something |
249 |
< |
return null; |
250 |
< |
} |
251 |
< |
|
252 |
< |
} |
253 |
< |
|
254 |
< |
public int getFilterUDPPort(){ |
255 |
< |
|
256 |
< |
return filterUDPPort; |
257 |
< |
} |
110 |
< |
|
111 |
< |
public int getFilterTCPPort(){ |
112 |
< |
|
113 |
< |
return filterTCPPort; |
114 |
< |
} |
242 |
> |
private String sendCommand(String command){ |
243 |
> |
|
244 |
> |
// System.out.println("Writing command: "+command); |
245 |
> |
socketOut.println(command); |
246 |
> |
socketOut.flush(); |
247 |
> |
try { |
248 |
> |
String response = socketIn.readLine(); |
249 |
> |
// System.out.println("Got: "+response); |
250 |
> |
return response; |
251 |
> |
} |
252 |
> |
catch ( IOException e ){ |
253 |
> |
// something went wrong |
254 |
> |
System.out.println("Error recieving response"); |
255 |
> |
return "ERROR"; |
256 |
> |
} |
257 |
> |
} |
258 |
|
|
259 |
< |
public String getProperty( String name ){ |
260 |
< |
|
261 |
< |
if ( myProperties.containsKey(name) ){ |
262 |
< |
// its in there, may return ERROR |
263 |
< |
return (String) myProperties.get(name); |
264 |
< |
} |
265 |
< |
else |
266 |
< |
{ |
267 |
< |
// can't have been in the config.values.txt file! |
268 |
< |
return "ERROR"; |
269 |
< |
} |
270 |
< |
} |
271 |
< |
|
272 |
< |
private String sendCommand(String command){ |
273 |
< |
|
274 |
< |
socketOut.println(command); |
275 |
< |
try { |
276 |
< |
return socketIn.readLine(); |
277 |
< |
} |
278 |
< |
catch ( IOException e ){ |
279 |
< |
// something went wrong |
280 |
< |
return "ERROR"; |
281 |
< |
} |
282 |
< |
} |
259 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
260 |
> |
|
261 |
> |
public boolean reloadConfig(){ |
262 |
> |
return configChanged; |
263 |
> |
} |
264 |
> |
|
265 |
> |
//---ATTRIBUTES--- |
266 |
> |
|
267 |
> |
private boolean configChanged; |
268 |
> |
private String lastModified; |
269 |
> |
private String fileList; |
270 |
> |
private String fQDN; |
271 |
> |
private int numProperties; |
272 |
> |
private HashMap myProperties; |
273 |
> |
private String filterName; |
274 |
> |
private int filterUDPPort; |
275 |
> |
private int filterTCPPort; |
276 |
> |
private ArrayList aList; |
277 |
> |
private BufferedReader socketIn; |
278 |
> |
private PrintWriter socketOut; |
279 |
> |
private int filterManagerRetryTime; |
280 |
> |
|
281 |
> |
|
282 |
> |
|
283 |
> |
//---STATIC ATTRIBUTES--- |
284 |
|
|
285 |
< |
} // class |
285 |
> |
} // class |