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, int printDebug ){ |
24 |
|
// the constructor for the class |
25 |
|
|
6 |
– |
|
26 |
|
configConnectionRetryTime = 1; // one second |
27 |
|
debug = printDebug; |
28 |
< |
MAX_CONNECTION_ATTEMPTS = 10; // |
28 |
> |
MAX_CONNECTION_ATTEMPTS = 10; |
29 |
|
|
30 |
|
configName = serverName; |
31 |
|
configPort = serverPort; |
32 |
|
|
33 |
|
if ( debug == 1 ){ |
34 |
|
std::cout << "Config Constructor\n"; |
35 |
< |
} |
35 |
> |
} // if |
36 |
|
|
37 |
|
|
38 |
|
// make a reference to smallnet |
40 |
|
std::cout << "Config::Constructor::Constructing SmallNet\n"; |
41 |
|
} |
42 |
|
net = new SmallNet(debug); |
43 |
< |
// net->setDebug(1); |
25 |
< |
|
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 |
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 |
< |
numProperties = 4; |
49 |
> |
propertyNames[4] = "AVERAGERUpdateTime"; |
50 |
> |
propertyNames[5] = "MAXTCPFilterRetries"; |
51 |
> |
numProperties = 5; |
52 |
|
|
39 |
– |
// read the Chat dialog we 'expect' to have with the config |
40 |
– |
// manager from disk. This is a new idea so that the client |
41 |
– |
// won't have to be hard coded with the dialog (incase it changes) |
42 |
– |
|
43 |
– |
// FIX ME |
44 |
– |
|
53 |
|
if ( loadConfig() == 0 ){ |
54 |
|
// successful just continue |
55 |
|
if ( debug == 1 ){ |
56 |
|
std::cout << "Config::Constructor::loadConfig() Successful\n"; |
57 |
< |
} |
58 |
< |
} |
51 |
< |
else { |
57 |
> |
} // if |
58 |
> |
} else { |
59 |
|
if ( debug == 1 ){ |
60 |
|
std::cout << "Config::Constructor::loadConfig() failed-retrying\n"; |
61 |
< |
} |
61 |
> |
} // if |
62 |
> |
|
63 |
|
// woops something went wrong. we'll just keep retrying |
64 |
|
// and increasing the time between trys |
65 |
|
int loadConfigResult = -1; |
70 |
|
while ( loadConfigResult != 0 ){ |
71 |
|
if ( retrys > MAX_CONNECTION_ATTEMPTS ){ |
72 |
|
std::cout << "Maxium number of retries reached, Quitting"; |
73 |
< |
// exit("Failed to connect"); |
66 |
< |
} |
73 |
> |
} // if |
74 |
|
// make the connection retry time greater |
75 |
|
configConnectionRetryTime = configConnectionRetryTime * 2; |
76 |
|
std::cout << "Waiting " << configConnectionRetryTime << " seconds before retrying\n"; |
79 |
|
retrys++; |
80 |
|
loadConfigResult = loadConfig(); |
81 |
|
} // while |
75 |
– |
|
76 |
– |
|
82 |
|
} // if |
83 |
|
|
84 |
|
if ( debug == 1 ){ |
85 |
|
std::cout << "Config::Constructor - Finished!\n"; |
86 |
< |
} |
86 |
> |
} // if |
87 |
|
|
88 |
|
} // Config |
89 |
|
|
93 |
|
|
94 |
|
if ( debug == 1 ){ |
95 |
|
std::cout << "Config::loadConfig()\n"; |
96 |
< |
} |
96 |
> |
} // if |
97 |
|
|
98 |
|
// make a connection |
99 |
|
|
100 |
|
if ( debug == 1 ){ |
101 |
|
std::cout << "Config::loadConfig::establishing config connection\n"; |
102 |
< |
} |
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 |
< |
} |
114 |
> |
} // if |
115 |
> |
|
116 |
|
int chat = chatConfig(); |
117 |
|
if ( chat != 0 ){ |
118 |
|
// and close the connection |
119 |
|
destroyConfig(); |
120 |
< |
// return chat; |
113 |
< |
} |
120 |
> |
} // if |
121 |
|
|
122 |
|
// and close the connection |
116 |
– |
|
123 |
|
if ( debug == 1 ){ |
124 |
|
std::cout << "Config::loadConfig::closing config connection\n"; |
125 |
< |
} |
125 |
> |
} // if |
126 |
|
destroyConfig(); |
127 |
|
|
128 |
|
// return everything ok |
134 |
|
// just an interface onto smallnet |
135 |
|
if ( debug == 1 ){ |
136 |
|
std::cout << "Config::establishHeartbeat()\n"; |
137 |
< |
} |
137 |
> |
} // if |
138 |
|
|
139 |
< |
int establish = net->connectHeartBeat(configName, configPort); |
140 |
< |
|
139 |
> |
int establish = net->connectHeartBeat(configName, configPort); |
140 |
> |
|
141 |
|
if ( debug == 1 ){ |
142 |
|
std::cout << "->" << establish << "\n"; |
143 |
< |
} |
143 |
> |
} // if |
144 |
|
|
145 |
|
return establish; |
146 |
|
|
147 |
|
} // establishHeartBeat |
148 |
|
|
143 |
– |
|
149 |
|
int Config::destroyHeartbeat(){ |
150 |
|
// just hooks into the smallnet interface |
151 |
|
if ( debug == 1 ){ |
152 |
|
std::cout << "Config::destroyHeartbeat()\n"; |
153 |
< |
} |
153 |
> |
} // if |
154 |
|
|
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 |
218 |
|
std::cout << "Config::establishConfig()\n"; |
219 |
|
} |
220 |
|
|
162 |
– |
|
163 |
– |
/* MODIFY THIS */ |
221 |
|
int response = net->connectConfig(configName, configPort); |
222 |
|
|
223 |
|
if ( debug == 1 ){ |
224 |
|
std::cout << "->" << response << "\n"; |
225 |
< |
} |
225 |
> |
} // if |
226 |
|
|
227 |
|
return response; |
228 |
|
|
233 |
|
// do some funky jibble.. ;) |
234 |
|
if ( debug == 1 ){ |
235 |
|
std::cout << "Config::chatConfig()\n"; |
236 |
< |
} |
236 |
> |
} // if |
237 |
|
|
238 |
|
string response; |
239 |
< |
|
183 |
< |
|
239 |
> |
|
240 |
|
// hard coeded for the moment |
241 |
|
if ( debug == 1 ){ |
242 |
|
std::cout << "Config::chatConfig::STARTCONFIG\n"; |
243 |
< |
} |
243 |
> |
} // if |
244 |
> |
|
245 |
|
response = net->heartBeatSend("STARTCONFIG"); |
246 |
|
if ( response != "OK" ){ |
247 |
|
// something went wrong |
191 |
– |
return -1; |
248 |
|
if ( debug == 1 ){ |
249 |
|
std::cout << "Config::chatConfig::ERROR\n"; |
250 |
< |
} |
251 |
< |
} |
250 |
> |
} // if |
251 |
> |
return -1; |
252 |
> |
} // if |
253 |
|
|
254 |
|
if ( debug == 1 ){ |
255 |
|
std::cout << "Config::chatConfig::LASTMODIFIED\n"; |
256 |
< |
} |
256 |
> |
} // if |
257 |
|
lastModified = net->heartBeatSend("LASTMODIFIED"); |
258 |
|
if ( debug == 1 ){ |
259 |
|
std::cout << "Config::chatConfig::FILELIST\n"; |
260 |
< |
} |
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 |
< |
} |
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 |
|
if ( debug == 1 ){ |
275 |
|
std::cout << "Config::chatConfig::" << propertyNames[i] << "\n"; |
276 |
< |
} |
276 |
> |
} // if |
277 |
|
propertyValues[i] = net->heartBeatSend(propertyNames[i]); |
278 |
|
} // for |
279 |
|
} // if |
280 |
|
|
281 |
|
if ( debug == 1 ){ |
282 |
|
std::cout << "Config::chatConfig::ENDCONFIG\n"; |
283 |
< |
} |
283 |
> |
} // if |
284 |
|
response = net->heartBeatSend("ENDCONFIG"); |
285 |
|
// doesn't matter what it returns |
286 |
|
|
287 |
|
if ( debug == 1 ){ |
288 |
|
std::cout << "Config::chatConfig::FILTER\n"; |
289 |
< |
} |
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); |
298 |
|
|
299 |
|
if ( debug == 1 ){ |
300 |
|
std::cout << "Config::chatConfig::END\n"; |
301 |
< |
} |
301 |
> |
} // if |
302 |
|
response = net->heartBeatSend("END"); |
303 |
|
|
304 |
|
return 0; |
308 |
|
int Config::destroyConfig(){ |
309 |
|
if ( debug == 1 ){ |
310 |
|
std::cout << "Config::destroyConfig()\n"; |
311 |
< |
} |
311 |
> |
} // if |
312 |
|
|
313 |
|
net->closeConfigConnection(); |
314 |
|
|
319 |
|
|
320 |
|
if ( debug == 1 ){ |
321 |
|
std::cout << "Config::getProperty()\n"; |
322 |
< |
} |
322 |
> |
} // if |
323 |
|
|
324 |
|
for ( int i=0; i<20; i++ ){ |
325 |
|
if ( propertyName == propertyNames[i] ){ |
338 |
|
|
339 |
|
if ( debug == 1 ){ |
340 |
|
std::cout << "Config::getProperty()\n"; |
341 |
< |
} |
341 |
> |
} // if |
342 |
|
|
343 |
|
string response = getStrProperty( propertyName ); |
344 |
|
if ( response == "ERROR" ){ |
348 |
|
int i = atoi(response.c_str()); |
349 |
|
|
350 |
|
if ( i > 0 ){ |
351 |
+ |
// valid |
352 |
|
return i; |
353 |
< |
} |
353 |
> |
} // if |
354 |
|
|
355 |
|
// otherwise return ERROR |
356 |
|
return -1; |