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(tmpIn); |
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 |
+ |
|
56 |
|
// do the funky jibble |
57 |
|
connect(serverName, serverPort); |
58 |
|
} |
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 |
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 |
< |
|
119 |
< |
if (sendCommand("STARTCONFIG") == "OK"){ |
127 |
> |
|
128 |
> |
if (sendCommand("STARTCONFIG").equals("OK")){ |
129 |
|
// everything is fine |
130 |
< |
sendCommand("LASTMODIFIED"); |
131 |
< |
sendCommand("FILELIST"); |
130 |
> |
// sendCommand("LASTMODIFIED"); |
131 |
> |
lastModified = sendCommand("LASTMODIFIED"); |
132 |
|
|
133 |
+ |
fileList = sendCommand("FILELIST"); |
134 |
|
// get all the properties |
135 |
< |
for ( int i = 0; i < numProperties; i++ ){ |
136 |
< |
String property = (String) aList.get(i); |
137 |
< |
myProperties.put(property, sendCommand(property)); |
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"); |
148 |
|
filterTCPPort = Integer.parseInt(tok.nextToken()); |
149 |
|
|
150 |
|
sendCommand("END"); |
151 |
< |
|
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 |
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 |
+ |
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 |
|
private String sendCommand(String command){ |
205 |
|
|
206 |
+ |
// System.out.println("Writing command: "+command); |
207 |
|
socketOut.println(command); |
208 |
+ |
socketOut.flush(); |
209 |
|
try { |
210 |
< |
return socketIn.readLine(); |
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 |
|
//---ACCESSOR/MUTATOR METHODS--- |
222 |
|
|
223 |
+ |
public boolean reloadConfig(){ |
224 |
+ |
return configChanged; |
225 |
+ |
} |
226 |
+ |
|
227 |
|
//---ATTRIBUTES--- |
228 |
|
|
229 |
< |
|
229 |
> |
private boolean configChanged; |
230 |
|
private String lastModified; |
231 |
+ |
private String fileList; |
232 |
|
private int numProperties; |
233 |
< |
private Hashtable myProperties; |
233 |
> |
private HashMap myProperties; |
234 |
|
private String filterName; |
235 |
|
private int filterUDPPort; |
236 |
|
private int filterTCPPort; |