RTC Toolkit  2.0.0
customLifeCycle.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_EXAMPLECUSTOM_CUSTOMLIFECYCLE_HPP
13 #define RTCTK_EXAMPLECUSTOM_CUSTOMLIFECYCLE_HPP
14 
15 #include "customCmdsImpl.hpp"
20 
21 namespace rtctk::exampleCustom {
22 
23 namespace cfw = componentFramework;
24 
25 template <typename Super>
27  static_assert(std::is_base_of_v<cfw::RtcComponent, Super>,
28  "'CustomLifeCycle' requires 'RtcComponent'");
29 
30  class BizLogicIf : public Super::BizLogicIf {
31  public:
32  virtual void ActivityFoo(cfw::StopToken st) {
33  }
34  virtual void ActivityBar(cfw::StopToken st, cfw::JsonPayload const& args) {
35  }
36  };
37 
38  class InputStage : public Super::InputStage {
39  public:
40  using Super::InputStage::InputStage;
41 
42  void Start() override {
43  Super::InputStage::Start();
44 
45  CustomCmdsImpl::Register(this->m_replier, this->m_engine);
46  }
47  };
48 
49  class OutputStage : public Super::OutputStage {
50  public:
52  : Super::OutputStage(engine, bl) {
53  using namespace cfw;
54 
55  // Handlers ###################################################################
56 
57  engine.RegisterRejectHandler<events::Foo, RequestRejected>();
58  engine.RegisterRejectHandler<events::Bar, RequestRejected>();
59 
60  m_custom_success_handler = [this] {
61  this->m_engine.PostEvent(std::make_unique<events::CustomDone>());
62  };
63 
64  m_custom_error_handler = [this](std::exception_ptr eptr) {
65  this->m_engine.PostEvent(std::make_unique<events::CustomError>(eptr));
66  };
67 
68  // Actions #####################################################################
69 
70  engine.RegisterActionStatic<events::Foo>("ActionFooEntry",
71  [this](auto const& ev) {
72  assert(not m_tmp_foo_request);
73  m_tmp_foo_request = ev.GetPayload();
74  });
75 
76  engine.RegisterActionStatic<events::CustomDone>("ActionFooDone",
77  [this](auto const&) {
78  assert(m_tmp_foo_request);
79  m_tmp_foo_request->SetReplyValue(STD_OK_REPLY);
80  m_tmp_foo_request.reset();
81  });
82 
83  engine.RegisterActionStatic<events::CustomError>("ActionFooFailed",
84  [this](auto const& ev) {
85  auto const& eptr = ev.GetPayload();
86  assert(m_tmp_foo_request);
87  m_tmp_foo_request->SetException(
89  m_tmp_foo_request.reset();
90  });
91 
92  engine.RegisterActionStatic<events::Bar>("ActionBarEntry",
93  [this](auto const& ev) {
94  assert(not m_tmp_bar_request);
95  m_tmp_bar_request = ev.GetPayload();
96  });
97 
98  engine.RegisterActionStatic<events::CustomDone>("ActionBarDone",
99  [this](auto const&) {
100  assert(m_tmp_bar_request);
101  m_tmp_bar_request->SetReplyValue(STD_OK_REPLY);
102  m_tmp_bar_request.reset();
103  });
104 
105  engine.RegisterActionStatic<events::CustomError>("ActionBarFailed",
106  [this](auto const& ev) {
107  auto const& eptr = ev.GetPayload();
108  assert(m_tmp_bar_request);
109  m_tmp_bar_request->SetException(
111  m_tmp_bar_request.reset();
112  });
113 
114  // Activities #####################################################################
115 
116  engine.RegisterActivity("ActivityFoo",
117  [this](StopToken stop_token) {
118  static_cast<BizLogicIf&>(this->m_logic).ActivityFoo(stop_token);
120 
121  engine.RegisterActivity("ActivityBar",
122  [this](StopToken stop_token) {
123  std::string args = m_tmp_bar_request->GetRequestPayload();
124  JsonPayload arg = JsonPayload::parse(args);
125  static_cast<BizLogicIf&>(this->m_logic).ActivityBar(stop_token, arg);
127  }
128 
129  protected:
130  std::optional<rad::cii::Request<std::string>> m_tmp_foo_request;
131  std::optional<rad::cii::Request<std::string, std::string>> m_tmp_bar_request;
132  std::function<void()> m_custom_success_handler;
133  std::function<void(std::exception_ptr)> m_custom_error_handler;
134  };
135 
136  class ModelBuilder : public Super::ModelBuilder {
137  public:
139  using namespace cfw;
140 
141  this->RegisterLayer({"CustomLifeCycle", {}});
142 
143  this->mm.AddState(Composite, "On:Operational:RegionCustom", "On:Operational");
144  this->mm.AddState(Initial, "On:Operational:CustomInitial", "On:Operational:RegionCustom");
145  this->mm.AddState(Simple, "On:Operational:CustomIdle", "On:Operational:RegionCustom");
146  this->mm.AddState(Simple, "On:Operational:CustomFoo", "On:Operational:RegionCustom", "ActivityFoo" ,"ActionFooEntry");
147  this->mm.AddState(Simple, "On:Operational:CustomBar", "On:Operational:RegionCustom", "ActivityBar" ,"ActionBarEntry");
148 
149  this->mm.AddTrans("On:Operational:CustomInitial" , "On:Operational:CustomIdle");
150  this->mm.AddTrans("On:Operational:CustomIdle" , "On:Operational:CustomFoo" , "events.Foo" ,"");
151  this->mm.AddTrans("On:Operational:CustomFoo" , "On:Operational:CustomIdle" , "events.CustomDone" ,"" ,"ActionFooDone" );
152  this->mm.AddTrans("On:Operational:CustomFoo" , "On:Operational:CustomIdle" , "events.CustomError" ,"" ,"ActionFooFailed" );
153  this->mm.AddTrans("On:Operational:CustomIdle" , "On:Operational:CustomBar" , "events.Bar" ,"");
154  this->mm.AddTrans("On:Operational:CustomBar" , "On:Operational:CustomIdle" , "events.CustomDone" ,"" ,"ActionBarDone" );
155  this->mm.AddTrans("On:Operational:CustomBar" , "On:Operational:CustomIdle" , "events.CustomError" ,"" ,"ActionBarFailed" );
156  }
157  };
158 };
159 
160 } // namespace rtctk::exampleCustom
161 
162 #endif
rtctk::componentFramework::Simple
@ Simple
Definition: model.hpp:23
rtctk::componentFramework::RequestRejected
Definition: rtcComponent.hpp:34
exceptions.hpp
Provides macros and utilities for exception handling.
rtctk::exampleCustom::CustomCmdsImpl::Register
static void Register(cfw::CommandReplier &replier, cfw::StateMachineEngine &engine)
Definition: customCmdsImpl.hpp:31
rtctk::exampleCustom::CustomLifeCycle
Definition: customLifeCycle.hpp:26
rtctk::exampleCustom::CustomLifeCycle::OutputStage::m_tmp_foo_request
std::optional< rad::cii::Request< std::string > > m_tmp_foo_request
Definition: customLifeCycle.hpp:130
rtctk::exampleCustom::CustomLifeCycle::InputStage
Definition: customLifeCycle.hpp:38
rtctk::exampleCustom::CustomLifeCycle::ModelBuilder
Definition: customLifeCycle.hpp:136
rtctk::exampleCustom::CustomLifeCycle::BizLogicIf::ActivityFoo
virtual void ActivityFoo(cfw::StopToken st)
Definition: customLifeCycle.hpp:32
rtctk::rtcSupervisor::Super
Runnable< RtcComponent > Super
Definition: rtcSupervisor.hpp:33
rtctk::componentFramework::STD_OK_REPLY
const std::string STD_OK_REPLY
Definition: stdComponent.hpp:83
rtctk::componentFramework::StopToken
rad::StopToken StopToken
Definition: stopToken.hpp:19
rtctk::componentFramework::StateMachineEngine::RegisterActionStatic
void RegisterActionStatic(std::string const &id, std::function< void(Event const &)> action)
Register action for statically known event type.
Definition: stateMachineEngine.hpp:79
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::exampleCustom::CustomLifeCycle::BizLogicIf::ActivityBar
virtual void ActivityBar(cfw::StopToken st, cfw::JsonPayload const &args)
Definition: customLifeCycle.hpp:34
rtctk::exampleCustom::CustomLifeCycle::OutputStage
Definition: customLifeCycle.hpp:49
rtctk::exampleCustom::CustomLifeCycle::BizLogicIf
Definition: customLifeCycle.hpp:30
jsonPayload.hpp
Defines the JSON payload type JsonPayload.
rtctk::componentFramework::StateMachineEngine::RegisterRejectHandler
void RegisterRejectHandler(std::string const &id, RejectMethod reject)
Register reject handler.
Definition: stateMachineEngine.cpp:98
stopToken.hpp
A simple Stop Token.
rtctk::exampleCustom
Definition: businessLogic.cpp:22
rtctk::componentFramework::Initial
@ Initial
Definition: model.hpp:26
rtctk::exampleCustom::CustomLifeCycle::OutputStage::m_custom_error_handler
std::function< void(std::exception_ptr)> m_custom_error_handler
Definition: customLifeCycle.hpp:133
rtctk::exampleCustom::CustomLifeCycle::InputStage::Start
void Start() override
Definition: customLifeCycle.hpp:42
rtctk::exampleCustom::CustomLifeCycle::ModelBuilder::ModelBuilder
ModelBuilder(cfw::StateMachineEngine &engine)
Definition: customLifeCycle.hpp:138
rtcComponent.hpp
Lifecycle of a basic 'RtcComponent'.
rtctk::componentFramework::RequestFailed
Definition: rtcComponent.hpp:44
customCmdsImpl.hpp
Implementation of custom MAL commands.
rtctk::exampleCustom::CustomLifeCycle::OutputStage::m_tmp_bar_request
std::optional< rad::cii::Request< std::string, std::string > > m_tmp_bar_request
Definition: customLifeCycle.hpp:131
rtctk::componentFramework::Composite
@ Composite
Definition: model.hpp:25
rtctk::exampleCustom::CustomLifeCycle::OutputStage::OutputStage
OutputStage(cfw::StateMachineEngine &engine, BizLogicIf &bl)
Definition: customLifeCycle.hpp:51
rtctk::componentFramework::StateMachineEngine::RegisterActivity
void RegisterActivity(std::string const &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition: stateMachineEngine.cpp:90
rtctk::componentFramework::JsonPayload
nlohmann::json JsonPayload
Type requirements:
Definition: jsonPayload.hpp:24
rtctk::exampleCustom::CustomLifeCycle::OutputStage::m_custom_success_handler
std::function< void()> m_custom_success_handler
Definition: customLifeCycle.hpp:132