merge MQ parameters in the variable_map container of FairMQProgramOptions class

This commit is contained in:
winckler
2016-08-04 17:53:58 +02:00
committed by Florian Uhlig
parent 2ed0c52ae8
commit bed58891ea
3 changed files with 205 additions and 23 deletions

View File

@@ -22,17 +22,21 @@ FairMQProgOptions::FairMQProgOptions()
, fMQParserOptions("MQ-Device parser options") , fMQParserOptions("MQ-Device parser options")
, fMQOptionsInCfg("MQ-Device options") , fMQOptionsInCfg("MQ-Device options")
, fMQOptionsInCmd("MQ-Device options") , fMQOptionsInCmd("MQ-Device options")
, fMQtree()
, fFairMQMap() , fFairMQMap()
, fHelpTitle("***** FAIRMQ Program Options ***** ") , fHelpTitle("***** FAIRMQ Program Options ***** ")
, fVersion("Beta version 0.1") , fVersion("Beta version 0.1")
, fMQKeyMap()
{ {
} }
// ----------------------------------------------------------------------------------
FairMQProgOptions::~FairMQProgOptions() FairMQProgOptions::~FairMQProgOptions()
{ {
} }
// ----------------------------------------------------------------------------------
void FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregistered) 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<std::string>(typeKey,channel.GetType());
UpdateVarMap<std::string>(methodKey,channel.GetMethod());
UpdateVarMap<std::string>(addressKey,channel.GetAddress());
UpdateVarMap<std::string>(propertyKey,channel.GetProperty());
UpdateVarMap<int>(sndBufSizeKey,channel.GetSndBufSize());
UpdateVarMap<int>(rcvBufSizeKey,channel.GetRcvBufSize());
UpdateVarMap<int>(rateLoggingKey,channel.GetRateLogging());
/*
LOG(DEBUG) << "Update MQ parameters of variable map";
LOG(DEBUG) << "key = " << typeKey <<"\t value = " << GetValue<std::string>(typeKey);
LOG(DEBUG) << "key = " << methodKey <<"\t value = " << GetValue<std::string>(methodKey);
LOG(DEBUG) << "key = " << addressKey <<"\t value = " << GetValue<std::string>(addressKey);
LOG(DEBUG) << "key = " << propertyKey <<"\t value = " << GetValue<std::string>(propertyKey);
LOG(DEBUG) << "key = " << sndBufSizeKey << "\t value = " << GetValue<int>(sndBufSizeKey);
LOG(DEBUG) << "key = " << rcvBufSizeKey <<"\t value = " << GetValue<int>(rcvBufSizeKey);
LOG(DEBUG) << "key = " << rateLoggingKey <<"\t value = " << GetValue<int>(rateLoggingKey);
*/
index++;
}
}
}
// ----------------------------------------------------------------------------------
int FairMQProgOptions::NotifySwitchOption() int FairMQProgOptions::NotifySwitchOption()
{ {
if (fVarMap.count("help")) if (fVarMap.count("help"))
@@ -192,6 +273,8 @@ int FairMQProgOptions::NotifySwitchOption()
return 0; return 0;
} }
// ----------------------------------------------------------------------------------
void FairMQProgOptions::InitOptionDescription() void FairMQProgOptions::InitOptionDescription()
{ {
// Id required in command line if config txt file not enabled // Id required in command line if config txt file not enabled
@@ -248,3 +331,72 @@ void FairMQProgOptions::InitOptionDescription()
AddToCfgFileOptions(fMQParserOptions, false); 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<<"."<<index<<"."<<member;
return 1;
}
}
// ----------------------------------------------------------------------------------
int FairMQProgOptions::UpdateChannelMap(const std::string& channelName, int index, const std::string& member, int val)
{
if(member == "sndBufSize")
{
fFairMQMap.at(channelName).at(index).UpdateSndBufSize(val);
return 0;
}
if(member == "rcvBufSize")
{
fFairMQMap.at(channelName).at(index).UpdateRcvBufSize(val);
return 0;
}
if(member == "rateLogging")
{
fFairMQMap.at(channelName).at(index).UpdateRateLogging(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<<"."<<index<<"."<<member;
return 1;
}
}

View File

@@ -17,13 +17,11 @@
#define FAIRMQPROGOPTIONS_H #define FAIRMQPROGOPTIONS_H
#include <unordered_map> #include <unordered_map>
#include <set>
#include "FairProgOptions.h" #include "FairProgOptions.h"
#include "FairMQChannel.h" #include "FairMQChannel.h"
#include <boost/property_tree/ptree.hpp>
namespace pt = boost::property_tree;
class FairMQProgOptions : public FairProgOptions class FairMQProgOptions : public FairProgOptions
{ {
@@ -54,23 +52,8 @@ class FairMQProgOptions : public FairProgOptions
return 0; 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() FairMQMap GetFairMQMap()
{ {
@@ -88,17 +71,64 @@ class FairMQProgOptions : public FairProgOptions
fVersion=version; 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<typename T>
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<T, int>::value || std::is_same<T, std::string>::value)
UpdateChannelMap(channelName, index, member, val);
}
return 0;
}
// replace FairMQChannelMap, and update variable map accordingly
int UpdateChannelMap(const FairMQMap& map);
protected: protected:
po::options_description fMQParserOptions; po::options_description fMQParserOptions;
po::options_description fMQOptionsInCfg; po::options_description fMQOptionsInCfg;
po::options_description fMQOptionsInCmd; po::options_description fMQOptionsInCmd;
pt::ptree fMQtree;
FairMQMap fFairMQMap; FairMQMap fFairMQMap;
std::string fHelpTitle; std::string fHelpTitle;
std::string fVersion; std::string fVersion;
typedef std::tuple<std::string,int,std::string> MQKey;//store key info
std::map<std::string,MQKey> fMQKeyMap;// key=full path - val=key info
virtual int NotifySwitchOption(); // for custom help & version printing virtual int NotifySwitchOption(); // for custom help & version printing
void InitOptionDescription(); 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<typename T>
int UpdateChannelMap(const std::string& channelName, int index, const std::string& member, T val)
{
return 0;
}
}; };

View File

@@ -138,9 +138,9 @@ class FairProgOptions
boost::filesystem::path fConfigFile; boost::filesystem::path fConfigFile;
virtual int NotifySwitchOption(); 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<typename T> template<typename T>
void UpadateVarMap(const std::string& key, const T& val) void UpdateVarMap(const std::string& key, const T& val)
{ {
replace(fVarMap, key, val); replace(fVarMap, key, val);
} }