ProcessConfigurationImpl.cpp

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #include <fesa-core/Utilities/ProcessConfigurationImpl.h>
00004 
00005 #include <fesa-core/Exception/FesaException.h>
00006 
00007 #include <cmw-util/FileUtils.h>
00008 
00009 #include <iostream>
00010 #include <sstream>
00011 #include <boost/assign/list_of.hpp>
00012 
00013 
00014 namespace
00015 {
00016 
00017 const std::string helpArg1 = "-h";
00018 const std::string helpArg2 = "-help";
00019 const std::string cmwServerNameArg = "-cmwServerName";
00020 const std::string instanceFileArg = "-instance";
00021 const std::string timingSimulationArg = "-timsim";
00022 const std::string noRTSchedulingArg = "-noRTSched";
00023 const std::string verboseModeArg = "-v";
00024 const std::string veryVerboseModeArg = "-vv";
00025 const std::string cfgFesaArg = "-cfgfesa";
00026 const std::string cfgLogArg = "-cfglog";
00027 const std::string cfgMsgArg = "-cfgmsg";
00028 const std::string cfgMsgLabArg = "-cfgmsglab";
00029 const std::string cfgCMWArg = "-cfgcmw";
00030 const std::string confDirArg = "-confdir";
00031 
00032 const std::string fesaCfgFileName = "fesa.cfg";
00033 const std::string logCfgFileName = "log.cfg";
00034 const std::string msgCfgFileName = "messages.cfg";
00035 const std::string msgLabCfgFileName = "messagesLab.cfg";
00036 const std::string cmwCfgFileName = "cmw.cfg";
00037 const std::string defaultTimSimCfgFileName = "./TimingSimulationConfig.xml";
00038 
00039 CMW::Util::CmdOptions::CmdArgList cmdArgList = boost::assign::list_of<CMW::Util::CmdArg*>
00040     (new CMW::Util::CmdArg(helpArg1, "Display usage", false))
00041     (new CMW::Util::CmdArg(helpArg2, "Display usage", false))
00042     (new CMW::Util::CmdArgString(cmwServerNameArg, "Set CMW server name", false))
00043     (new CMW::Util::CmdArgString(instanceFileArg, "Specify location and name of the instantiation file", false))
00044     (new CMW::Util::CmdArgString(timingSimulationArg, "Enable simulated timing. It will be configured either from the xml-file specified with this argument or from TimingSimulationConfig.xml in the current directory by default", false))
00045     (new CMW::Util::CmdArg(noRTSchedulingArg, "Start FESA process without RT scheduling", false))
00046     (new CMW::Util::CmdArg(verboseModeArg, "Enable verbose mode", false))
00047     (new CMW::Util::CmdArg(veryVerboseModeArg, "Enable very verbose mode", false))
00048     (new CMW::Util::CmdArgString(cfgFesaArg, "Specify location and name of custom FESA configuration file", false))
00049     (new CMW::Util::CmdArgString(cfgLogArg, "Specify location and name of custom logging configuration file", false))
00050     (new CMW::Util::CmdArgString(cfgMsgArg, "Specify location and name of custom file with exception messages", false))
00051     (new CMW::Util::CmdArgString(cfgMsgLabArg, "Specify location and name of custom file with lab specific exception messages", false))
00052     (new CMW::Util::CmdArgString(cfgCMWArg, "Specify location and name of custom middleware configuration file", false))
00053     (new CMW::Util::CmdArgString(confDirArg, "Specify directory where configuration files are", false));
00054 
00055 
00056 std::string
00057 lookUpFile(const std::vector<std::string>& dirs, const std::string& fileName)
00058 {
00059     std::string file;
00060     std::vector<std::string>::const_iterator iter;
00061     for (iter = dirs.begin(); iter != dirs.end(); ++iter)
00062     {
00063         const std::string name = *iter + fileName;
00064         if (CMW::Util::FileUtils::fileExists(name) == true)
00065         {
00066             file = name;
00067             break;
00068         }
00069     }
00070     return file;
00071 }
00072 
00073 } // namespace
00074 
00075 
00076 namespace fesa
00077 {
00078 
00079 ProcessConfigurationImpl::ProcessConfigurationImpl(int argc, char* argv[]) 
00080     : cmdOptions_(cmdArgList),
00081       timingSimulationMode_(false),
00082       loggingMode_(LoggingMode::normal)
00083 {
00084     try
00085     {
00086         cmdOptions_.parse(argc, argv);
00087     }
00088     catch (const CMW::Util::UtilException& ex)
00089     {
00090         std::ostringstream errorStrStream;
00091         errorStrStream << ex.what() << ". Run " << argv[0] << " -h to get usage information";
00092         throw FesaException(__FILE__, __LINE__, errorStrStream.str());
00093     }
00094 }
00095 
00096 
00097 ProcessConfigurationImpl::~ProcessConfigurationImpl()
00098 {
00099 }
00100 
00101 
00102 bool
00103 ProcessConfigurationImpl::isHelpNeeded() const
00104 {
00105     if (cmdOptions_.isOptionDefined(helpArg1) == true || cmdOptions_.isOptionDefined(helpArg2) == true)
00106     {
00107         return true;
00108     }
00109     return false;
00110 }
00111 
00112 
00113 void
00114 ProcessConfigurationImpl::printHelp() const
00115 {
00116     std::cout << cmdOptions_.usage() << std::endl;
00117 }
00118 
00119 
00120 void
00121 ProcessConfigurationImpl::load(const ConfigDirs& configDirs)
00122 {
00123     // Configuration files directory
00124     ConfigDirs dirs(configDirs);
00125     if (cmdOptions_.isOptionDefined(confDirArg) == true)
00126     {
00127         dirs.insert(dirs.begin(), cmdOptions_.getOption(confDirArg));
00128     }
00129     // FESA configuration file
00130     if (cmdOptions_.isOptionDefined(cfgFesaArg) == true)
00131     {
00132         fesaCfgFile_ = cmdOptions_.getOption(cfgFesaArg);
00133     }
00134     else
00135     {
00136         fesaCfgFile_ = lookUpFile(dirs, fesaCfgFileName);
00137     }
00138     properties_ = CMW::Util::Properties(fesaCfgFile_);
00139     std::cout << "FESA configuration loaded from " << fesaCfgFile_ << std::endl; // Logging is not initialized at this point
00140     // Logging configuration file
00141     if (cmdOptions_.isOptionDefined(cfgLogArg) == true)
00142     {
00143         logCfgFile_ = cmdOptions_.getOption(cfgLogArg);
00144     }
00145     else
00146     {
00147         logCfgFile_ = lookUpFile(dirs, logCfgFileName);
00148     }
00149     // Exception messages file
00150     if (cmdOptions_.isOptionDefined(cfgMsgArg) == true)
00151     {
00152         msgCfgFile_ = cmdOptions_.getOption(cfgMsgArg);
00153     }
00154     else
00155     {
00156         msgCfgFile_ = lookUpFile(dirs, msgCfgFileName);
00157     }
00158     // Lab-specific exception messages file
00159     if (cmdOptions_.isOptionDefined(cfgMsgLabArg) == true)
00160     {
00161         msgLabCfgFile_ = cmdOptions_.getOption(cfgMsgLabArg);
00162     }
00163     else
00164     {
00165         msgLabCfgFile_ = lookUpFile(dirs, msgLabCfgFileName);
00166     }
00167     // CMW configuration file
00168     if (cmdOptions_.isOptionDefined(cfgCMWArg) == true)
00169     {
00170         cmwCfgFile_ = cmdOptions_.getOption(cfgCMWArg);
00171     }
00172     else
00173     {
00174         cmwCfgFile_ = lookUpFile(dirs, cmwCfgFileName);
00175     }
00176     // CMW server name
00177     if (cmdOptions_.isOptionDefined(cmwServerNameArg) == true)
00178     {
00179         cmwServerName_ = cmdOptions_.getOption(cmwServerNameArg);
00180     }
00181     // Instantiation file
00182     if (cmdOptions_.isOptionDefined(instanceFileArg) == true)
00183     {
00184         properties_.setProperty(PropertyTag::INSTANCE_FILE, cmdOptions_.getOption(instanceFileArg) );
00185     }
00186     // Timing simulation
00187     if (cmdOptions_.isOptionDefined(timingSimulationArg) == true)
00188     {
00189         timingSimulationMode_ = true;
00190         timingSimulationCfgFile_ = cmdOptions_.getOption(timingSimulationArg);
00191         if (timingSimulationCfgFile_.empty() == true)
00192         {
00193             timingSimulationCfgFile_ = defaultTimSimCfgFileName;
00194         }
00195         properties_.setProperty(PropertyTag::TIMING_SIMULATION, timingSimulationCfgFile_);
00196     }
00197     // RT scheduling
00198     if (cmdOptions_.isOptionDefined(noRTSchedulingArg) == true)
00199     {
00200         properties_.setProperty(PropertyTag::NO_RT_SCHEDULING, "true");
00201     }
00202     // Verbose mode
00203     if (cmdOptions_.isOptionDefined(verboseModeArg) == true)
00204     {
00205         loggingMode_ = LoggingMode::verbose;
00206     }
00207     // Very verbose mode
00208     if (cmdOptions_.isOptionDefined(veryVerboseModeArg) == true)
00209     {
00210         loggingMode_ = LoggingMode::veryVerbose;
00211     }
00212 }
00213 
00214 
00215 const std::string&
00216 ProcessConfigurationImpl::getLogConfigFile() const
00217 {
00218     return logCfgFile_;
00219 }
00220 
00221 
00222 const std::string&
00223 ProcessConfigurationImpl::getCMWConfigFile() const
00224 {
00225     return cmwCfgFile_;
00226 }
00227 
00228 
00229 const std::string&
00230 ProcessConfigurationImpl::getMsgConfigFile() const
00231 {
00232     return msgCfgFile_;
00233 }
00234 
00235 
00236 const std::string&
00237 ProcessConfigurationImpl::getLabMsgConfigFile() const
00238 {
00239     return msgLabCfgFile_;
00240 }
00241 
00242 
00243 bool
00244 ProcessConfigurationImpl::isInTimingSimulationMode() const
00245 {
00246     return timingSimulationMode_;
00247 }
00248 
00249 
00250 const std::string&
00251 ProcessConfigurationImpl::getCMWServerName() const
00252 {
00253     return cmwServerName_;
00254 }
00255 
00256 
00257 void
00258 ProcessConfigurationImpl::setCMWServerName(const std::string& name)
00259 {
00260     cmwServerName_ = name;
00261 }
00262 
00263 
00264 LoggingMode::LoggingMode
00265 ProcessConfigurationImpl::getLoggingMode() const
00266 {
00267     return loggingMode_;
00268 }
00269 
00270 
00271 bool
00272 ProcessConfigurationImpl::isDefined(const std::string& tag) const
00273 {
00274     std::string value;
00275     if (properties_.getProperty(tag, value) == false)
00276     {
00277         return false;
00278     }
00279     return true;
00280 }
00281 
00282 
00283 std::string
00284 ProcessConfigurationImpl::getStringValue(const std::string& tag) const
00285 {
00286     std::string value;
00287     if (properties_.getProperty(tag, value) == false)
00288     {
00289         throw FesaException(__FILE__, __LINE__, FesaErrorCFGParameterNotFound.c_str(), tag.c_str());
00290     }
00291     return value;
00292 }
00293 
00294 
00295 int
00296 ProcessConfigurationImpl::getIntValue(const std::string& tag) const
00297 {
00298     std::string valueStr = getStringValue(tag);
00299     const size_t found = valueStr.find_first_not_of("0123456789"); // Is string numerical?
00300     if (found != valueStr.npos)
00301     {
00302         throw FesaException(__FILE__, __LINE__, FesaErrorCFGParameterIsNotNumeric.c_str(), tag.c_str());
00303     }
00304     int value = 0;
00305     std::istringstream valueStrStream(valueStr);
00306     valueStrStream >> value;
00307     return value;
00308 }
00309 
00310 
00311 bool
00312 ProcessConfigurationImpl::getBoolValue(const std::string& tag) const
00313 {
00314     std::string valueStr = getStringValue(tag);
00315     bool value = false;
00316     if (valueStr == "true" || valueStr == "yes" || valueStr == "1")
00317     {
00318         value = true;
00319     }
00320     else if (valueStr == "false" || valueStr == "no" || valueStr == "0")
00321     {
00322         value = false;
00323     }
00324     else
00325     {
00326         throw FesaException(__FILE__, __LINE__, FesaErrorCFGParameterIsNotBoolean.c_str(), tag.c_str());
00327     }
00328     return value;
00329 }
00330 
00331 } // fesa

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1