Config.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:11k
- /* ====================================================================
- * The Vovida Software License, Version 1.0
- *
- * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The names "VOCAL", "Vovida Open Communication Application Library",
- * and "Vovida Open Communication Application Library (VOCAL)" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact vocal@vovida.org.
- *
- * 4. Products derived from this software may not be called "VOCAL", nor
- * may "VOCAL" appear in their name, without prior written
- * permission of Vovida Networks, Inc.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
- * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
- * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * ====================================================================
- *
- * This software consists of voluntary contributions made by Vovida
- * Networks, Inc. and many individuals on behalf of Vovida Networks,
- * Inc. For more information on Vovida Networks, Inc., please see
- * <http://www.vovida.org/>.
- *
- */
- static const char* const Config_cxx_Version =
- "$Id: Config.cxx,v 1.3 2001/05/24 11:01:37 icahoon Exp $";
- #include "Config.hxx"
- #include "ParsePair.hxx"
- #include "VLog.hxx"
- #include "Lock.hxx"
- #include <unistd.h>
- using Vocal::Configuration::Config;
- using Vocal::Transport::IPAddress;
- using Vocal::Threads::ReadLock;
- using Vocal::Threads::WriteLock;
- using Vocal::Configuration::GetOpt;
- using Vocal::Configuration::ParsePair;
- using Vocal::Configuration::NameValueMap;
- using Vocal::Configuration::NO_VALUE;
- using Vocal::Configuration::VALUE_PRESENT;
- using Vocal::Configuration::VALUE_OPTIONAL;
- using Vocal::Logging::VLog;
- using Vocal::ReturnCode;
- using Vocal::SUCCESS;
- Config::Config()
- : cfgStr("config"),
- pserverStr("pserver"),
- nodaemonStr("nodaemon"),
- loglevelStr("loglvl"),
- debuggerStr("debugger"),
- xmlcfgStr("xmlcfg"),
- cfgChar('f'),
- pserverChar('P'),
- nodaemonChar('n'),
- loglevelChar('l'),
- debuggerChar('D'),
- xmlcfgChar('x'),
- myConfigFile(""),
- myPservers(),
- myPid(getpid()),
- myAsDaemon(true),
- myLogLevel(LOG_ERR),
- myUnderDebugger(false),
- myApplicationName(""),
- myXmlCfg(false),
- myMutex()
- {
- }
- Config::~Config()
- {
- }
- void
- Config::usage() const
- {
- }
-
- void
- Config::loadCfgFileErrorMsg(ReturnCode) const
- {
- }
- void
- Config::loadProvisioningErrorMsg(ReturnCode) const
- {
- }
- ReturnCode
- Config::load(int argc, char ** argv)
- {
- myMutex.writelock();
-
- ReturnCode rc = SUCCESS;
- // Read the command line
- //
- if ( (rc = parseCmdLine(argc, argv)) != SUCCESS )
- {
- myMutex.unlock();
- usage();
- return ( rc );
- }
- // Load the config file
- //
- if ( (rc = loadCfgFile()) != SUCCESS )
- {
- myMutex.unlock();
- loadCfgFileErrorMsg(rc);
- return ( rc );
- }
- // Load the configuration information from provisioning.
- //
- if ( (rc = loadProvisioning()) != SUCCESS )
- {
- myMutex.unlock();
- loadProvisioningErrorMsg(rc);
- return ( rc );
- }
- populate();
-
- rc = postParse();
-
- myMutex.unlock();
- return ( rc );
- }
- string
- Config::configFile() const
- {
- ReadLock lock(myMutex);
- return ( myConfigFile );
- }
- list<IPAddress>
- Config::pservers() const
- {
- ReadLock lock(myMutex);
- return ( myPservers );
- }
- void
- Config::pid()
- {
- WriteLock lock(myMutex);
- myPid = getpid();
- }
- pid_t
- Config::pid() const
- {
- ReadLock lock(myMutex);
- return ( myPid );
- }
-
-
- bool
- Config::asDaemon() const
- {
- ReadLock lock(myMutex);
- return ( myAsDaemon );
- }
- int
- Config::logLevel() const
- {
- ReadLock lock(myMutex);
- return ( myLogLevel );
- }
- bool
- Config::underDebugger() const
- {
- ReadLock lock(myMutex);
- return ( myUnderDebugger );
- }
- string
- Config::applicationName() const
- {
- ReadLock lock(myMutex);
- return ( myApplicationName );
- }
- bool
- Config::xmlConfig() const
- {
- ReadLock lock(myMutex);
- return ( myXmlCfg );
- }
- ostream &
- Config::writeTo(ostream & out) const
- {
- ReadLock lock(myMutex);
- out << myApplicationName << ": " << __DATE__ << ", " << __TIME__
- << "n pid: " << myPid
- << "n log level: " << VLog::logName(myLogLevel)
- << "n as daemon: " << ( myAsDaemon ? "yes" : "no" )
- << "n config file: " << myConfigFile;
-
- if ( myConfigFile.size() && myXmlCfg )
- {
- out << " as XML";
- }
-
- out << "n pservers: ";
-
- for ( list<IPAddress>::const_iterator i = myPservers.begin();
- i != myPservers.end();
- i++
- )
- {
- out << ( i == myPservers.begin() ? "" : ", " ) << *i;
- }
-
- out << "n under debugger: " << ( myUnderDebugger ? "yes" : "no" );
-
- return ( out );
- }
- const NameValueMap
- Config::options() const
- {
- ReadLock lock(myMutex);
- return ( myOptions.options() );
- }
- ReturnCode
- Config::parseCmdLine(int argc, char ** argv)
- {
- string appName = argv[0];
-
- size_t pos = appName.rfind('/');
- if ( pos != string::npos )
- {
- myApplicationName = appName.substr(pos+1);
- }
- else
- {
- myApplicationName = appName;
- }
- ReturnCode rc = SUCCESS;
- if ( ( rc = myOptions.add(cfgStr.c_str(), cfgChar, VALUE_OPTIONAL) )
- != SUCCESS
-
- || ( rc = myOptions.add(pserverStr.c_str(), pserverChar, VALUE_PRESENT) )
- != SUCCESS
-
- || ( rc = myOptions.add(nodaemonStr.c_str(), nodaemonChar) )
- != SUCCESS
-
- || ( rc = myOptions.add(loglevelStr.c_str(), loglevelChar, VALUE_PRESENT) )
- != SUCCESS
-
- || ( rc = myOptions.add(debuggerStr.c_str(), debuggerChar) )
- != SUCCESS
-
- || ( rc = myOptions.add(xmlcfgStr.c_str(), xmlcfgChar) )
- != SUCCESS
- || ( rc = preParse() ) != SUCCESS
-
- || ( rc = myOptions.parse(argc, argv) ) != SUCCESS
-
- )
- {
- return ( rc );
- }
- const NameValueMap & optionMap = myOptions.options();
- NameValueMap::const_iterator i;
- // Load config file value.
- //
- if ( (i = optionMap.find(cfgStr)) != optionMap.end() )
- {
- if ( i->second.type == NO_VALUE )
- {
- myConfigFile = myApplicationName + ".conf";
- }
- else
- {
- myConfigFile = i->second.value.back();
- }
- }
- // Load xmlcfg value.
- //
- if ( (i = optionMap.find(xmlcfgStr)) != optionMap.end() )
- {
- myXmlCfg = true;
- }
- return ( rc );
- }
- ReturnCode
- Config::preParse()
- {
- return ( SUCCESS );
- }
-
-
- ReturnCode
- Config::postParse()
- {
- return ( SUCCESS );
- }
-
- ReturnCode
- Config::loadCfgFile()
- {
- if ( myConfigFile.size() == 0 )
- {
- // No config file to load. That's ok.
- //
- return ( SUCCESS );
- }
- ParsePair cfgFile;
- cfgFile.tagName(myApplicationName);
-
- ReturnCode rc = cfgFile.parse(
- myXmlCfg ? ParsePair::CFG_XML_FILE
- : ParsePair::CFG_FILE,
- myConfigFile);
-
- if ( rc == SUCCESS )
- {
- const NameValueMap & pairs = cfgFile.pairs();
- for ( NameValueMap::const_iterator i = pairs.begin();
- i != pairs.end();
- ++i
- )
- {
- myOptions.addValue(i->first, i->second);
- }
- const NameValueMap & optionMap = myOptions.options();
- NameValueMap::const_iterator i;
- // Load pservers value.
- //
- if ( (i = optionMap.find(pserverStr)) != optionMap.end()
- && i->second.type != NO_VALUE
- )
- {
- IPAddress pserverAddr;
- const list<string> & pservers = i->second.value;
- for ( list<string>::const_iterator j = pservers.begin();
- j != pservers.end();
- ++j
- )
- {
- pserverAddr.setIP(j->c_str());
- myPservers.push_back(pserverAddr);
- }
- }
- }
-
- return ( rc );
- }
- ReturnCode
- Config::loadProvisioning()
- {
- if ( myPservers.size() == 0 )
- {
- // No pservers to load. That's ok.
- //
- return ( SUCCESS );
- }
-
- // Connect to pserver here.
- //
- return ( SUCCESS );
- }
- void
- Config::populate()
- {
- const NameValueMap & optionMap = myOptions.options();
- NameValueMap::const_iterator i;
-
- // Load nodaemon value.
- //
- if ( (i = optionMap.find(nodaemonStr)) != optionMap.end() )
- {
- myAsDaemon = false;
- }
-
- // Load log level value.
- //
- if ( (i = optionMap.find(loglevelStr)) != optionMap.end()
- && i->second.type != NO_VALUE
- )
- {
- myLogLevel = VLog::logValue(i->second.value.back().c_str());
- }
- // Load underdebugger value.
- //
- if ( (i = optionMap.find(debuggerStr)) != optionMap.end() )
- {
- myUnderDebugger = true;
- }
- }