--- projects/cms/source/host/c++/Config.cpp 2001/01/24 19:32:32 1.1 +++ projects/cms/source/host/c++/Config.cpp 2001/03/27 00:06:11 1.6 @@ -1,49 +1,68 @@ #include "Config.h" -Config::Config( string serverName, int serverPort ){ +Config::Config( string serverName, int serverPort, int printDebug ){ // the constructor for the class configConnectionRetryTime = 1; // one second + debug = printDebug; + MAX_CONNECTION_ATTEMPTS = 10; - // make a reference to smallnet - SmallNet net(); + configName = serverName; + configPort = serverPort; - // read the values which we want to retrieve from the - // config manager from the "config.values.txt" file - //propertyNames = new string[20]; - //propertyValues = new string[20]; + if ( debug == 1 ){ + std::cout << "Config Constructor\n"; + } // if - // FIX ME + + // make a reference to smallnet + if ( debug == 1 ){ + std::cout << "Config::Constructor::Constructing SmallNet\n"; + } + net = new SmallNet(debug); + // sod that we are using defined values now.. propertyNames[0] = "UDPUpdateTime"; propertyNames[1] = "TCPUpdateTime"; propertyNames[2] = "FilterRetryTime"; propertyNames[3] = "ConfigRetryTime"; + propertyNames[4] = "AVERAGERUpdateTime"; + propertyNames[5] = "MAXTCPFilterRetries"; + numProperties = 5; - // read the Chat dialog we 'expect' to have with the config - // manager from disk. This is a new idea so that the client - // won't have to be hard coded with the dialog (incase it changes) - - // FIX ME - if ( loadConfig() == 0 ){ // successful just continue - } - else { + if ( debug == 1 ){ + std::cout << "Config::Constructor::loadConfig() Successful\n"; + } // if + } else { + if ( debug == 1 ){ + std::cout << "Config::Constructor::loadConfig() failed-retrying\n"; + } // if + // woops something went wrong. we'll just keep retrying // and increasing the time between trys int loadConfigResult = -1; // the following could cause problems if it is fed the // incorrect filter name and/or port it'll never stop trying + int retrys = 1; // # of times we have tried to connect but failed.. while ( loadConfigResult != 0 ){ + if ( retrys > MAX_CONNECTION_ATTEMPTS ){ + std::cout << "Maxium number of retries reached, Quitting"; + } // if // make the connection retry time greater configConnectionRetryTime = configConnectionRetryTime * 2; + std::cout << "Waiting " << configConnectionRetryTime << " seconds before retrying\n"; sleep(configConnectionRetryTime); // seconds + // increment the number of times we have tried to connect + retrys++; loadConfigResult = loadConfig(); } // while - + } // if + if ( debug == 1 ){ + std::cout << "Config::Constructor - Finished!\n"; } // if } // Config @@ -52,21 +71,38 @@ int Config::loadConfig(){ // establishes a connection to the config manager (filter manager?) and // tries to load all the info which it needs, if it fails returns !0 + if ( debug == 1 ){ + std::cout << "Config::loadConfig()\n"; + } // if + // make a connection + + if ( debug == 1 ){ + std::cout << "Config::loadConfig::establishing config connection\n"; + } // if + int established = establishConfig(); if ( established != 0 ){ + destroyConfig(); return established; - } + } // if // do the talk ;) + + if ( debug == 1 ){ + std::cout << "Config::loadConfig::Processing chat script\n"; + } // if + int chat = chatConfig(); if ( chat != 0 ){ // and close the connection destroyConfig(); - return chat; - } + } // if // and close the connection + if ( debug == 1 ){ + std::cout << "Config::loadConfig::closing config connection\n"; + } // if destroyConfig(); // return everything ok @@ -76,90 +112,227 @@ int Config::loadConfig(){ int Config::establishHeartbeat(){ // just an interface onto smallnet + if ( debug == 1 ){ + std::cout << "Config::establishHeartbeat()\n"; + } // if - int establish = 0; //net.connectHeartbeat(configName, configPort); + int establish = net->connectHeartBeat(configName, configPort); + + if ( debug == 1 ){ + std::cout << "->" << establish << "\n"; + } // if return establish; } // establishHeartBeat - int Config::destroyHeartbeat(){ // just hooks into the smallnet interface + if ( debug == 1 ){ + std::cout << "Config::destroyHeartbeat()\n"; + } // if - net.closeHeartBeatConnection(); + net->closeHeartBeatConnection(); } // destroyHeartBeat +int Config::chatHeartBeat(){ + + string response; + + response = net->heartBeatSend("HEARTBEAT"); + if ( response != "OK" ){ + // something went wrong + if ( debug == 1 ){ + std::cout << "Config::chatHeartBeat::ERROR in HEARTBEAT\n"; + } // if + return -1; + } // if + + response = net->heartBeatSend("CONFIG"); + if ( response != "OK" ){ + // something went wrong + if ( debug == 1 ){ + std::cout << "Config::chatHeartBeat::ERROR in CONFIG\n"; + } // if + return -1; + } // if + + response = net->heartBeatSend(fileList); + if ( response != "OK" ){ + // something went wrong + if ( debug == 1 ){ + std::cout << "Config::chatHeartBeat::ERROR in fileList\n"; + } // if + return -1; + } // if + + response = net->heartBeatSend(lastModified); + if ( response != "OK" ){ + // something went wrong + if ( debug == 1 ){ + std::cout << "Config::chatHeartBeat::ERROR in lastModified\n"; + } // if + return -1; + } // if + + response = net->heartBeatSend("ENDHEARTBEAT"); + if ( response != "OK" ){ + // something went wrong + if ( debug == 1 ){ + std::cout << "Config::chatHeartBeat::ERROR in ENDHEARTBEAT\n"; + } // if + return -1; + } // if + + // seems ok to me + return 0; + +} // chat Heart Beat + int Config::establishConfig(){ // ask smallnet to make a connection to the filtermanager so we can get some config // details + if ( debug == 1 ){ + std::cout << "Config::establishConfig()\n"; + } - net.connectConfig(); + int response = net->connectConfig(configName, configPort); + if ( debug == 1 ){ + std::cout << "->" << response << "\n"; + } // if - return 0; + return response; } // establishConfig int Config::chatConfig(){ // do some funky jibble.. ;) + if ( debug == 1 ){ + std::cout << "Config::chatConfig()\n"; + } // if string response; - + // hard coeded for the moment - response = net.heartBeatSend("STARTCONFIG"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::STARTCONFIG\n"; + } // if + + response = net->heartBeatSend("STARTCONFIG"); if ( response != "OK" ){ // something went wrong - return -1; - } + if ( debug == 1 ){ + std::cout << "Config::chatConfig::ERROR\n"; + } // if + return -1; + } // if - lastModified = net.heartBeatSend("LASTMODIFIED"); - fileList = net.heartBeatSend("FILELIST"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::LASTMODIFIED\n"; + } // if + lastModified = net->heartBeatSend("LASTMODIFIED"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::FILELIST\n"; + } // if + fileList = net->heartBeatSend("FILELIST"); + fQDN = net->heartBeatSend("FQDN"); + // now send the properties we want + if ( debug == 1 ){ + std::cout << "Config::chatConfig::requesting properties\n"; + } // if if ( numProperties > 0 ){ // we actually need to look for some properties for ( int i=0; i < numProperties; i++ ){ // lets hope that this works! ;) - propertyValues[i] = net.heartBeatSend(propertyNames[i]); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::" << propertyNames[i] << "\n"; + } // if + propertyValues[i] = net->heartBeatSend(propertyNames[i]); } // for } // if - response = net.heartBeatSend("ENDCONFIG"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::ENDCONFIG\n"; + } // if + response = net->heartBeatSend("ENDCONFIG"); // doesn't matter what it returns - response = net.heartBeatSend("FILTER"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::FILTER\n"; + } // if + + response = net->heartBeatSend("FILTER"); // now we need to pull repsonse apart to get the filter information out + int first = response.find(";",0); + int second = response.find(";",first+1); + UDPFilterName = response.substr(0,first); + UDPFilterPort = atoi( response.substr( first+1 ,second ).c_str()); + TCPFilterPort = atoi(response.substr(second+1).c_str()); - // FIX ME + if ( debug == 1 ){ + std::cout << "Config::chatConfig::END\n"; + } // if + response = net->heartBeatSend("END"); - response = net.heartBeatSend("END"); - return 0; } // chatConfig int Config::destroyConfig(){ + if ( debug == 1 ){ + std::cout << "Config::destroyConfig()\n"; + } // if - net.closeConfigConnection(); + net->closeConfigConnection(); } // destroyConfig -string getProperty(string propertyName){ +string Config::getStrProperty(string propertyName){ - /* // doesn't compile with this for some reason! + + if ( debug == 1 ){ + std::cout << "Config::getProperty()\n"; + } // if + for ( int i=0; i<20; i++ ){ if ( propertyName == propertyNames[i] ){ - return propertyValues[i]; + if ( propertyValues[i] != "" ){ + return propertyValues[i]; + } // if } // if } // for - */ // otherwise return ERROR return "ERROR"; -} // getProperty +} // getStrProperty + +int Config::getIntProperty( string propertyName ){ + + if ( debug == 1 ){ + std::cout << "Config::getProperty()\n"; + } // if + + string response = getStrProperty( propertyName ); + if ( response == "ERROR" ){ + return -1; + } // if + + int i = atoi(response.c_str()); + + if ( i > 0 ){ + // valid + return i; + } // if + + // otherwise return ERROR + return -1; + +} // getIntProperty \ No newline at end of file