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 |
|
|
configConnectionRetryTime = 1; // one second |
7 |
ab11 |
1.3 |
debug = printDebug; |
8 |
ab11 |
1.6 |
MAX_CONNECTION_ATTEMPTS = 10; |
9 |
ab11 |
1.3 |
|
10 |
|
|
configName = serverName; |
11 |
|
|
configPort = serverPort; |
12 |
|
|
|
13 |
|
|
if ( debug == 1 ){ |
14 |
|
|
std::cout << "Config Constructor\n"; |
15 |
ab11 |
1.6 |
} // if |
16 |
ab11 |
1.3 |
|
17 |
ab11 |
1.1 |
|
18 |
|
|
// make a reference to smallnet |
19 |
ab11 |
1.3 |
if ( debug == 1 ){ |
20 |
|
|
std::cout << "Config::Constructor::Constructing SmallNet\n"; |
21 |
|
|
} |
22 |
|
|
net = new SmallNet(debug); |
23 |
ab11 |
1.6 |
|
24 |
ab11 |
1.1 |
// 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 |
ab11 |
1.5 |
propertyNames[4] = "AVERAGERUpdateTime"; |
30 |
|
|
propertyNames[5] = "MAXTCPFilterRetries"; |
31 |
|
|
numProperties = 5; |
32 |
ab11 |
1.1 |
|
33 |
|
|
if ( loadConfig() == 0 ){ |
34 |
|
|
// successful just continue |
35 |
ab11 |
1.3 |
if ( debug == 1 ){ |
36 |
|
|
std::cout << "Config::Constructor::loadConfig() Successful\n"; |
37 |
ab11 |
1.6 |
} // if |
38 |
|
|
} else { |
39 |
ab11 |
1.3 |
if ( debug == 1 ){ |
40 |
|
|
std::cout << "Config::Constructor::loadConfig() failed-retrying\n"; |
41 |
ab11 |
1.6 |
} // if |
42 |
|
|
|
43 |
ab11 |
1.1 |
// 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 |
ab11 |
1.3 |
int retrys = 1; // # of times we have tried to connect but failed.. |
50 |
ab11 |
1.1 |
while ( loadConfigResult != 0 ){ |
51 |
ab11 |
1.3 |
if ( retrys > MAX_CONNECTION_ATTEMPTS ){ |
52 |
|
|
std::cout << "Maxium number of retries reached, Quitting"; |
53 |
ab11 |
1.6 |
} // if |
54 |
ab11 |
1.1 |
// make the connection retry time greater |
55 |
|
|
configConnectionRetryTime = configConnectionRetryTime * 2; |
56 |
ab11 |
1.3 |
std::cout << "Waiting " << configConnectionRetryTime << " seconds before retrying\n"; |
57 |
ab11 |
1.1 |
sleep(configConnectionRetryTime); // seconds |
58 |
ab11 |
1.3 |
// increment the number of times we have tried to connect |
59 |
|
|
retrys++; |
60 |
ab11 |
1.1 |
loadConfigResult = loadConfig(); |
61 |
|
|
} // while |
62 |
|
|
} // if |
63 |
|
|
|
64 |
ab11 |
1.3 |
if ( debug == 1 ){ |
65 |
|
|
std::cout << "Config::Constructor - Finished!\n"; |
66 |
ab11 |
1.6 |
} // if |
67 |
ab11 |
1.3 |
|
68 |
ab11 |
1.1 |
} // 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 |
ab11 |
1.3 |
if ( debug == 1 ){ |
75 |
|
|
std::cout << "Config::loadConfig()\n"; |
76 |
ab11 |
1.6 |
} // if |
77 |
ab11 |
1.3 |
|
78 |
ab11 |
1.1 |
// make a connection |
79 |
ab11 |
1.3 |
|
80 |
|
|
if ( debug == 1 ){ |
81 |
|
|
std::cout << "Config::loadConfig::establishing config connection\n"; |
82 |
ab11 |
1.6 |
} // if |
83 |
|
|
|
84 |
ab11 |
1.1 |
int established = establishConfig(); |
85 |
|
|
if ( established != 0 ){ |
86 |
ab11 |
1.6 |
destroyConfig(); |
87 |
ab11 |
1.1 |
return established; |
88 |
ab11 |
1.6 |
} // if |
89 |
ab11 |
1.1 |
|
90 |
|
|
// do the talk ;) |
91 |
ab11 |
1.3 |
|
92 |
|
|
if ( debug == 1 ){ |
93 |
|
|
std::cout << "Config::loadConfig::Processing chat script\n"; |
94 |
ab11 |
1.6 |
} // if |
95 |
|
|
|
96 |
ab11 |
1.1 |
int chat = chatConfig(); |
97 |
|
|
if ( chat != 0 ){ |
98 |
|
|
// and close the connection |
99 |
|
|
destroyConfig(); |
100 |
ab11 |
1.6 |
} // if |
101 |
ab11 |
1.1 |
|
102 |
|
|
// and close the connection |
103 |
ab11 |
1.3 |
if ( debug == 1 ){ |
104 |
|
|
std::cout << "Config::loadConfig::closing config connection\n"; |
105 |
ab11 |
1.6 |
} // if |
106 |
ab11 |
1.1 |
destroyConfig(); |
107 |
|
|
|
108 |
|
|
// return everything ok |
109 |
|
|
return 0; |
110 |
|
|
|
111 |
|
|
} // loadConfig |
112 |
|
|
|
113 |
|
|
int Config::establishHeartbeat(){ |
114 |
|
|
// just an interface onto smallnet |
115 |
ab11 |
1.3 |
if ( debug == 1 ){ |
116 |
|
|
std::cout << "Config::establishHeartbeat()\n"; |
117 |
ab11 |
1.6 |
} // if |
118 |
ab11 |
1.1 |
|
119 |
ab11 |
1.6 |
int establish = net->connectHeartBeat(configName, configPort); |
120 |
|
|
|
121 |
ab11 |
1.3 |
if ( debug == 1 ){ |
122 |
|
|
std::cout << "->" << establish << "\n"; |
123 |
ab11 |
1.6 |
} // if |
124 |
ab11 |
1.1 |
|
125 |
|
|
return establish; |
126 |
|
|
|
127 |
|
|
} // establishHeartBeat |
128 |
|
|
|
129 |
|
|
int Config::destroyHeartbeat(){ |
130 |
|
|
// just hooks into the smallnet interface |
131 |
ab11 |
1.3 |
if ( debug == 1 ){ |
132 |
|
|
std::cout << "Config::destroyHeartbeat()\n"; |
133 |
ab11 |
1.6 |
} // if |
134 |
ab11 |
1.1 |
|
135 |
ab11 |
1.3 |
net->closeHeartBeatConnection(); |
136 |
ab11 |
1.1 |
|
137 |
|
|
} // destroyHeartBeat |
138 |
ab11 |
1.5 |
|
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 |
ab11 |
1.6 |
} // if |
149 |
|
|
return -1; |
150 |
|
|
} // if |
151 |
ab11 |
1.5 |
|
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 |
ab11 |
1.6 |
} // if |
158 |
|
|
return -1; |
159 |
|
|
} // if |
160 |
ab11 |
1.5 |
|
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 |
ab11 |
1.6 |
} // if |
167 |
|
|
return -1; |
168 |
|
|
} // if |
169 |
ab11 |
1.5 |
|
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 |
ab11 |
1.6 |
} // if |
176 |
|
|
return -1; |
177 |
|
|
} // if |
178 |
ab11 |
1.5 |
|
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 |
ab11 |
1.6 |
} // if |
185 |
|
|
return -1; |
186 |
|
|
} // if |
187 |
ab11 |
1.5 |
|
188 |
|
|
// seems ok to me |
189 |
|
|
return 0; |
190 |
|
|
|
191 |
|
|
} // chat Heart Beat |
192 |
ab11 |
1.1 |
|
193 |
|
|
|
194 |
|
|
int Config::establishConfig(){ |
195 |
|
|
// ask smallnet to make a connection to the filtermanager so we can get some config |
196 |
|
|
// details |
197 |
ab11 |
1.3 |
if ( debug == 1 ){ |
198 |
|
|
std::cout << "Config::establishConfig()\n"; |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
int response = net->connectConfig(configName, configPort); |
202 |
ab11 |
1.1 |
|
203 |
ab11 |
1.3 |
if ( debug == 1 ){ |
204 |
|
|
std::cout << "->" << response << "\n"; |
205 |
ab11 |
1.6 |
} // if |
206 |
ab11 |
1.1 |
|
207 |
ab11 |
1.3 |
return response; |
208 |
ab11 |
1.1 |
|
209 |
|
|
} // establishConfig |
210 |
|
|
|
211 |
|
|
|
212 |
|
|
int Config::chatConfig(){ |
213 |
|
|
// do some funky jibble.. ;) |
214 |
ab11 |
1.3 |
if ( debug == 1 ){ |
215 |
|
|
std::cout << "Config::chatConfig()\n"; |
216 |
ab11 |
1.6 |
} // if |
217 |
ab11 |
1.1 |
|
218 |
|
|
string response; |
219 |
ab11 |
1.6 |
|
220 |
ab11 |
1.1 |
// hard coeded for the moment |
221 |
ab11 |
1.3 |
if ( debug == 1 ){ |
222 |
|
|
std::cout << "Config::chatConfig::STARTCONFIG\n"; |
223 |
ab11 |
1.6 |
} // if |
224 |
|
|
|
225 |
ab11 |
1.3 |
response = net->heartBeatSend("STARTCONFIG"); |
226 |
ab11 |
1.1 |
if ( response != "OK" ){ |
227 |
|
|
// something went wrong |
228 |
ab11 |
1.3 |
if ( debug == 1 ){ |
229 |
|
|
std::cout << "Config::chatConfig::ERROR\n"; |
230 |
ab11 |
1.6 |
} // if |
231 |
|
|
return -1; |
232 |
|
|
} // if |
233 |
ab11 |
1.1 |
|
234 |
ab11 |
1.3 |
if ( debug == 1 ){ |
235 |
|
|
std::cout << "Config::chatConfig::LASTMODIFIED\n"; |
236 |
ab11 |
1.6 |
} // if |
237 |
ab11 |
1.3 |
lastModified = net->heartBeatSend("LASTMODIFIED"); |
238 |
|
|
if ( debug == 1 ){ |
239 |
|
|
std::cout << "Config::chatConfig::FILELIST\n"; |
240 |
ab11 |
1.6 |
} // if |
241 |
ab11 |
1.3 |
fileList = net->heartBeatSend("FILELIST"); |
242 |
ab11 |
1.1 |
|
243 |
ab11 |
1.4 |
fQDN = net->heartBeatSend("FQDN"); |
244 |
|
|
|
245 |
ab11 |
1.1 |
// now send the properties we want |
246 |
|
|
|
247 |
ab11 |
1.3 |
if ( debug == 1 ){ |
248 |
|
|
std::cout << "Config::chatConfig::requesting properties\n"; |
249 |
ab11 |
1.6 |
} // if |
250 |
ab11 |
1.1 |
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 |
ab11 |
1.3 |
if ( debug == 1 ){ |
255 |
|
|
std::cout << "Config::chatConfig::" << propertyNames[i] << "\n"; |
256 |
ab11 |
1.6 |
} // if |
257 |
ab11 |
1.3 |
propertyValues[i] = net->heartBeatSend(propertyNames[i]); |
258 |
ab11 |
1.1 |
} // for |
259 |
|
|
} // if |
260 |
|
|
|
261 |
ab11 |
1.3 |
if ( debug == 1 ){ |
262 |
|
|
std::cout << "Config::chatConfig::ENDCONFIG\n"; |
263 |
ab11 |
1.6 |
} // if |
264 |
ab11 |
1.3 |
response = net->heartBeatSend("ENDCONFIG"); |
265 |
ab11 |
1.1 |
// doesn't matter what it returns |
266 |
|
|
|
267 |
ab11 |
1.3 |
if ( debug == 1 ){ |
268 |
|
|
std::cout << "Config::chatConfig::FILTER\n"; |
269 |
ab11 |
1.6 |
} // if |
270 |
|
|
|
271 |
ab11 |
1.3 |
response = net->heartBeatSend("FILTER"); |
272 |
ab11 |
1.1 |
// now we need to pull repsonse apart to get the filter information out |
273 |
ab11 |
1.3 |
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 |
ab11 |
1.1 |
|
279 |
ab11 |
1.3 |
if ( debug == 1 ){ |
280 |
|
|
std::cout << "Config::chatConfig::END\n"; |
281 |
ab11 |
1.6 |
} // if |
282 |
ab11 |
1.3 |
response = net->heartBeatSend("END"); |
283 |
ab11 |
1.1 |
|
284 |
|
|
return 0; |
285 |
|
|
|
286 |
|
|
} // chatConfig |
287 |
|
|
|
288 |
|
|
int Config::destroyConfig(){ |
289 |
ab11 |
1.3 |
if ( debug == 1 ){ |
290 |
|
|
std::cout << "Config::destroyConfig()\n"; |
291 |
ab11 |
1.6 |
} // if |
292 |
ab11 |
1.1 |
|
293 |
ab11 |
1.3 |
net->closeConfigConnection(); |
294 |
ab11 |
1.1 |
|
295 |
|
|
} // destroyConfig |
296 |
|
|
|
297 |
ab11 |
1.3 |
string Config::getStrProperty(string propertyName){ |
298 |
|
|
|
299 |
|
|
|
300 |
|
|
if ( debug == 1 ){ |
301 |
|
|
std::cout << "Config::getProperty()\n"; |
302 |
ab11 |
1.6 |
} // if |
303 |
ab11 |
1.1 |
|
304 |
|
|
for ( int i=0; i<20; i++ ){ |
305 |
|
|
if ( propertyName == propertyNames[i] ){ |
306 |
ab11 |
1.3 |
if ( propertyValues[i] != "" ){ |
307 |
|
|
return propertyValues[i]; |
308 |
|
|
} // if |
309 |
ab11 |
1.1 |
} // if |
310 |
|
|
} // for |
311 |
|
|
|
312 |
|
|
// otherwise return ERROR |
313 |
|
|
return "ERROR"; |
314 |
|
|
|
315 |
ab11 |
1.3 |
} // getStrProperty |
316 |
|
|
|
317 |
|
|
int Config::getIntProperty( string propertyName ){ |
318 |
|
|
|
319 |
|
|
if ( debug == 1 ){ |
320 |
|
|
std::cout << "Config::getProperty()\n"; |
321 |
ab11 |
1.6 |
} // if |
322 |
ab11 |
1.3 |
|
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 |
ab11 |
1.6 |
// valid |
332 |
ab11 |
1.3 |
return i; |
333 |
ab11 |
1.6 |
} // if |
334 |
ab11 |
1.3 |
|
335 |
|
|
// otherwise return ERROR |
336 |
|
|
return -1; |
337 |
|
|
|
338 |
|
|
} // getIntProperty |