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 configurator 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 |
> |
|
56 |
> |
// do the funky jibble |
57 |
> |
connect(serverName, serverPort); |
58 |
> |
} |
59 |
> |
|
60 |
> |
//---PUBLIC METHODS--- |
61 |
> |
|
62 |
> |
public InetAddress getFilterName(){ |
63 |
> |
// will return the most recient IP address (if it is dynamic for whatever reason |
64 |
> |
try { |
65 |
> |
return InetAddress.getByName(filterName); |
66 |
> |
} |
67 |
> |
catch ( UnknownHostException e ){ |
68 |
> |
// do something |
69 |
> |
return null; |
70 |
> |
} |
71 |
> |
|
72 |
> |
} |
73 |
> |
|
74 |
> |
/** |
75 |
> |
* Used to retrieve the port to send UDP packets to on the filter |
76 |
> |
* |
77 |
> |
* @return an integer corrisponding to the UDP port of the filter |
78 |
> |
*/ |
79 |
> |
public int getFilterUDPPort(){ |
80 |
> |
|
81 |
> |
return filterUDPPort; |
82 |
> |
} |
83 |
> |
|
84 |
> |
/** |
85 |
> |
* Used to retrieve the port to send TCP heartbeats to on the filter |
86 |
> |
* |
87 |
> |
* @return an integer corrisponding to the TCP of the filter |
88 |
> |
*/ |
89 |
> |
public int getFilterTCPPort(){ |
90 |
> |
|
91 |
> |
return filterTCPPort; |
92 |
> |
} |
93 |
> |
|
94 |
> |
/** |
95 |
> |
* Used to get the hostname of the filter we are to talk to. |
96 |
> |
* |
97 |
> |
* @return a string representing the hostname of the filter |
98 |
> |
*/ |
99 |
> |
public String getProperty( String name ){ |
100 |
> |
|
101 |
> |
if ( myProperties.containsKey(name) ){ |
102 |
> |
// its in there, may return ERROR |
103 |
> |
return (String) myProperties.get(name); |
104 |
> |
} |
105 |
> |
else |
106 |
> |
{ |
107 |
> |
// can't have been in the config.values.txt file! |
108 |
> |
return "ERROR"; |
109 |
> |
} |
110 |
> |
} |
111 |
> |
|
112 |
> |
|
113 |
> |
//---PRIVATE METHODS--- |
114 |
> |
|
115 |
> |
private void connect( String serverName, int serverPort ){ |
116 |
> |
Socket mySocket; |
117 |
> |
configChanged = false; |
118 |
> |
|
119 |
> |
System.out.println("Establishing connection with config manager"); |
120 |
> |
|
121 |
> |
// might throw a UnknownHostException |
122 |
> |
try { |
123 |
> |
mySocket = new Socket( serverName, serverPort ); |
124 |
> |
// ok we have our socket connected ok. grab their input and output streams |
125 |
> |
socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); |
126 |
> |
socketOut = new PrintWriter(mySocket.getOutputStream()); |
127 |
> |
|
128 |
> |
if (sendCommand("STARTCONFIG").equals("OK")){ |
129 |
> |
// everything is fine |
130 |
> |
// sendCommand("LASTMODIFIED"); |
131 |
> |
lastModified = sendCommand("LASTMODIFIED"); |
132 |
> |
|
133 |
> |
fileList = sendCommand("FILELIST"); |
134 |
> |
// get all the properties |
135 |
> |
if ( numProperties > 0 ){ |
136 |
> |
// sendCommand("CONFIG"); |
137 |
> |
for ( int i = 0; i < numProperties; i++ ){ |
138 |
> |
String property = (String) aList.get(i); |
139 |
> |
myProperties.put(property, sendCommand(property)); |
140 |
> |
} |
141 |
> |
} |
142 |
> |
|
143 |
> |
sendCommand("ENDCONFIG"); |
144 |
> |
String filter_data = sendCommand("FILTER"); |
145 |
> |
StringTokenizer tok = new StringTokenizer(filter_data,";"); |
146 |
> |
filterName = tok.nextToken(); |
147 |
> |
filterUDPPort = Integer.parseInt(tok.nextToken()); |
148 |
> |
filterTCPPort = Integer.parseInt(tok.nextToken()); |
149 |
> |
|
150 |
> |
sendCommand("END"); |
151 |
> |
|
152 |
> |
} |
153 |
> |
|
154 |
> |
|
155 |
> |
// close the socket |
156 |
> |
mySocket.close(); |
157 |
> |
System.out.println("Completed communication with config manager"); |
158 |
> |
|
159 |
> |
} |
160 |
> |
catch ( UnknownHostException e ){ |
161 |
> |
// what to do |
162 |
> |
System.out.println("Host not found"); |
163 |
> |
} |
164 |
> |
catch ( IOException e ){ |
165 |
> |
// what to do |
166 |
> |
System.out.println("Unable to read from socket, might not be open"); |
167 |
> |
} |
168 |
> |
|
169 |
> |
} // connect |
170 |
> |
|
171 |
> |
|
172 |
> |
public void sendHeartBeat(){ |
173 |
|
|
174 |
+ |
Socket mySocket; |
175 |
+ |
try { |
176 |
+ |
|
177 |
+ |
mySocket = new Socket( filterName, filterTCPPort ); |
178 |
+ |
// ok we have our socket connected ok. grab their input and output streams |
179 |
+ |
socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); |
180 |
+ |
socketOut = new PrintWriter(mySocket.getOutputStream()); |
181 |
+ |
|
182 |
+ |
|
183 |
+ |
if ( sendCommand("HEARTBEAT").equals("OK") ){ |
184 |
+ |
if ( sendCommand("CONFIG").equals("OK") ){ |
185 |
+ |
sendCommand(fileList); |
186 |
+ |
if ( sendCommand(lastModified).equals("ERROR") ){ |
187 |
+ |
// config has changed |
188 |
+ |
configChanged = true; |
189 |
+ |
} |
190 |
+ |
sendCommand("ENDHEARTBEAT"); |
191 |
+ |
} |
192 |
+ |
} |
193 |
|
} |
194 |
< |
catch ( UnknownHostException e ){ |
195 |
< |
// what to do |
196 |
< |
} |
197 |
< |
catch ( IOException e ){ |
198 |
< |
// what to do |
199 |
< |
} |
200 |
< |
|
201 |
< |
} // connect |
194 |
> |
catch ( UnknownHostException e ){ |
195 |
> |
// what to do |
196 |
> |
System.out.println("Host not found"); |
197 |
> |
} |
198 |
> |
catch ( IOException e ){ |
199 |
> |
// what to do |
200 |
> |
System.out.println("Unable to read from socket, might not be open"); |
201 |
> |
} |
202 |
> |
} |
203 |
|
|
204 |
< |
public InetAddress getFilterName(){ |
205 |
< |
// will return the most recient IP address (if it is dynamic for whatever reason |
206 |
< |
try { |
207 |
< |
return InetAddress.getByName(filterName); |
208 |
< |
} |
209 |
< |
catch ( UnknownHostException e ){ |
210 |
< |
// do something |
211 |
< |
return null; |
212 |
< |
} |
213 |
< |
|
214 |
< |
} |
215 |
< |
|
216 |
< |
public int getFilterUDPPort(){ |
217 |
< |
|
218 |
< |
return filterUDPPort; |
219 |
< |
} |
110 |
< |
|
111 |
< |
public int getFilterTCPPort(){ |
112 |
< |
|
113 |
< |
return filterTCPPort; |
114 |
< |
} |
204 |
> |
private String sendCommand(String command){ |
205 |
> |
|
206 |
> |
// System.out.println("Writing command: "+command); |
207 |
> |
socketOut.println(command); |
208 |
> |
socketOut.flush(); |
209 |
> |
try { |
210 |
> |
String response = socketIn.readLine(); |
211 |
> |
// System.out.println("Got: "+response); |
212 |
> |
return response; |
213 |
> |
} |
214 |
> |
catch ( IOException e ){ |
215 |
> |
// something went wrong |
216 |
> |
System.out.println("Error recieving response"); |
217 |
> |
return "ERROR"; |
218 |
> |
} |
219 |
> |
} |
220 |
|
|
221 |
< |
public String getProperty( String name ){ |
222 |
< |
|
223 |
< |
if ( myProperties.containsKey(name) ){ |
224 |
< |
// its in there, may return ERROR |
225 |
< |
return (String) myProperties.get(name); |
226 |
< |
} |
227 |
< |
else |
228 |
< |
{ |
229 |
< |
// can't have been in the config.values.txt file! |
230 |
< |
return "ERROR"; |
231 |
< |
} |
232 |
< |
} |
233 |
< |
|
234 |
< |
private String sendCommand(String command){ |
235 |
< |
|
236 |
< |
socketOut.println(command); |
237 |
< |
try { |
238 |
< |
return socketIn.readLine(); |
239 |
< |
} |
240 |
< |
catch ( IOException e ){ |
241 |
< |
// something went wrong |
242 |
< |
return "ERROR"; |
243 |
< |
} |
139 |
< |
} |
221 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
222 |
> |
|
223 |
> |
public boolean reloadConfig(){ |
224 |
> |
return configChanged; |
225 |
> |
} |
226 |
> |
|
227 |
> |
//---ATTRIBUTES--- |
228 |
> |
|
229 |
> |
private boolean configChanged; |
230 |
> |
private String lastModified; |
231 |
> |
private String fileList; |
232 |
> |
private int numProperties; |
233 |
> |
private HashMap myProperties; |
234 |
> |
private String filterName; |
235 |
> |
private int filterUDPPort; |
236 |
> |
private int filterTCPPort; |
237 |
> |
private ArrayList aList; |
238 |
> |
private BufferedReader socketIn; |
239 |
> |
private PrintWriter socketOut; |
240 |
> |
|
241 |
> |
|
242 |
> |
|
243 |
> |
//---STATIC ATTRIBUTES--- |
244 |
|
|
245 |
|
} // class |