RTC Toolkit 5.0.0
Loading...
Searching...
No Matches
customLifeCycle.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_EXAMPLECUSTOM_CUSTOMLIFECYCLE_HPP
14#define RTCTK_EXAMPLECUSTOM_CUSTOMLIFECYCLE_HPP
15
16#include "customCmdsImpl.hpp"
21
22namespace rtctk::exampleCustom {
23
24namespace cfw = componentFramework;
25
26template <typename Super>
27struct CustomLifeCycle : Super {
28 static_assert(std::is_base_of_v<cfw::RtcComponent, Super>,
29 "'CustomLifeCycle' requires 'RtcComponent'");
30
31 class BizLogicIf : public Super::BizLogicIf {
32 public:
34 }
36 }
37 };
38
39 class InputStage : public Super::InputStage {
40 public:
41 using Super::InputStage::InputStage;
42
43 void Start() override {
44 Super::InputStage::Start();
45
46 CustomCmdsImpl::Register(this->m_replier, this->m_engine);
47 }
48 };
49
50 class OutputStage : public Super::OutputStage {
51 public:
53 : Super::OutputStage(engine, bl) {
54 using namespace cfw;
55
56 // Handlers ###################################################################
57
58 engine.RegisterRejectHandler<events::Foo, RequestRejected>();
59 engine.RegisterRejectHandler<events::Bar, RequestRejected>();
60
62 this->m_engine.PostEvent(std::make_unique<events::CustomDone>());
63 };
64
65 m_custom_error_handler = [this](std::exception_ptr eptr) {
66 this->m_engine.PostEvent(std::make_unique<events::CustomError>(std::move(eptr)));
67 };
68
69 // Actions #####################################################################
70
71 engine.RegisterActionStatic<events::Foo>("ActionFooEntry", [this](const auto& ev) {
73 m_tmp_foo_request = ev.GetPayload();
74 });
75
76 engine.RegisterActionStatic<events::CustomDone>("ActionFooDone", [this](const auto&) {
78 m_tmp_foo_request->SetReplyValue(STD_OK_REPLY);
79 m_tmp_foo_request.reset();
80 });
81
82 engine.RegisterActionStatic<events::CustomError>(
83 "ActionFooFailed", [this](const auto& ev) {
84 const auto& eptr = ev.GetPayload();
86 m_tmp_foo_request->SetException(
88 m_tmp_foo_request.reset();
89 });
90
91 engine.RegisterActionStatic<events::Bar>("ActionBarEntry", [this](const auto& ev) {
93 m_tmp_bar_request = ev.GetPayload();
94 });
95
96 engine.RegisterActionStatic<events::CustomDone>("ActionBarDone", [this](const auto&) {
98 m_tmp_bar_request->SetReplyValue(STD_OK_REPLY);
99 m_tmp_bar_request.reset();
100 });
101
102 engine.RegisterActionStatic<events::CustomError>(
103 "ActionBarFailed", [this](const auto& ev) {
104 const auto& eptr = ev.GetPayload();
106 m_tmp_bar_request->SetException(
108 m_tmp_bar_request.reset();
109 });
110
111 // Activities #####################################################################
112
113 engine.RegisterActivity(
114 "ActivityFoo",
115 [this](StopToken stop_token) {
116 static_cast<BizLogicIf&>(this->m_logic).ActivityFoo(stop_token);
117 },
120
121 engine.RegisterActivity(
122 "ActivityBar",
123 [this](StopToken stop_token) {
124 std::string args = m_tmp_bar_request->GetRequestPayload();
125 JsonPayload arg = JsonPayload::parse(args);
126 static_cast<BizLogicIf&>(this->m_logic).ActivityBar(stop_token, arg);
127 },
130 }
131
132 protected:
133 std::optional<rad::cii::Request<std::string>> m_tmp_foo_request;
134 std::optional<rad::cii::Request<std::string, std::string>> m_tmp_bar_request;
136 std::function<void(std::exception_ptr)> m_custom_error_handler;
137 };
138
139 class ModelBuilder : public Super::ModelBuilder {
140 public:
142 using namespace cfw;
143
144 // clang-format off
145 this->mm.ModStateType("On::Operational", Parallel);
146
147 this->mm.AddState(Composite, "On::Operational:Custom", "On::Operational");
148 this->mm.AddState(Initial, "On::Operational:Custom:Initial", "On::Operational:Custom");
149 this->mm.AddState(Simple, "On::Operational:Custom:Idle", "On::Operational:Custom");
150 this->mm.AddState(Simple, "On::Operational:Custom:Foo", "On::Operational:Custom", "ActivityFoo" ,"ActionFooEntry");
151 this->mm.AddState(Simple, "On::Operational:Custom:Bar", "On::Operational:Custom", "ActivityBar" ,"ActionBarEntry");
152
153 this->mm.AddTrans("On::Operational:Custom:Initial" , "On::Operational:Custom:Idle");
154 this->mm.AddTrans("On::Operational:Custom:Idle" , "On::Operational:Custom:Foo" , "events.Foo" ,"");
155 this->mm.AddTrans("On::Operational:Custom:Foo" , "On::Operational:Custom:Idle" , "events.CustomDone" ,"" ,"ActionFooDone" );
156 this->mm.AddTrans("On::Operational:Custom:Foo" , "On::Operational:Custom:Idle" , "events.CustomError" ,"" ,"ActionFooFailed" );
157 this->mm.AddTrans("On::Operational:Custom:Idle" , "On::Operational:Custom:Bar" , "events.Bar" ,"");
158 this->mm.AddTrans("On::Operational:Custom:Bar" , "On::Operational:Custom:Idle" , "events.CustomDone" ,"" ,"ActionBarDone" );
159 this->mm.AddTrans("On::Operational:Custom:Bar" , "On::Operational:Custom:Idle" , "events.CustomError" ,"" ,"ActionBarFailed" );
160 // clang-format on
161 }
162 };
163};
164
165} // namespace rtctk::exampleCustom
166
167#endif
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition exceptions.hpp:159
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition exceptions.hpp:177
Thrown if the command was accepted but the task to run failed.
Definition rtcComponent.hpp:53
Thrown if a command is not allowed in current state or guard.
Definition rtcComponent.hpp:40
Definition stateMachineEngine.hpp:35
static void Register(cfw::CommandReplier &replier, cfw::StateMachineEngine &engine)
Definition customCmdsImpl.hpp:32
Definition customLifeCycle.hpp:31
virtual void ActivityFoo(cfw::StopToken st)
Definition customLifeCycle.hpp:33
virtual void ActivityBar(cfw::StopToken st, const cfw::JsonPayload &args)
Definition customLifeCycle.hpp:35
Definition customLifeCycle.hpp:39
void Start() override
Definition customLifeCycle.hpp:43
Definition customLifeCycle.hpp:139
ModelBuilder(cfw::StateMachineEngine &engine)
Definition customLifeCycle.hpp:141
std::optional< rad::cii::Request< std::string, std::string > > m_tmp_bar_request
Definition customLifeCycle.hpp:134
OutputStage(cfw::StateMachineEngine &engine, BizLogicIf &bl)
Definition customLifeCycle.hpp:52
std::function< void()> m_custom_success_handler
Definition customLifeCycle.hpp:135
std::optional< rad::cii::Request< std::string > > m_tmp_foo_request
Definition customLifeCycle.hpp:133
std::function< void(std::exception_ptr)> m_custom_error_handler
Definition customLifeCycle.hpp:136
Implementation of custom MAL commands.
Provides macros and utilities for exception handling.
Defines the JSON payload type JsonPayload.
Definition commandReplier.cpp:22
@ Simple
Definition model.hpp:23
@ Composite
Definition model.hpp:23
@ Parallel
Definition model.hpp:23
@ Initial
Definition model.hpp:23
rad::StopToken StopToken
Definition stopToken.hpp:20
const std::string STD_OK_REPLY
Definition stdComponent.hpp:86
nlohmann::json JsonPayload
Type requirements:
Definition jsonPayload.hpp:25
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
Definition businessLogic.cpp:23
Lifecycle of a basic 'RtcComponent'.
A simple Stop Token.
Definition customLifeCycle.hpp:27