ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/c++/Config.cpp
Revision: 1.7
Committed: Sat May 18 18:15:56 2002 UTC (22 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.6: +19 -0 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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