RTC Toolkit  2.0.0
suspendable.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_SUSPENDABLE_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_SUSPENDABLE_HPP
14 
18 
19 namespace rtctk::componentFramework {
20 
26 template <typename Super>
27 struct Suspendable : Super {
28  static_assert(is_base_of_template_v<Loopaware, Super>, "'Suspendable' requires 'Loopaware'");
29 
30  class BizLogicIf : public Super::BizLogicIf {
31  public:
32  virtual void ActivitySuspending(StopToken st) {};
33  virtual void ActivityResuming(StopToken st) {};
34  virtual void ActivitySuspended(StopToken st) {};
35  };
36 
37  class InputStage : public Super::InputStage {
38  public:
39  using Super::InputStage::InputStage;
40 
41  void Start() override {
42  Super::InputStage::Start();
43 
44  SuspCmdsImpl::Register(this->m_replier, this->m_engine);
45  }
46  };
47 
48  class OutputStage : public Super::OutputStage {
49  public:
51  // Handlers ###################################################################
52 
53  engine.RegisterRejectHandler<events::Suspend, RequestRejected>();
54  engine.RegisterRejectHandler<events::Resume, RequestRejected>();
55 
56  // No Disable in states ###############################################################
57 
58  this->m_no_disable_in_states.push_back("On:Operational:Suspending");
59  this->m_no_disable_in_states.push_back("On:Operational:Resuming");
60  this->m_no_disable_in_states.push_back("On:Operational:Suspended");
61 
62  // No Update in states ################################################################
63 
64  this->m_no_update_in_states.push_back("On:Operational:Suspending");
65  this->m_no_update_in_states.push_back("On:Operational:Resuming");
66 
67  // Actions ############################################################################
68 
69  engine.RegisterAction("ActionSuspendingEntry", [this](auto c) {
70  this->m_tmp_request = GetPayloadNothrow<events::Suspend>(c);
71  });
72 
73  engine.RegisterAction("ActionSuspendingDone", [this](auto c) {
74  if (this->m_tmp_request) {
75  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
76  this->m_tmp_request = nullptr;
77  }
78  });
79 
80  engine.RegisterAction("ActionResumingEntry", [this](auto c) {
81  this->m_tmp_request = GetPayloadNothrow<events::Resume>(c);
82  });
83 
84  engine.RegisterAction("ActionResumingDone", [this](auto c) {
85  if (this->m_tmp_request) {
86  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
87  this->m_tmp_request = nullptr;
88  }
89  });
90 
91  // Activities #########################################################################
92 
93  engine.RegisterActivity("ActivitySuspending",
94  [this](StopToken stop_token) {
95  static_cast<BizLogicIf&>(this->m_logic).ActivitySuspending(stop_token);
96  }, this->m_success_handler, this->m_error_handler);
97 
98  engine.RegisterActivity("ActivityResuming",
99  [this](StopToken stop_token) {
100  static_cast<BizLogicIf&>(this->m_logic).ActivityResuming(stop_token);
101  }, this->m_success_handler, this->m_error_handler);
102 
103  engine.RegisterActivity("ActivitySuspended",
104  [this](StopToken stop_token) {
105  static_cast<BizLogicIf&>(this->m_logic).ActivitySuspended(stop_token);
106  }, {}, this->m_error_handler);
107  }
108  };
109 
110  class ModelBuilder : public Super::ModelBuilder {
111  public:
113  this->RegisterLayer({"Suspendable", {}});
114 
115  std::string parent_region = this->mm.GetParentId("On:Operational:Closed");
116 
117  this->mm.AddState(Simple, "On:Operational:Suspended" ,parent_region ,"ActivitySuspended");
118  this->mm.AddState(Simple, "On:Operational:Suspending" ,parent_region ,"ActivitySuspending" , "ActionSuspendingEntry");
119  this->mm.AddState(Simple, "On:Operational:Resuming" ,parent_region ,"ActivityResuming" , "ActionResumingEntry" );
120 
121  this->mm.AddTrans("On:Operational:Closed" , "On:Operational:Suspending" , "events.Suspend");
122  this->mm.AddTrans("On:Operational:Suspending" , "On:Operational:Suspended" , "events.Done", "" ,"ActionSuspendingDone");
123  this->mm.AddTrans("On:Operational:Suspended" , "On:Operational:Resuming" , "events.Resume");
124  this->mm.AddTrans("On:Operational:Resuming" , "On:Operational:Closed" , "events.Done", "" ,"ActionResumingDone");
125  this->mm.AddTrans("On:Operational:Suspended" , "On:Operational:Opening" , "events.Open");
126  }
127  };
128 };
129 
130 } // namespace rtctk::componentFramework
131 
132 #endif // RTCTK_COMPONENTFRAMEWORK_SUSPENDABLE_HPP
rtctk::componentFramework::Simple
@ Simple
Definition: model.hpp:23
rtctk::componentFramework::RequestRejected
Definition: rtcComponent.hpp:34
rtctk::componentFramework::Suspendable
Life cycle extension to make Loopaware RtcComponent Suspendable.
Definition: suspendable.hpp:27
rtctk::componentFramework::Suspendable::BizLogicIf::ActivitySuspended
virtual void ActivitySuspended(StopToken st)
Definition: suspendable.hpp:34
loopaware.hpp
Lifecycle Extension that makes an RTC Component 'Loopaware'.
rtctk::componentFramework::Suspendable::ModelBuilder::ModelBuilder
ModelBuilder(StateMachineEngine &engine)
Definition: suspendable.hpp:112
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::Suspendable::InputStage::Start
void Start() override
Definition: suspendable.hpp:41
rtctk::rtcSupervisor::Super
Runnable< RtcComponent > Super
Definition: rtcSupervisor.hpp:33
rtctk::componentFramework::Suspendable::ModelBuilder
Definition: suspendable.hpp:110
rtctk::componentFramework::STD_OK_REPLY
const std::string STD_OK_REPLY
Definition: stdComponent.hpp:83
rtctk::componentFramework::Suspendable::BizLogicIf::ActivitySuspending
virtual void ActivitySuspending(StopToken st)
Definition: suspendable.hpp:32
rtctk::componentFramework::StopToken
rad::StopToken StopToken
Definition: stopToken.hpp:19
rtctk::componentFramework::StateMachineEngine
Definition: stateMachineEngine.hpp:31
rtctk::componentFramework::Suspendable::InputStage
Definition: suspendable.hpp:37
rtctk::componentFramework::StateMachineEngine::RegisterAction
void RegisterAction(std::string const &id, ActionMethod action)
Register action.
Definition: stateMachineEngine.cpp:65
rtctk::componentFramework::Suspendable::BizLogicIf
Definition: suspendable.hpp:30
rtctk::componentFramework::Suspendable::OutputStage::OutputStage
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition: suspendable.hpp:50
rtctk::componentFramework::SuspCmdsImpl::Register
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition: suspCmdsImpl.hpp:34
rtctk::componentFramework::Suspendable::OutputStage
Definition: suspendable.hpp:48
rtctk::componentFramework::StateMachineEngine::RegisterRejectHandler
void RegisterRejectHandler(std::string const &id, RejectMethod reject)
Register reject handler.
Definition: stateMachineEngine.cpp:98
rtctk::componentFramework::Suspendable::BizLogicIf::ActivityResuming
virtual void ActivityResuming(StopToken st)
Definition: suspendable.hpp:33
suspCmdsImpl.hpp
Implementation of MAL commands for layer 'Suspendable'.
stopToken.hpp
A simple Stop Token.
rtctk::componentFramework::StateMachineEngine::RegisterActivity
void RegisterActivity(std::string const &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition: stateMachineEngine.cpp:90