1 |
ab11 |
1.1 |
#include "Config.h" |
2 |
|
|
|
3 |
ab11 |
1.3 |
Config::Config( string serverName, int serverPort, int printDebug ){ |
4 |
ab11 |
1.1 |
// the constructor for the class |
5 |
|
|
|
6 |
ab11 |
1.3 |
|
7 |
ab11 |
1.1 |
configConnectionRetryTime = 1; // one second |
8 |
ab11 |
1.3 |
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 |
ab11 |
1.1 |
|
19 |
|
|
// make a reference to smallnet |
20 |
ab11 |
1.3 |
if ( debug == 1 ){ |
21 |
|
|
std::cout << "Config::Constructor::Constructing SmallNet\n"; |
22 |
|
|
} |
23 |
|
|
net = new SmallNet(debug); |
24 |
|
|
// net->setDebug(1); |
25 |
ab11 |
1.1 |
|
26 |
|
|
// read the values which we want to retrieve from the |
27 |
|
|
// config manager from the "config.values.txt" file |
28 |
|
|
//propertyNames = new string[20]; |
29 |
|
|
//propertyValues = new string[20]; |
30 |
|
|
|
31 |
|
|
// FIX ME |
32 |
|
|
// sod that we are using defined values now.. |
33 |
|
|
propertyNames[0] = "UDPUpdateTime"; |
34 |
|
|
propertyNames[1] = "TCPUpdateTime"; |
35 |
|
|
propertyNames[2] = "FilterRetryTime"; |
36 |
|
|
propertyNames[3] = "ConfigRetryTime"; |
37 |
ab11 |
1.5 |
propertyNames[4] = "AVERAGERUpdateTime"; |
38 |
|
|
propertyNames[5] = "MAXTCPFilterRetries"; |
39 |
|
|
numProperties = 5; |
40 |
ab11 |
1.1 |
|
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 |
43 |
|
|
// won't have to be hard coded with the dialog (incase it changes) |
44 |
|
|
|
45 |
|
|
// FIX ME |
46 |
|
|
|
47 |
|
|
if ( loadConfig() == 0 ){ |
48 |
|
|
// successful just continue |
49 |
ab11 |
1.3 |
if ( debug == 1 ){ |
50 |
|
|
std::cout << "Config::Constructor::loadConfig() Successful\n"; |
51 |
|
|
} |
52 |
ab11 |
1.1 |
} |
53 |
|
|
else { |
54 |
ab11 |
1.3 |
if ( debug == 1 ){ |
55 |
|
|
std::cout << "Config::Constructor::loadConfig() failed-retrying\n"; |
56 |
|
|
} |
57 |
ab11 |
1.1 |
// 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 |
ab11 |
1.3 |
int retrys = 1; // # of times we have tried to connect but failed.. |
64 |
ab11 |
1.1 |
while ( loadConfigResult != 0 ){ |
65 |
ab11 |
1.3 |
if ( retrys > MAX_CONNECTION_ATTEMPTS ){ |
66 |
|
|
std::cout << "Maxium number of retries reached, Quitting"; |
67 |
|
|
// exit("Failed to connect"); |
68 |
|
|
} |
69 |
ab11 |
1.1 |
// make the connection retry time greater |
70 |
|
|
configConnectionRetryTime = configConnectionRetryTime * 2; |
71 |
ab11 |
1.3 |
std::cout << "Waiting " << configConnectionRetryTime << " seconds before retrying\n"; |
72 |
ab11 |
1.1 |
sleep(configConnectionRetryTime); // seconds |
73 |
ab11 |
1.3 |
// increment the number of times we have tried to connect |
74 |
|
|
retrys++; |
75 |
ab11 |
1.1 |
loadConfigResult = loadConfig(); |
76 |
|
|
} // while |
77 |
|
|
|
78 |
|
|
|
79 |
|
|
} // if |
80 |
|
|
|
81 |
ab11 |
1.3 |
if ( debug == 1 ){ |
82 |
|
|
std::cout << "Config::Constructor - Finished!\n"; |
83 |
|
|
} |
84 |
|
|
|
85 |
ab11 |
1.1 |
} // 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 |
ab11 |
1.3 |
if ( debug == 1 ){ |
92 |
|
|
std::cout << "Config::loadConfig()\n"; |
93 |
|
|
} |
94 |
|
|
|
95 |
ab11 |
1.1 |
// make a connection |
96 |
ab11 |
1.3 |
|
97 |
|
|
if ( debug == 1 ){ |
98 |
|
|
std::cout << "Config::loadConfig::establishing config connection\n"; |
99 |
|
|
} |
100 |
ab11 |
1.1 |
int established = establishConfig(); |
101 |
|
|
if ( established != 0 ){ |
102 |
|
|
return established; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
// do the talk ;) |
106 |
ab11 |
1.3 |
|
107 |
|
|
if ( debug == 1 ){ |
108 |
|
|
std::cout << "Config::loadConfig::Processing chat script\n"; |
109 |
|
|
} |
110 |
ab11 |
1.1 |
int chat = chatConfig(); |
111 |
|
|
if ( chat != 0 ){ |
112 |
|
|
// and close the connection |
113 |
|
|
destroyConfig(); |
114 |
ab11 |
1.3 |
// return chat; |
115 |
ab11 |
1.1 |
} |
116 |
|
|
|
117 |
|
|
// and close the connection |
118 |
ab11 |
1.3 |
|
119 |
|
|
if ( debug == 1 ){ |
120 |
|
|
std::cout << "Config::loadConfig::closing config connection\n"; |
121 |
|
|
} |
122 |
ab11 |
1.1 |
destroyConfig(); |
123 |
|
|
|
124 |
|
|
// return everything ok |
125 |
|
|
return 0; |
126 |
|
|
|
127 |
|
|
} // loadConfig |
128 |
|
|
|
129 |
|
|
int Config::establishHeartbeat(){ |
130 |
|
|
// just an interface onto smallnet |
131 |
ab11 |
1.3 |
if ( debug == 1 ){ |
132 |
|
|
std::cout << "Config::establishHeartbeat()\n"; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
int establish = net->connectHeartBeat(configName, configPort); |
136 |
ab11 |
1.1 |
|
137 |
ab11 |
1.3 |
if ( debug == 1 ){ |
138 |
|
|
std::cout << "->" << establish << "\n"; |
139 |
|
|
} |
140 |
ab11 |
1.1 |
|
141 |
|
|
return establish; |
142 |
|
|
|
143 |
|
|
} // establishHeartBeat |
144 |
|
|
|
145 |
|
|
|
146 |
|
|
int Config::destroyHeartbeat(){ |
147 |
|
|
// just hooks into the smallnet interface |
148 |
ab11 |
1.3 |
if ( debug == 1 ){ |
149 |
|
|
std::cout << "Config::destroyHeartbeat()\n"; |
150 |
|
|
} |
151 |
ab11 |
1.1 |
|
152 |
ab11 |
1.3 |
net->closeHeartBeatConnection(); |
153 |
ab11 |
1.1 |
|
154 |
|
|
} // destroyHeartBeat |
155 |
ab11 |
1.5 |
|
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 |
ab11 |
1.1 |
|
210 |
|
|
|
211 |
|
|
int Config::establishConfig(){ |
212 |
|
|
// ask smallnet to make a connection to the filtermanager so we can get some config |
213 |
|
|
// details |
214 |
ab11 |
1.3 |
if ( debug == 1 ){ |
215 |
|
|
std::cout << "Config::establishConfig()\n"; |
216 |
|
|
} |
217 |
|
|
|
218 |
ab11 |
1.1 |
|
219 |
ab11 |
1.3 |
/* MODIFY THIS */ |
220 |
|
|
int response = net->connectConfig(configName, configPort); |
221 |
ab11 |
1.1 |
|
222 |
ab11 |
1.3 |
if ( debug == 1 ){ |
223 |
|
|
std::cout << "->" << response << "\n"; |
224 |
|
|
} |
225 |
ab11 |
1.1 |
|
226 |
ab11 |
1.3 |
return response; |
227 |
ab11 |
1.1 |
|
228 |
|
|
} // establishConfig |
229 |
|
|
|
230 |
|
|
|
231 |
|
|
int Config::chatConfig(){ |
232 |
|
|
// do some funky jibble.. ;) |
233 |
ab11 |
1.3 |
if ( debug == 1 ){ |
234 |
|
|
std::cout << "Config::chatConfig()\n"; |
235 |
|
|
} |
236 |
ab11 |
1.1 |
|
237 |
|
|
string response; |
238 |
|
|
|
239 |
ab11 |
1.3 |
|
240 |
ab11 |
1.1 |
// hard coeded for the moment |
241 |
ab11 |
1.3 |
if ( debug == 1 ){ |
242 |
|
|
std::cout << "Config::chatConfig::STARTCONFIG\n"; |
243 |
|
|
} |
244 |
|
|
response = net->heartBeatSend("STARTCONFIG"); |
245 |
ab11 |
1.1 |
if ( response != "OK" ){ |
246 |
|
|
// something went wrong |
247 |
|
|
return -1; |
248 |
ab11 |
1.3 |
if ( debug == 1 ){ |
249 |
|
|
std::cout << "Config::chatConfig::ERROR\n"; |
250 |
|
|
} |
251 |
ab11 |
1.1 |
} |
252 |
|
|
|
253 |
ab11 |
1.3 |
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 |
ab11 |
1.1 |
|
262 |
ab11 |
1.4 |
fQDN = net->heartBeatSend("FQDN"); |
263 |
|
|
|
264 |
ab11 |
1.1 |
// now send the properties we want |
265 |
|
|
|
266 |
ab11 |
1.3 |
if ( debug == 1 ){ |
267 |
|
|
std::cout << "Config::chatConfig::requesting properties\n"; |
268 |
|
|
} |
269 |
ab11 |
1.1 |
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 |
ab11 |
1.3 |
if ( debug == 1 ){ |
274 |
|
|
std::cout << "Config::chatConfig::" << propertyNames[i] << "\n"; |
275 |
|
|
} |
276 |
|
|
propertyValues[i] = net->heartBeatSend(propertyNames[i]); |
277 |
ab11 |
1.1 |
} // for |
278 |
|
|
} // if |
279 |
|
|
|
280 |
ab11 |
1.3 |
if ( debug == 1 ){ |
281 |
|
|
std::cout << "Config::chatConfig::ENDCONFIG\n"; |
282 |
|
|
} |
283 |
|
|
response = net->heartBeatSend("ENDCONFIG"); |
284 |
ab11 |
1.1 |
// doesn't matter what it returns |
285 |
|
|
|
286 |
ab11 |
1.3 |
if ( debug == 1 ){ |
287 |
|
|
std::cout << "Config::chatConfig::FILTER\n"; |
288 |
|
|
} |
289 |
|
|
response = net->heartBeatSend("FILTER"); |
290 |
ab11 |
1.1 |
// now we need to pull repsonse apart to get the filter information out |
291 |
ab11 |
1.3 |
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 |
ab11 |
1.1 |
|
297 |
ab11 |
1.3 |
if ( debug == 1 ){ |
298 |
|
|
std::cout << "Config::chatConfig::END\n"; |
299 |
|
|
} |
300 |
|
|
response = net->heartBeatSend("END"); |
301 |
ab11 |
1.1 |
|
302 |
|
|
return 0; |
303 |
|
|
|
304 |
|
|
} // chatConfig |
305 |
|
|
|
306 |
|
|
int Config::destroyConfig(){ |
307 |
ab11 |
1.3 |
if ( debug == 1 ){ |
308 |
|
|
std::cout << "Config::destroyConfig()\n"; |
309 |
|
|
} |
310 |
ab11 |
1.1 |
|
311 |
ab11 |
1.3 |
net->closeConfigConnection(); |
312 |
ab11 |
1.1 |
|
313 |
|
|
} // destroyConfig |
314 |
|
|
|
315 |
ab11 |
1.3 |
string Config::getStrProperty(string propertyName){ |
316 |
|
|
|
317 |
|
|
|
318 |
|
|
if ( debug == 1 ){ |
319 |
|
|
std::cout << "Config::getProperty()\n"; |
320 |
|
|
} |
321 |
ab11 |
1.1 |
|
322 |
|
|
for ( int i=0; i<20; i++ ){ |
323 |
|
|
if ( propertyName == propertyNames[i] ){ |
324 |
ab11 |
1.3 |
if ( propertyValues[i] != "" ){ |
325 |
|
|
return propertyValues[i]; |
326 |
|
|
} // if |
327 |
ab11 |
1.1 |
} // if |
328 |
|
|
} // for |
329 |
|
|
|
330 |
|
|
// otherwise return ERROR |
331 |
|
|
return "ERROR"; |
332 |
|
|
|
333 |
ab11 |
1.3 |
} // 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 |