$treeview $search $mathjax $extrastylesheet
otsdaq_utilities
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "otsdaq-utilities/ECLWriter/ECLSupervisor.h" 00002 #include "otsdaq-core/CgiDataUtilities/CgiDataUtilities.h" 00003 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h" 00004 #include "otsdaq-core/Macros/CoutMacros.h" 00005 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00006 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h" 00007 #include "otsdaq-core/SOAPUtilities/SOAPParameters.h" 00008 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h" 00009 #include "otsdaq-core/TablePluginDataFormats/XDAQContextTable.h" 00010 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h" 00011 00012 #include "otsdaq-utilities/ECLWriter/ECLConnection.h" 00013 00014 #include <dirent.h> /*DIR and dirent*/ 00015 #include <sys/stat.h> /*mkdir*/ 00016 00017 #include <xdaq/NamespaceURI.h> 00018 00019 #include <iomanip> 00020 #include <iostream> 00021 #include "otsdaq-core/TableCore/TableGroupKey.h" 00022 00023 using namespace ots; 00024 00025 #undef __MF_SUBJECT__ 00026 #define __MF_SUBJECT__ "ECL" 00027 00028 XDAQ_INSTANTIATOR_IMPL(ECLSupervisor) 00029 00030 //======================================================================================================================== 00031 ECLSupervisor::ECLSupervisor(xdaq::ApplicationStub* s) 00032 : CoreSupervisorBase(s) 00033 , theConfigurationManager_( 00034 new ConfigurationManager) //(Singleton<ConfigurationManager>::getInstance()) 00036 // load a partial configuration (new 00037 // ConfigurationManager) 00038 , supervisorContextUID_( 00039 theConfigurationManager_->__GET_CONFIG__(XDAQContextTable) 00040 ->getContextUID(getApplicationContext()->getContextDescriptor()->getURL())) 00041 , supervisorApplicationUID_( 00042 theConfigurationManager_->__GET_CONFIG__(XDAQContextTable) 00043 ->getApplicationUID( 00044 getApplicationContext()->getContextDescriptor()->getURL(), 00045 getApplicationDescriptor()->getLocalId())) 00046 , supervisorConfigurationPath_("/" + supervisorContextUID_ + 00047 "/LinkToApplicationTable/" + 00048 supervisorApplicationUID_ + "/LinkToSupervisorTable") 00049 { 00050 INIT_MF("ECLSupervisor"); 00051 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00052 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00053 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00054 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00055 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00056 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00057 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00058 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00059 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00060 __COUT__ << __PRETTY_FUNCTION__ << std::endl; 00061 00062 __COUT__ << __PRETTY_FUNCTION__ << "done data manager" << std::endl; 00063 00064 init(); 00065 } 00066 00067 //======================================================================================================================== 00068 ECLSupervisor::~ECLSupervisor(void) { destroy(); } 00069 //======================================================================================================================== 00070 void ECLSupervisor::init(void) 00071 { 00072 // called by constructor 00073 // allSupervisorInfo_.init(getApplicationContext()); 00074 } 00075 00076 //======================================================================================================================== 00077 void ECLSupervisor::destroy(void) 00078 { 00079 // called by destructor 00080 delete theConfigurationManager_; 00081 } 00082 00083 //======================================================================================================================== 00084 void ECLSupervisor::defaultPage(xgi::Input* in, xgi::Output* out) 00085 00086 { 00087 //__COUT__ << this->getApplicationContext()->getURL() << __E__; 00088 00089 *out << "<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame " 00090 "src='/WebPath/html/ECL.html?urn=" 00091 << this->getApplicationDescriptor()->getLocalId() << "'></frameset></html>"; 00092 } 00093 00094 //======================================================================================================================== 00095 void ECLSupervisor::transitionConfiguring(toolbox::Event::Reference e) 00096 00097 { 00098 // try 00099 { 00100 // theConfigurationTableGroupKey_ = 00101 // theConfigurationManager_->makeTheTableGroupKey(atoi(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue("TableGroupKey").c_str())); 00102 // theConfigurationManager_->activateTableGroupKey(theConfigurationTableGroupKey_,0); 00103 00104 std::pair<std::string /*group name*/, TableGroupKey> theGroup( 00105 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) 00106 .getParameters() 00107 .getValue("ConfigurationTableGroupName"), 00108 TableGroupKey(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) 00109 .getParameters() 00110 .getValue("ConfigurationTableGroupKey"))); 00111 00112 __COUT__ << "Configuration table group name: " << theGroup.first 00113 << " key: " << theGroup.second << std::endl; 00114 00115 theConfigurationManager_->loadTableGroup(theGroup.first, theGroup.second, true); 00116 00117 ConfigurationTree configLinkNode = 00118 theConfigurationManager_->getSupervisorTableNode(supervisorContextUID_, 00119 supervisorApplicationUID_); 00120 00121 ECLUser = configLinkNode.getNode("ECLUserName").getValue<std::string>(); 00122 ECLHost = configLinkNode.getNode("ECLInstanceURL").getValue<std::string>(); 00123 ECLPwd = configLinkNode.getNode("ECLPassword").getValue<std::string>(); 00124 ExperimentName = configLinkNode.getNode("ExperimentName").getValue<std::string>(); 00125 } 00126 // catch(...) 00127 //{ 00128 // {__SS__;__THROW__(ss.str()+"Error configuring the visual supervisor most likely a 00129 // plugin name is wrong or your configuration table is outdated and doesn't match the 00130 // new plugin definition!");} 00131 //} 00132 } 00133 00134 //======================================================================================================================== 00135 void ECLSupervisor::transitionStarting(toolbox::Event::Reference e) 00136 00137 { 00138 try 00139 { 00140 __COUT_INFO__ << "ECLSupervisor sending Start Run log message to ECL" 00141 << std::endl; 00142 run = SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) 00143 .getParameters() 00144 .getValue("RunNumber"); 00145 run_start = std::chrono::steady_clock::now(); 00146 duration_ms = 0; 00147 Write(WriteState::kStart); 00148 } 00149 catch(...) 00150 { 00151 __COUT_INFO__ << "ERROR! Couldn't Start the ECLSupervisor" << std::endl; 00152 } 00153 } 00154 00155 //======================================================================================================================== 00156 void ECLSupervisor::transitionStopping(toolbox::Event::Reference e) 00157 00158 { 00159 try 00160 { 00161 __COUT_INFO__ << "ECLSupervisor sending Stop Run log message to ECL" << std::endl; 00162 Write(WriteState::kStop); 00163 } 00164 catch(...) 00165 { 00166 __COUT_INFO__ << "ERROR! Couldn't Stop the ECLSupervisor" << std::endl; 00167 } 00168 } 00169 00170 //======================================================================================================================== 00171 void ECLSupervisor::transitionPausing(toolbox::Event::Reference e) 00172 00173 { 00174 try 00175 { 00176 __COUT_INFO__ << "ECLSupervisor sending Pause Run log message to ECL" 00177 << std::endl; 00178 Write(WriteState::kPause); 00179 duration_ms += std::chrono::duration_cast<std::chrono::milliseconds>( 00180 std::chrono::steady_clock::now() - run_start) 00181 .count(); 00182 } 00183 catch(...) 00184 { 00185 __COUT_INFO__ << "ERROR! Couldn't Pause the ECLSupervisor" << std::endl; 00186 } 00187 } 00188 00189 //======================================================================================================================== 00190 void ECLSupervisor::transitionResuming(toolbox::Event::Reference e) 00191 00192 { 00193 try 00194 { 00195 __COUT_INFO__ << "ECLSupervisor sending Resume Run log message to ECL" 00196 << std::endl; 00197 run_start = std::chrono::steady_clock::now(); 00198 Write(WriteState::kResume); 00199 } 00200 catch(...) 00201 { 00202 __COUT_INFO__ << "ERROR! Couldn't Resume the ECLSupervisor" << std::endl; 00203 } 00204 } 00205 00206 void ECLSupervisor::enteringError(toolbox::Event::Reference e) 00207 { 00208 try 00209 { 00210 __COUT_INFO__ << "ECLSupervisor sending Error log message to ECL" << std::endl; 00211 Write(WriteState::kError); 00212 } 00213 catch(...) 00214 { 00215 __COUT_INFO__ << "ERROR! Couldn't Error the ECLSupervisor" << std::endl; 00216 } 00217 } 00218 00219 //======================================================================================================================== 00220 // xoap::MakeSystemLogbookEntry 00221 // make a system logbook entry into active experiment's logbook from Supervisor only 00222 // TODO: (how to enforce?) 00223 xoap::MessageReference ECLSupervisor::MakeSystemLogbookEntry(xoap::MessageReference msg) 00224 00225 { 00226 SOAPParameters parameters("EntryText"); 00227 // SOAPParametersV parameters(1); 00228 // parameters[0].setName("EntryText"); 00229 SOAPUtilities::receive(msg, parameters); 00230 std::string EntryText = parameters.getValue("EntryText"); 00231 00232 __COUT__ << "Received External Supervisor System Entry " << EntryText << std::endl; 00233 00234 std::string retStr = "Success"; 00235 00236 ECLEntry_t eclEntry; 00237 eclEntry.author(ECLUser); 00238 eclEntry.category("Facility/DAQ"); 00239 Form_t form; 00240 Field_t field; 00241 Form_t::field_sequence fields; 00242 std::string users = 00243 theRemoteWebUsers_.getActiveUserList(allSupervisorInfo_.getGatewayDescriptor()); 00244 00245 form.name("OTSDAQ System Logbook Entry"); 00246 00247 field = Field_t(EscapeECLString(ExperimentName), "Experiment"); 00248 fields.push_back(field); 00249 00250 field = Field_t(EscapeECLString(run), "RunNumber"); 00251 fields.push_back(field); 00252 00253 field = Field_t(EscapeECLString(users), "ActiveUsers"); 00254 fields.push_back(field); 00255 00256 field = Field_t(EscapeECLString(EntryText), "Entry"); 00257 fields.push_back(field); 00258 00259 ECLConnection eclConn(ECLUser, ECLPwd, ECLHost); 00260 if(!eclConn.Post(eclEntry)) 00261 { 00262 retStr = "Failure"; 00263 } 00264 00265 // fill return parameters 00266 SOAPParameters retParameters("Status", retStr); 00267 00268 return SOAPUtilities::makeSOAPMessageReference("LogbookEntryStatusResponse", 00269 retParameters); 00270 } 00271 00272 int ECLSupervisor::Write(WriteState state) 00273 { 00274 ECLEntry_t eclEntry; 00275 eclEntry.author(ECLUser); 00276 eclEntry.category("Facility/DAQ"); 00277 Form_t form; 00278 Field_t field; 00279 Form_t::field_sequence fields; 00280 std::string users = 00281 theRemoteWebUsers_.getActiveUserList(allSupervisorInfo_.getGatewayDescriptor()); 00282 00283 switch(state) 00284 { 00285 case WriteState::kStart: 00286 form.name("OTSDAQ Start Run"); 00287 break; 00288 case WriteState::kStop: 00289 form.name("OTSDAQ Stop Run"); 00290 break; 00291 case WriteState::kResume: 00292 form.name("OTSDAQ Resume Run"); 00293 break; 00294 case WriteState::kPause: 00295 form.name("OTSDAQ Pause Run"); 00296 break; 00297 case WriteState::kError: 00298 form.name("OTSDAQ Run Error"); 00299 break; 00300 } 00301 00302 field = Field_t(EscapeECLString(ExperimentName), "Experiment"); 00303 fields.push_back(field); 00304 00305 field = Field_t(EscapeECLString(run), "RunNumber"); 00306 fields.push_back(field); 00307 00308 field = Field_t(EscapeECLString(users), "ActiveUsers"); 00309 fields.push_back(field); 00310 00311 if(state != WriteState::kStart && state != WriteState::kResume) 00312 { 00313 int dur = std::chrono::duration_cast<std::chrono::milliseconds>( 00314 std::chrono::steady_clock::now() - run_start) 00315 .count() + 00316 duration_ms; 00317 int dur_s = dur / 1000; 00318 dur = dur % 1000; 00319 int dur_m = dur_s / 60; 00320 dur_s = dur_s % 60; 00321 int dur_h = dur_m / 60; 00322 dur_m = dur_m % 60; 00323 00324 std::ostringstream dur_ss; 00325 dur_ss << std::setw(2) << std::setfill('0') << dur_h << ":" << std::setw(2) 00326 << std::setfill('0') << dur_m << ":" << std::setw(2) << std::setfill('0') 00327 << dur_s << "." << dur; 00328 00329 field = Field_t(EscapeECLString(dur_ss.str()), "Duration"); 00330 fields.push_back(field); 00331 } 00332 00333 form.field(fields); 00334 00335 eclEntry.form(form); 00336 00337 ECLConnection eclConn(ECLUser, ECLPwd, ECLHost); 00338 if(!eclConn.Post(eclEntry)) 00339 { 00340 return -1; 00341 } 00342 00343 return 0; 00344 } 00345 00346 //************************************************************ 00347 00348 std::string ECLSupervisor::EscapeECLString(std::string input) 00349 { 00350 std::string output = input; 00351 size_t pos = output.find('&'); 00352 while(pos != std::string::npos) 00353 { 00354 output = output.replace(pos, 1, "&"); 00355 pos = output.find('&', pos + 2); 00356 } 00357 00358 pos = output.find('"'); 00359 while(pos != std::string::npos) 00360 { 00361 output = output.replace(pos, 1, """); 00362 pos = output.find('"', pos + 1); 00363 } 00364 00365 pos = output.find('\''); 00366 while(pos != std::string::npos) 00367 { 00368 output = output.replace(pos, 1, "'"); 00369 pos = output.find('\'', pos + 1); 00370 } 00371 00372 pos = output.find('<'); 00373 while(pos != std::string::npos) 00374 { 00375 output = output.replace(pos, 1, "<"); 00376 pos = output.find('<', pos + 1); 00377 } 00378 00379 pos = output.find('>'); 00380 while(pos != std::string::npos) 00381 { 00382 output = output.replace(pos, 1, ">"); 00383 pos = output.find('>', pos + 1); 00384 } 00385 00386 return output; 00387 }