RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
suspendable.hpp
Go to the documentation of this file.
1
11
12#ifndef RTCTK_COMPONENTFRAMEWORK_SUSPENDABLE_HPP
13#define RTCTK_COMPONENTFRAMEWORK_SUSPENDABLE_HPP
14
18
20
27template <typename Super>
28struct Suspendable : Super {
29 static_assert(is_base_of_template_v<Loopaware, Super>, "'Suspendable' requires 'Loopaware'");
30
34 class BizLogicIf : public Super::BizLogicIf {
35 public:
36 virtual void ActivitySuspending(StopToken st) {};
37 virtual void ActivityResuming(StopToken st) {};
38 virtual void ActivitySuspended(StopToken st) {};
39 };
40
44 class InputStage : public Super::InputStage {
45 public:
46 using Super::InputStage::InputStage;
47
48 void Start() override {
49 Super::InputStage::Start();
50
51 SuspCmdsImpl::Register(this->m_replier, this->m_engine);
52 }
53 };
54
58 class OutputStage : public Super::OutputStage {
59 public:
60 OutputStage(StateMachineEngine& engine, BizLogicIf& bl) : Super::OutputStage(engine, bl) {
61 // Handlers ###################################################################
62
63 engine.RegisterRejectHandler<events::Suspend, RequestRejected>();
64 engine.RegisterRejectHandler<events::Resume, RequestRejected>();
65
66 // No Disable in states ###############################################################
67
68 this->m_no_disable_in_states.push_back("On::Operational::Suspending");
69 this->m_no_disable_in_states.push_back("On::Operational::Resuming");
70 this->m_no_disable_in_states.push_back("On::Operational::Suspended");
71
72 // No Update in states ################################################################
73
74 this->m_no_update_in_states.push_back("On::Operational::Suspending");
75 this->m_no_update_in_states.push_back("On::Operational::Resuming");
76
77 // Actions ############################################################################
78
79 engine.RegisterAction("ActionSuspendingEntry", [this](auto c) {
80 this->m_tmp_request = GetPayloadNothrow<events::Suspend>(c);
81 });
82
83 engine.RegisterAction("ActionSuspendingDone", [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 engine.RegisterAction("ActionResumingEntry", [this](auto c) {
91 this->m_tmp_request = GetPayloadNothrow<events::Resume>(c);
92 });
93
94 engine.RegisterAction("ActionResumingDone", [this](auto c) {
95 if (this->m_tmp_request) {
96 this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
97 this->m_tmp_request = nullptr;
98 }
99 });
100
101 // Activities #########################################################################
102
103 engine.RegisterActivity(
104 "ActivitySuspending",
105 [this](StopToken stop_token) {
106 static_cast<BizLogicIf&>(this->m_logic).ActivitySuspending(stop_token);
107 },
108 this->m_success_handler,
109 this->m_error_handler);
110
111 engine.RegisterActivity(
112 "ActivityResuming",
113 [this](StopToken stop_token) {
114 static_cast<BizLogicIf&>(this->m_logic).ActivityResuming(stop_token);
115 },
116 this->m_success_handler,
117 this->m_error_handler);
118
119 engine.RegisterActivity(
120 "ActivitySuspended",
121 [this](StopToken stop_token) {
122 static_cast<BizLogicIf&>(this->m_logic).ActivitySuspended(stop_token);
123 },
124 {},
125 this->m_error_handler);
126 }
127 };
128
132 class ModelBuilder : public Super::ModelBuilder {
133 public:
134 explicit ModelBuilder(StateMachineEngine& engine) : Super::ModelBuilder(engine) {
135 // clang-format off
136 std::string parent_region = this->mm.GetParentId("On::Operational::Closed");
137
138 this->mm.AddState(Simple, "On::Operational::Suspended" ,parent_region ,"ActivitySuspended");
139 this->mm.AddState(Simple, "On::Operational::Suspending" ,parent_region ,"ActivitySuspending" , "ActionSuspendingEntry");
140 this->mm.AddState(Simple, "On::Operational::Resuming" ,parent_region ,"ActivityResuming" , "ActionResumingEntry" );
141
142 this->mm.AddTrans("On::Operational::Closed" , "On::Operational::Suspending" , "events.Suspend");
143 this->mm.AddTrans("On::Operational::Suspending" , "On::Operational::Suspended" , "events.Done", "" ,"ActionSuspendingDone");
144 this->mm.AddTrans("On::Operational::Suspended" , "On::Operational::Resuming" , "events.Resume");
145 this->mm.AddTrans("On::Operational::Resuming" , "On::Operational::Closed" , "events.Done", "" ,"ActionResumingDone");
146 this->mm.AddTrans("On::Operational::Suspended" , "On::Operational::Opening" , "events.Open");
147 // clang-format on
148 }
149 };
150};
151
152} // namespace rtctk::componentFramework
153
154#endif // RTCTK_COMPONENTFRAMEWORK_SUSPENDABLE_HPP
Thrown if a command is not allowed in current state or guard.
Definition rtcComponent.hpp:39
Definition stateMachineEngine.hpp:34
void RegisterAction(const std::string &id, ActionMethod action)
Register action.
Definition stateMachineEngine.cpp:85
void RegisterRejectHandler(const std::string &id, RejectMethod reject)
Register reject handler.
Definition stateMachineEngine.cpp:128
void RegisterActivity(const std::string &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition stateMachineEngine.cpp:120
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition suspCmdsImpl.hpp:36
virtual void ActivityResuming(StopToken st)
Definition suspendable.hpp:37
virtual void ActivitySuspended(StopToken st)
Definition suspendable.hpp:38
virtual void ActivitySuspending(StopToken st)
Definition suspendable.hpp:36
void Start() override
Definition suspendable.hpp:48
ModelBuilder(StateMachineEngine &engine)
Definition suspendable.hpp:134
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition suspendable.hpp:60
Lifecycle Extension that makes an RTC Component 'Loopaware'.
Definition commandReplier.cpp:21
@ Simple
Definition model.hpp:22
rad::StopToken StopToken
Definition stopToken.hpp:19
std::shared_ptr< typename EVENT::payload_t > GetPayloadNothrow(scxml4cpp::Context *c)
Definition stateMachineEngine.hpp:255
const std::string STD_OK_REPLY
Definition stdComponent.hpp:85
constexpr bool is_base_of_template_v
Definition utils.hpp:36
A simple Stop Token.
Life cycle extension to make Loopaware RtcComponent Suspendable.
Definition suspendable.hpp:28
Implementation of MAL commands for layer 'Suspendable'.