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