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