$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #ifndef _ots_GatewaySupervisor_h 00002 #define _ots_GatewaySupervisor_h 00003 00004 #include "otsdaq-core/CoreSupervisors/CorePropertySupervisorBase.h" 00005 #include "otsdaq-core/FiniteStateMachine/RunControlStateMachine.h" 00006 #include "otsdaq-core/GatewaySupervisor/ARTDAQCommandable.h" 00007 #include "otsdaq-core/GatewaySupervisor/Iterator.h" 00008 #include "otsdaq-core/SOAPUtilities/SOAPMessenger.h" 00009 #include "otsdaq-core/SupervisorInfo/AllSupervisorInfo.h" 00010 #include "otsdaq-core/SystemMessenger/SystemMessenger.h" 00011 #include "otsdaq-core/TableCore/TableGroupKey.h" 00012 #include "otsdaq-core/WebUsersUtilities/WebUsers.h" 00013 #include "otsdaq-core/WorkLoopManager/WorkLoopManager.h" 00014 00015 #include "otsdaq-core/CodeEditor/CodeEditor.h" 00016 00017 #pragma GCC diagnostic push 00018 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 00019 #include <xdaq/Application.h> 00020 #pragma GCC diagnostic pop 00021 #include "otsdaq-core/Macros/XDAQApplicationMacros.h" 00022 //#include <toolbox/fsm/FiniteStateMachine.h> 00023 #include <toolbox/task/WorkLoop.h> 00024 #include <xdata/String.h> 00025 #include <xgi/Method.h> 00026 00027 #include <set> 00028 #include <string> 00029 00030 // defines used also by OtsConfigurationWizardSupervisor 00031 #define FSM_LAST_CONFIGURED_GROUP_ALIAS_FILE \ 00032 std::string("FSMLastConfiguredGroupAlias.hist") 00033 #define FSM_LAST_STARTED_GROUP_ALIAS_FILE std::string("FSMLastStartedGroupAlias.hist") 00034 00035 namespace ots 00036 { 00037 class ConfigurationManager; 00038 class TableGroupKey; 00039 class WorkLoopManager; 00040 00041 // GatewaySupervisor 00042 // This class is the gateway server for all otsdaq requests in "Normal Mode." It 00043 // validates user access for every request. It synchronizes the state machines of all 00044 // other supervisors. 00045 class GatewaySupervisor : public xdaq::Application, 00046 public SOAPMessenger, 00047 public RunControlStateMachine, 00048 public CorePropertySupervisorBase 00049 { 00050 friend class WizardSupervisor; 00051 friend class Iterator; 00052 friend class ARTDAQCommandable; 00053 00054 public: 00055 XDAQ_INSTANTIATOR(); 00056 00057 GatewaySupervisor(xdaq::ApplicationStub* s); 00058 virtual ~GatewaySupervisor(void); 00059 00060 void init(void); 00061 00062 void Default(xgi::Input* in, xgi::Output* out); 00063 // void TmpTest (xgi::Input* in, 00064 // xgi::Output* out 00065 // ) 00066 // ; 00067 00068 void loginRequest(xgi::Input* in, xgi::Output* out); 00069 void request(xgi::Input* in, xgi::Output* out); 00070 void tooltipRequest(xgi::Input* in, xgi::Output* out); 00071 00072 // State Machine requests handlers 00073 00074 void stateMachineXgiHandler(xgi::Input* in, xgi::Output* out); 00075 // void stateMachineResultXgiHandler (xgi::Input* in, 00076 // xgi::Output* out 00077 // ) 00078 // ; 00079 00080 xoap::MessageReference stateMachineXoapHandler(xoap::MessageReference msg); 00081 xoap::MessageReference stateMachineResultXoapHandler(xoap::MessageReference msg); 00082 00083 bool stateMachineThread(toolbox::task::WorkLoop* workLoop); 00084 00085 // Status requests handlers 00086 void infoRequestHandler(xgi::Input* in, xgi::Output* out); 00087 void infoRequestResultHandler(xgi::Input* in, xgi::Output* out); 00088 bool infoRequestThread(toolbox::task::WorkLoop* workLoop); 00089 00090 // External GatewaySupervisor XOAP handlers 00091 xoap::MessageReference supervisorCookieCheck(xoap::MessageReference msg); 00092 xoap::MessageReference supervisorGetActiveUsers(xoap::MessageReference msg); 00093 xoap::MessageReference supervisorSystemMessage(xoap::MessageReference msg); 00094 xoap::MessageReference supervisorGetUserInfo(xoap::MessageReference msg); 00095 xoap::MessageReference supervisorSystemLogbookEntry(xoap::MessageReference msg); 00096 xoap::MessageReference supervisorLastConfigGroupRequest(xoap::MessageReference msg); 00097 00098 // Finite State Machine States 00099 void stateInitial(toolbox::fsm::FiniteStateMachine& fsm); 00100 void statePaused(toolbox::fsm::FiniteStateMachine& fsm); 00101 void stateRunning(toolbox::fsm::FiniteStateMachine& fsm); 00102 void stateHalted(toolbox::fsm::FiniteStateMachine& fsm); 00103 void stateConfigured(toolbox::fsm::FiniteStateMachine& fsm); 00104 void inError(toolbox::fsm::FiniteStateMachine& fsm); 00105 00106 void transitionConfiguring(toolbox::Event::Reference e); 00107 void transitionHalting(toolbox::Event::Reference e); 00108 void transitionInitializing(toolbox::Event::Reference e); 00109 void transitionPausing(toolbox::Event::Reference e); 00110 void transitionResuming(toolbox::Event::Reference e); 00111 void transitionStarting(toolbox::Event::Reference e); 00112 void transitionStopping(toolbox::Event::Reference e); 00113 void transitionShuttingDown(toolbox::Event::Reference e); 00114 void transitionStartingUp(toolbox::Event::Reference e); 00115 void enteringError(toolbox::Event::Reference e); 00116 00117 void makeSystemLogbookEntry(std::string entryText); 00118 00119 void checkForAsyncError(void); 00120 00121 // CorePropertySupervisorBase override functions 00122 virtual void setSupervisorPropertyDefaults( 00123 void) override; // override to control supervisor specific defaults 00124 virtual void forceSupervisorPropertyValues(void) override; // override to force 00125 // supervisor property 00126 // values (and ignore user 00127 // settings) 00128 00129 private: 00130 unsigned int getNextRunNumber(const std::string& fsmName = ""); 00131 bool setNextRunNumber(unsigned int runNumber, const std::string& fsmName = ""); 00132 static std::pair<std::string /*group name*/, TableGroupKey> loadGroupNameAndKey( 00133 const std::string& fileName, std::string& returnedTimeString); 00134 void saveGroupNameAndKey( 00135 const std::pair<std::string /*group name*/, TableGroupKey>& theGroup, 00136 const std::string& fileName); 00137 static xoap::MessageReference lastConfigGroupRequestHandler( 00138 const SOAPParameters& parameters); 00139 static void launchStartOTSCommand(const std::string& command, 00140 ConfigurationManager* cfgMgr); 00141 static void indicateOtsAlive(const CorePropertySupervisorBase* properties = 0); 00142 00143 static void StateChangerWorkLoop(GatewaySupervisor* supervisorPtr); 00144 std::string attemptStateMachineTransition(HttpXmlDocument* xmldoc, 00145 std::ostringstream* out, 00146 const std::string& command, 00147 const std::string& fsmName, 00148 const std::string& fsmWindowName, 00149 const std::string& username, 00150 const std::vector<std::string>& parameters); 00151 void broadcastMessage(xoap::MessageReference msg); 00152 00153 struct BroadcastMessageIterationsDoneStruct 00154 { 00155 // Creating std::vector<std::vector<bool>> 00156 // because of problems with the standard library 00157 // not allowing passing by reference of bool types. 00158 // Broadcast thread implementation requires passing by reference. 00159 ~BroadcastMessageIterationsDoneStruct() 00160 { 00161 for(auto& arr : iterationsDone_) 00162 delete[] arr; 00163 iterationsDone_.clear(); 00164 arraySizes_.clear(); 00165 } // end destructor 00166 00167 void push(const unsigned int& size) 00168 { 00169 iterationsDone_.push_back(new bool[size]); 00170 arraySizes_.push_back(size); 00171 00172 // initialize to false 00173 for(unsigned int i = 0; i < size; ++i) 00174 iterationsDone_[iterationsDone_.size() - 1][i] = false; 00175 } // end push() 00176 00177 bool* operator[](unsigned int i) { return iterationsDone_[i]; } 00178 const bool* operator[](unsigned int i) const { return iterationsDone_[i]; } 00179 unsigned int size(unsigned int i = -1) 00180 { 00181 if(i == (unsigned int)-1) 00182 return iterationsDone_.size(); 00183 return arraySizes_[i]; 00184 } 00185 00186 private: 00187 std::vector<bool*> iterationsDone_; 00188 std::vector<unsigned int> arraySizes_; 00189 }; // end BroadcastMessageIterationsDoneStruct definition 00190 00191 struct BroadcastThreadStruct 00192 { 00193 //=================== 00194 BroadcastThreadStruct() 00195 : threadIndex_(-1) 00196 , exitThread_(false) 00197 , working_(true) 00198 , workToDo_(false) 00199 , error_(false) 00200 { 00201 } // end BroadcastThreadStruct constructor() 00202 00203 struct BroadcastMessageStruct 00204 { 00205 //=================== 00206 BroadcastMessageStruct(const SupervisorInfo& appInfo, 00207 xoap::MessageReference message, 00208 const std::string& command, 00209 const unsigned int& iteration, 00210 bool& iterationsDone) 00211 : appInfo_(appInfo) 00212 , message_(message) 00213 , command_(command) 00214 , iteration_(iteration) 00215 , iterationsDone_(iterationsDone) 00216 { 00217 } 00218 00219 const SupervisorInfo& appInfo_; 00220 xoap::MessageReference message_; 00221 const std::string& command_; 00222 const unsigned int& iteration_; 00223 bool& iterationsDone_; 00224 00225 std::string reply_; 00226 }; // end BroadcastMessageStruct definition 00227 00228 //=================== 00229 void setMessage(const SupervisorInfo& appInfo, 00230 xoap::MessageReference message, 00231 const std::string& command, 00232 const unsigned int& iteration, 00233 bool& iterationsDone) 00234 { 00235 messages_.clear(); 00236 messages_.push_back(BroadcastThreadStruct::BroadcastMessageStruct( 00237 appInfo, message, command, iteration, iterationsDone)); 00238 workToDo_ = true; 00239 } // end setMessage() 00240 00241 const SupervisorInfo& getAppInfo() { return messages_[0].appInfo_; } 00242 xoap::MessageReference getMessage() { return messages_[0].message_; } 00243 const std::string& getCommand() { return messages_[0].command_; } 00244 const unsigned int& getIteration() { return messages_[0].iteration_; } 00245 std::string& getReply() { return messages_[0].reply_; } 00246 bool& getIterationsDone() { return messages_[0].iterationsDone_; } 00247 00248 // each thread accesses these members 00249 std::mutex threadMutex; 00250 unsigned int threadIndex_; 00251 volatile bool exitThread_, working_, workToDo_, error_; 00252 // always just 1 message (for now) 00253 std::vector<BroadcastThreadStruct::BroadcastMessageStruct> messages_; 00254 00255 }; // end BroadcastThreadStruct declaration 00256 static void broadcastMessageThread( 00257 GatewaySupervisor* supervisorPtr, 00258 GatewaySupervisor::BroadcastThreadStruct* threadStruct); 00259 bool handleBroadcastMessageTarget(const SupervisorInfo& appInfo, 00260 xoap::MessageReference message, 00261 const std::string& command, 00262 const unsigned int& iteration, 00263 std::string& reply, 00264 unsigned int threadIndex = 0); 00265 00266 bool supervisorGuiHasBeenLoaded_; // use to indicate first access by user of ots 00267 // since execution 00268 00269 // Member Variables 00270 00271 WebUsers theWebUsers_; 00272 SystemMessenger theSystemMessenger_; 00273 ARTDAQCommandable theArtdaqCommandable_; 00274 00275 WorkLoopManager stateMachineWorkLoopManager_; 00276 toolbox::BSem stateMachineSemaphore_; 00277 WorkLoopManager infoRequestWorkLoopManager_; 00278 toolbox::BSem infoRequestSemaphore_; 00279 00280 std::string activeStateMachineName_; // when multiple state machines, this is the 00281 // name of the state machine which executed the 00282 // configure transition 00283 std::string activeStateMachineWindowName_; 00284 std::pair<std::string /*group name*/, TableGroupKey> 00285 theConfigurationTableGroup_; // used to track the active configuration group at 00286 // states after the configure state 00287 00288 Iterator theIterator_; 00289 std::mutex stateMachineAccessMutex_; // for sharing state machine access with 00290 // iterator thread 00291 std::string stateMachineLastCommandInput_; 00292 00293 enum 00294 { 00295 VERBOSE_MUTEX = 0 00296 }; 00297 00298 CodeEditor codeEditor_; 00299 00300 std::mutex broadcastCommandMessageIndexMutex_, broadcastIterationsDoneMutex_; 00301 unsigned int broadcastCommandMessageIndex_; 00302 bool broadcastIterationsDone_; 00303 00304 // temporary member variable to avoid redeclaration in repetitive functions 00305 char tmpStringForConversions_[100]; 00306 00307 // Trash tests 00308 void wait(int milliseconds, std::string who = "") const; 00309 unsigned long long counterTest_; 00310 std::vector<int> vectorTest_; 00311 std::string securityType_; 00312 }; 00313 00314 } // namespace ots 00315 00316 #endif