CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RBStateMachine.h
Go to the documentation of this file.
1 //
3 // RBStateMachine.h
4 // -------
5 //
6 // Finite state machine for the Resource Broker.
7 //
8 // Created on: Dec 9, 2011
9 // Andrei Spataru : aspataru@cern.ch
11 
12 #ifndef RBBOOSTSTATEMACHINE_H_
13 #define RBBOOSTSTATEMACHINE_H_
14 
17 
18 #include "xdaq2rc/RcmsStateNotifier.h"
19 #include "xdata/String.h"
20 #include "xdata/Bag.h"
21 #include "xdaq/Application.h"
22 
23 #include <boost/statechart/event.hpp>
24 #include <boost/statechart/in_state_reaction.hpp>
25 #include <boost/statechart/state_machine.hpp>
26 #include <boost/statechart/state.hpp>
27 #include <boost/statechart/transition.hpp>
28 #include <boost/mpl/list.hpp>
29 #include <boost/shared_ptr.hpp>
30 
31 #include "toolbox/task/Action.h"
32 #include "toolbox/task/WorkLoop.h"
33 #include "toolbox/task/WorkLoopFactory.h"
34 
35 #include <iostream>
36 #include <string>
37 #include <vector>
38 #include <semaphore.h>
39 
40 namespace bsc = boost::statechart;
41 
42 namespace evf {
43 
44 namespace rb_statemachine {
45 
46 enum States {
58 };
59 
61 typedef boost::shared_ptr<SharedResources> SharedResourcesPtr_t;
62 
66 
67 // Outer states:
68 class Failed;
69 class Normal;
70 
71 // Inner states of Normal:
72 class Halted;
73 class Halting;
74 class Configuring;
75 class Ready;
76 
77 // Inner states of Ready:
78 class Stopped;
79 class Enabled;
80 class Enabling;
81 // state hidden from RCMS
82 class Stopping;
83 
84 // Inner states of Enabled:
85 // state hidden from RCMS
86 class Running;
87 
91 
92 class Configure: public bsc::event<Configure> {
93 };
94 class ConfigureDone: public bsc::event<ConfigureDone> {
95 };
96 class Enable: public bsc::event<Enable> {
97 };
98 class EnableDone: public bsc::event<EnableDone> {
99 };
100 class Stop: public bsc::event<Stop> {
101 };
102 class StopDone: public bsc::event<StopDone> {
103 };
104 class Halt: public bsc::event<Halt> {
105 };
106 class HaltDone: public bsc::event<HaltDone> {
107 };
108 class Fail: public bsc::event<Fail> {
109 };
110 
111 //______________________________________________________________________________
119 class BaseState {
120 
121 public:
122 
123  BaseState();
124  virtual ~BaseState() = 0;
125  std::string stateName() const;
127 
128  /*
129  * I2O message handling capability of states.
130  * All states with special behavior must override these functions.
131  */
135  virtual bool discardDataEvent(MemRef_t* bufRef) const {
136  std::cout
137  << "RBStateMachine: current state does not support operation >>discardDataEvent<<"
138  << std::endl;
139  // bool value doesn't matter... return value ignored by caller: FUResourceBroker
140  return false;
141  }
142 
146  virtual bool discardDqmEvent(MemRef_t* bufRef) const {
147  std::cout
148  << "RBStateMachine: current state does not support operation >>discardDqmEvent<<"
149  << std::endl;
150  // bool value doesn't matter... return value ignored by caller: FUResourceBroker
151  return false;
152  }
153 
154  /*
155  * DEFAULT implementations of state-dependent actions.
156  * All states with special behavior must override these functions.
157  */
158 
162  virtual void do_stateNotify() = 0;
163 
167  virtual int stateID() const = 0;
168 
172  virtual void do_stateAction() const {
173  // do nothing if state does not override this function
174  /*
175  std::cout << "RBStateMachine: no >>STATE ACTION<< defined for state: "
176  << stateName() << std::endl;
177  */
178  }
179 
180 protected:
181 
182  virtual std::string do_stateName() const = 0;
183 
184  virtual void do_moveToFailedState(xcept::Exception& exception) const = 0;
185  void fail();
186 
187  void safeEntryAction();
188  virtual void do_entryActionWork() = 0;
189 
190  void safeExitAction();
191  virtual void do_exitActionWork() = 0;
192 
193 };
194 
195 //______________________________________________________________________________
201 class RBStateMachine: public bsc::state_machine<RBStateMachine, Normal> {
202 
203 public:
204 
205  RBStateMachine(xdaq::Application* app, SharedResourcesPtr_t sr);
206  ~RBStateMachine();
207 
212  BaseState const& getCurrentState() const throw (std::bad_cast);
213 
214  BaseState & getCurrentStateNC() const throw (std::bad_cast);
215 
217  return sharedResources_;
218  }
219  inline std::string getExternallyVisibleState() {
220  return visibleStateName_.value_;
221  }
222  inline xdata::String* getExternallyVisibleStatePtr() {
223  return &visibleStateName_;
224  }
225  inline std::string getInternalStateName() {
226  return internalStateName_;
227  }
228  inline xdaq::Application* getApp() const {
229  return app_;
230  }
231  void setExternallyVisibleState(const std::string& s);
232  void setInternalStateName(const std::string& s);
236  inline bool firstTimeInHalted() const {
237  return firstTimeInHalted_;
238  }
240  firstTimeInHalted_ = false;
241  }
242 
246  xdata::Bag<xdaq2rc::ClassnameAndInstance>* rcmsStateListener();
250  xdata::Boolean* foundRcmsStateListener();
251  void findRcmsStateListener(xdaq::Application* app);
255  void rcmsStateChangeNotify();
256 
257  // state-transition safety
262  pthread_rwlock_wrlock(&transitionLock_);
263  }
268  pthread_rwlock_rdlock(&transitionLock_);
269  }
274  pthread_rwlock_unlock(&transitionLock_);
275  }
276 
277 private:
278  void updateWebGUIExternalState(std::string newStateName) const;
279  void updateWebGUIInternalState(std::string newStateName) const;
280 
281 private:
282 
283  xdaq::Application* app_;
285  xdaq2rc::RcmsStateNotifier rcmsStateNotifier_;
286  xdata::String visibleStateName_;
287  std::string internalStateName_;
289 
290  pthread_rwlock_t transitionLock_;
291 };
292 
296 
297 //______________________________________________________________________________
302 class Failed: public bsc::state<Failed, RBStateMachine>, public BaseState {
303 
304 public:
305 
306  Failed( my_context);
307  virtual ~Failed();
308 
309  // state-dependent actions
310  virtual void do_stateNotify();
311 
312  virtual int stateID() const {
314  }
315 
316 private:
317 
318  virtual std::string do_stateName() const;
319  virtual void do_entryActionWork();
320  virtual void do_exitActionWork();
321  virtual void do_moveToFailedState(xcept::Exception& exception) const;
322 
323 };
324 
325 //______________________________________________________________________________
331 class Normal: public bsc::state<Normal, RBStateMachine, Halted>,
332  public BaseState {
333 
334 public:
335 
336  typedef bsc::transition<Fail, Failed> FT;
337  typedef boost::mpl::list<FT> reactions;
338 
339  // state-dependent actions
340  virtual void do_stateNotify();
341 
342  virtual int stateID() const {
344  }
345 
346  Normal( my_context);
347  virtual ~Normal();
348 
349 private:
350 
351  virtual std::string do_stateName() const;
352  virtual void do_entryActionWork();
353  virtual void do_exitActionWork();
354  virtual void do_moveToFailedState(xcept::Exception& exception) const;
355 };
356 
357 //______________________________________________________________________________
363 class Halted: public bsc::state<Halted, Normal>, public BaseState {
364 
365 public:
366 
367  typedef bsc::transition<Configure, Configuring> RT;
368  typedef boost::mpl::list<RT> reactions;
369 
370  // state-dependent actions
371  virtual void do_stateNotify();
372 
373  virtual int stateID() const {
375  }
376 
377  Halted( my_context);
378  virtual ~Halted();
379 
380 private:
381 
382  virtual std::string do_stateName() const;
383  virtual void do_entryActionWork();
384  virtual void do_exitActionWork();
385  virtual void do_moveToFailedState(xcept::Exception& exception) const;
386 
387 };
388 
389 //______________________________________________________________________________
395 class Configuring: public bsc::state<Configuring, Normal>, public BaseState {
396 
397 public:
398 
399  typedef bsc::transition<ConfigureDone, Ready> CR;
400  typedef boost::mpl::list<CR> reactions;
401 
402  Configuring( my_context);
403  virtual ~Configuring();
404 
405  // state-dependent actions
406  virtual void do_stateNotify();
407  virtual int stateID() const {
409  }
410  virtual void do_stateAction() const;
411 
412 private:
413 
417  void connectToBUandSM() const throw (evf::Exception);
418 
419 private:
420 
421  virtual std::string do_stateName() const;
422  virtual void do_entryActionWork();
423  virtual void do_exitActionWork();
424  virtual void do_moveToFailedState(xcept::Exception& exception) const;
425 
426 };
427 
428 //______________________________________________________________________________
434 class Ready: public bsc::state<Ready, Normal, Stopped>, public BaseState {
435 
436 public:
437 
438  typedef bsc::transition<Halt, Halting> HT;
439  typedef boost::mpl::list<HT> reactions;
440 
441  // state-dependent actions
442  virtual void do_stateNotify();
443 
444  virtual int stateID() const {
445  return rb_statemachine::READY;
446  }
447 
448  Ready( my_context);
449  virtual ~Ready();
450 
451 private:
452 
453  virtual std::string do_stateName() const;
454  virtual void do_entryActionWork();
455  virtual void do_exitActionWork();
456  virtual void do_moveToFailedState(xcept::Exception& exception) const;
457 
458 };
459 
460 //______________________________________________________________________________
466 class Stopped: public bsc::state<Stopped, Ready>, public BaseState {
467 
468 public:
469 
470  typedef bsc::transition<Enable, Enabling> ET;
471  typedef boost::mpl::list<ET> reactions;
472 
473  // state-dependent actions
474  virtual void do_stateNotify();
475 
476  virtual int stateID() const {
478  }
479 
480  Stopped( my_context);
481  virtual ~Stopped();
482 
483 private:
484 
485  virtual std::string do_stateName() const;
486  virtual void do_entryActionWork();
487  virtual void do_exitActionWork();
488  virtual void do_moveToFailedState(xcept::Exception& exception) const;
489 
490 };
491 
492 //______________________________________________________________________________
498 class Enabling: public bsc::state<Enabling, Ready>,
499  public BaseState,
500  public toolbox::lang::Class {
501 
502 public:
503 
504  typedef bsc::transition<EnableDone, Enabled> ED;
505  typedef boost::mpl::list<ED> reactions;
506 
507  Enabling( my_context);
508  virtual ~Enabling();
509 
510  // state-dependent actions
511  virtual void do_stateNotify();
512  virtual int stateID() const {
514  }
515  virtual void do_stateAction() const;
516 
517 private:
518 
519  virtual std::string do_stateName() const;
520  virtual void do_entryActionWork();
521  virtual void do_exitActionWork();
522  virtual void do_moveToFailedState(xcept::Exception& exception) const;
523 
524 };
525 
526 //______________________________________________________________________________
532 class Enabled: public bsc::state<Enabled, Ready, Running>, public BaseState {
533 
534 public:
535 
536  typedef bsc::transition<Stop, Stopping> ST;
537  typedef boost::mpl::list<ST> reactions;
538 
539  // state-dependent actions
540  virtual void do_stateNotify();
541 
542  virtual int stateID() const {
544  }
545 
546  Enabled( my_context);
547  virtual ~Enabled();
548 
549 private:
550 
551  virtual std::string do_stateName() const;
552  virtual void do_entryActionWork();
553  virtual void do_exitActionWork();
554  virtual void do_moveToFailedState(xcept::Exception& exception) const;
555 
556 };
557 
558 //______________________________________________________________________________
564 class Running: public bsc::state<Running, Enabled>,
565  public BaseState,
566  public toolbox::lang::Class {
567 
568 public:
569 
570  Running( my_context);
571  virtual ~Running();
572 
573  // I2O message handling capability of state
574  virtual bool discardDataEvent(MemRef_t* bufRef) const;
575  virtual bool discardDqmEvent(MemRef_t* bufRef) const;
576 
577  // state-dependent actions
578  virtual void do_stateNotify();
579  virtual void do_stateAction() const;
580  virtual int stateID() const {
582  }
583 
584 private:
585 
586  virtual std::string do_stateName() const;
587  virtual void do_entryActionWork();
588  virtual void do_exitActionWork();
589  virtual void do_moveToFailedState(xcept::Exception& exception) const;
590 };
591 
592 //______________________________________________________________________________
598 class Stopping: public bsc::state<Stopping, Ready>, public BaseState {
599 
600 public:
601 
602  typedef bsc::transition<StopDone, Stopped> SD;
603  typedef boost::mpl::list<SD> reactions;
604 
605  Stopping( my_context);
606  virtual ~Stopping();
607 
608  //I2O capability
609  virtual bool discardDataEvent(MemRef_t* bufRef) const;
610  virtual bool discardDqmEvent(MemRef_t* bufRef) const;
611 
612  // state-dependent actions
613  virtual void do_stateNotify();
614  virtual int stateID() const {
616  }
617  virtual void do_stateAction() const;
618 
619 private:
620 
621  void emergencyStop() const;
622 
623 private:
624 
625  virtual std::string do_stateName() const;
626  virtual void do_entryActionWork();
627  virtual void do_exitActionWork();
628  virtual void do_moveToFailedState(xcept::Exception& exception) const;
629 
630  bool destructionIsDone() const;
631 };
632 
633 //______________________________________________________________________________
639 class Halting: public bsc::state<Halting, Normal>, public BaseState {
640 
641 public:
642 
643  typedef bsc::transition<HaltDone, Halted> HD;
644  typedef boost::mpl::list<HD> reactions;
645 
646  Halting( my_context);
647  virtual ~Halting();
648 
649  //I2O capability
650  virtual bool discardDataEvent(MemRef_t* bufRef) const;
651  virtual bool discardDqmEvent(MemRef_t* bufRef) const;
652 
653  // state-dependent actions
654  virtual void do_stateNotify();
655  virtual int stateID() const {
657  }
658  virtual void do_stateAction() const;
659 
660 private:
661 
662  virtual std::string do_stateName() const;
663  virtual void do_entryActionWork();
664  virtual void do_exitActionWork();
665  virtual void do_moveToFailedState(xcept::Exception& exception) const;
666 
667  bool destructionIsDone() const;
668 
669  void doAsync() const;
670 
671 };
672 
673 typedef boost::shared_ptr<RBStateMachine> RBStateMachinePtr;
674 
675 } // end namespace rb_statemachine
676 
677 } // end namespace evf
678 
679 #endif /* RBBOOSTSTATEMACHINE_H_ */
boost::mpl::list< ED > reactions
virtual bool discardDataEvent(MemRef_t *bufRef) const
virtual void do_stateAction() const
virtual int stateID() const =0
boost::mpl::list< RT > reactions
virtual int stateID() const
not [yet] run
Definition: HLTenums.h:21
virtual int stateID() const
bsc::transition< HaltDone, Halted > HD
boost::shared_ptr< SharedResources > SharedResourcesPtr_t
virtual void do_entryActionWork()
Definition: Normal.cc:13
toolbox::mem::Reference MemRef_t
Definition: FUTypes.h:10
virtual std::string do_stateName() const
Definition: Failed.cc:42
virtual int stateID() const
virtual int stateID() const
boost::mpl::list< CR > reactions
xdaq2rc::RcmsStateNotifier rcmsStateNotifier_
boost::mpl::list< ET > reactions
RBStateMachine(xdaq::Application *app, SharedResourcesPtr_t sr)
SharedResourcesPtr_t getSharedResources() const
virtual std::string do_stateName() const
Definition: Normal.cc:35
virtual int stateID() const
bsc::transition< Fail, Failed > FT
virtual void do_exitActionWork()=0
virtual std::string do_stateName() const
Definition: Halted.cc:48
virtual void do_entryActionWork()
Definition: Halted.cc:14
virtual void do_moveToFailedState(xcept::Exception &exception) const
Definition: Normal.cc:39
virtual std::string do_stateName() const
Definition: Configuring.cc:81
bsc::transition< EnableDone, Enabled > ED
void updateWebGUIInternalState(std::string newStateName) const
void updateWebGUIExternalState(std::string newStateName) const
virtual void do_exitActionWork()
Definition: Halted.cc:44
bsc::transition< ConfigureDone, Ready > CR
BaseState & getCurrentStateNC() const
void findRcmsStateListener(xdaq::Application *app)
virtual int stateID() const
bsc::transition< StopDone, Stopped > SD
boost::mpl::list< HT > reactions
boost::mpl::list< SD > reactions
bsc::transition< Stop, Stopping > ST
virtual int stateID() const
bsc::transition< Halt, Halting > HT
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
virtual void do_moveToFailedState(xcept::Exception &exception) const
Definition: Configuring.cc:85
virtual int stateID() const
void moveToFailedState(xcept::Exception &exception) const
Definition: BaseState.cc:35
virtual std::string do_stateName() const =0
xdata::String * getExternallyVisibleStatePtr()
virtual bool discardDqmEvent(MemRef_t *bufRef) const
virtual void do_moveToFailedState(xcept::Exception &exception) const =0
virtual void do_exitActionWork()
Definition: Normal.cc:32
bsc::transition< Configure, Configuring > RT
xdata::Bag< xdaq2rc::ClassnameAndInstance > * rcmsStateListener()
boost::shared_ptr< RBStateMachine > RBStateMachinePtr
void setInternalStateName(const std::string &s)
virtual void do_moveToFailedState(xcept::Exception &exception) const
Definition: Halted.cc:52
virtual int stateID() const
virtual void do_exitActionWork()
Definition: Failed.cc:39
string const
Definition: compareJSON.py:14
virtual void do_stateNotify()
Definition: Normal.cc:16
virtual void do_stateNotify()
Definition: Halted.cc:22
boost::mpl::list< FT > reactions
void setExternallyVisibleState(const std::string &s)
char state
Definition: procUtils.cc:75
xdaq::Application * getApp() const
virtual int stateID() const
#define private
Definition: FWFileEntry.h:18
BaseState const & getCurrentState() const
virtual void do_entryActionWork()
Definition: Failed.cc:14
boost::mpl::list< ST > reactions
virtual void do_stateNotify()
Definition: Failed.cc:17
tuple cout
Definition: gather_cfg.py:121
virtual void do_moveToFailedState(xcept::Exception &exception) const
Definition: Failed.cc:46
virtual void do_stateAction() const
Definition: Configuring.cc:31
virtual void do_entryActionWork()=0
bsc::transition< Enable, Enabling > ET
std::string stateName() const
Definition: BaseState.cc:31
boost::mpl::list< HD > reactions