1 |
|
#include "Config.h" |
2 |
|
|
3 |
< |
Config::Config( string serverName, int serverPort ){ |
3 |
> |
Config::Config( string serverName, int serverPort, int printDebug ){ |
4 |
|
// the constructor for the class |
5 |
|
|
6 |
+ |
|
7 |
|
configConnectionRetryTime = 1; // one second |
8 |
+ |
debug = printDebug; |
9 |
+ |
MAX_CONNECTION_ATTEMPTS = 10; // |
10 |
|
|
11 |
+ |
configName = serverName; |
12 |
+ |
configPort = serverPort; |
13 |
+ |
|
14 |
+ |
if ( debug == 1 ){ |
15 |
+ |
std::cout << "Config Constructor\n"; |
16 |
+ |
} |
17 |
+ |
|
18 |
+ |
|
19 |
|
// make a reference to smallnet |
20 |
< |
SmallNet net(); |
20 |
> |
if ( debug == 1 ){ |
21 |
> |
std::cout << "Config::Constructor::Constructing SmallNet\n"; |
22 |
> |
} |
23 |
> |
net = new SmallNet(debug); |
24 |
> |
// net->setDebug(1); |
25 |
|
|
26 |
|
// read the values which we want to retrieve from the |
27 |
|
// config manager from the "config.values.txt" file |
34 |
|
propertyNames[1] = "TCPUpdateTime"; |
35 |
|
propertyNames[2] = "FilterRetryTime"; |
36 |
|
propertyNames[3] = "ConfigRetryTime"; |
37 |
+ |
propertyNames[4] = "AVERAGERUpdateTime"; |
38 |
+ |
propertyNames[5] = "MAXTCPFilterRetries"; |
39 |
+ |
numProperties = 5; |
40 |
|
|
41 |
|
// read the Chat dialog we 'expect' to have with the config |
42 |
|
// manager from disk. This is a new idea so that the client |
46 |
|
|
47 |
|
if ( loadConfig() == 0 ){ |
48 |
|
// successful just continue |
49 |
+ |
if ( debug == 1 ){ |
50 |
+ |
std::cout << "Config::Constructor::loadConfig() Successful\n"; |
51 |
+ |
} |
52 |
|
} |
53 |
|
else { |
54 |
+ |
if ( debug == 1 ){ |
55 |
+ |
std::cout << "Config::Constructor::loadConfig() failed-retrying\n"; |
56 |
+ |
} |
57 |
|
// woops something went wrong. we'll just keep retrying |
58 |
|
// and increasing the time between trys |
59 |
|
int loadConfigResult = -1; |
60 |
|
|
61 |
|
// the following could cause problems if it is fed the |
62 |
|
// incorrect filter name and/or port it'll never stop trying |
63 |
+ |
int retrys = 1; // # of times we have tried to connect but failed.. |
64 |
|
while ( loadConfigResult != 0 ){ |
65 |
+ |
if ( retrys > MAX_CONNECTION_ATTEMPTS ){ |
66 |
+ |
std::cout << "Maxium number of retries reached, Quitting"; |
67 |
+ |
// exit("Failed to connect"); |
68 |
+ |
} |
69 |
|
// make the connection retry time greater |
70 |
|
configConnectionRetryTime = configConnectionRetryTime * 2; |
71 |
+ |
std::cout << "Waiting " << configConnectionRetryTime << " seconds before retrying\n"; |
72 |
|
sleep(configConnectionRetryTime); // seconds |
73 |
+ |
// increment the number of times we have tried to connect |
74 |
+ |
retrys++; |
75 |
|
loadConfigResult = loadConfig(); |
76 |
|
} // while |
77 |
|
|
78 |
|
|
79 |
|
} // if |
80 |
|
|
81 |
+ |
if ( debug == 1 ){ |
82 |
+ |
std::cout << "Config::Constructor - Finished!\n"; |
83 |
+ |
} |
84 |
+ |
|
85 |
|
} // Config |
86 |
|
|
87 |
|
int Config::loadConfig(){ |
88 |
|
// establishes a connection to the config manager (filter manager?) and |
89 |
|
// tries to load all the info which it needs, if it fails returns !0 |
90 |
|
|
91 |
+ |
if ( debug == 1 ){ |
92 |
+ |
std::cout << "Config::loadConfig()\n"; |
93 |
+ |
} |
94 |
+ |
|
95 |
|
// make a connection |
96 |
+ |
|
97 |
+ |
if ( debug == 1 ){ |
98 |
+ |
std::cout << "Config::loadConfig::establishing config connection\n"; |
99 |
+ |
} |
100 |
|
int established = establishConfig(); |
101 |
|
if ( established != 0 ){ |
102 |
|
return established; |
103 |
|
} |
104 |
|
|
105 |
|
// do the talk ;) |
106 |
+ |
|
107 |
+ |
if ( debug == 1 ){ |
108 |
+ |
std::cout << "Config::loadConfig::Processing chat script\n"; |
109 |
+ |
} |
110 |
|
int chat = chatConfig(); |
111 |
|
if ( chat != 0 ){ |
112 |
|
// and close the connection |
113 |
|
destroyConfig(); |
114 |
< |
return chat; |
114 |
> |
// return chat; |
115 |
|
} |
116 |
|
|
117 |
|
// and close the connection |
118 |
+ |
|
119 |
+ |
if ( debug == 1 ){ |
120 |
+ |
std::cout << "Config::loadConfig::closing config connection\n"; |
121 |
+ |
} |
122 |
|
destroyConfig(); |
123 |
|
|
124 |
|
// return everything ok |
128 |
|
|
129 |
|
int Config::establishHeartbeat(){ |
130 |
|
// just an interface onto smallnet |
131 |
+ |
if ( debug == 1 ){ |
132 |
+ |
std::cout << "Config::establishHeartbeat()\n"; |
133 |
+ |
} |
134 |
|
|
135 |
< |
int establish = 0; //net.connectHeartbeat(configName, configPort); |
135 |
> |
int establish = net->connectHeartBeat(configName, configPort); |
136 |
|
|
137 |
+ |
if ( debug == 1 ){ |
138 |
+ |
std::cout << "->" << establish << "\n"; |
139 |
+ |
} |
140 |
+ |
|
141 |
|
return establish; |
142 |
|
|
143 |
|
} // establishHeartBeat |
145 |
|
|
146 |
|
int Config::destroyHeartbeat(){ |
147 |
|
// just hooks into the smallnet interface |
148 |
+ |
if ( debug == 1 ){ |
149 |
+ |
std::cout << "Config::destroyHeartbeat()\n"; |
150 |
+ |
} |
151 |
|
|
152 |
< |
net.closeHeartBeatConnection(); |
152 |
> |
net->closeHeartBeatConnection(); |
153 |
|
|
154 |
|
} // destroyHeartBeat |
155 |
|
|
156 |
+ |
int Config::chatHeartBeat(){ |
157 |
+ |
|
158 |
+ |
string response; |
159 |
+ |
|
160 |
+ |
response = net->heartBeatSend("HEARTBEAT"); |
161 |
+ |
if ( response != "OK" ){ |
162 |
+ |
// something went wrong |
163 |
+ |
return -1; |
164 |
+ |
if ( debug == 1 ){ |
165 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in HEARTBEAT\n"; |
166 |
+ |
} |
167 |
+ |
} |
168 |
+ |
|
169 |
+ |
response = net->heartBeatSend("CONFIG"); |
170 |
+ |
if ( response != "OK" ){ |
171 |
+ |
// something went wrong |
172 |
+ |
return -1; |
173 |
+ |
if ( debug == 1 ){ |
174 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in CONFIG\n"; |
175 |
+ |
} |
176 |
+ |
} |
177 |
+ |
|
178 |
+ |
response = net->heartBeatSend(fileList); |
179 |
+ |
if ( response != "OK" ){ |
180 |
+ |
// something went wrong |
181 |
+ |
return -1; |
182 |
+ |
if ( debug == 1 ){ |
183 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in fileList\n"; |
184 |
+ |
} |
185 |
+ |
} |
186 |
+ |
|
187 |
+ |
response = net->heartBeatSend(lastModified); |
188 |
+ |
if ( response != "OK" ){ |
189 |
+ |
// something went wrong |
190 |
+ |
return -1; |
191 |
+ |
if ( debug == 1 ){ |
192 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in lastModified\n"; |
193 |
+ |
} |
194 |
+ |
} |
195 |
+ |
|
196 |
+ |
response = net->heartBeatSend("ENDHEARTBEAT"); |
197 |
+ |
if ( response != "OK" ){ |
198 |
+ |
// something went wrong |
199 |
+ |
return -1; |
200 |
+ |
if ( debug == 1 ){ |
201 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in ENDHEARTBEAT\n"; |
202 |
+ |
} |
203 |
+ |
} |
204 |
+ |
|
205 |
+ |
// seems ok to me |
206 |
+ |
return 0; |
207 |
+ |
|
208 |
+ |
} // chat Heart Beat |
209 |
|
|
210 |
+ |
|
211 |
|
int Config::establishConfig(){ |
212 |
|
// ask smallnet to make a connection to the filtermanager so we can get some config |
213 |
|
// details |
214 |
+ |
if ( debug == 1 ){ |
215 |
+ |
std::cout << "Config::establishConfig()\n"; |
216 |
+ |
} |
217 |
|
|
99 |
– |
net.connectConfig(); |
218 |
|
|
219 |
+ |
/* MODIFY THIS */ |
220 |
+ |
int response = net->connectConfig(configName, configPort); |
221 |
|
|
222 |
< |
return 0; |
222 |
> |
if ( debug == 1 ){ |
223 |
> |
std::cout << "->" << response << "\n"; |
224 |
> |
} |
225 |
|
|
226 |
+ |
return response; |
227 |
+ |
|
228 |
|
} // establishConfig |
229 |
|
|
230 |
|
|
231 |
|
int Config::chatConfig(){ |
232 |
|
// do some funky jibble.. ;) |
233 |
+ |
if ( debug == 1 ){ |
234 |
+ |
std::cout << "Config::chatConfig()\n"; |
235 |
+ |
} |
236 |
|
|
237 |
|
string response; |
238 |
|
|
239 |
+ |
|
240 |
|
// hard coeded for the moment |
241 |
< |
response = net.heartBeatSend("STARTCONFIG"); |
241 |
> |
if ( debug == 1 ){ |
242 |
> |
std::cout << "Config::chatConfig::STARTCONFIG\n"; |
243 |
> |
} |
244 |
> |
response = net->heartBeatSend("STARTCONFIG"); |
245 |
|
if ( response != "OK" ){ |
246 |
|
// something went wrong |
247 |
|
return -1; |
248 |
+ |
if ( debug == 1 ){ |
249 |
+ |
std::cout << "Config::chatConfig::ERROR\n"; |
250 |
+ |
} |
251 |
|
} |
252 |
|
|
253 |
< |
lastModified = net.heartBeatSend("LASTMODIFIED"); |
254 |
< |
fileList = net.heartBeatSend("FILELIST"); |
253 |
> |
if ( debug == 1 ){ |
254 |
> |
std::cout << "Config::chatConfig::LASTMODIFIED\n"; |
255 |
> |
} |
256 |
> |
lastModified = net->heartBeatSend("LASTMODIFIED"); |
257 |
> |
if ( debug == 1 ){ |
258 |
> |
std::cout << "Config::chatConfig::FILELIST\n"; |
259 |
> |
} |
260 |
> |
fileList = net->heartBeatSend("FILELIST"); |
261 |
|
|
262 |
+ |
fQDN = net->heartBeatSend("FQDN"); |
263 |
+ |
|
264 |
|
// now send the properties we want |
265 |
|
|
266 |
+ |
if ( debug == 1 ){ |
267 |
+ |
std::cout << "Config::chatConfig::requesting properties\n"; |
268 |
+ |
} |
269 |
|
if ( numProperties > 0 ){ |
270 |
|
// we actually need to look for some properties |
271 |
|
for ( int i=0; i < numProperties; i++ ){ |
272 |
|
// lets hope that this works! ;) |
273 |
< |
propertyValues[i] = net.heartBeatSend(propertyNames[i]); |
273 |
> |
if ( debug == 1 ){ |
274 |
> |
std::cout << "Config::chatConfig::" << propertyNames[i] << "\n"; |
275 |
> |
} |
276 |
> |
propertyValues[i] = net->heartBeatSend(propertyNames[i]); |
277 |
|
} // for |
278 |
|
} // if |
279 |
|
|
280 |
< |
response = net.heartBeatSend("ENDCONFIG"); |
280 |
> |
if ( debug == 1 ){ |
281 |
> |
std::cout << "Config::chatConfig::ENDCONFIG\n"; |
282 |
> |
} |
283 |
> |
response = net->heartBeatSend("ENDCONFIG"); |
284 |
|
// doesn't matter what it returns |
285 |
|
|
286 |
< |
response = net.heartBeatSend("FILTER"); |
286 |
> |
if ( debug == 1 ){ |
287 |
> |
std::cout << "Config::chatConfig::FILTER\n"; |
288 |
> |
} |
289 |
> |
response = net->heartBeatSend("FILTER"); |
290 |
|
// now we need to pull repsonse apart to get the filter information out |
291 |
+ |
int first = response.find(";",0); |
292 |
+ |
int second = response.find(";",first+1); |
293 |
+ |
UDPFilterName = response.substr(0,first); |
294 |
+ |
UDPFilterPort = atoi( response.substr( first+1 ,second ).c_str()); |
295 |
+ |
TCPFilterPort = atoi(response.substr(second+1).c_str()); |
296 |
|
|
297 |
< |
// FIX ME |
297 |
> |
if ( debug == 1 ){ |
298 |
> |
std::cout << "Config::chatConfig::END\n"; |
299 |
> |
} |
300 |
> |
response = net->heartBeatSend("END"); |
301 |
|
|
140 |
– |
response = net.heartBeatSend("END"); |
141 |
– |
|
302 |
|
return 0; |
303 |
|
|
304 |
|
} // chatConfig |
305 |
|
|
306 |
|
int Config::destroyConfig(){ |
307 |
+ |
if ( debug == 1 ){ |
308 |
+ |
std::cout << "Config::destroyConfig()\n"; |
309 |
+ |
} |
310 |
|
|
311 |
< |
net.closeConfigConnection(); |
311 |
> |
net->closeConfigConnection(); |
312 |
|
|
313 |
|
} // destroyConfig |
314 |
|
|
315 |
< |
string getProperty(string propertyName){ |
315 |
> |
string Config::getStrProperty(string propertyName){ |
316 |
|
|
317 |
< |
/* // doesn't compile with this for some reason! |
317 |
> |
|
318 |
> |
if ( debug == 1 ){ |
319 |
> |
std::cout << "Config::getProperty()\n"; |
320 |
> |
} |
321 |
> |
|
322 |
|
for ( int i=0; i<20; i++ ){ |
323 |
|
if ( propertyName == propertyNames[i] ){ |
324 |
< |
return propertyValues[i]; |
324 |
> |
if ( propertyValues[i] != "" ){ |
325 |
> |
return propertyValues[i]; |
326 |
> |
} // if |
327 |
|
} // if |
328 |
|
} // for |
160 |
– |
*/ |
329 |
|
|
330 |
|
// otherwise return ERROR |
331 |
|
return "ERROR"; |
332 |
|
|
333 |
< |
} // getProperty |
333 |
> |
} // getStrProperty |
334 |
> |
|
335 |
> |
int Config::getIntProperty( string propertyName ){ |
336 |
> |
|
337 |
> |
if ( debug == 1 ){ |
338 |
> |
std::cout << "Config::getProperty()\n"; |
339 |
> |
} |
340 |
> |
|
341 |
> |
string response = getStrProperty( propertyName ); |
342 |
> |
if ( response == "ERROR" ){ |
343 |
> |
return -1; |
344 |
> |
} // if |
345 |
> |
|
346 |
> |
int i = atoi(response.c_str()); |
347 |
> |
|
348 |
> |
if ( i > 0 ){ |
349 |
> |
return i; |
350 |
> |
} |
351 |
> |
|
352 |
> |
// otherwise return ERROR |
353 |
> |
return -1; |
354 |
> |
|
355 |
> |
} // getIntProperty |