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