RTC Toolkit  1.0.0
runnable.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_RUNABLE_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_RUNABLE_HPP
14 
15 #include <Rtctkif.hpp>
18 
19 namespace rtctk::componentFramework {
20 
21 template <typename Super>
22 struct Loopaware;
23 
29 template <typename Super>
30 struct Runnable : Super {
31  static_assert(std::is_base_of_v<RtcComponent, Super>, "'Runnable' requires 'RtcComponent'");
32  static_assert(not is_base_of_template_v<Loopaware, Super>, "'Runnable' excludes 'Loopaware'");
33 
34  class BizLogicIf : public Super::BizLogicIf {
35  public:
36  virtual void ActivityGoingRunning(StopToken st) {};
37  virtual void ActivityGoingIdle(StopToken st) {};
38  virtual void ActivityRunning(StopToken st) {};
39  virtual void ActivityRecovering(StopToken st) {};
40  };
41 
42  class InputStage : public Super::InputStage {
43  public:
44  using Super::InputStage::InputStage;
45 
46  void Start() override {
47  Super::InputStage::Start();
48 
49  FuncCmdsImpl::Register(this->m_replier, this->m_engine);
50  }
51  };
52 
53  class OutputStage : public Super::OutputStage {
54  public:
56  // Handlers ###################################################################
57 
58  engine.RegisterRejectHandler<events::Run, RequestRejected>();
59  engine.RegisterRejectHandler<events::Idle, RequestRejected>();
60  engine.RegisterRejectHandler<events::Recover, RequestRejected>();
61 
62  // No Disable in states ###############################################################
63 
64  this->m_no_disable_in_states.push_back("On.Operational.Error");
65  this->m_no_disable_in_states.push_back("On.Operational.Recovering");
66  this->m_no_disable_in_states.push_back("On.Operational.GoingRunning");
67  this->m_no_disable_in_states.push_back("On.Operational.GoingIdle");
68  this->m_no_disable_in_states.push_back("On.Operational.Running");
69 
70  // No Update in states ################################################################
71 
72  this->m_no_update_in_states.push_back("On.Operational.GoingRunning");
73  this->m_no_update_in_states.push_back("On.Operational.GoingIdle");
74  this->m_no_update_in_states.push_back("On.Operational.Error");
75  this->m_no_update_in_states.push_back("On.Operational.Recovering");
76 
77  // Run #####################################################################
78 
79  engine.RegisterAction("ActionGoingRunningEntry", [this](auto c) {
80  this->m_tmp_request = GetPayloadNothrow<events::Run>(c);
81  });
82 
83  engine.RegisterAction("ActionGoingRunningDone", [this](auto c) {
84  if (this->m_tmp_request) {
85  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
86  this->m_tmp_request = nullptr;
87  }
88  });
89 
90  // Idle #####################################################################
91 
92  engine.RegisterAction("ActionGoingIdleEntry", [this](auto c) {
93  this->m_tmp_request = GetPayloadNothrow<events::Idle>(c);
94  });
95 
96  engine.RegisterAction("ActionGoingIdleDone", [this](auto c) {
97  if (this->m_tmp_request) {
98  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
99  this->m_tmp_request = nullptr;
100  }
101  });
102 
103  // Error #####################################################################
104 
105  engine.RegisterAction("ActionErrorEntry", [this](auto c) {
106  if(auto eptr = GetPayloadNothrow<events::Error>(c); eptr) {
107  if (this->m_tmp_request) {
108  this->m_tmp_request->SetException(
110  this->m_tmp_request = nullptr;
111  }
112  }
113  });
114 
115  // Recover #####################################################################
116 
117  engine.RegisterAction("ActionRecoveringEntry", [this](auto c) {
118  this->m_tmp_request = GetPayloadNothrow<events::Recover>(c);
119  });
120 
121  engine.RegisterAction("ActionRecoveringDone", [this](auto c) {
122  if (this->m_tmp_request) {
123  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
124  this->m_tmp_request = nullptr;
125  }
126  });
127 
128  // Activities #####################################################################
129 
130  engine.RegisterActivity("ActivityGoingRunning",
131  [this](StopToken stop_token) {
132  static_cast<BizLogicIf&>(this->m_logic).ActivityGoingRunning(stop_token);
133  }, this->m_success_handler, this->m_error_handler);
134 
135  engine.RegisterActivity("ActivityGoingIdle",
136  [this](StopToken stop_token) {
137  static_cast<BizLogicIf&>(this->m_logic).ActivityGoingIdle(stop_token);
138  }, this->m_success_handler, this->m_error_handler);
139 
140  engine.RegisterActivity("ActivityRunning",
141  [this](StopToken stop_token) {
142  static_cast<BizLogicIf&>(this->m_logic).ActivityRunning(stop_token);
143  }, this->m_success_handler, this->m_error_handler);
144 
145  engine.RegisterActivity("ActivityRecovering",
146  [this](StopToken stop_token) {
147  static_cast<BizLogicIf&>(this->m_logic).ActivityRecovering(stop_token);
148  }, this->m_success_handler, this->m_error_handler);
149  }
150  };
151 
152  struct ModelBuilder : public Super::ModelBuilder {
153  public:
155  this->RegisterLayer({"Runnable", {}});
156 
157  const std::string parent_region = "On.Operational.RegionMain";
158 
159  this->mm.AddState(Composite, parent_region, "On.Operational");
160 
161  this->mm.AddState(Initial, "On.Operational.Initial", parent_region);
162  this->mm.AddState(Simple, "On.Operational.Idle", parent_region);
163  this->mm.AddState(Simple, "On.Operational.Error", parent_region ,"" ,"ActionErrorEntry");
164  this->mm.AddState(Simple, "On.Operational.Running", parent_region ,"ActivityRunning");
165  this->mm.AddState(Simple, "On.Operational.Recovering", parent_region ,"ActivityRecovering" ,"ActionRecoveringEntry");
166  this->mm.AddState(Simple, "On.Operational.GoingRunning", parent_region ,"ActivityGoingRunning" ,"ActionGoingRunningEntry");
167  this->mm.AddState(Simple, "On.Operational.GoingIdle", parent_region ,"ActivityGoingIdle" ,"ActionGoingIdleEntry");
168 
169  this->mm.AddTrans("On.Operational.Initial" , "On.Operational.Idle" );
170  this->mm.AddTrans(parent_region , "On.Operational.Error" , "events.Error");
171 
172  this->mm.AddTrans("On.Operational.Error" , "On.Operational.Recovering" , "events.Recover");
173  this->mm.AddTrans("On.Operational.Recovering" , "On.Operational.Idle" , "events.Done", "" ,"ActionRecoveringDone");
174 
175  this->mm.AddTrans("On.Operational.Idle" , "On.Operational.GoingRunning" , "events.Run");
176  this->mm.AddTrans("On.Operational.GoingRunning" , "On.Operational.Running" , "events.Done", "" ,"ActionGoingRunningDone");
177 
178  this->mm.AddTrans("On.Operational.Running" , "On.Operational.GoingIdle" , "events.Idle");
179  this->mm.AddTrans("On.Operational.GoingIdle" , "On.Operational.Idle" , "events.Done", "" ,"ActionGoingIdleDone");
180 
181  this->mm.AddTrans("On.Operational.Running" , "On.Operational.GoingIdle" , "events.Done");
182  }
183  };
184 };
185 
186 } // namespace rtctk::componentFramework
187 
188 #endif // RTCTK_COMPONENTFRAMEWORK_RUNABLE_HPP
rtctk::componentFramework::Simple
@ Simple
Definition: model.hpp:23
rtctk::componentFramework::RequestRejected
Definition: stdComponent.hpp:52
rtctk::componentFramework::Runnable::BizLogicIf
Definition: runnable.hpp:34
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::Runnable::InputStage::Start
void Start() override
Definition: runnable.hpp:46
rtctk::rtcSupervisor::Super
Runnable< RtcComponent > Super
Definition: rtcSupervisor.hpp:31
rtctk::componentFramework::Runnable::OutputStage
Definition: runnable.hpp:53
rtctk::componentFramework::STD_OK_REPLY
const std::string STD_OK_REPLY
Definition: stdComponent.hpp:36
rtctk::componentFramework::StopToken
rad::StopToken StopToken
Definition: stopToken.hpp:19
rtctk::componentFramework::StateMachineEngine
Definition: stateMachineEngine.hpp:31
rtctk::componentFramework::NestedExceptionPrinter
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition: exceptions.hpp:157
rtctk::componentFramework::Runnable::ModelBuilder
Definition: runnable.hpp:152
rtctk::componentFramework::StateMachineEngine::RegisterAction
void RegisterAction(std::string const &id, ActionMethod action)
Register action.
Definition: stateMachineEngine.cpp:62
rtctk::componentFramework::Runnable::OutputStage::OutputStage
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition: runnable.hpp:55
rtctk::componentFramework::Runnable::BizLogicIf::ActivityGoingRunning
virtual void ActivityGoingRunning(StopToken st)
Definition: runnable.hpp:36
rtctk::componentFramework::FuncCmdsImpl::Register
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition: rtcCmdsImpl.hpp:34
rtctk::componentFramework::Runnable::InputStage
Definition: runnable.hpp:42
rtctk::componentFramework::NestedExceptionPrinter::Str
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition: exceptions.hpp:175
rtctk::componentFramework::StateMachineEngine::RegisterRejectHandler
void RegisterRejectHandler(std::string const &id, RejectMethod reject)
Register reject handler.
Definition: stateMachineEngine.cpp:95
stopToken.hpp
A simple Stop Token.
rtctk::componentFramework::Runnable::BizLogicIf::ActivityGoingIdle
virtual void ActivityGoingIdle(StopToken st)
Definition: runnable.hpp:37
rtctk::componentFramework::Initial
@ Initial
Definition: model.hpp:26
rtctk::componentFramework::Runnable::BizLogicIf::ActivityRunning
virtual void ActivityRunning(StopToken st)
Definition: runnable.hpp:38
rtcComponent.hpp
Lifecycle of a basic 'RtcComponent'.
rtctk::componentFramework::RequestFailed
Definition: stdComponent.hpp:65
rtctk::componentFramework::Runnable::BizLogicIf::ActivityRecovering
virtual void ActivityRecovering(StopToken st)
Definition: runnable.hpp:39
rtctk::componentFramework::Runnable
Life cycle extension to make RtcComponent Runnable.
Definition: runnable.hpp:30
rtctk::componentFramework::Runnable::ModelBuilder::ModelBuilder
ModelBuilder(StateMachineEngine &engine)
Definition: runnable.hpp:154
rtctk::componentFramework::Composite
@ Composite
Definition: model.hpp:25
rtctk::componentFramework::StateMachineEngine::RegisterActivity
void RegisterActivity(std::string const &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition: stateMachineEngine.cpp:87