00001 #include "ConfigurationInterface.h"
00002 #include "Sct/SctNames.h"
00003 #include "Sct/IoExceptions.h"
00004 #include "Sct/StdExceptionWrapper.h"
00005 #include "Sct/ConfigurationException.h"
00006 #include <iomanip>
00007 #include <is/info.h>
00008 #include <is/infodictionary.h>
00009 #include <is/infoiterator.h>
00010 #include <is/inforeceiver.h>
00011 #include <is/infoT.h>
00012
00013 using namespace SctConfiguration;
00014
00015 namespace SctAnalysis{
00016
00017 ConfigurationInterface::ConfigurationInterface(boost::shared_ptr<Configuration> c) : m_config(c)
00018 {
00019 m_infoReceiver = boost::shared_ptr<ISInfoReceiver>(new ISInfoReceiver(Sct::SctNames::getPartition()));
00020 m_infoReceiver->subscribe("ConfigurationServer", "SystemStructureChangeCount", updateCallback, this);
00021 m_infoReceiver->subscribe("ConfigurationServer", "ModuleMappingChangeCount", updateCallback, this);
00022 }
00023
00024 ConfigurationInterface::~ConfigurationInterface(){
00025 }
00026
00027 std::string ConfigurationInterface::getDcsId(const std::string& modulename) const {
00028
00029 std::map<std::string, std::string>::const_iterator it = m_map.find(modulename );
00030 if (it==m_map.end()) {
00031
00032 return "";
00033 } else {
00034
00035 return (*it).second;
00036 }
00037 }
00038
00039 bool ConfigurationInterface::empty() const{
00040 return m_map.empty();
00041 }
00042
00043 void ConfigurationInterface::update(){
00044 boost::recursive_mutex::scoped_lock lock(m_access);
00045 m_map.clear();
00046 std::list<std::string> modules;
00047 try{
00048 modules = m_config->listAllModules();
00049 }catch(Sct::Throwable& e){
00050 Sct::ConfigurationException("Error getting power mappings", e, __FILE__, __LINE__).sendToMrs(MRS_INFORMATION);
00051 }catch(SctConfiguration::ConfigurationException& e){
00052 std::ostringstream oss;
00053 oss << "Error getting power mappings " << e.what();
00054 Sct::ConfigurationException(oss.str(), __FILE__, __LINE__).sendToMrs(MRS_INFORMATION);
00055 }catch(std::exception& e){
00056 Sct::StdExceptionWrapper err(e);
00057 Sct::ConfigurationException("Error getting power mappings", err, __FILE__, __LINE__).sendToMrs(MRS_INFORMATION);
00058 }
00059
00060 unsigned nprob=0;
00061 for (std::list<std::string>::const_iterator i=modules.begin(); i!= modules.end(); ++i){
00062 unsigned crate, channel, partition, mur, mod;
00063 try{
00064 m_config->translateFromSN(*i, mur, mod);
00065 m_config->translateToPowerSupply(mur, mod, partition, crate, channel);
00066 m_map[*i]=encode(partition, crate, channel);
00067 std::cout << "Mapped module " << *i << " to " << encode(partition, crate, channel) << std::endl;
00068 }catch(SctConfiguration::ConfigurationException& e){
00069 if (nprob<10) std::cerr << e.what() << std::endl;
00070 if (nprob==0) std::cerr << "[No more error messages will be printed for this update]" << std::endl;
00071 nprob++;
00072 }
00073 }
00074 print(std::cout);
00075 }
00076
00077 std::string ConfigurationInterface::encode(unsigned partition, unsigned crate, unsigned channel){
00078 std::ostringstream oss;
00079 oss << "Crate" << std::setw(2) << std::setfill('0') << crate
00080 << ".Channel" << std::setw(2) << std::setfill('0') << channel;
00081 return oss.str();
00082 }
00083
00084 void ConfigurationInterface::print(std::ostream& os) const{
00085 boost::recursive_mutex::scoped_lock lock(m_access);
00086 std::cout << "ConfigurationInterface::print" << std::endl;
00087 os << "Configuration Interface has power maps for "
00088 << m_map.size() << " modules" << std::endl;
00089 }
00090
00091 void ConfigurationInterface::updateCallback(ISCallbackInfo* isc){
00092 if (isc->reason() == is::Created || isc->reason() == is::Updated ){
00093 ConfigurationInterface* rock=static_cast<ConfigurationInterface*>(isc->parameter());
00094 rock->update();
00095 }
00096 }
00097
00098 }