ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/c++/Config.cpp
Revision: 1.9
Committed: Mon Jun 10 14:10:43 2002 UTC (22 years, 3 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +0 -0 lines
State: FILE REMOVED
Log Message:
Tidy up of files. These are all old things that are not only no longer used
but are also probably useless to anyone other than us. This saves checking
them out all the time, and makes the "cms/source" tree contain only current
stuff. They'll still exist in the attic's though :)

File Contents

# User Rev Content
1 tdb 1.7 /*
2     * i-scream central monitoring system
3 tdb 1.8 * http://www.i-scream.org.uk
4 tdb 1.7 * 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 ab11 1.1 #include "Config.h"
22    
23 ab11 1.3 Config::Config( string serverName, int serverPort, int printDebug ){
24 ab11 1.1 // the constructor for the class
25    
26     configConnectionRetryTime = 1; // one second
27 ab11 1.3 debug = printDebug;
28 ab11 1.6 MAX_CONNECTION_ATTEMPTS = 10;
29 ab11 1.3
30     configName = serverName;
31     configPort = serverPort;
32    
33     if ( debug == 1 ){
34     std::cout << "Config Constructor\n";
35 ab11 1.6 } // if
36 ab11 1.3
37 ab11 1.1
38     // make a reference to smallnet
39 ab11 1.3 if ( debug == 1 ){
40     std::cout << "Config::Constructor::Constructing SmallNet\n";
41     }
42     net = new SmallNet(debug);
43 ab11 1.6
44 ab11 1.1 // 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 ab11 1.5 propertyNames[4] = "AVERAGERUpdateTime";
50     propertyNames[5] = "MAXTCPFilterRetries";
51     numProperties = 5;
52 ab11 1.1
53     if ( loadConfig() == 0 ){
54     // successful just continue
55 ab11 1.3 if ( debug == 1 ){
56     std::cout << "Config::Constructor::loadConfig() Successful\n";
57 ab11 1.6 } // if
58     } else {
59 ab11 1.3 if ( debug == 1 ){
60     std::cout << "Config::Constructor::loadConfig() failed-retrying\n";
61 ab11 1.6 } // if
62    
63 ab11 1.1 // 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 ab11 1.3 int retrys = 1; // # of times we have tried to connect but failed..
70 ab11 1.1 while ( loadConfigResult != 0 ){
71 ab11 1.3 if ( retrys > MAX_CONNECTION_ATTEMPTS ){
72     std::cout << "Maxium number of retries reached, Quitting";
73 ab11 1.6 } // if
74 ab11 1.1 // make the connection retry time greater
75     configConnectionRetryTime = configConnectionRetryTime * 2;
76 ab11 1.3 std::cout << "Waiting " << configConnectionRetryTime << " seconds before retrying\n";
77 ab11 1.1 sleep(configConnectionRetryTime); // seconds
78 ab11 1.3 // increment the number of times we have tried to connect
79     retrys++;
80 ab11 1.1 loadConfigResult = loadConfig();
81     } // while
82     } // if
83    
84 ab11 1.3 if ( debug == 1 ){
85     std::cout << "Config::Constructor - Finished!\n";
86 ab11 1.6 } // if
87 ab11 1.3
88 ab11 1.1 } // Config
89    
90     int Config::loadConfig(){
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 ab11 1.3 if ( debug == 1 ){
95     std::cout << "Config::loadConfig()\n";
96 ab11 1.6 } // if
97 ab11 1.3
98 ab11 1.1 // make a connection
99 ab11 1.3
100     if ( debug == 1 ){
101     std::cout << "Config::loadConfig::establishing config connection\n";
102 ab11 1.6 } // if
103    
104 ab11 1.1 int established = establishConfig();
105     if ( established != 0 ){
106 ab11 1.6 destroyConfig();
107 ab11 1.1 return established;
108 ab11 1.6 } // if
109 ab11 1.1
110     // do the talk ;)
111 ab11 1.3
112     if ( debug == 1 ){
113     std::cout << "Config::loadConfig::Processing chat script\n";
114 ab11 1.6 } // if
115    
116 ab11 1.1 int chat = chatConfig();
117     if ( chat != 0 ){
118     // and close the connection
119     destroyConfig();
120 ab11 1.6 } // if
121 ab11 1.1
122     // and close the connection
123 ab11 1.3 if ( debug == 1 ){
124     std::cout << "Config::loadConfig::closing config connection\n";
125 ab11 1.6 } // if
126 ab11 1.1 destroyConfig();
127    
128     // return everything ok
129     return 0;
130    
131     } // loadConfig
132    
133     int Config::establishHeartbeat(){
134     // just an interface onto smallnet
135 ab11 1.3 if ( debug == 1 ){
136     std::cout << "Config::establishHeartbeat()\n";
137 ab11 1.6 } // if
138 ab11 1.1
139 ab11 1.6 int establish = net->connectHeartBeat(configName, configPort);
140    
141 ab11 1.3 if ( debug == 1 ){
142     std::cout << "->" << establish << "\n";
143 ab11 1.6 } // if
144 ab11 1.1
145     return establish;
146    
147     } // establishHeartBeat
148    
149     int Config::destroyHeartbeat(){
150     // just hooks into the smallnet interface
151 ab11 1.3 if ( debug == 1 ){
152     std::cout << "Config::destroyHeartbeat()\n";
153 ab11 1.6 } // if
154 ab11 1.1
155 ab11 1.3 net->closeHeartBeatConnection();
156 ab11 1.1
157     } // destroyHeartBeat
158 ab11 1.5
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 ab11 1.6 } // if
169     return -1;
170     } // if
171 ab11 1.5
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 ab11 1.6 } // if
178     return -1;
179     } // if
180 ab11 1.5
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 ab11 1.6 } // if
187     return -1;
188     } // if
189 ab11 1.5
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 ab11 1.6 } // if
196     return -1;
197     } // if
198 ab11 1.5
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 ab11 1.6 } // if
205     return -1;
206     } // if
207 ab11 1.5
208     // seems ok to me
209     return 0;
210    
211     } // chat Heart Beat
212 ab11 1.1
213    
214     int Config::establishConfig(){
215     // ask smallnet to make a connection to the filtermanager so we can get some config
216     // details
217 ab11 1.3 if ( debug == 1 ){
218     std::cout << "Config::establishConfig()\n";
219     }
220    
221     int response = net->connectConfig(configName, configPort);
222 ab11 1.1
223 ab11 1.3 if ( debug == 1 ){
224     std::cout << "->" << response << "\n";
225 ab11 1.6 } // if
226 ab11 1.1
227 ab11 1.3 return response;
228 ab11 1.1
229     } // establishConfig
230    
231    
232     int Config::chatConfig(){
233     // do some funky jibble.. ;)
234 ab11 1.3 if ( debug == 1 ){
235     std::cout << "Config::chatConfig()\n";
236 ab11 1.6 } // if
237 ab11 1.1
238     string response;
239 ab11 1.6
240 ab11 1.1 // hard coeded for the moment
241 ab11 1.3 if ( debug == 1 ){
242     std::cout << "Config::chatConfig::STARTCONFIG\n";
243 ab11 1.6 } // if
244    
245 ab11 1.3 response = net->heartBeatSend("STARTCONFIG");
246 ab11 1.1 if ( response != "OK" ){
247     // something went wrong
248 ab11 1.3 if ( debug == 1 ){
249     std::cout << "Config::chatConfig::ERROR\n";
250 ab11 1.6 } // if
251     return -1;
252     } // if
253 ab11 1.1
254 ab11 1.3 if ( debug == 1 ){
255     std::cout << "Config::chatConfig::LASTMODIFIED\n";
256 ab11 1.6 } // if
257 ab11 1.3 lastModified = net->heartBeatSend("LASTMODIFIED");
258     if ( debug == 1 ){
259     std::cout << "Config::chatConfig::FILELIST\n";
260 ab11 1.6 } // if
261 ab11 1.3 fileList = net->heartBeatSend("FILELIST");
262 ab11 1.1
263 ab11 1.4 fQDN = net->heartBeatSend("FQDN");
264    
265 ab11 1.1 // now send the properties we want
266    
267 ab11 1.3 if ( debug == 1 ){
268     std::cout << "Config::chatConfig::requesting properties\n";
269 ab11 1.6 } // if
270 ab11 1.1 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 ab11 1.3 if ( debug == 1 ){
275     std::cout << "Config::chatConfig::" << propertyNames[i] << "\n";
276 ab11 1.6 } // if
277 ab11 1.3 propertyValues[i] = net->heartBeatSend(propertyNames[i]);
278 ab11 1.1 } // for
279     } // if
280    
281 ab11 1.3 if ( debug == 1 ){
282     std::cout << "Config::chatConfig::ENDCONFIG\n";
283 ab11 1.6 } // if
284 ab11 1.3 response = net->heartBeatSend("ENDCONFIG");
285 ab11 1.1 // doesn't matter what it returns
286    
287 ab11 1.3 if ( debug == 1 ){
288     std::cout << "Config::chatConfig::FILTER\n";
289 ab11 1.6 } // if
290    
291 ab11 1.3 response = net->heartBeatSend("FILTER");
292 ab11 1.1 // now we need to pull repsonse apart to get the filter information out
293 ab11 1.3 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 ab11 1.1
299 ab11 1.3 if ( debug == 1 ){
300     std::cout << "Config::chatConfig::END\n";
301 ab11 1.6 } // if
302 ab11 1.3 response = net->heartBeatSend("END");
303 ab11 1.1
304     return 0;
305    
306     } // chatConfig
307    
308     int Config::destroyConfig(){
309 ab11 1.3 if ( debug == 1 ){
310     std::cout << "Config::destroyConfig()\n";
311 ab11 1.6 } // if
312 ab11 1.1
313 ab11 1.3 net->closeConfigConnection();
314 ab11 1.1
315     } // destroyConfig
316    
317 ab11 1.3 string Config::getStrProperty(string propertyName){
318    
319    
320     if ( debug == 1 ){
321     std::cout << "Config::getProperty()\n";
322 ab11 1.6 } // if
323 ab11 1.1
324     for ( int i=0; i<20; i++ ){
325     if ( propertyName == propertyNames[i] ){
326 ab11 1.3 if ( propertyValues[i] != "" ){
327     return propertyValues[i];
328     } // if
329 ab11 1.1 } // if
330     } // for
331    
332     // otherwise return ERROR
333     return "ERROR";
334    
335 ab11 1.3 } // getStrProperty
336    
337     int Config::getIntProperty( string propertyName ){
338    
339     if ( debug == 1 ){
340     std::cout << "Config::getProperty()\n";
341 ab11 1.6 } // if
342 ab11 1.3
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 ab11 1.6 // valid
352 ab11 1.3 return i;
353 ab11 1.6 } // if
354 ab11 1.3
355     // otherwise return ERROR
356     return -1;
357    
358     } // getIntProperty