00001 #include "otsdaq-core/Supervisor/Supervisor.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00004 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
00005 #include "otsdaq-core/CgiDataUtilities/CgiDataUtilities.h"
00006 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
00007 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
00008
00009 #include "otsdaq-core/WorkLoopManager/WorkLoopManager.h"
00010 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
00011 #include "otsdaq-core/ConfigurationInterface/ConfigurationManagerRW.h"
00012 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
00013 #include <cgicc/HTMLClasses.h>
00014 #include <cgicc/HTTPCookie.h>
00015 #include <cgicc/HTMLDoctype.h>
00016 #include <cgicc/HTTPHeader.h>
00017 #include <xgi/Utils.h>
00018
00019 #include <xoap/Method.h>
00020 #include <xdaq/NamespaceURI.h>
00021 #include <toolbox/task/WorkLoopFactory.h>
00022 #include <toolbox/fsm/FailedEvent.h>
00023
00024 #include <fstream>
00025 #include <thread>
00026 #include <chrono>
00027 #include <sys/stat.h>
00028
00029 using namespace ots;
00030
00031
00032 #define ICON_FILE_NAME std::string(getenv("SERVICE_DATA_PATH")) + "/OtsWizardData/iconList.dat"
00033 #define RUN_NUMBER_PATH std::string(getenv("SERVICE_DATA_PATH")) + "/RunNumber/"
00034 #define RUN_NUMBER_FILE_NAME "NextRunNumber.txt"
00035 #define FSM_LAST_GROUP_ALIAS_PATH std::string(getenv("SERVICE_DATA_PATH")) + "/RunControlData/"
00036 #define FSM_LAST_GROUP_ALIAS_FILE_START std::string("FSMLastGroupAlias-")
00037 #define FSM_USERS_PREFERENCES_FILETYPE "pref"
00038
00039
00040 #undef __MF_SUBJECT__
00041 #define __MF_SUBJECT__ "OmniSuper"
00042
00043
00044 XDAQ_INSTANTIATOR_IMPL(Supervisor)
00045
00046
00047
00048 Supervisor::Supervisor(xdaq::ApplicationStub * s) throw (xdaq::exception::Exception)
00049 :xdaq::Application (s)
00050 ,SOAPMessenger (this)
00051 ,RunControlStateMachine ("Supervisor")
00052 ,outputDir_ ("")
00053 ,theConfigurationManager_ (new ConfigurationManager)
00054
00055 ,stateMachineWorkLoopManager_(toolbox::task::bind(this, &Supervisor::stateMachineThread,"StateMachine"))
00056 ,stateMachineSemaphore_ (toolbox::BSem::FULL)
00057 ,infoRequestWorkLoopManager_ (toolbox::task::bind(this, &Supervisor::infoRequestThread, "InfoRequest"))
00058 ,infoRequestSemaphore_ (toolbox::BSem::FULL)
00059 , activeStateMachineName_ ("")
00060 ,counterTest_ (0)
00061 {
00062 INIT_MF("Supervisor");
00063 __MOUT__ << std::endl;
00064
00065
00066 mkdir((FSM_LAST_GROUP_ALIAS_PATH).c_str(), 0755);
00067 mkdir((RUN_NUMBER_PATH).c_str(), 0755);
00068
00069 securityType_ = theWebUsers_.getSecurity();
00070
00071 __MOUT__ << "Security: " << securityType_ << std::endl;
00072
00073 xgi::bind(this, &Supervisor::Default, "Default");
00074 xgi::bind(this, &Supervisor::loginRequest, "LoginRequest");
00075 xgi::bind(this, &Supervisor::request, "Request");
00076 xgi::bind(this, &Supervisor::stateMachineXgiHandler, "StateMachineXgiHandler");
00077 xgi::bind(this, &Supervisor::infoRequestHandler, "InfoRequestHandler");
00078 xgi::bind(this, &Supervisor::infoRequestResultHandler, "InfoRequestResultHandler");
00079 xgi::bind(this, &Supervisor::tooltipRequest, "TooltipRequest");
00080
00081 xoap::bind(this, &Supervisor::supervisorCookieCheck, "SupervisorCookieCheck", XDAQ_NS_URI);
00082 xoap::bind(this, &Supervisor::supervisorGetActiveUsers, "SupervisorGetActiveUsers", XDAQ_NS_URI);
00083 xoap::bind(this, &Supervisor::supervisorSystemMessage, "SupervisorSystemMessage", XDAQ_NS_URI);
00084 xoap::bind(this, &Supervisor::supervisorGetUserInfo, "SupervisorGetUserInfo", XDAQ_NS_URI);
00085 xoap::bind(this, &Supervisor::supervisorSystemLogbookEntry, "SupervisorSystemLogbookEntry", XDAQ_NS_URI);
00086 xoap::bind(this, &Supervisor::supervisorLastConfigGroupRequest, "SupervisorLastConfigGroupRequest", XDAQ_NS_URI);
00087
00088
00089
00090
00091
00092
00093
00094 init();
00095
00096
00097
00098 }
00099
00100
00101
00102 Supervisor::~Supervisor(void)
00103 {
00104 delete theConfigurationManager_;
00105 makeSystemLogbookEntry("ots halted.");
00106 }
00107
00108
00109 void Supervisor::init(void)
00110 {
00111
00112 theSupervisorDescriptorInfo_.init(getApplicationContext());
00113 theSupervisorsInfo_.init(theSupervisorDescriptorInfo_);
00114
00115 supervisorGuiHasBeenLoaded_ = false;
00116
00117 const XDAQContextConfiguration* contextConfiguration = theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration);
00118
00119 supervisorContextUID_ = contextConfiguration->getContextUID(
00120 getApplicationContext()->getContextDescriptor()->getURL()
00121 );
00122 __MOUT__ << "Context UID:" << supervisorContextUID_ << std::endl;
00123
00124 supervisorApplicationUID_ = contextConfiguration->getApplicationUID(
00125 getApplicationContext()->getContextDescriptor()->getURL(),
00126 getApplicationDescriptor()->getLocalId()
00127 );
00128
00129 __MOUT__ << "Application UID:" << supervisorApplicationUID_ << std::endl;
00130
00131 ConfigurationTree configLinkNode = theConfigurationManager_->getSupervisorConfigurationNode(
00132 supervisorContextUID_, supervisorApplicationUID_);
00133
00134
00135
00136
00137 std::string supervisorUID;
00138 if(!configLinkNode.isDisconnected())
00139 supervisorUID = configLinkNode.getValue();
00140 else
00141 supervisorUID = ViewColumnInfo::DATATYPE_LINK_DEFAULT;
00142
00143 __MOUT__ << "Supervisor UID:" << supervisorUID << std::endl;
00144 }
00145
00146
00147 void Supervisor::URLDisplayThread(Supervisor *supervisorPtr)
00148 {
00149 INIT_MF("Supervisor");
00150
00151
00152 int i = 0;
00153 for (; i < 5; ++i)
00154 {
00155 std::this_thread::sleep_for (std::chrono::seconds(2));
00156 std:: cout << __COUT_HDR_FL__ << "\n*********************************************************************" << std::endl;
00157 std:: cout << __COUT_HDR_FL__ << "\n\n"
00158 << supervisorPtr->getApplicationContext()->getContextDescriptor()->getURL()
00159
00160 << "/urn:xdaq-application:lid="
00161 << supervisorPtr->getApplicationDescriptor()->getLocalId() << "/"
00162 << "\n" << std::endl;
00163 std:: cout << __COUT_HDR_FL__ << "\n*********************************************************************" << std::endl;
00164 }
00165 }
00166
00167
00168
00169
00170
00172 void Supervisor::makeSystemLogbookEntry(std::string entryText)
00173 {
00174 __MOUT__ << "Making System Logbook Entry: " << entryText << std::endl;
00175
00176
00177 if(!theSupervisorDescriptorInfo_.getLogbookDescriptor())
00178 {
00179 __MOUT__ << "Just kidding... Logbook Descriptor not found." << std:: endl;
00180 return;
00181 }
00182
00183
00184 {
00185 std::string replace[] =
00186 {"\"", "'", "&", "<", ">", "\n", " "};
00187 std::string with[] =
00188 {"%22","%27", "%26", "%3C", "%3E", "%0A%0D", "%20%20"};
00189
00190 int numOfKeys = 7;
00191
00192 size_t f;
00193 for(int i=0;i<numOfKeys;++i)
00194 {
00195 while((f=entryText.find(replace[i])) != std::string::npos)
00196 {
00197 entryText = entryText.substr(0,f) + with[i] + entryText.substr(f+replace[i].length());
00198
00199 }
00200 }
00201 }
00202
00203
00204 SOAPParameters parameters("EntryText",entryText);
00205
00206
00207
00208 xoap::MessageReference retMsg = SOAPMessenger::sendWithSOAPReply(theSupervisorDescriptorInfo_.getLogbookDescriptor(), "MakeSystemLogbookEntry",parameters);
00209
00210 SOAPParameters retParameters("Status");
00211
00212
00213 receive(retMsg, retParameters);
00214
00215 __MOUT__ << "Returned Status: " << retParameters.getValue("Status") << std::endl;
00216
00217 }
00218
00219
00220 void Supervisor::Default(xgi::Input* in, xgi::Output* out)
00221 throw (xgi::exception::Exception)
00222 {
00223
00224 if (!supervisorGuiHasBeenLoaded_ && (supervisorGuiHasBeenLoaded_ = true))
00225 makeSystemLogbookEntry("ots started.");
00226
00227 *out <<
00228 "<!DOCTYPE HTML><html lang='en'><head><title>ots</title></head>" <<
00229 "<frameset col='100%' row='100%'>" <<
00230 "<frame src='/WebPath/html/Supervisor.html?urn=" <<
00231 this->getApplicationDescriptor()->getLocalId() << "=securityType=" <<
00232 securityType_ << "'></frameset></html>";
00233 }
00234
00235
00236
00237
00238 void Supervisor::stateMachineXgiHandler(xgi::Input* in, xgi::Output* out)
00239 throw (xgi::exception::Exception)
00240 {
00241 cgicc::Cgicc cgi(in);
00242
00243 uint8_t userPermissions;
00244 uint64_t uid;
00245 std::string userWithLock;
00246 std::string cookieCode = CgiDataUtilities::postData(cgi, "CookieCode");
00247 if (!theWebUsers_.cookieCodeIsActiveForRequest(cookieCode, &userPermissions,
00248 &uid, "0", 1, &userWithLock))
00249 {
00250 *out << cookieCode;
00251 return;
00252 }
00253
00254
00255 std::string username = "";
00256 username = theWebUsers_.getUsersUsername(uid);
00257 if (userWithLock != "" && userWithLock != username)
00258 {
00259 *out << WebUsers::REQ_USER_LOCKOUT_RESPONSE;
00260 __MOUT__ << "User " << username << " is locked out. " << userWithLock << " has lock." << std::endl;
00261 return;
00262 }
00263
00264
00265 HttpXmlDocument xmldoc(cookieCode);
00266 std::string command = CgiDataUtilities::getData(cgi, "StateMachine");
00267 std::string fsmName = CgiDataUtilities::getData(cgi, "fsmName");
00268 std::string fsmWindowName = CgiDataUtilities::getData(cgi, "fsmWindowName");
00269 fsmWindowName = CgiDataUtilities::decodeURIComponent(fsmWindowName);
00270 std::string currentState = theStateMachine_.getCurrentStateName();
00271
00272 __MOUT__ << "fsmName = " << fsmName << std::endl;
00273 __MOUT__ << "fsmWindowName = " << fsmWindowName << std::endl;
00274 __MOUT__ << "activeStateMachineName_ = " << activeStateMachineName_ << std::endl;
00275
00276
00277 if (theStateMachine_.isInTransition())
00278 {
00279 __SS__ << "Error - Can not accept request since State Machine is already in transition!" << std::endl;
00280 __MOUT_ERR__ << "\n" << ss.str();
00281
00282 xmldoc.addTextElementToData("state_tranisition_attempted", "0");
00283 xmldoc.addTextElementToData("state_tranisition_attempted_err",
00284 ss.str());
00285 xmldoc.outputXmlDocument((std::ostringstream*) out, false, true);
00286 return;
00287 }
00288
00290
00291
00292
00293
00294
00295 if(activeStateMachineName_ != "" &&
00296 activeStateMachineName_ != fsmName)
00297 {
00298 __MOUT__ << "currentState = " <<
00299 currentState << std::endl;
00300 if(currentState != "Halted" &&
00301 currentState != "Initial")
00302 {
00303
00304
00305 __SS__ << "Error - Can not accept request since State Machine " <<
00306 "with window name '" <<
00307 activeStateMachineWindowName_ << "' (UID: " <<
00308 activeStateMachineName_ << ") "
00309 "is currently " <<
00310 "in control of State Machine progress. ";
00311 ss << "\n\nIn order for this State Machine with window name '" <<
00312 fsmWindowName << "' (UID: " << fsmName << ") "
00313 "to control progress, please transition to Halted using the active " <<
00314 "State Machine '" << activeStateMachineWindowName_ << ".'" << std::endl;
00315 __MOUT_ERR__ << "\n" << ss.str();
00316
00317 xmldoc.addTextElementToData("state_tranisition_attempted", "0");
00318 xmldoc.addTextElementToData("state_tranisition_attempted_err",
00319 ss.str());
00320 xmldoc.outputXmlDocument((std::ostringstream*) out, false, true);
00321 return;
00322 }
00323 else
00324 {
00325 activeStateMachineName_ = "";
00326 activeStateMachineWindowName_ = "";
00327 }
00328 }
00329
00330
00331
00332
00333 SOAPParameters parameters;
00334 if (command == "Configure")
00335 {
00336 if(currentState != "Halted")
00337 {
00338 __SS__ << "Error - Can only transition to Configured if the current " <<
00339 "state is Halted. Perhaps your state machine is out of sync." <<
00340 std::endl;
00341 __MOUT_ERR__ << "\n" << ss.str();
00342
00343 xmldoc.addTextElementToData("state_tranisition_attempted", "0");
00344 xmldoc.addTextElementToData("state_tranisition_attempted_err",
00345 ss.str());
00346 xmldoc.outputXmlDocument((std::ostringstream*) out, false, true);
00347 return;
00348 }
00349
00350
00351
00352 parameters.addParameter("ConfigurationAlias",
00353 CgiDataUtilities::postData(cgi, "ConfigurationAlias"));
00354
00355 std::string configurationAlias = parameters.getValue("ConfigurationAlias");
00356 __MOUT__ << "Configure --> Name: ConfigurationAlias Value: " <<
00357 configurationAlias << std::endl;
00358
00359
00360 std::string fn = FSM_LAST_GROUP_ALIAS_PATH + FSM_LAST_GROUP_ALIAS_FILE_START +
00361 username + "." + FSM_USERS_PREFERENCES_FILETYPE;
00362
00363 __MOUT__ << "Save FSM preferences: " << fn << std::endl;
00364 FILE *fp = fopen(fn.c_str(),"w");
00365 if(!fp)
00366 throw std::runtime_error("Could not open file: " + fn);
00367 fprintf(fp,"FSM_last_configuration_alias %s",configurationAlias.c_str());
00368 fclose(fp);
00369
00370 activeStateMachineName_ = fsmName;
00371 activeStateMachineWindowName_ = fsmWindowName;
00372 }
00373 else if (command == "Start")
00374 {
00375 if(currentState != "Configured")
00376 {
00377 __SS__ << "Error - Can only transition to Configured if the current " <<
00378 "state is Halted. Perhaps your state machine is out of sync. " <<
00379 "(Likely the server was restarted or another user changed the state)" <<
00380 std::endl;
00381 __MOUT_ERR__ << "\n" << ss.str();
00382
00383 xmldoc.addTextElementToData("state_tranisition_attempted", "0");
00384 xmldoc.addTextElementToData("state_tranisition_attempted_err",
00385 ss.str());
00386 xmldoc.outputXmlDocument((std::ostringstream*) out, false, true);
00387 return;
00388 }
00389
00390 unsigned int runNumber = getNextRunNumber();
00391 parameters.addParameter("RunNumber", runNumber);
00392 setNextRunNumber(++runNumber);
00393 }
00394
00395 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(
00396 command, parameters);
00397
00398 xoap::MessageReference reply = stateMachineXoapHandler(message);
00399
00400
00401
00402 xmldoc.addTextElementToData("state_tranisition_attempted", "1");
00403 xmldoc.outputXmlDocument((std::ostringstream*) out, false);
00404 __MOUT__ << "Done - Xgi Request!" << std::endl;
00405 }
00406
00407
00408 void Supervisor::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out)
00409 throw (xgi::exception::Exception)
00410 {
00411 cgicc::Cgicc cgi(in);
00412 __MOUT__ << "Xgi Request!" << std::endl;
00413
00414 uint8_t userPermissions;
00415 std::string cookieCode = CgiDataUtilities::postData(cgi, "CookieCode");
00416 if (!theWebUsers_.cookieCodeIsActiveForRequest(cookieCode,
00417 &userPermissions))
00418 {
00419 *out << cookieCode;
00420 return;
00421 }
00422
00423 HttpXmlDocument xmldoc(cookieCode);
00424
00425 std::string command = CgiDataUtilities::getData(cgi, "StateMachine");
00426
00427 SOAPParameters parameters;
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(
00447 CgiDataUtilities::getData(cgi, "StateMachine"), parameters);
00448
00449 xoap::MessageReference reply = stateMachineResultXoapHandler(message);
00450
00451
00452
00453 __MOUT__ << "Done - Xgi Request!" << std::endl;
00454 }
00455
00456
00457 xoap::MessageReference Supervisor::stateMachineXoapHandler(xoap::MessageReference message)
00458 throw (xoap::exception::Exception)
00459 {
00460 __MOUT__ << "Soap Handler!" << std::endl;
00461 stateMachineWorkLoopManager_.removeProcessedRequests();
00462 stateMachineWorkLoopManager_.processRequest(message);
00463 __MOUT__ << "Done - Soap Handler!" << std::endl;
00464 return message;
00465 }
00466
00467
00468 xoap::MessageReference Supervisor::stateMachineResultXoapHandler(
00469 xoap::MessageReference message)
00470 throw (xoap::exception::Exception)
00471 {
00472 __MOUT__ << "Soap Handler!" << std::endl;
00473
00474
00475 __MOUT__ << "Done - Soap Handler!" << std::endl;
00476 return message;
00477 }
00478
00479
00480 bool Supervisor::stateMachineThread(toolbox::task::WorkLoop* workLoop)
00481 {
00482 stateMachineSemaphore_.take();
00483 __MOUT__ << "Re-sending message..." << SOAPUtilities::translate( stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
00484 std::string reply = send(
00485 theSupervisorDescriptorInfo_.getSupervisorDescriptor(),
00486 stateMachineWorkLoopManager_.getMessage(workLoop));
00487 stateMachineWorkLoopManager_.report(workLoop, reply, 100, true);
00488
00489 __MOUT__ << "Done with message" << std::endl;
00490 stateMachineSemaphore_.give();
00491 return false;
00492
00493 }
00494
00495
00496
00497
00498
00499
00500 void Supervisor::infoRequestHandler(xgi::Input* in, xgi::Output* out)
00501 throw (xgi::exception::Exception)
00502 {
00503 __MOUT__ << "Starting to Request!" << std::endl;
00504 cgicc::Cgicc cgi(in);
00505
00506
00507
00508
00509 std::string cookieCode = CgiDataUtilities::postData(cgi, "CookieCode");
00510 uint8_t userPermissions;
00511 if (!theWebUsers_.cookieCodeIsActiveForRequest(cookieCode, &userPermissions))
00512 {
00513 *out << cookieCode;
00514 return;
00515 }
00516
00517
00518 HttpXmlDocument xmldoc(cookieCode);
00519
00520
00521
00522
00523
00524 HttpXmlDocument tmpDoc = infoRequestWorkLoopManager_.processRequest(cgi);
00525
00526 xmldoc.copyDataChildren(tmpDoc);
00527
00528 xmldoc.outputXmlDocument((std::ostringstream*) out, false);
00529 }
00530
00531
00532 void Supervisor::infoRequestResultHandler(xgi::Input* in, xgi::Output* out)
00533 throw (xgi::exception::Exception)
00534 {
00535 __MOUT__ << "Starting ask!" << std::endl;
00536 cgicc::Cgicc cgi(in);
00537
00538
00539
00540
00541 std::string cookieCode = CgiDataUtilities::postData(cgi, "CookieCode");
00542 uint8_t userPermissions;
00543 if (!theWebUsers_.cookieCodeIsActiveForRequest(cookieCode, &userPermissions))
00544 {
00545 *out << cookieCode;
00546 return;
00547 }
00548
00549
00550 HttpXmlDocument xmldoc(cookieCode);
00551
00552 infoRequestWorkLoopManager_.getRequestResult(cgi, xmldoc);
00553
00554
00555 xmldoc.outputXmlDocument((std::ostringstream*) out, false);
00556
00557 __MOUT__ << "Done asking!" << std::endl;
00558 }
00559
00560
00561 bool Supervisor::infoRequestThread(toolbox::task::WorkLoop* workLoop)
00562 {
00563
00564
00565
00566 infoRequestSemaphore_.take();
00567
00568 vectorTest_.clear();
00569
00570 for (unsigned long long i = 0; i < 100000000; i++)
00571 {
00572 counterTest_ += 2;
00573 vectorTest_.push_back(counterTest_);
00574 }
00575
00576 infoRequestWorkLoopManager_.report(workLoop,
00577 "RESULT: This is the best result ever", 50, false);
00578 std::string workLoopName = workLoop->getName();
00579 __MOUT__ << workLoopName << " test: " << counterTest_
00580 << " vector size: " << vectorTest_.size() << std::endl;
00581 wait(400, "InfoRequestThread ----- locked");
00582 infoRequestSemaphore_.give();
00583
00584 wait(200, "InfoRequestThread");
00585
00586 infoRequestSemaphore_.take();
00587
00588 vectorTest_.clear();
00589
00590 for (unsigned long long i = 0; i < 100000000; i++)
00591 {
00592 counterTest_ += 2;
00593 vectorTest_.push_back(counterTest_);
00594 }
00595
00596 wait(400, "InfoRequestThread ----- locked");
00597 __MOUT__ << workLoopName << " test: " << counterTest_ << " vector size: " << vectorTest_.size() << std::endl;
00598 infoRequestSemaphore_.give();
00599
00600
00601 infoRequestWorkLoopManager_.report(workLoop,
00602 theStateMachine_.getCurrentStateName(), 100, true);
00603
00604 return false;
00605
00606 }
00607
00608
00609 void Supervisor::stateInitial(toolbox::fsm::FiniteStateMachine & fsm)
00610 throw (toolbox::fsm::exception::Exception)
00611 {
00612 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
00613
00614
00615 }
00616
00617
00618 void Supervisor::statePaused(toolbox::fsm::FiniteStateMachine & fsm)
00619 throw (toolbox::fsm::exception::Exception)
00620 {
00621 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634 }
00635
00636
00637 void Supervisor::stateRunning(toolbox::fsm::FiniteStateMachine & fsm)
00638 throw (toolbox::fsm::exception::Exception)
00639 {
00640
00641 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654 }
00655
00656
00657 void Supervisor::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
00658 throw (toolbox::fsm::exception::Exception)
00659 {
00660 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
00661 __MOUT__ << "Fsm is in transition?" << theStateMachine_.isInTransition() << std::endl;
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684 }
00685
00686
00687 void Supervisor::stateConfigured(toolbox::fsm::FiniteStateMachine & fsm)
00688 throw (toolbox::fsm::exception::Exception)
00689 {
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723 }
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756 void Supervisor::inError(toolbox::fsm::FiniteStateMachine & fsm)
00757 throw (toolbox::fsm::exception::Exception)
00758 {
00759 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
00760
00761 }
00762
00763
00764 void Supervisor::enteringError(toolbox::Event::Reference e)
00765 throw (toolbox::fsm::exception::Exception)
00766 {
00767 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
00768
00769
00770 toolbox::fsm::FailedEvent& failedEvent = dynamic_cast<toolbox::fsm::FailedEvent&> (*e);
00771 __SS__ << "\nFailure performing transition from " << failedEvent.getFromState() << "-" <<
00772 theStateMachine_.getStateName(failedEvent.getFromState()) <<
00773 " to " << failedEvent.getToState() << "-" <<
00774 theStateMachine_.getStateName(failedEvent.getToState()) <<
00775 ".\n\nException:\n" << failedEvent.getException().what();
00776 __MOUT_ERR__ << "\n" << ss.str();
00777
00778 theStateMachine_.setErrorMessage(ss.str());
00779
00780
00781
00782
00783
00784
00785 }
00786
00790
00791 void Supervisor::getSupervisorsStatus(void)
00792 throw (toolbox::fsm::exception::Exception)
00793 {
00794 theSupervisorsInfo_.getSupervisorInfo().setStatus(
00795 theStateMachine_.getCurrentStateName());
00796
00797 try
00798 {
00799 SupervisorDescriptors::const_iterator it =
00800 theSupervisorDescriptorInfo_.getFEDescriptors().begin();
00801 for (; it != theSupervisorDescriptorInfo_.getFEDescriptors().end();
00802 it++)
00803 {
00804 try
00805 {
00806 std::string state = send(it->second,
00807 "StateMachineStateRequest");
00808
00809 theSupervisorsInfo_.getFESupervisorInfo(it->first).setStatus(
00810 state);
00811
00812 __MOUT__ << "PixelFESupervisor instance " << it->first << " is in FSM state " << state << std::endl;
00813 __MOUT__ << "Look! Here's a FEW! @@@" << std::endl;
00814 }
00815 catch (xdaq::exception::Exception& e)
00816 {
00817
00818 }
00819 }
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838 for (auto& it : theSupervisorDescriptorInfo_.getARTDAQFEDataManagerDescriptors())
00839 {
00840 try
00841 {
00842 std::string state = send(it.second,
00843 "StateMachineStateRequest");
00844 theSupervisorsInfo_.getARTDAQFEDataManagerSupervisorInfo(it.first).setStatus(
00845 state);
00846 __MOUT__ << "PixelFERSupervisor instance " << it.first << " is in FSM state " << state << std::endl;
00847 __MOUT__ << "Look! Here's a FER! @@@" << std::endl;
00848 }
00849 catch (xdaq::exception::Exception& e)
00850 {
00851
00852 }
00853 }
00854 for (auto& it : theSupervisorDescriptorInfo_.getARTDAQDataManagerDescriptors())
00855 {
00856 try
00857 {
00858 std::string state = send(it.second,
00859 "StateMachineStateRequest");
00860 theSupervisorsInfo_.getARTDAQDataManagerSupervisorInfo(it.first).setStatus(
00861 state);
00862 __MOUT__ << "PixelFERSupervisor instance " << it.first << " is in FSM state " << state << std::endl;
00863 __MOUT__ << "Look! Here's a FER! @@@" << std::endl;
00864 }
00865 catch (xdaq::exception::Exception& e)
00866 {
00867
00868 }
00869 }
00870 }
00871 catch (xdaq::exception::Exception& e)
00872 {
00873 __MOUT__
00874 << "No PixelFESupervisor found in the \"daq\" group in the Configuration XML file."
00875 << std::endl;
00876
00877 }
00878
00879 }
00880
00881
00882 void Supervisor::transitionConfiguring(toolbox::Event::Reference e)
00883 throw (toolbox::fsm::exception::Exception)
00884 {
00885
00886
00887 theProgressBar_.step();
00888
00889 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
00890
00891 std::string systemAlias = SOAPUtilities::translate(
00892 theStateMachine_.getCurrentMessage()).getParameters().getValue("ConfigurationAlias");
00893
00894 __MOUT__ << "Transition parameter: " << systemAlias << std::endl;
00895
00896 theProgressBar_.step();
00897
00898
00899 try
00900 {
00901 theConfigurationManager_->init();
00902 }
00903 catch(...)
00904 {
00905 __SS__ << "\nTransition to Configuring interrupted! " <<
00906 "The Configuration Manager could not be initialized." << std::endl;
00907
00908 __MOUT_ERR__ << "\n" << ss.str();
00909 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
00910 return;
00911 }
00912
00913 theProgressBar_.step();
00914
00915
00916 try
00917 {
00918 theConfigurationGroup_ = theConfigurationManager_->getConfigurationGroupFromAlias(systemAlias);
00919 }
00920 catch(...)
00921 {
00922 __MOUT_INFO__ << "Exception occurred" << std::endl;
00923 }
00924
00925 theProgressBar_.step();
00926
00927 if(theConfigurationGroup_.second.isInvalid())
00928 {
00929 __SS__ << "\nTransition to Configuring interrupted! System Alias " <<
00930 systemAlias << " could not be translated to a group name and key." << std::endl;
00931
00932 __MOUT_ERR__ << "\n" << ss.str();
00933 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
00934 return;
00935 }
00936
00937 theProgressBar_.step();
00938
00939 __MOUT__ << "Configuration group name: " << theConfigurationGroup_.first << " key: " <<
00940 theConfigurationGroup_.second << std::endl;
00941
00942
00943 {
00944 std::stringstream ss;
00945 ss << "Configuring '" << systemAlias << "' which translates to " <<
00946 theConfigurationGroup_.first << " (" << theConfigurationGroup_.second << ").";
00947 makeSystemLogbookEntry(ss.str());
00948 }
00949
00950 theProgressBar_.step();
00951
00952
00953 try
00954 {
00955 theConfigurationManager_->loadConfigurationGroup(theConfigurationGroup_.first, theConfigurationGroup_.second, true);
00956
00957
00958 ConfigurationManagerRW tmpCfgMgr("TheSupervisor");
00959 tmpCfgMgr.activateConfigurationGroup(theConfigurationGroup_.first, theConfigurationGroup_.second);
00960 }
00961 catch(...)
00962 {
00963 __SS__ << "\nTransition to Configuring interrupted! System Alias " <<
00964 systemAlias << " was translated to " << theConfigurationGroup_.first <<
00965 " (" << theConfigurationGroup_.second << ") but could not be loaded and initialized." << std::endl;
00966 ss << "\n\nTo debug this problem, try activating this group in the Configuration GUI " <<
00967 " and detailed errors will be shown." << std::endl;
00968 __MOUT_ERR__ << "\n" << ss.str();
00969 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
00970 return;
00971 }
00972
00973
00974 {
00975 ConfigurationTree configLinkNode = theConfigurationManager_->getSupervisorConfigurationNode(
00976 supervisorContextUID_, supervisorApplicationUID_);
00977 if(!configLinkNode.isDisconnected())
00978 {
00979 try
00980 {
00981 ConfigurationTree fsmLinkNode = configLinkNode.getNode("LinkToStateMachineConfiguration");
00982 if(!fsmLinkNode.isDisconnected() &&
00983 fsmLinkNode.getNode(activeStateMachineName_ +
00984 "/EnableConfigurationDumpOnConfigureTransition").getValue<bool>())
00985 {
00986
00987 theConfigurationManager_->dumpActiveConfiguration(
00988 fsmLinkNode.getNode(activeStateMachineName_ +
00989 "/ConfigurationDumpOnConfigureFilePath").getValue<std::string>() +
00990 fsmLinkNode.getNode(activeStateMachineName_ +
00991 "/ConfigurationDumpOnConfigureFileRadix").getValue<std::string>() +
00992 "_" +
00993 std::to_string(time(0)) +
00994 ".dump",
00995 fsmLinkNode.getNode(activeStateMachineName_ +
00996 "/ConfigurationDumpOnConfigureFormat").getValue<std::string>()
00997 );
00998 }
00999 else
01000 __MOUT_INFO__ << "FSM Link disconnected." << std::endl;
01001 }
01002 catch(std::runtime_error &e) {
01003 __SS__ << "\nTransition to Configuring interrupted! There was an error identified " <<
01004 "during the configuration dump attempt:\n\n " <<
01005 e.what() << std::endl;
01006 __MOUT_ERR__ << "\n" << ss.str();
01007 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
01008 return;
01009 }
01010 catch(...) {
01011 __SS__ << "\nTransition to Configuring interrupted! There was an error identified " <<
01012 "during the configuration dump attempt.\n\n " <<
01013 std::endl;
01014 __MOUT_ERR__ << "\n" << ss.str();
01015 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
01016 return;
01017 }
01018
01019 }
01020 }
01021
01022 theProgressBar_.step();
01023 SOAPParameters parameters;
01024 parameters.addParameter("ConfigurationGroupName", theConfigurationGroup_.first);
01025 parameters.addParameter("ConfigurationGroupKey", theConfigurationGroup_.second.toString());
01026
01027
01028 xoap::MessageReference message = theStateMachine_.getCurrentMessage();
01029 SOAPUtilities::addParameters(message,parameters);
01030 broadcastMessage(message);
01031 theProgressBar_.step();
01032
01033
01034
01035
01036 saveGroupNameAndKey(theConfigurationGroup_,FSM_LAST_CONFIGURED_GROUP_ALIAS_FILE);
01037
01038 __MOUT__ << "Done" << std::endl;
01039 theProgressBar_.complete();
01040 }
01041
01042
01043 void Supervisor::transitionHalting(toolbox::Event::Reference e)
01044 throw (toolbox::fsm::exception::Exception)
01045 {
01046 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
01047
01048 makeSystemLogbookEntry("Run halting.");
01049
01050 broadcastMessage(theStateMachine_.getCurrentMessage());
01051 }
01052
01053
01054 void Supervisor::transitionInitializing(toolbox::Event::Reference e)
01055 throw (toolbox::fsm::exception::Exception)
01056 {
01057 __MOUT__ << theStateMachine_.getCurrentStateName() << std::endl;
01058
01059
01060 getSupervisorsStatus();
01061
01062 if(!broadcastMessage(theStateMachine_.getCurrentMessage()))
01063 {
01064 __MOUT__ << "I can't Initialize the supervisors!" << std::endl;
01065 }
01066
01067 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
01068 __MOUT__ << "Fsm current transition: " << theStateMachine_.getCurrentTransitionName(e->type()) << std::endl;
01069 __MOUT__ << "Fsm final state: " << theStateMachine_.getTransitionFinalStateName(e->type()) << std::endl;
01070 }
01071
01072
01073 void Supervisor::transitionPausing(toolbox::Event::Reference e)
01074 throw (toolbox::fsm::exception::Exception)
01075 {
01076 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
01077
01078 makeSystemLogbookEntry("Run pausing.");
01079
01080 broadcastMessage(theStateMachine_.getCurrentMessage());
01081 }
01082
01083
01084 void Supervisor::transitionResuming(toolbox::Event::Reference e)
01085 throw (toolbox::fsm::exception::Exception)
01086 {
01087 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
01088
01089 makeSystemLogbookEntry("Run resuming.");
01090
01091 broadcastMessage(theStateMachine_.getCurrentMessage());
01092 }
01093
01094
01095 void Supervisor::transitionStarting(toolbox::Event::Reference e)
01096 throw (toolbox::fsm::exception::Exception)
01097 {
01098 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
01099
01100 SOAPParameters parameters("RunNumber");
01101 receive(theStateMachine_.getCurrentMessage(), parameters);
01102
01103 std::string runNumber = parameters.getValue("RunNumber");
01104 __MOUT__ << runNumber << std::endl;
01105
01106
01107 {
01108 ConfigurationTree configLinkNode = theConfigurationManager_->getSupervisorConfigurationNode(
01109 supervisorContextUID_, supervisorApplicationUID_);
01110 if(!configLinkNode.isDisconnected())
01111 {
01112 try
01113 {
01114 ConfigurationTree fsmLinkNode = configLinkNode.getNode("LinkToStateMachineConfiguration");
01115 if(!fsmLinkNode.isDisconnected() &&
01116 fsmLinkNode.getNode(activeStateMachineName_ +
01117 "/EnableConfigurationDumpOnRunTransition").getValue<bool>())
01118 {
01119
01120 theConfigurationManager_->dumpActiveConfiguration(
01121 fsmLinkNode.getNode(activeStateMachineName_ +
01122 "/ConfigurationDumpOnRunFilePath").getValue<std::string>() +
01123 fsmLinkNode.getNode(activeStateMachineName_ +
01124 "/ConfigurationDumpOnRunFileRadix").getValue<std::string>() +
01125 "_Run" +
01126 runNumber +
01127 ".dump",
01128 fsmLinkNode.getNode(activeStateMachineName_ +
01129 "/ConfigurationDumpOnRunFormat").getValue<std::string>()
01130 );
01131 }
01132 else
01133 __MOUT_INFO__ << "FSM Link disconnected." << std::endl;
01134 }
01135 catch(std::runtime_error &e) {
01136 __SS__ << "\nTransition to Configuring interrupted! There was an error identified " <<
01137 "during the configuration dump attempt:\n\n " <<
01138 e.what() << std::endl;
01139 __MOUT_ERR__ << "\n" << ss.str();
01140 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
01141 return;
01142 }
01143 catch(...) {
01144 __SS__ << "\nTransition to Configuring interrupted! There was an error identified " <<
01145 "during the configuration dump attempt.\n\n " <<
01146 std::endl;
01147 __MOUT_ERR__ << "\n" << ss.str();
01148 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
01149 return;
01150 }
01151
01152 }
01153 }
01154
01155
01156 makeSystemLogbookEntry("Run " + runNumber + " starting.");
01157
01158 broadcastMessage(theStateMachine_.getCurrentMessage());
01159
01160
01161 saveGroupNameAndKey(theConfigurationGroup_,FSM_LAST_STARTED_GROUP_ALIAS_FILE);
01162 }
01163
01164
01165 void Supervisor::transitionStopping(toolbox::Event::Reference e)
01166 throw (toolbox::fsm::exception::Exception)
01167 {
01168 __MOUT__ << "Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
01169
01170 makeSystemLogbookEntry("Run stopping.");
01171
01172 broadcastMessage(theStateMachine_.getCurrentMessage());
01173 }
01174
01178
01179
01180 bool Supervisor::broadcastMessage(xoap::MessageReference message)
01181 {
01182 std::string command = SOAPUtilities::translate(message).getCommand();
01183 bool proceed = true;
01184
01185 for(auto& it: theSupervisorDescriptorInfo_.getFEDescriptors())
01186 {
01187 RunControlStateMachine::theProgressBar_.step();
01188 __MOUT__ << "Sending message to FESupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01189 __MOUT__ << "Sending message to FESupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01190 __MOUT__ << "Sending message to FESupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01191 __MOUT__ << "Sending message to FESupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01192 __MOUT__ << "Sending message to FESupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01193 std::string reply = send(it.second, message);
01194 if (reply != command + "Done")
01195 {
01196
01197 std::stringstream error;
01198 error << "Can't " << command << " FESupervisor, instance = " << it.first;
01199 XCEPT_RAISE(xdaq::exception::Exception, error.str());
01200 __MOUT__ << error.str() << std::endl;
01201 proceed = false;
01202
01203 } else
01204 {
01205 __MOUT__ << "FESupervisor supervisor " << (it.first) << " was " << command << "'d correctly!" << std::endl;
01206 }
01207 }
01208
01209 for(auto& it: theSupervisorDescriptorInfo_.getDataManagerDescriptors())
01210 {
01211 RunControlStateMachine::theProgressBar_.step();
01212 __MOUT__ << "Sending message to DataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01213 __MOUT__ << "Sending message to DataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01214 __MOUT__ << "Sending message to DataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01215 __MOUT__ << "Sending message to DataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01216 __MOUT__ << "Sending message to DataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01217 std::string reply = send(it.second, message);
01218 if (reply != command + "Done")
01219 {
01220
01221 std::stringstream error;
01222 error << "Can't " << command << " DataManagerSupervisor, instance = " << it.first;
01223 XCEPT_RAISE(xdaq::exception::Exception, error.str());
01224 __MOUT__ << error.str() << std::endl;
01225 proceed = false;
01226 } else
01227 {
01228 __MOUT__ << "DataManagerSupervisor " << (it.first) << " was " << command << "'d correctly!" << std::endl;
01229 }
01230 }
01231
01232 for (auto& it: theSupervisorDescriptorInfo_.getFEDataManagerDescriptors())
01233 {
01234 RunControlStateMachine::theProgressBar_.step();
01235 __MOUT__ << "Sending message to FEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01236 __MOUT__ << "Sending message to FEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01237 __MOUT__ << "Sending message to FEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01238 __MOUT__ << "Sending message to FEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01239 __MOUT__ << "Sending message to FEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01240 std::string reply = send(it.second, message);
01241 if (reply != command + "Done")
01242 {
01243
01244 std::stringstream error;
01245 error << "Can't " << command << " FEDataManagerSupervisor, instance = " << it.first;
01246 XCEPT_RAISE(xdaq::exception::Exception, error.str());
01247 __MOUT__ << error.str() << std::endl;
01248 proceed = false;
01249 } else
01250 {
01251 __MOUT__ << "FEDataManagerSupervisor " << (it.first) << " was " << command << "'d correctly!" << std::endl;
01252 }
01253 }
01254
01255 for(auto& it: theSupervisorDescriptorInfo_.getVisualDescriptors())
01256 {
01257 RunControlStateMachine::theProgressBar_.step();
01258 __MOUT__ << "Sending message to VisualSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01259 __MOUT__ << "Sending message to VisualSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01260 __MOUT__ << "Sending message to VisualSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01261 __MOUT__ << "Sending message to VisualSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01262 __MOUT__ << "Sending message to VisualSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01263 std::string reply = send(it.second, message);
01264 if (reply != command + "Done")
01265 {
01266
01267 std::stringstream error;
01268 error << "Can't " << command
01269 << " VisualSupervisor, instance = " << it.first;
01270 XCEPT_RAISE(xdaq::exception::Exception, error.str());
01271 __MOUT__ << error.str() << std::endl;
01272 proceed = false;
01273 } else
01274 {
01275 __MOUT__ << "VisualSupervisor supervisor " << (it.first) << " was " << command << "'d correctly!" << std::endl;
01276 }
01277 }
01278
01279
01280 bool artdaqRestarted = false;
01281 bool artdaqWasRestarted = false;
01282
01283 RunControlStateMachine::theProgressBar_.step();
01284 if(command == "Halt" || command == "Initialize")
01285 {
01286
01287
01288
01289
01290
01291 FILE *fp = fopen((std::string(getenv("SERVICE_DATA_PATH")) +
01292 "/StartOTS_action.cmd").c_str(),"w");
01293 if(fp)
01294 {
01295 fprintf(fp,"RESET_MPI");
01296 fclose(fp);
01297
01298 }
01299
01300 artdaqRestarted = true;
01301
01302 SOAPParameters parameters;
01303 message = SOAPUtilities::makeSOAPMessageReference(
01304 "Initialize", parameters);
01305 command = SOAPUtilities::translate(message).getCommand();
01306 __MOUT__ << "command now is " << command << std::endl;
01307 }
01308 RunControlStateMachine::theProgressBar_.step();
01309
01310
01311 int MAX_ARTDAQ_RESTARTS = 10;
01312 int ARTDAQ_RESTART_DELAY = 2;
01313 int artdaqRestartCount = 0;
01314 bool preArtdaqProceed = proceed;
01315
01316 ARTDAQ_RETRY:
01317 if(artdaqWasRestarted)
01318 {
01319 ++artdaqRestartCount;
01320 proceed = preArtdaqProceed;
01321 if(artdaqRestartCount < MAX_ARTDAQ_RESTARTS)
01322 artdaqWasRestarted = false;
01323 for(int i=0;i<ARTDAQ_RESTART_DELAY;++i)
01324 {
01325 sleep(1);
01326 __MOUT__ << "Waiting on artdaq reboot... " << i << " for " << artdaqRestartCount << "x" << std::endl;
01327 }
01328 }
01329
01330 for(auto& it: theSupervisorDescriptorInfo_.getARTDAQFEDataManagerDescriptors())
01331 {
01332 RunControlStateMachine::theProgressBar_.step();
01333 __MOUT__ << "Sending message to ARTDAQFEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01334 __MOUT__ << "Sending message to ARTDAQFEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01335 __MOUT__ << "Sending message to ARTDAQFEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01336 __MOUT__ << "Sending message to ARTDAQFEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01337 __MOUT__ << "Sending message to ARTDAQFEDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01338
01339 try
01340 {
01341 std::string reply = send(it.second, message);
01342 if (reply != command + "Done")
01343 {
01344
01345 std::stringstream error;
01346 error << "Can't " << command << " ARTDAQFEDataManagerSupervisor, instance = " << it.first;
01347 XCEPT_RAISE(xdaq::exception::Exception, error.str());
01348 __MOUT__ << error.str() << std::endl;
01349 proceed = false;
01350 }
01351 else
01352 {
01353 __MOUT__ << "ARTDAQFEDataManagerSupervisor supervisor " << (it.first) << " was " << command << "'d correctly!" << std::endl;
01354 }
01355 }
01356 catch(xdaq::exception::Exception &e)
01357 {
01358 if(artdaqRestarted && !artdaqWasRestarted)
01359 {
01360 artdaqWasRestarted = true;
01361 goto ARTDAQ_RETRY;
01362 }
01363 else
01364 throw;
01365 }
01366 }
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405 for(auto& it: theSupervisorDescriptorInfo_.getARTDAQDataManagerDescriptors())
01406 {
01407 RunControlStateMachine::theProgressBar_.step();
01408 __MOUT__ << "Sending message to ARTDAQDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01409 __MOUT__ << "Sending message to ARTDAQDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01410 __MOUT__ << "Sending message to ARTDAQDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01411 __MOUT__ << "Sending message to ARTDAQDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01412 __MOUT__ << "Sending message to ARTDAQDataManagerSupervisors: " << it.second->getLocalId() << " : " << command << std::endl;
01413 try
01414 {
01415 std::string reply = send(it.second, message);
01416 if (reply != command + "Done")
01417 {
01418
01419 std::stringstream error;
01420 error << "Can't " << command << " ARTDAQDataManagerSupervisors, instance = " << it.first;
01421 XCEPT_RAISE(xdaq::exception::Exception, error.str());
01422 __MOUT__ << error.str() << std::endl;
01423 proceed = false;
01424 }
01425 else
01426 {
01427 __MOUT__ << "ARTDAQDataManagerSupervisors supervisor " << (it.first) << " was " << command << "'d correctly!" << std::endl;
01428 }
01429 }
01430 catch(xdaq::exception::Exception &e)
01431 {
01432 if(artdaqRestarted && !artdaqWasRestarted)
01433 {
01434 artdaqWasRestarted = true;
01435 goto ARTDAQ_RETRY;
01436 }
01437 else
01438 throw;
01439 }
01440 }
01441 for(auto& it: theSupervisorDescriptorInfo_.getARTDAQBuilderDescriptors())
01442 {
01443 RunControlStateMachine::theProgressBar_.step();
01444 __MOUT__ << "Sending message to ARTDAQBuilderSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01445 __MOUT__ << "Sending message to ARTDAQBuilderSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01446 __MOUT__ << "Sending message to ARTDAQBuilderSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01447 __MOUT__ << "Sending message to ARTDAQBuilderSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01448 __MOUT__ << "Sending message to ARTDAQBuilderSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01449
01450 try
01451 {
01452 std::string reply = send(it.second, message);
01453 if (reply != command + "Done")
01454 {
01455
01456 std::stringstream error;
01457 error << "Can't " << command << " ARTDAQBuilderSupervisor, instance = " << it.first;
01458 XCEPT_RAISE(xdaq::exception::Exception, error.str());
01459 __MOUT__ << error.str() << std::endl;
01460 proceed = false;
01461 }
01462 else
01463 {
01464 __MOUT__ << "ARTDAQBuilderSupervisor supervisor " << (it.first) << " was " << command << "'d correctly!" << std::endl;
01465 }
01466 }
01467 catch(xdaq::exception::Exception &e)
01468 {
01469 if(artdaqRestarted && !artdaqWasRestarted)
01470 {
01471 artdaqWasRestarted = true;
01472 goto ARTDAQ_RETRY;
01473 }
01474 else
01475 throw;
01476 }
01477 }
01478
01479 for(auto& it: theSupervisorDescriptorInfo_.getARTDAQAggregatorDescriptors())
01480 {
01481 RunControlStateMachine::theProgressBar_.step();
01482 __MOUT__ << "Sending message to ARTDAQAggregatorSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01483 __MOUT__ << "Sending message to ARTDAQAggregatorSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01484 __MOUT__ << "Sending message to ARTDAQAggregatorSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01485 __MOUT__ << "Sending message to ARTDAQAggregatorSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01486 __MOUT__ << "Sending message to ARTDAQAggregatorSupervisor: " << it.second->getLocalId() << " : " << command << std::endl;
01487
01488 try
01489 {
01490 std::string reply = send(it.second, message);
01491 if (reply != command + "Done")
01492 {
01493
01494 std::stringstream error;
01495 error << "Can't " << command << " ARTDAQAggregatorSupervisor, instance = " << it.first;
01496 XCEPT_RAISE(xdaq::exception::Exception, error.str());
01497 __MOUT__ << error.str() << std::endl;
01498 proceed = false;
01499 }
01500 else
01501 {
01502 __MOUT__ << "ARTDAQAggregatorSupervisor supervisor " << (it.first) << " was " << command << "'d correctly!" << std::endl;
01503 }
01504 }
01505 catch(xdaq::exception::Exception &e)
01506 {
01507 if(artdaqRestarted && !artdaqWasRestarted)
01508 {
01509 artdaqWasRestarted = true;
01510 goto ARTDAQ_RETRY;
01511 }
01512 else
01513 throw;
01514 }
01515 }
01516
01517 return proceed;
01518 }
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547 void Supervisor::wait(int milliseconds, std::string who) const
01548 {
01549 for (int s = 1; s <= milliseconds; s++)
01550 {
01551 usleep(1000);
01552
01553 if (s % 100 == 0)
01554 __MOUT__ << s << " msecs " << who << std::endl;
01555 }
01556 }
01557
01558
01559
01560
01561
01562
01563 void Supervisor::loginRequest(xgi::Input * in, xgi::Output * out)
01564 throw (xgi::exception::Exception)
01565 {
01566 cgicc::Cgicc cgi(in);
01567 std::string Command = CgiDataUtilities::getData(cgi, "RequestType");
01568
01569 __MOUT__ << Command << std::endl;
01570
01571
01572
01573
01574
01575
01576
01577
01578 std::vector<std::string> loggedOutUsernames;
01579 theWebUsers_.cleanupExpiredEntries(&loggedOutUsernames);
01580 for (unsigned int i = 0; i < loggedOutUsernames.size(); ++i)
01581 makeSystemLogbookEntry(loggedOutUsernames[i] + " login timed out.");
01582
01583 if (Command == "sessionId")
01584 {
01585
01586
01587
01588
01589
01590 std::string uuid = CgiDataUtilities::postData(cgi, "uuid");
01591
01592 std::string sid = theWebUsers_.createNewLoginSession(uuid);
01593
01594 __MOUT__ << "uuid = " << uuid << std::endl;
01595 __MOUT__ << "SessionId = " << sid.substr(0, 10) << std::endl;
01596 *out << sid;
01597 }
01598 else if (Command == "checkCookie")
01599 {
01600 uint64_t uid;
01601 std::string uuid;
01602 std::string jumbledUser;
01603 std::string cookieCode;
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613 uuid = CgiDataUtilities::postData(cgi, "uuid");
01614 jumbledUser = CgiDataUtilities::postData(cgi, "ju");
01615 cookieCode = CgiDataUtilities::postData(cgi, "cc");
01616
01617 __MOUT__ << "Cookie Code = " << cookieCode.substr(0, 10) << std::endl;
01618 __MOUT__ << "jumbledUser = " << jumbledUser.substr(0, 10) << std::endl;
01619 __MOUT__ << "uuid = " << uuid << std::endl;
01620
01621
01622 uid = theWebUsers_.isCookieCodeActiveForLogin(uuid, cookieCode,
01623 jumbledUser);
01624
01625 if (uid == theWebUsers_.NOT_FOUND_IN_DATABASE)
01626 {
01627 __MOUT__ << "cookieCode invalid" << std::endl;
01628 jumbledUser = "";
01629 cookieCode = "0";
01630 }
01631
01632
01633 HttpXmlDocument xmldoc(cookieCode, jumbledUser);
01634
01635 theWebUsers_.insertSettingsForUser(uid, &xmldoc);
01636
01637 xmldoc.outputXmlDocument((std::ostringstream*) out);
01638
01639 }
01640 else if (Command == "login")
01641 {
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651 std::string uuid = CgiDataUtilities::postData(cgi, "uuid");
01652 std::string newAccountCode = CgiDataUtilities::postData(cgi, "nac");
01653 std::string jumbledUser = CgiDataUtilities::postData(cgi, "ju");
01654 std::string jumbledPw = CgiDataUtilities::postData(cgi, "jp");
01655
01656 __MOUT__ << "jumbledUser = " << jumbledUser.substr(0, 10) << std::endl;
01657 __MOUT__ << "jumbledPw = " << jumbledPw.substr(0, 10) << std::endl;
01658 __MOUT__ << "uuid = " << uuid << std::endl;
01659 __MOUT__ << "nac =-" << newAccountCode << "-" << std::endl;
01660
01661 uint64_t uid = theWebUsers_.attemptActiveSession(uuid, jumbledUser,
01662 jumbledPw, newAccountCode);
01663
01664
01665 if (uid == theWebUsers_.NOT_FOUND_IN_DATABASE)
01666 {
01667 __MOUT__ << "cookieCode invalid" << std::endl;
01668 jumbledUser = "";
01669 if (newAccountCode != "1")
01670 newAccountCode = "0";
01671 }
01672
01673 __MOUT__ << "new cookieCode = " << newAccountCode.substr(0, 10) << std::endl;
01674
01675 HttpXmlDocument xmldoc(newAccountCode, jumbledUser);
01676
01677 theWebUsers_.insertSettingsForUser(uid, &xmldoc);
01678
01679
01680
01681 if (uid != theWebUsers_.NOT_FOUND_IN_DATABASE)
01682 {
01683 uint64_t asCnt = theWebUsers_.getActiveSessionCountForUser(uid) - 1;
01684 char asStr[20];
01685 sprintf(asStr, "%lu", asCnt);
01686 xmldoc.addTextElementToData("user_active_session_count", asStr);
01687 }
01688
01689 xmldoc.outputXmlDocument((std::ostringstream*) out);
01690
01691
01692 makeSystemLogbookEntry(
01693 theWebUsers_.getUsersUsername(uid) + " logged in.");
01694 }
01695 else if (Command == "logout")
01696 {
01697 std::string cookieCode = CgiDataUtilities::postData(cgi, "CookieCode");
01698 std::string logoutOthers = CgiDataUtilities::postData(cgi,
01699 "LogoutOthers");
01700
01701 __MOUT__ << "Cookie Code = " << cookieCode.substr(0, 10) << std::endl;
01702 __MOUT__ << "logoutOthers = " << logoutOthers << std::endl;
01703
01704 uint64_t uid;
01705 if (theWebUsers_.cookieCodeLogout(cookieCode, logoutOthers == "1", &uid)
01706 != theWebUsers_.NOT_FOUND_IN_DATABASE)
01707 {
01708
01709
01710 if (!theWebUsers_.isUserIdActive(uid))
01711 makeSystemLogbookEntry(
01712 theWebUsers_.getUsersUsername(uid) + " logged out.");
01713 }
01714 }
01715 else
01716 {
01717 __MOUT__ << __LINE__ << "\tInvalid Command" << std::endl;
01718 *out << "0";
01719 }
01720 }
01721
01722
01723 void Supervisor::tooltipRequest(xgi::Input * in, xgi::Output * out)
01724 throw (xgi::exception::Exception)
01725 {
01726 cgicc::Cgicc cgi(in);
01727
01728 std::string Command = CgiDataUtilities::getData(cgi, "RequestType");
01729 __MOUT__ << Command << std::endl;
01730
01731
01732
01733
01734
01735 std::string cookieCode = CgiDataUtilities::postData(cgi, "CookieCode");
01736 uint8_t userPermissions;
01737 uint64_t uid;
01738
01739 if (!theWebUsers_.cookieCodeIsActiveForRequest(cookieCode, &userPermissions,
01740 &uid, "0", false))
01741 {
01742 *out << cookieCode;
01743 return;
01744 }
01745
01746
01747
01748 HttpXmlDocument xmldoc(cookieCode);
01749
01750 if(Command == "check")
01751 {
01752 WebUsers::tooltipCheckForUsername(
01753 theWebUsers_.getUsersUsername(uid),
01754 &xmldoc,
01755 CgiDataUtilities::getData(cgi, "srcFile"),
01756 CgiDataUtilities::getData(cgi, "srcFunc"),
01757 CgiDataUtilities::getData(cgi, "srcId"));
01758 }
01759 else if(Command == "setNeverShow")
01760 {
01761 WebUsers::tooltipSetNeverShowForUsername(
01762 theWebUsers_.getUsersUsername(uid),
01763 &xmldoc,
01764 CgiDataUtilities::getData(cgi, "srcFile"),
01765 CgiDataUtilities::getData(cgi, "srcFunc"),
01766 CgiDataUtilities::getData(cgi, "srcId"),
01767 CgiDataUtilities::getData(cgi, "doNeverShow") == "1"?true:false,
01768 CgiDataUtilities::getData(cgi, "temporarySilence") == "1"?true:false);
01769
01770 }
01771 else
01772 __MOUT__ << "Command Request, " << Command << ", not recognized." << std::endl;
01773
01774 xmldoc.outputXmlDocument((std::ostringstream*) out, false, true);
01775 }
01776
01777
01778 void Supervisor::request(xgi::Input * in, xgi::Output * out)
01779 throw (xgi::exception::Exception)
01780 {
01781 cgicc::Cgicc cgi(in);
01782
01783 std::string Command = CgiDataUtilities::getData(cgi, "RequestType");
01784
01785
01786
01787
01788
01789
01790 std::string cookieCode = CgiDataUtilities::postData(cgi, "CookieCode");
01791 uint8_t userPermissions;
01792 uint64_t uid;
01793 std::string userWithLock;
01794
01795 if (!theWebUsers_.cookieCodeIsActiveForRequest(cookieCode, &userPermissions,
01796 &uid, "0", Command != "getSystemMessages", &userWithLock))
01797 {
01798 *out << cookieCode;
01799 return;
01800 }
01801
01802
01803
01804
01805
01806
01807
01808
01809
01810
01811
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821 HttpXmlDocument xmldoc(cookieCode);
01822
01823 if (Command == "getSettings")
01824 {
01825 std::string accounts = CgiDataUtilities::getData(cgi, "accounts");
01826
01827 __MOUT__ << "Get Settings Request" << std::endl;
01828 __MOUT__ << "accounts = " << accounts << std::endl;
01829 theWebUsers_.insertSettingsForUser(uid, &xmldoc, accounts == "1");
01830 }
01831 else if (Command == "setSettings")
01832 {
01833 std::string bgcolor = CgiDataUtilities::postData(cgi, "bgcolor");
01834 std::string dbcolor = CgiDataUtilities::postData(cgi, "dbcolor");
01835 std::string wincolor = CgiDataUtilities::postData(cgi, "wincolor");
01836 std::string layout = CgiDataUtilities::postData(cgi, "layout");
01837 std::string syslayout = CgiDataUtilities::postData(cgi, "syslayout");
01838
01839 __MOUT__ << "Set Settings Request" << std::endl;
01840 __MOUT__ << "bgcolor = " << bgcolor << std::endl;
01841 __MOUT__ << "dbcolor = " << dbcolor << std::endl;
01842 __MOUT__ << "wincolor = " << wincolor << std::endl;
01843 __MOUT__ << "layout = " << layout << std::endl;
01844 __MOUT__ << "syslayout = " << syslayout << std::endl;
01845 theWebUsers_.changeSettingsForUser(uid, bgcolor, dbcolor, wincolor,
01846 layout, syslayout);
01847 theWebUsers_.insertSettingsForUser(uid, &xmldoc, true);
01848 }
01849 else if (Command == "accountSettings")
01850 {
01851 std::string type = CgiDataUtilities::postData(cgi, "type");
01852 int type_int = -1;
01853
01854 if (type == "updateAccount")
01855 type_int = 0;
01856 else if (type == "createAccount")
01857 type_int = 1;
01858 else if (type == "deleteAccount")
01859 type_int = 2;
01860
01861 std::string username = CgiDataUtilities::postData(cgi, "username");
01862 std::string displayname = CgiDataUtilities::postData(cgi,
01863 "displayname");
01864 std::string permissions = CgiDataUtilities::postData(cgi,
01865 "permissions");
01866 std::string accounts = CgiDataUtilities::getData(cgi, "accounts");
01867
01868 __MOUT__ << "accountSettings Request" << std::endl;
01869 __MOUT__ << "type = " << type << " - " << type_int << std::endl;
01870 __MOUT__ << "username = " << username << std::endl;
01871 __MOUT__ << "displayname = " << displayname << std::endl;
01872 __MOUT__ << "permissions = " << permissions << std::endl;
01873
01874 theWebUsers_.modifyAccountSettings(uid, type_int, username, displayname,
01875 permissions);
01876
01877 __MOUT__ << "accounts = " << accounts << std::endl;
01878
01879 theWebUsers_.insertSettingsForUser(uid, &xmldoc, accounts == "1");
01880 }
01881 else if(Command == "stateMatchinePreferences")
01882 {
01883 std::string set = CgiDataUtilities::getData(cgi, "set");
01884 const std::string DEFAULT_FSM_VIEW = "Default_FSM_View";
01885 if(set == "1")
01886 theWebUsers_.setGenericPreference(uid, DEFAULT_FSM_VIEW,
01887 CgiDataUtilities::getData(cgi, DEFAULT_FSM_VIEW));
01888 else
01889 theWebUsers_.getGenericPreference(uid, DEFAULT_FSM_VIEW, &xmldoc);
01890 }
01891 else if(Command == "getAliasList")
01892 {
01893 std::string username = theWebUsers_.getUsersUsername(uid);
01894 std::string fsmName = CgiDataUtilities::getData(cgi, "fsmName");
01895 __MOUT__ << "fsmName = " << fsmName << std::endl;
01896
01897 std::string stateMachineAliasFilter = "*";
01898
01899 std::map<std::string ,
01900 std::pair<std::string , ConfigurationGroupKey> > aliasMap =
01901 theConfigurationManager_->getGroupAliasesConfiguration();
01902
01903
01904
01905 ConfigurationTree configLinkNode = theConfigurationManager_->getSupervisorConfigurationNode(
01906 supervisorContextUID_, supervisorApplicationUID_);
01907
01908 if(!configLinkNode.isDisconnected())
01909 {
01910 try
01911 {
01912 ConfigurationTree fsmLinkNode = configLinkNode.getNode("LinkToStateMachineConfiguration");
01913 if(!fsmLinkNode.isDisconnected())
01914 stateMachineAliasFilter =
01915 fsmLinkNode.getNode(fsmName + "/SystemAliasFilter").getValue<std::string>();
01916 else
01917 __MOUT_INFO__ << "FSM Link disconnected." << std::endl;
01918 }
01919 catch(std::runtime_error &e) { __MOUT_INFO__ << e.what() << std::endl; }
01920 catch(...) { __MOUT_ERR__ << "Unknown error. Should never happen." << std::endl; }
01921 }
01922 else
01923 __MOUT_INFO__ << "FSM Link disconnected." << std::endl;
01924
01925 __MOUT__ << "stateMachineAliasFilter = " << stateMachineAliasFilter << std::endl;
01926
01927
01928
01929
01930
01931 {
01932 bool invertFilter = stateMachineAliasFilter.size() && stateMachineAliasFilter[0] == '!';
01933 std::vector<std::string> filterArr;
01934
01935 size_t i = 0;
01936 if(invertFilter) ++i;
01937 size_t f;
01938 std::string tmp;
01939 while((f = stateMachineAliasFilter.find('*',i)) != std::string::npos)
01940 {
01941 tmp = stateMachineAliasFilter.substr(i,f-i);
01942 i = f+1;
01943 filterArr.push_back(tmp);
01944
01945
01946
01947 }
01948 if(i <= stateMachineAliasFilter.size())
01949 {
01950 tmp = stateMachineAliasFilter.substr(i);
01951 filterArr.push_back(tmp);
01952
01953 }
01954
01955
01956 bool filterMatch;
01957
01958
01959 for(auto& aliasMapPair : aliasMap)
01960 {
01961
01962
01963 filterMatch = true;
01964
01965 if(filterArr.size() == 1)
01966 {
01967 if(filterArr[0] != "" &&
01968 filterArr[0] != "*" &&
01969 aliasMapPair.first != filterArr[0])
01970 filterMatch = false;
01971 }
01972 else
01973 {
01974 i = -1;
01975 for(f=0;f<filterArr.size();++f)
01976 {
01977 if(!filterArr[f].size()) continue;
01978
01979 if(f == 0)
01980 {
01981 if((i = aliasMapPair.first.find(filterArr[f])) != 0)
01982 {
01983 filterMatch = false;
01984 break;
01985 }
01986 }
01987 else if(f == filterArr.size()-1)
01988 {
01989 if(aliasMapPair.first.rfind(filterArr[f]) !=
01990 aliasMapPair.first.size() - filterArr[f].size())
01991 {
01992 filterMatch = false;
01993 break;
01994 }
01995 }
01996 else if((i = aliasMapPair.first.find(filterArr[f])) ==
01997 std::string::npos)
01998 {
01999 filterMatch = false;
02000 break;
02001 }
02002 }
02003 }
02004
02005 if(invertFilter) filterMatch = !filterMatch;
02006
02007
02008
02009 if(!filterMatch) continue;
02010
02011 xmldoc.addTextElementToData("config_alias", aliasMapPair.first);
02012 xmldoc.addTextElementToData("config_key",
02013 ConfigurationGroupKey::getFullGroupString(aliasMapPair.second.first,
02014 aliasMapPair.second.second).c_str());
02015 }
02016 }
02017
02018
02019 std::string fn = FSM_LAST_GROUP_ALIAS_PATH + FSM_LAST_GROUP_ALIAS_FILE_START +
02020 username + "." + FSM_USERS_PREFERENCES_FILETYPE;
02021 __MOUT__ << "Load preferences: " << fn << std::endl;
02022 FILE *fp = fopen(fn.c_str(),"r");
02023 if(fp)
02024 {
02025 char tmpLastAlias[500];
02026 fscanf(fp,"%*s %s",tmpLastAlias);
02027 __MOUT__ << "tmpLastAlias: " << tmpLastAlias << std::endl;
02028
02029 xmldoc.addTextElementToData("UserLastConfigAlias",tmpLastAlias);
02030 fclose(fp);
02031 }
02032 }
02033 else if (Command == "getFecList")
02034 {
02035 xmldoc.addTextElementToData("fec_list", "");
02036
02037 for (unsigned int i = 0; i< theSupervisorDescriptorInfo_.getFEDescriptors().size(); ++i)
02038 {
02039 xmldoc.addTextElementToParent("fec_url",
02040 theSupervisorDescriptorInfo_.getFEURL(i), "fec_list");
02041 xmldoc.addTextElementToParent(
02042 "fec_urn",
02043 theSupervisorDescriptorInfo_.getFEDescriptor(i)->getURN(),
02044 "fec_list");
02045 }
02046 }
02047 else if (Command == "getSystemMessages")
02048 {
02049 xmldoc.addTextElementToData("systemMessages",
02050 theSysMessenger_.getSysMsg(
02051 theWebUsers_.getUsersDisplayName(uid)));
02052
02053 xmldoc.addTextElementToData("username_with_lock",
02054 theWebUsers_.getUserWithLock());
02055
02056
02057 }
02058 else if (Command == "setUserWithLock")
02059 {
02060 std::string username = CgiDataUtilities::postData(cgi, "username");
02061 std::string lock = CgiDataUtilities::postData(cgi, "lock");
02062 std::string accounts = CgiDataUtilities::getData(cgi, "accounts");
02063
02064 __MOUT__ << Command << std::endl;
02065 __MOUT__ << "username " << username << std::endl;
02066 __MOUT__ << "lock " << lock << std::endl;
02067 __MOUT__ << "accounts " << accounts << std::endl;
02068 __MOUT__ << "uid " << uid << std::endl;
02069
02070 std::string tmpUserWithLock = theWebUsers_.getUserWithLock();
02071 if(!theWebUsers_.setUserWithLock(uid, lock == "1", username))
02072 xmldoc.addTextElementToData("server_alert",
02073 std::string("Set user lock action failed. You must have valid permissions and ") +
02074 "locking user must be currently logged in.");
02075
02076 theWebUsers_.insertSettingsForUser(uid, &xmldoc, accounts == "1");
02077
02078 if (tmpUserWithLock != theWebUsers_.getUserWithLock())
02079 theSysMessenger_.addSysMsg("*", theWebUsers_.getUserWithLock()
02080 == "" ? tmpUserWithLock + " has unlocked ots."
02081 : theWebUsers_.getUserWithLock()
02082 + " has locked ots.");
02083 }
02084 else if (Command == "getStateMachine")
02085 {
02086
02087 std::vector<toolbox::fsm::State> states;
02088 states = theStateMachine_.getStates();
02089 char stateStr[2];
02090 stateStr[1] = '\0';
02091 std::string transName;
02092 std::string transParameter;
02093 for (unsigned int i = 0; i < states.size(); ++i)
02094 {
02095 stateStr[0] = states[i];
02096 DOMElement* stateParent = xmldoc.addTextElementToData("state", stateStr);
02097
02098 xmldoc.addTextElementToParent("state_name", theStateMachine_.getStateName(states[i]), stateParent);
02099
02100
02101
02102
02103 std::map<std::string, toolbox::fsm::State, std::less<std::string> >
02104 trans = theStateMachine_.getTransitions(states[i]);
02105 std::set<std::string> actionNames = theStateMachine_.getInputs(states[i]);
02106
02107 std::map<std::string, toolbox::fsm::State, std::less<std::string> >::iterator
02108 it = trans.begin();
02109 std::set<std::string>::iterator ait = actionNames.begin();
02110 for (; it != trans.end() && ait != actionNames.end(); ++it, ++ait)
02111 {
02112
02113
02114 stateStr[0] = it->second;
02115 xmldoc.addTextElementToParent("state_transition", stateStr, stateParent);
02116
02117
02118
02119 xmldoc.addTextElementToParent("state_transition_action", *ait, stateParent);
02120
02121 transName = theStateMachine_.getTransitionName(states[i], *ait);
02122
02123
02124 xmldoc.addTextElementToParent("state_transition_name",
02125 transName, stateParent);
02126 transParameter = theStateMachine_.getTransitionParameter(states[i], *ait);
02127
02128
02129 xmldoc.addTextElementToParent("state_transition_parameter", transParameter, stateParent);
02130 }
02131 }
02132
02133 }
02134 else if (Command == "getCurrentState")
02135 {
02136 xmldoc.addTextElementToData("current_state", theStateMachine_.getCurrentStateName());
02137 xmldoc.addTextElementToData("in_transition", theStateMachine_.isInTransition() ? "1" : "0");
02138 if (theStateMachine_.isInTransition())
02139 xmldoc.addTextElementToData("transition_progress", theProgressBar_.readPercentageString());
02140 else
02141 xmldoc.addTextElementToData("transition_progress", "100");
02142
02143
02144 char tmp[20];
02145 sprintf(tmp,"%lu",theStateMachine_.getTimeInState());
02146 xmldoc.addTextElementToData("time_in_state", tmp);
02147
02148
02149
02150
02151
02152
02154 std::string fsmName = CgiDataUtilities::getData(cgi, "fsmName");
02155
02156
02157
02158
02159
02160
02161
02162 if(!theStateMachine_.isInTransition())
02163 {
02164 std::string stateMachineRunAlias = "Run";
02165
02166
02167 ConfigurationTree configLinkNode = theConfigurationManager_->getSupervisorConfigurationNode(
02168 supervisorContextUID_, supervisorApplicationUID_);
02169
02170 if(!configLinkNode.isDisconnected())
02171 {
02172 try
02173 {
02174 ConfigurationTree fsmLinkNode = configLinkNode.getNode("LinkToStateMachineConfiguration");
02175 if(!fsmLinkNode.isDisconnected())
02176 stateMachineRunAlias =
02177 fsmLinkNode.getNode(fsmName + "/RunDisplayAlias").getValue<std::string>();
02178
02179
02180 }
02181 catch(std::runtime_error &e) { __MOUT_INFO__ << e.what() << std::endl; }
02182 catch(...) { __MOUT_ERR__ << "Unknown error. Should never happen." << std::endl; }
02183 }
02184
02185
02186
02187
02188
02189 xmldoc.addTextElementToData("stateMachineRunAlias", stateMachineRunAlias);
02191
02192
02193
02194 if(theStateMachine_.getCurrentStateName() == "Running" ||
02195 theStateMachine_.getCurrentStateName() == "Paused")
02196 sprintf(tmp,"Current %s Number: %u",stateMachineRunAlias.c_str(),getNextRunNumber(activeStateMachineName_)-1);
02197 else
02198 sprintf(tmp,"Next %s Number: %u",stateMachineRunAlias.c_str(),getNextRunNumber(fsmName));
02199 xmldoc.addTextElementToData("run_number", tmp);
02200 }
02201 }
02202 else if(Command == "getErrorInStateMatchine")
02203 {
02204 xmldoc.addTextElementToData("FSM_Error", theStateMachine_.getErrorMessage());
02205 }
02206 else if(Command == "getDesktopIcons")
02207 {
02208
02209 std::string iconFileName = ICON_FILE_NAME;
02210 std::ifstream iconFile;
02211 std::string iconList = "";
02212 std::string line;
02213 iconFile.open(iconFileName.c_str());
02214
02215 if(!iconFile)
02216 {
02217 __MOUT__ << "Error opening file: "<< iconFileName << std::endl;
02218 system("pause");
02219 return;
02220 }
02221 if(iconFile.is_open())
02222 {
02223 __MOUT__ << "Opened File: " << iconFileName << std::endl;
02224 while(std::getline(iconFile, line))
02225 {
02226 iconList = line;
02227 }
02228 __MOUT__ << iconList << std::endl;
02229
02230
02231 iconFile.close();
02232 }
02233 xmldoc.addTextElementToData("iconList", iconList);
02234
02235 }
02236 else if(Command == "launchConfig")
02237 {
02238 if(userPermissions != 255)
02239 {
02240 __MOUT__ << "Insufficient Permissions" << std::endl;
02241 }
02242 else
02243 {
02244 __MOUT__ << "Self-destruct." << std::endl;
02245
02246
02247 }
02248 }
02249 else if(Command == "resetUserTooltips")
02250 {
02251 WebUsers::resetAllUserTooltips(theWebUsers_.getUsersUsername(uid));
02252 }
02253 else
02254 __MOUT__ << "Command Request, " << Command << ", not recognized." << std::endl;
02255
02256
02257
02258
02259 xmldoc.outputXmlDocument((std::ostringstream*) out, false, true);
02260
02261
02262 }
02263
02264
02265
02266
02267 xoap::MessageReference Supervisor::supervisorGetUserInfo(
02268 xoap::MessageReference message)
02269 throw (xoap::exception::Exception)
02270 {
02271 SOAPParameters parameters;
02272 parameters.addParameter("CookieCode");
02273 receive(message, parameters);
02274 std::string cookieCode = parameters.getValue("CookieCode");
02275
02276 std::string username, displayName;
02277 uint64_t activeSessionIndex;
02278
02279 theWebUsers_.getUserInfoForCookie(cookieCode, &username, &displayName,
02280 &activeSessionIndex);
02281
02282
02283
02284
02285
02286 SOAPParameters retParameters;
02287 retParameters.addParameter("Username", username);
02288 retParameters.addParameter("DisplayName", displayName);
02289 char tmpStr[100];
02290 sprintf(tmpStr, "%lu", activeSessionIndex);
02291 retParameters.addParameter("ActiveSessionIndex", tmpStr);
02292
02293 return SOAPUtilities::makeSOAPMessageReference("UserInfoResponse",
02294 retParameters);
02295 }
02296
02297
02298
02299
02300 xoap::MessageReference Supervisor::supervisorCookieCheck(xoap::MessageReference message)
02301 throw (xoap::exception::Exception)
02302 {
02303
02304
02305
02306 SOAPParameters parameters;
02307 parameters.addParameter("CookieCode");
02308 parameters.addParameter("RefreshOption");
02309 receive(message, parameters);
02310 std::string cookieCode = parameters.getValue("CookieCode");
02311 std::string refreshOption = parameters.getValue("RefreshOption");
02312
02313
02314
02315 uint8_t userPermissions = 0;
02316 std::string userWithLock = "";
02317 theWebUsers_.cookieCodeIsActiveForRequest(cookieCode, &userPermissions, 0,
02318 "0", refreshOption == "1", &userWithLock);
02319
02320
02321
02322
02323 SOAPParameters retParameters;
02324 retParameters.addParameter("CookieCode", cookieCode);
02325 char tmp[5];
02326 sprintf(tmp, "%d", userPermissions);
02327 retParameters.addParameter("Permissions", tmp);
02328 retParameters.addParameter("UserWithLock", userWithLock);
02329
02330
02331
02332 return SOAPUtilities::makeSOAPMessageReference("CookieResponse",
02333 retParameters);
02334 }
02335
02336
02337
02338
02339 xoap::MessageReference Supervisor::supervisorGetActiveUsers(
02340 xoap::MessageReference message)
02341 throw (xoap::exception::Exception)
02342 {
02343 __MOUT__ << std::endl;
02344
02345 SOAPParameters
02346 parameters("UserList", theWebUsers_.getActiveUsersString());
02347 return SOAPUtilities::makeSOAPMessageReference("ActiveUserResponse",
02348 parameters);
02349 }
02350
02351
02352
02353
02354
02355 xoap::MessageReference Supervisor::supervisorSystemMessage(
02356 xoap::MessageReference message)
02357 throw (xoap::exception::Exception)
02358 {
02359 SOAPParameters parameters;
02360 parameters.addParameter("ToUser");
02361 parameters.addParameter("Message");
02362 receive(message, parameters);
02363
02364 __MOUT__ << "toUser: " << parameters.getValue("ToUser").substr(
02365 0, 10) << ", message: " << parameters.getValue("Message").substr(0,
02366 10) << std::endl;
02367
02368 theSysMessenger_.addSysMsg(parameters.getValue("ToUser"),
02369 parameters.getValue("Message"));
02370 return SOAPUtilities::makeSOAPMessageReference("SystemMessageResponse");
02371 }
02372
02373
02374
02375
02376
02377 xoap::MessageReference Supervisor::supervisorSystemLogbookEntry(
02378 xoap::MessageReference message)
02379 throw (xoap::exception::Exception)
02380 {
02381 SOAPParameters parameters;
02382 parameters.addParameter("EntryText");
02383 receive(message, parameters);
02384
02385 __MOUT__ << "EntryText: " << parameters.getValue("EntryText").substr(
02386 0, 10) << std::endl;
02387
02388 makeSystemLogbookEntry(parameters.getValue("EntryText"));
02389
02390 return SOAPUtilities::makeSOAPMessageReference("SystemLogbookResponse");
02391 }
02392
02393
02394
02395
02396
02397
02398 xoap::MessageReference Supervisor::supervisorLastConfigGroupRequest(
02399 xoap::MessageReference message)
02400 throw (xoap::exception::Exception)
02401 {
02402 SOAPParameters parameters;
02403 parameters.addParameter("ActionOfLastGroup");
02404 receive(message, parameters);
02405
02406 return Supervisor::lastConfigGroupRequestHandler(parameters);
02407 }
02408
02409
02410
02411
02412
02413
02414
02415 xoap::MessageReference Supervisor::lastConfigGroupRequestHandler(
02416 const SOAPParameters ¶meters)
02417 {
02418 std::string action = parameters.getValue("ActionOfLastGroup");
02419 __MOUT__ << "ActionOfLastGroup: " << action.substr(
02420 0, 10) << std::endl;
02421
02422 std::string fileName = "";
02423 if(action == "Configured")
02424 fileName = FSM_LAST_CONFIGURED_GROUP_ALIAS_FILE;
02425 else if(action == "Started")
02426 fileName = FSM_LAST_STARTED_GROUP_ALIAS_FILE;
02427 else
02428 {
02429 __MOUT_ERR__ << "Invalid last group action requested." << std::endl;
02430 return SOAPUtilities::makeSOAPMessageReference("LastConfigGroupResponseFailure");
02431 }
02432 std::string timeString;
02433 std::pair<std::string , ConfigurationGroupKey> theGroup =
02434 loadGroupNameAndKey(fileName,timeString);
02435
02436
02437 SOAPParameters retParameters;
02438 retParameters.addParameter("GroupName", theGroup.first);
02439 retParameters.addParameter("GroupKey", theGroup.second.toString());
02440 retParameters.addParameter("GroupAction", action);
02441 retParameters.addParameter("GroupActionTime", timeString);
02442
02443
02444 return SOAPUtilities::makeSOAPMessageReference("LastConfigGroupResponse",
02445 retParameters);
02446 }
02447
02448
02449
02450
02451
02452
02453
02454
02455 unsigned int Supervisor::getNextRunNumber(const std::string &fsmNameIn)
02456 {
02457 std::string runNumberFileName = RUN_NUMBER_PATH + "/";
02458 std::string fsmName = fsmNameIn == ""?activeStateMachineName_:fsmNameIn;
02459
02460 for(unsigned int i=0;i<fsmName.size();++i)
02461 if( (fsmName[i] >= 'a' && fsmName[i] <= 'z') ||
02462 (fsmName[i] >= 'A' && fsmName[i] <= 'Z') ||
02463 (fsmName[i] >= '0' && fsmName[i] <= '9'))
02464 runNumberFileName += fsmName[i];
02465 runNumberFileName += RUN_NUMBER_FILE_NAME;
02466
02467
02468 std::ifstream runNumberFile(runNumberFileName.c_str());
02469 if (!runNumberFile.is_open())
02470 {
02471 __MOUT__ << "Can't open file: " << runNumberFileName << std::endl;
02472
02473 __MOUT__ << "Creating file and setting Run Number to 1: " << runNumberFileName << std::endl;
02474 FILE *fp = fopen(runNumberFileName.c_str(),"w");
02475 fprintf(fp,"1");
02476 fclose(fp);
02477
02478 runNumberFile.open(runNumberFileName.c_str());
02479 if(!runNumberFile.is_open())
02480 {
02481 __MOUT__ << "Can't create file: " << runNumberFileName << std::endl;
02482 throw std::runtime_error("Error.");
02483 }
02484 }
02485 std::string runNumberString;
02486 runNumberFile >> runNumberString;
02487 runNumberFile.close();
02488 return atoi(runNumberString.c_str());
02489 }
02490
02491
02492 bool Supervisor::setNextRunNumber(unsigned int runNumber, const std::string &fsmNameIn)
02493 {
02494 std::string runNumberFileName = RUN_NUMBER_PATH + "/";
02495 std::string fsmName = fsmNameIn == ""?activeStateMachineName_:fsmNameIn;
02496
02497 for(unsigned int i=0;i<fsmName.size();++i)
02498 if( (fsmName[i] >= 'a' && fsmName[i] <= 'z') ||
02499 (fsmName[i] >= 'A' && fsmName[i] <= 'Z') ||
02500 (fsmName[i] >= '0' && fsmName[i] <= '9'))
02501 runNumberFileName += fsmName[i];
02502 runNumberFileName += RUN_NUMBER_FILE_NAME;
02503 __MOUT__ << "runNumberFileName: " << runNumberFileName << std::endl;
02504
02505 std::ofstream runNumberFile(runNumberFileName.c_str());
02506 if (!runNumberFile.is_open())
02507 {
02508 __MOUT__ << "Can't open file: " << runNumberFileName << std::endl;
02509 throw std::runtime_error("Error.");
02510 }
02511 std::stringstream runNumberStream;
02512 runNumberStream << runNumber;
02513 runNumberFile << runNumberStream.str().c_str();
02514 runNumberFile.close();
02515 return true;
02516 }
02517
02518
02519
02520
02521
02522
02523
02524 std::pair<std::string ,
02525 ConfigurationGroupKey> Supervisor::loadGroupNameAndKey(const std::string &fileName,
02526 std::string &returnedTimeString)
02527 {
02528 std::string fullPath = FSM_LAST_GROUP_ALIAS_PATH + "/" + fileName;
02529
02530 FILE *groupFile = fopen(fullPath.c_str(),"r");
02531 if (!groupFile)
02532 {
02533 __MOUT__ << "Can't open file: " << fullPath << std::endl;
02534
02535 __MOUT__ << "Returning empty groupName and key -1" << std::endl;
02536
02537 return std::pair<std::string ,
02538 ConfigurationGroupKey>("",ConfigurationGroupKey());
02539 }
02540
02541 char line[500];
02542
02543 std::pair<std::string ,
02544 ConfigurationGroupKey> theGroup;
02545
02546 fgets(line,500,groupFile);
02547 theGroup.first = line;
02548
02549 fgets(line,500,groupFile);
02550 int key;
02551 sscanf(line,"%d",&key);
02552 theGroup.second = key;
02553
02554 fgets(line,500,groupFile);
02555 time_t timestamp;
02556 sscanf(line,"%ld",×tamp);
02557 struct tm tmstruct;
02558 ::localtime_r(×tamp, &tmstruct);
02559 ::strftime(line, 30, "%c %Z", &tmstruct);
02560 returnedTimeString = line;
02561 fclose(groupFile);
02562
02563
02564 __MOUT__ << "theGroup.first= " << theGroup.first <<
02565 " theGroup.second= " << theGroup.second << std::endl;
02566
02567 return theGroup;
02568 }
02569
02570
02571 void Supervisor::saveGroupNameAndKey(const std::pair<std::string ,
02572 ConfigurationGroupKey> &theGroup,
02573 const std::string &fileName)
02574 {
02575 std::string fullPath = FSM_LAST_GROUP_ALIAS_PATH + "/" + fileName;
02576
02577 std::ofstream groupFile(fullPath.c_str());
02578 if (!groupFile.is_open())
02579 {
02580 __SS__ << "Can't open file: " << fullPath << std::endl;
02581 __MOUT_ERR__ << "\n" << ss.str();
02582 throw std::runtime_error("Error.\n" + ss.str());
02583 }
02584 std::stringstream outss;
02585 outss << theGroup.first << "\n" << theGroup.second << "\n" << time(0);
02586 groupFile << outss.str().c_str();
02587 groupFile.close();
02588 }
02589
02590
02591
02593
02594
02597
02598
02599
02600
02601
02602
02603
02604
02605
02606
02607
02608
02609
02610
02611
02612
02613
02614
02615
02616
02617
02618
02619
02620
02621
02622
02623