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