--- projects/cms/source/host/c++/Config.cpp 2001/02/26 14:42:43 1.2 +++ projects/cms/source/host/c++/Config.cpp 2001/02/26 14:59:54 1.3 @@ -1,12 +1,27 @@ #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; // + configName = serverName; + configPort = serverPort; + + if ( debug == 1 ){ + std::cout << "Config Constructor\n"; + } + + // make a reference to smallnet - SmallNet net(); + if ( debug == 1 ){ + std::cout << "Config::Constructor::Constructing SmallNet\n"; + } + net = new SmallNet(debug); + // net->setDebug(1); // read the values which we want to retrieve from the // config manager from the "config.values.txt" file @@ -19,6 +34,7 @@ Config::Config( string serverName, int serverPort ){ propertyNames[1] = "TCPUpdateTime"; propertyNames[2] = "FilterRetryTime"; propertyNames[3] = "ConfigRetryTime"; + numProperties = 4; // read the Chat dialog we 'expect' to have with the config // manager from disk. This is a new idea so that the client @@ -28,45 +44,79 @@ Config::Config( string serverName, int serverPort ){ if ( loadConfig() == 0 ){ // successful just continue + if ( debug == 1 ){ + std::cout << "Config::Constructor::loadConfig() Successful\n"; + } } else { + if ( debug == 1 ){ + std::cout << "Config::Constructor::loadConfig() failed-retrying\n"; + } // 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"; + // exit("Failed to connect"); + } // 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"; + } + } // Config 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"; + } + // make a connection + + if ( debug == 1 ){ + std::cout << "Config::loadConfig::establishing config connection\n"; + } int established = establishConfig(); if ( established != 0 ){ return established; } // do the talk ;) + + if ( debug == 1 ){ + std::cout << "Config::loadConfig::Processing chat script\n"; + } int chat = chatConfig(); if ( chat != 0 ){ // and close the connection destroyConfig(); - return chat; + // return chat; } // and close the connection + + if ( debug == 1 ){ + std::cout << "Config::loadConfig::closing config connection\n"; + } destroyConfig(); // return everything ok @@ -76,9 +126,16 @@ int Config::loadConfig(){ int Config::establishHeartbeat(){ // just an interface onto smallnet + if ( debug == 1 ){ + std::cout << "Config::establishHeartbeat()\n"; + } - int establish = 0; //net.connectHeartbeat(configName, configPort); + int establish = net->connectHeartBeat(configName, configPort); + if ( debug == 1 ){ + std::cout << "->" << establish << "\n"; + } + return establish; } // establishHeartBeat @@ -86,8 +143,11 @@ int Config::establishHeartbeat(){ int Config::destroyHeartbeat(){ // just hooks into the smallnet interface + if ( debug == 1 ){ + std::cout << "Config::destroyHeartbeat()\n"; + } - net.closeHeartBeatConnection(); + net->closeHeartBeatConnection(); } // destroyHeartBeat @@ -95,71 +155,143 @@ int Config::destroyHeartbeat(){ 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(); + /* MODIFY THIS */ + int response = net->connectConfig(configName, configPort); - return 0; + if ( debug == 1 ){ + std::cout << "->" << response << "\n"; + } + return response; + } // establishConfig int Config::chatConfig(){ // do some funky jibble.. ;) + if ( debug == 1 ){ + std::cout << "Config::chatConfig()\n"; + } string response; + // hard coeded for the moment - response = net.heartBeatSend("STARTCONFIG"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::STARTCONFIG\n"; + } + response = net->heartBeatSend("STARTCONFIG"); if ( response != "OK" ){ // something went wrong return -1; + if ( debug == 1 ){ + std::cout << "Config::chatConfig::ERROR\n"; + } } - lastModified = net.heartBeatSend("LASTMODIFIED"); - fileList = net.heartBeatSend("FILELIST"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::LASTMODIFIED\n"; + } + lastModified = net->heartBeatSend("LASTMODIFIED"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::FILELIST\n"; + } + fileList = net->heartBeatSend("FILELIST"); // now send the properties we want + if ( debug == 1 ){ + std::cout << "Config::chatConfig::requesting properties\n"; + } 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"; + } + propertyValues[i] = net->heartBeatSend(propertyNames[i]); } // for } // if - response = net.heartBeatSend("ENDCONFIG"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::ENDCONFIG\n"; + } + response = net->heartBeatSend("ENDCONFIG"); // doesn't matter what it returns - response = net.heartBeatSend("FILTER"); + if ( debug == 1 ){ + std::cout << "Config::chatConfig::FILTER\n"; + } + 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"; + } + response = net->heartBeatSend("END"); - response = net.heartBeatSend("END"); - return 0; } // chatConfig int Config::destroyConfig(){ + if ( debug == 1 ){ + std::cout << "Config::destroyConfig()\n"; + } - 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"; + } + 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"; + } + + string response = getStrProperty( propertyName ); + if ( response == "ERROR" ){ + return -1; + } // if + + int i = atoi(response.c_str()); + + if ( i > 0 ){ + return i; + } + + // otherwise return ERROR + return -1; + +} // getIntProperty \ No newline at end of file