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