diff --git a/fairmq/options/FairMQProgOptions.cxx b/fairmq/options/FairMQProgOptions.cxx index 13d6ae9f..72a17b2c 100644 --- a/fairmq/options/FairMQProgOptions.cxx +++ b/fairmq/options/FairMQProgOptions.cxx @@ -22,17 +22,21 @@ FairMQProgOptions::FairMQProgOptions() , fMQParserOptions("MQ-Device parser options") , fMQOptionsInCfg("MQ-Device options") , fMQOptionsInCmd("MQ-Device options") - , fMQtree() , fFairMQMap() , fHelpTitle("***** FAIRMQ Program Options ***** ") , fVersion("Beta version 0.1") + , fMQKeyMap() { } +// ---------------------------------------------------------------------------------- + FairMQProgOptions::~FairMQProgOptions() { } +// ---------------------------------------------------------------------------------- + void FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregistered) { @@ -175,6 +179,83 @@ void FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregist } } + +// ---------------------------------------------------------------------------------- + +int FairMQProgOptions::Store(const FairMQMap& channels) +{ + fFairMQMap = channels; + UpdateMQValues(); + return 0; +} + + +// ---------------------------------------------------------------------------------- + +// replace FairMQChannelMap, and update variable map accordingly +int FairMQProgOptions::UpdateChannelMap(const FairMQMap& channels) +{ + fFairMQMap=channels; + UpdateMQValues(); + return 0; +} + +// ---------------------------------------------------------------------------------- + +// read FairMQChannelMap and insert/update corresponding values in variable map +// create key for variable map as follow : channelName.index.memberName +void FairMQProgOptions::UpdateMQValues() +{ + + for(const auto& p : fFairMQMap) + { + int index = 0; + for(const auto& channel : p.second) + { + std::string typeKey = p.first + "." + std::to_string(index) + ".type"; + std::string methodKey = p.first + "." + std::to_string(index) + ".method"; + std::string addressKey = p.first + "." + std::to_string(index) + ".address"; + std::string propertyKey = p.first + "." + std::to_string(index) + ".property"; + std::string sndBufSizeKey = p.first + "." + std::to_string(index) + ".sndBufSize"; + std::string rcvBufSizeKey = p.first + "." + std::to_string(index) + ".rcvBufSize"; + std::string rateLoggingKey = p.first + "." + std::to_string(index) + ".rateLogging"; + + fMQKeyMap[typeKey] = std::make_tuple(p.first,index,"type"); + fMQKeyMap[methodKey] = std::make_tuple(p.first,index,"method"); + fMQKeyMap[addressKey] = std::make_tuple(p.first,index,"address"); + fMQKeyMap[propertyKey] = std::make_tuple(p.first,index,"property"); + fMQKeyMap[sndBufSizeKey] = std::make_tuple(p.first,index,"sndBufSize"); + fMQKeyMap[rcvBufSizeKey] = std::make_tuple(p.first,index,"rcvBufSize"); + fMQKeyMap[rateLoggingKey] = std::make_tuple(p.first,index,"rateLogging"); + + UpdateVarMap(typeKey,channel.GetType()); + UpdateVarMap(methodKey,channel.GetMethod()); + UpdateVarMap(addressKey,channel.GetAddress()); + UpdateVarMap(propertyKey,channel.GetProperty()); + UpdateVarMap(sndBufSizeKey,channel.GetSndBufSize()); + UpdateVarMap(rcvBufSizeKey,channel.GetRcvBufSize()); + UpdateVarMap(rateLoggingKey,channel.GetRateLogging()); + + /* + LOG(DEBUG) << "Update MQ parameters of variable map"; + LOG(DEBUG) << "key = " << typeKey <<"\t value = " << GetValue(typeKey); + LOG(DEBUG) << "key = " << methodKey <<"\t value = " << GetValue(methodKey); + LOG(DEBUG) << "key = " << addressKey <<"\t value = " << GetValue(addressKey); + LOG(DEBUG) << "key = " << propertyKey <<"\t value = " << GetValue(propertyKey); + LOG(DEBUG) << "key = " << sndBufSizeKey << "\t value = " << GetValue(sndBufSizeKey); + LOG(DEBUG) << "key = " << rcvBufSizeKey <<"\t value = " << GetValue(rcvBufSizeKey); + LOG(DEBUG) << "key = " << rateLoggingKey <<"\t value = " << GetValue(rateLoggingKey); + */ + index++; + } + + } + +} + +// ---------------------------------------------------------------------------------- + + int FairMQProgOptions::NotifySwitchOption() { if (fVarMap.count("help")) @@ -192,6 +273,8 @@ int FairMQProgOptions::NotifySwitchOption() return 0; } +// ---------------------------------------------------------------------------------- + void FairMQProgOptions::InitOptionDescription() { // Id required in command line if config txt file not enabled @@ -248,3 +331,72 @@ void FairMQProgOptions::InitOptionDescription() AddToCfgFileOptions(fMQParserOptions, false); } } + +// ---------------------------------------------------------------------------------- + +int FairMQProgOptions::UpdateChannelMap(const std::string& channelName, int index, const std::string& member, const std::string& val) +{ + if(member == "type") + { + fFairMQMap.at(channelName).at(index).UpdateType(val); + return 0; + } + + if(member == "method") + { + fFairMQMap.at(channelName).at(index).UpdateMethod(val); + return 0; + } + + if(member == "address") + { + fFairMQMap.at(channelName).at(index).UpdateAddress(val); + return 0; + } + + if(member == "property") + { + fFairMQMap.at(channelName).at(index).UpdateProperty(val); + return 0; + } + else + { + //if we get there it means something is wrong + LOG(ERROR) << "update of FairMQChannel map failed for the following key: " + << channelName<<"."< +#include #include "FairProgOptions.h" #include "FairMQChannel.h" -#include - -namespace pt = boost::property_tree; class FairMQProgOptions : public FairProgOptions { @@ -54,23 +52,8 @@ class FairMQProgOptions : public FairProgOptions return 0; } - int Store(const po::variables_map& vm) - { - fVarMap = vm; - return 0; - } - int Store(const pt::ptree& tree) - { - fMQtree = tree; - return 0; - } - - int Store(const FairMQMap& channels) - { - fFairMQMap = channels; - return 0; - } + FairMQMap GetFairMQMap() { @@ -88,17 +71,64 @@ class FairMQProgOptions : public FairProgOptions fVersion=version; } + // store key-value of type T into variable_map. + // If key is found in fMQKeyMap, update the FairMQChannelMap accordingly + // Note that the fMQKeyMap is filled: + // - if UpdateChannelMap(const FairMQMap& map) method is called + // - if UserParser template method is called (it is called in the ParseAll method if json or xml MQ-config files is provided) + + template + int UpdateValue(const std::string& key, const T& val) + { + UpdateVarMap(key,val); + + if(fMQKeyMap.count(key)) + { + std::string channelName; + int index = 0; + std::string member; + std::tie(channelName, index, member) = fMQKeyMap.at(key); + + if(std::is_same::value || std::is_same::value) + UpdateChannelMap(channelName, index, member, val); + + } + return 0; + } + + // replace FairMQChannelMap, and update variable map accordingly + int UpdateChannelMap(const FairMQMap& map); + + + protected: po::options_description fMQParserOptions; po::options_description fMQOptionsInCfg; po::options_description fMQOptionsInCmd; - pt::ptree fMQtree; FairMQMap fFairMQMap; std::string fHelpTitle; std::string fVersion; + typedef std::tuple MQKey;//store key info + std::map fMQKeyMap;// key=full path - val=key info + virtual int NotifySwitchOption(); // for custom help & version printing void InitOptionDescription(); + + // read FairMQChannelMap and insert/update corresponding values in variable map + // create key for variable map as follow : channelName.index.memberName + void UpdateMQValues(); + int Store(const FairMQMap& channels); + + private: + int UpdateChannelMap(const std::string& channelName, int index, const std::string& member, const std::string& val); + int UpdateChannelMap(const std::string& channelName, int index, const std::string& member, int val); + // for cases other than int and string + template + int UpdateChannelMap(const std::string& channelName, int index, const std::string& member, T val) + { + return 0; + } }; diff --git a/fairmq/options/FairProgOptions.h b/fairmq/options/FairProgOptions.h index 91ac82c0..e90b9232 100644 --- a/fairmq/options/FairProgOptions.h +++ b/fairmq/options/FairProgOptions.h @@ -138,9 +138,9 @@ class FairProgOptions boost::filesystem::path fConfigFile; virtual int NotifySwitchOption(); - // UpadateVarMap() and replace() --> helper functions to modify the value of variable map after calling po::store + // UpdateVarMap() and replace() --> helper functions to modify the value of variable map after calling po::store template - void UpadateVarMap(const std::string& key, const T& val) + void UpdateVarMap(const std::string& key, const T& val) { replace(fVarMap, key, val); }