CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/EventFilter/Utilities/src/IndependentWebGUI.cc

Go to the documentation of this file.
00001 
00002 //
00003 // IndependentWebGUI
00004 // ------
00005 //
00006 //            10/19/2006 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
00007 //            20/01/2012 Andrei Spataru <aspataru@cern.ch>
00009 
00010 
00011 #include "EventFilter/Utilities/interface/IndependentWebGUI.h"
00012 
00013 #include "xcept/Exception.h"
00014 #include "xcept/tools.h"
00015 
00016 #include "cgicc/CgiDefs.h"
00017 #include "cgicc/Cgicc.h"
00018 #include "cgicc/HTMLClasses.h"
00019 
00020 #include <sstream>
00021 
00022 
00023 using namespace std;
00024 using namespace evf;
00025 using namespace cgicc;
00026 
00027 
00029 // construction/destruction
00031 
00032 //______________________________________________________________________________
00033 IndependentWebGUI::IndependentWebGUI(xdaq::Application* app)
00034   : app_(app)
00035   , log_(app->getApplicationContext()->getLogger())
00036   , appInfoSpace_(0)
00037   , monInfoSpace_(0)
00038   , itemGroupListener_(0)
00039   , parametersExported_(false)
00040   , countersAddedToParams_(false)
00041   , largeAppIcon_("/evf/images/rbicon.jpg")
00042   , smallAppIcon_("/evf/images/rbicon.jpg")
00043   , smallDbgIcon_("/evf/images/bugicon.jpg")
00044   , smallCtmIcon_("/evf/images/spoticon.jpg")
00045   , hyperDAQIcon_("/hyperdaq/images/HyperDAQ.jpg")
00046   , currentExternalStateName_("N/A")
00047   , currentInternalStateName_("N/A")
00048   , versionString_("__version string not set__")
00049 {
00050   // initialize application information
00051   string       appClass=app_->getApplicationDescriptor()->getClassName();
00052   unsigned int instance=app_->getApplicationDescriptor()->getInstance();
00053   stringstream oss;
00054   oss<<appClass<<instance;
00055 
00056   sourceId_=oss.str();
00057   urn_     ="/"+app_->getApplicationDescriptor()->getURN();
00058 
00059   std::stringstream oss2;
00060   oss2<<"urn:xdaq-monitorable-"<<appClass;
00061   string monInfoSpaceName=oss2.str();
00062   toolbox::net::URN urn = app_->createQualifiedInfoSpace(monInfoSpaceName);
00063 
00064   appInfoSpace_=app_->getApplicationInfoSpace();
00065   monInfoSpace_=xdata::getInfoSpaceFactory()->get(urn.toString());
00066   app_->getApplicationDescriptor()->setAttribute("icon",largeAppIcon_);
00067 
00068   // bind xgi callbacks
00069   xgi::bind(this,&IndependentWebGUI::defaultWebPage,"defaultWebPage");
00070   xgi::bind(this,&IndependentWebGUI::debugWebPage,  "debugWebPage");
00071   xgi::bind(this,&IndependentWebGUI::css,           "styles.css");
00072 
00073   // set itemGroupListener
00074   itemGroupListener_ = dynamic_cast<xdata::ActionListener*>(app_);
00075   if (0!=itemGroupListener_) {
00076     appInfoSpace()->addGroupRetrieveListener(itemGroupListener_);
00077     monInfoSpace()->addGroupRetrieveListener(itemGroupListener_);
00078   }
00079 }
00080 
00081 
00082 //______________________________________________________________________________
00083 IndependentWebGUI::~IndependentWebGUI()
00084 {
00085 
00086 }
00087 
00088 
00090 // implementation of public member functions
00092 
00093 //______________________________________________________________________________
00094 void IndependentWebGUI::defaultWebPage(Input_t *in,Output_t *out) throw (IndependentWebGUI::XgiException_t)
00095 {
00096   updateParams();
00097 
00098   *out<<"<html>"<<endl;
00099   htmlHead(in,out,sourceId_);
00100   *out<<body()<<endl;
00101   htmlHeadline(in,out);
00102   *out<<"<table cellpadding=\"25\"><tr valign=\"top\"><td>"<<endl;
00103   htmlTable(in,out,"Standard Parameters",standardParams_);
00104   *out<<"</td><td>"<<endl;
00105   htmlTable(in,out,"Monitored Parameters",monitorParams_);
00106   *out<<"</td></tr></table>"<<endl;
00107   *out<<body()<<endl<<"</html>"<<endl;
00108   return;
00109 }
00110 
00111 
00112 //______________________________________________________________________________
00113 void IndependentWebGUI::debugWebPage(Input_t *in,Output_t *out) throw (IndependentWebGUI::XgiException_t)
00114 {
00115   updateParams();
00116 
00117   *out<<"<html>"<<endl;
00118   htmlHead(in,out,sourceId_+" [DEBUG]");
00119   *out<<body()<<endl;
00120   htmlHeadline(in,out);
00121   *out<<"<table cellpadding=\"25\"><tr valign=\"top\"><td>"<<endl;
00122   htmlTable(in,out,"Debug Parameters",debugParams_);
00123   *out<<"</td></tr></table>"<<endl;
00124   *out<<body()<<endl<<"</html>"<<endl;
00125   return;
00126 }
00127 
00128 
00129 //______________________________________________________________________________
00130 void IndependentWebGUI::css(Input_t *in,Output_t *out) throw (IndependentWebGUI::XgiException_t)
00131 {
00132   css_.css(in,out);
00133 }
00134 
00135 
00136 //______________________________________________________________________________
00137 void IndependentWebGUI::addStandardParam(CString_t& name,Param_t* param)
00138 {
00139   if (parametersExported_) {
00140     LOG4CPLUS_ERROR(log_,"Failed to add standard parameter '"<<name<<"'.");
00141     return;
00142   }
00143   standardParams_.push_back(make_pair(name,param));
00144 }
00145 
00146 
00147 //______________________________________________________________________________
00148 void IndependentWebGUI::addMonitorParam(CString_t& name,Param_t* param)
00149 {
00150   if (parametersExported_) {
00151     LOG4CPLUS_ERROR(log_,"Failed to add monitor parameter '"<<name<<"'.");
00152     return;
00153   }
00154   monitorParams_.push_back(make_pair(name,param));
00155 }
00156 
00157 
00158 //______________________________________________________________________________
00159 void IndependentWebGUI::addDebugParam(CString_t& name,Param_t* param)
00160 {
00161   if (parametersExported_) {
00162     LOG4CPLUS_ERROR(log_,"Failed to add debug parameter '"<<name<<"'.");
00163     return;
00164   }
00165   debugParams_.push_back(make_pair(name,param));
00166 }
00167 
00168 
00169 //______________________________________________________________________________
00170 void IndependentWebGUI::addStandardCounter(CString_t& name,Counter_t* counter)
00171 {
00172   if (countersAddedToParams_) {
00173     LOG4CPLUS_ERROR(log_,"can't add standard counter '"<<name
00174                     <<"' to IndependentWebGUI of "<<sourceId_);
00175   }
00176   standardCounters_.push_back(make_pair(name,counter));
00177 }
00178 
00179 
00180 //______________________________________________________________________________
00181 void IndependentWebGUI::addMonitorCounter(CString_t& name,Counter_t* counter)
00182 {
00183   if (countersAddedToParams_) {
00184     LOG4CPLUS_ERROR(log_,"can't add monitor counter '"<<name
00185                     <<"' to IndependentWebGUI of "<<sourceId_);
00186   }
00187   monitorCounters_.push_back(make_pair(name,counter));
00188 }
00189 
00190 
00191 //______________________________________________________________________________
00192 void IndependentWebGUI::addDebugCounter(CString_t& name,Counter_t* counter)
00193 {
00194   if (countersAddedToParams_) {
00195     LOG4CPLUS_ERROR(log_,"can't add debug counter '"<<name
00196                     <<"' to IndependentWebGUI of "<<sourceId_);
00197   }
00198   debugCounters_.push_back(make_pair(name,counter));
00199 }
00200 
00201 
00202 //______________________________________________________________________________
00203 void IndependentWebGUI::exportParameters()
00204 {
00205   if (parametersExported_) return;
00206 
00207   if (!countersAddedToParams_) addCountersToParams();
00208 
00209   addParamsToInfoSpace(standardParams_,appInfoSpace());
00210   addParamsToInfoSpace(monitorParams_, appInfoSpace());
00211   addParamsToInfoSpace(debugParams_,   appInfoSpace());
00212 
00213   addParamsToInfoSpace(monitorParams_,monInfoSpace());
00214 
00215   parametersExported_=true;
00216 }
00217 
00218 
00219 //______________________________________________________________________________
00220 void IndependentWebGUI::resetCounters()
00221 {
00222   // standard counters
00223   for (unsigned int i=0;i<standardCounters_.size();i++) {
00224     Counter_t* counter=standardCounters_[i].second;
00225     *counter=0;
00226   }
00227   // monitor counters
00228   for (unsigned int i=0;i<monitorCounters_.size();i++) {
00229     Counter_t* counter=monitorCounters_[i].second;
00230     *counter=0;
00231   }
00232   // debug counters
00233   for (unsigned int i=0;i<debugCounters_.size();i++) {
00234     Counter_t* counter=debugCounters_[i].second;
00235     *counter=0;
00236   }
00237 }
00238 
00239 
00240 //______________________________________________________________________________
00241 void IndependentWebGUI::addItemChangedListener(CString_t& name,xdata::ActionListener* l)
00242 {
00243   if (!parametersExported_) {
00244     LOG4CPLUS_ERROR(log_,"Can't add ItemChangedListener for parameter '"<<name
00245                     <<"' before IndependentWebGUI::exportParameters() is called.");
00246     return;
00247   }
00248 
00249   try {
00250     appInfoSpace()->addItemChangedListener(name,l);
00251   }
00252   catch (xcept::Exception) {
00253     LOG4CPLUS_ERROR(log_,"failed to add ItemChangedListener to "
00254                     <<"application infospace for parameter '"<<name<<"'.");
00255   }
00256 
00257   if (isMonitorParam(name)) {
00258     try {
00259       monInfoSpace()->addItemChangedListener(name,l);
00260     }
00261     catch (xcept::Exception) {
00262       LOG4CPLUS_ERROR(log_,"failed to add ItemChangedListener to "
00263                       <<"monitor infospace for parameter '"<<name<<"'.");
00264     }
00265   }
00266 
00267 }
00268 
00269 
00271 // implementation of private member functions
00273 
00274 //______________________________________________________________________________
00275 void IndependentWebGUI::addParamsToInfoSpace(const ParamVec_t& params,
00276                                   xdata::InfoSpace* infoSpace)
00277 {
00278   for (unsigned int i=0;i<params.size();i++) {
00279     string   name =params[i].first;
00280     Param_t* value=params[i].second;
00281     try {
00282       infoSpace->fireItemAvailable(name,value);
00283     }
00284     catch (xcept::Exception &e) {
00285       LOG4CPLUS_ERROR(log_,"Can't add parameter '"<<name<<"' to info space '"
00286                       <<infoSpace->name()<<"': "
00287                       <<xcept::stdformat_exception_history(e));
00288     }
00289   }
00290 }
00291 
00292 
00293 //______________________________________________________________________________
00294 void IndependentWebGUI::addCountersToParams()
00295 {
00296   if (countersAddedToParams_) return;
00297 
00298   // standard counters
00299   for (unsigned int i=0;i<standardCounters_.size();i++) {
00300     standardParams_.push_back(make_pair(standardCounters_[i].first,
00301                                         standardCounters_[i].second));
00302   }
00303   // monitor counters
00304   for (unsigned int i=0;i<monitorCounters_.size();i++) {
00305     monitorParams_.push_back(make_pair(monitorCounters_[i].first,
00306                                        monitorCounters_[i].second));
00307   }
00308   // debug counters
00309   for (unsigned int i=0;i<debugCounters_.size();i++) {
00310     debugParams_.push_back(make_pair(debugCounters_[i].first,
00311                                      debugCounters_[i].second));
00312   }
00313   countersAddedToParams_=true;
00314 }
00315 
00316 
00317 //______________________________________________________________________________
00318 bool IndependentWebGUI::isMonitorParam(CString_t& name)
00319 {
00320   ParamVec_t::const_iterator it;
00321   for (it=monitorParams_.begin();it!=monitorParams_.end();++it)
00322     if (it->first==name) return true;
00323   return false;
00324 }
00325 
00326 
00327 //______________________________________________________________________________
00328 void IndependentWebGUI::updateParams()
00329 {
00330   if (0!=itemGroupListener_) {
00331     std::list<std::string> emptyList;
00332     appInfoSpace()->fireItemGroupRetrieve(emptyList,itemGroupListener_);
00333   }
00334 }
00335 
00336 
00337 //______________________________________________________________________________
00338 void IndependentWebGUI::htmlTable(Input_t*in,Output_t*out,
00339                        CString_t& title,const ParamVec_t& params)
00340 {
00341   *out<<table().set("frame","void").set("rules","rows")
00342                .set("class","modules").set("width","300")<<endl
00343       <<tr()<<th(title).set("colspan","2")<<tr()<<endl
00344       <<tr()
00345       <<th("Parameter").set("align","left")
00346       <<th("Value").set("align","right")
00347       <<tr()
00348       <<endl;
00349 
00350   for (unsigned int i=0;i<params.size();i++) {
00351     string valueAsString;
00352     try {
00353       valueAsString = params[i].second->toString();
00354     }
00355     catch (xcept::Exception& e) {
00356       valueAsString = e.what();
00357     }
00358     *out<<tr()
00359         <<td(params[i].first).set("align","left")
00360         <<td(valueAsString).set("align","right")
00361         <<tr()<<endl;
00362   }
00363   *out<<table()<<endl;
00364 }
00365 
00366 
00367 //______________________________________________________________________________
00368 void IndependentWebGUI::htmlHead(Input_t *in,Output_t* out,CString_t& pageTitle)
00369 {
00370   *out<<head()<<endl<<cgicc::link().set("type","text/css")
00371                                    .set("rel","stylesheet")
00372                                    .set("href",urn_+"/styles.css")
00373       <<endl<<title(pageTitle.c_str())<<endl<<head()<<endl;
00374 }
00375 
00376 
00377 //______________________________________________________________________________
00378 void IndependentWebGUI::htmlHeadline(Input_t *in,Output_t *out)
00379 {
00380   string externalStateName=currentExternalStateName_;
00381   string internalStateName=currentInternalStateName_;
00382   string version=versionString_;
00383 
00384   *out<<table().set("border","0").set("width","100%")<<endl
00385       <<tr()<<td().set("align","left")<<endl
00386       <<img().set("align","middle").set("src",largeAppIcon_)
00387              .set("alt","main")    .set("width","64")
00388              .set("height","64")   .set("border","")
00389       <<endl
00390       <<b()<<sourceId_<<b()
00391       <<td()<<endl
00392       <<td()<<endl
00393            <<a().set("style", "font-size:x-large")<<currentExternalStateName_<<a()
00394            <<b().set("style", "font-size:small")<<"      /      "<<currentInternalStateName_<<b()
00395       <<td()<<endl
00396       <<td().set("width","32")<<endl
00397       <<a().set("href","/urn:xdaq-application:lid=3")
00398       <<img().set("align","middle").set("src",hyperDAQIcon_)
00399              .set("alt","HyperDAQ").set("width","32")
00400              .set("height","32")   .set("border","")
00401       <<a()
00402       <<td()<<endl
00403       <<td().set("width","32")<<td()
00404       <<td().set("width","32")
00405       <<a().set("href",urn_+"/defaultWebPage")
00406       <<img().set("align","middle").set("src",smallAppIcon_)
00407              .set("alt","Debug")   .set("width","32")
00408              .set("height","32")   .set("border","")
00409       <<a()
00410       <<td()<<endl
00411       <<td().set("width","32")<<td()
00412       <<td().set("width","32")
00413       <<a().set("href",urn_+"/debugWebPage")
00414       <<img().set("align","middle").set("src",smallDbgIcon_)
00415              .set("alt","Debug")   .set("width","32")
00416              .set("height","32")   .set("border","")
00417       <<a()
00418       <<td()<<endl
00419       <<td().set("width","32")<<td()
00420       <<td().set("width","32")
00421       <<a().set("href",urn_+"/customWebPage")
00422       <<img().set("align","middle").set("src",smallCtmIcon_)
00423              .set("alt","Debug")   .set("width","32")
00424              .set("height","32")   .set("border","")
00425       <<a()
00426       <<td()<<tr()<<table()<<endl;
00427 
00428   *out<<p().set("style", "font-size:small").set("align", "right")<<version<<p()<<endl;
00429   *out<<hr()<<endl;
00430 }
00431