RTC Toolkit  1.0.0
rtcComponent.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_RTCCOMPONENT_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_RTCCOMPONENT_HPP
14 
19 
20 namespace rtctk::componentFramework {
21 
23  UseInitialising<Enabled>,
24  UseEnabling<Enabled>,
25  UseDisabling<Enabled>>;
26 
34 
35  class BizLogicIf : public Super::BizLogicIf {
36  public:
37  virtual void ActivityUpdating(StopToken st, Payload args) {
38  }
39  virtual bool GuardUpdatingAllowed(Payload args) {
40  return true;
41  }
42  };
43 
44  class InputStage : public Super::InputStage {
45  public:
47 
48  void Start() override {
50 
52  }
53  };
54 
56  public:
58  // Handlers ###################################################################
59 
60  engine.RegisterRejectHandler<events::Update, RequestRejected>();
61 
62  m_update_success_handler = [this] {
63  this->m_engine.PostEvent(std::make_unique<events::UpdateDone>());
64  };
65 
66  m_update_error_handler = [this](std::exception_ptr eptr) {
67  this->m_engine.PostEvent(std::make_unique<events::UpdateError>(eptr));
68  };
69 
70  // No Disable in states ###############################################################
71 
72  m_no_disable_in_states.push_back("On.Operational.UpdateBusy");
73 
74  // Actions ############################################################################
75 
76  engine.RegisterAction("ActionUpdatingEntry", [this](auto c) {
77  m_tmp_update_request = GetPayloadNothrow<events::Update>(c);
78  });
79 
80  engine.RegisterAction("ActionUpdatingDone", [this](auto c) {
82  m_tmp_update_request->SetReplyValue(STD_OK_REPLY);
83  m_tmp_update_request = nullptr;
84  }
85  });
86 
87  engine.RegisterAction("ActionUpdatingFailed", [this](auto c) {
88  if(auto eptr = GetPayloadNothrow<events::UpdateError>(c); eptr) {
90  m_tmp_update_request->SetException(
92  m_tmp_update_request = nullptr;
93  }
94  }
95  });
96 
97  // Guards ############################################################################
98 
99  engine.RegisterGuard("GuardUpdatingAllowed", [this](auto c) {
100  auto req = GetPayloadNothrow<events::Update>(c);
101  if (req == nullptr) {
102  // no event or request
103  return false;
104  }
105  std::string args = req->GetRequestPayload();
106  // TODO: parse the args and present them nicely to the BL
107 
108  auto act_state = this->m_engine.GetState();
109  for (auto& s : m_no_update_in_states) {
110  if (act_state.find(s) != std::string::npos) {
111  return false;
112  }
113  }
114  return static_cast<BizLogicIf&>(this->m_logic).GuardUpdatingAllowed(args);
115  });
116 
117  engine.RegisterGuard("GuardDisablingAllowed", [this](auto c) {
118  auto act_state = this->m_engine.GetState();
119  for (auto& s : m_no_disable_in_states) {
120  if (act_state.find(s) != std::string::npos) {
121  return false;
122  }
123  }
124  return true;
125  });
126 
127  // Activities #####################################################################
128 
129  engine.RegisterActivity("ActivityUpdating",
130  [this](StopToken stop_token) {
131  std::string args = m_tmp_update_request->GetRequestPayload();
132  // TODO: parse the args and present them nicely to the BL
133  static_cast<BizLogicIf&>(this->m_logic).ActivityUpdating(stop_token, args);
135  }
136 
137  protected:
138  std::shared_ptr<rad::cii::Request<std::string, std::string>> m_tmp_update_request;
139  std::function<void()> m_update_success_handler;
140  std::function<void(std::exception_ptr)> m_update_error_handler;
141  std::list<std::string> m_no_update_in_states;
142  std::list<std::string> m_no_disable_in_states;
143  };
144 
146  public:
148  this->RegisterLayer({"RtcComponent", {}});
149 
150  const std::string parent_region = "On.Operational.RegionUpdate";
151 
152  this->mm.ModStateType("On.Operational", Parallel);
153  this->mm.AddState(Composite, parent_region, "On.Operational");
154 
155  this->mm.AddState(Initial, "On.Operational.UpdateInitial", parent_region);
156  this->mm.AddState(Simple, "On.Operational.UpdateIdle", parent_region);
157  this->mm.AddState(Simple, "On.Operational.UpdateBusy", parent_region , "ActivityUpdating" ,"ActionUpdatingEntry");
158 
159  this->mm.AddTrans("On.Operational.UpdateInitial" , "On.Operational.UpdateIdle" );
160  this->mm.AddTrans("On.Operational.UpdateIdle" , "On.Operational.UpdateBusy" , "events.Update", "GuardUpdatingAllowed");
161  this->mm.AddTrans("On.Operational.UpdateBusy" , "On.Operational.UpdateIdle" , "events.UpdateDone", "" ,"ActionUpdatingDone" );
162  this->mm.AddTrans("On.Operational.UpdateBusy" , "On.Operational.UpdateIdle" , "events.UpdateError", "" ,"ActionUpdatingFailed" );
163 
164  this->mm.ModTransGuard("On.Operational", "On.NotOperational.Disabling", "events.Disable", "", "GuardDisablingAllowed");
165  }
166  };
167 };
168 
169 } // namespace rtctk::componentFramework
170 
171 #endif // RTCTK_COMPONENTFRAMEWORK_RTCCOMPONENT_HPP
rtctk::componentFramework::RtcComponent::BizLogicIf
Definition: rtcComponent.hpp:35
rtctk::componentFramework::Simple
@ Simple
Definition: model.hpp:23
rtctk::componentFramework::RequestRejected
Definition: stdComponent.hpp:52
rtctk::componentFramework::RtcComponent::OutputStage
Definition: rtcComponent.hpp:55
rtctk::componentFramework::ModelManipulator::ModTransGuard
void ModTransGuard(std::string const &source_id, std::string const &target_id, std::string const &event_id, std::string const &guard_id, std::string const &new_guard_id)
Modifies the guard of a transition.
Definition: modelManipulator.cpp:295
rtctk::componentFramework::Parallel
@ Parallel
Definition: model.hpp:24
rtctk::componentFramework::RtcComponent
Basic life cycle for RtcComponent.
Definition: rtcComponent.hpp:32
rtctk::componentFramework::ModelManipulator::AddTrans
void AddTrans(std::string const &source_id, std::string const &target_id, std::string const &event_id="", std::string const &guard_id="", std::string const &action_id="")
Adds a new transition.
Definition: modelManipulator.cpp:227
rtctk::componentFramework::RtcComponent::ModelBuilder::ModelBuilder
ModelBuilder(StateMachineEngine &engine)
Definition: rtcComponent.hpp:147
rtctk::componentFramework::StdComponent::InputStage::m_replier
CommandReplier & m_replier
Definition: stdComponent.hpp:125
rtctk::componentFramework::ModelBuilderBase::mm
ModelManipulator mm
Definition: modelBuilderBase.hpp:86
rtctk::componentFramework::RtcComponent::OutputStage::m_update_error_handler
std::function< void(std::exception_ptr)> m_update_error_handler
Definition: rtcComponent.hpp:140
rtctk::componentFramework::ModelManipulator::ModStateType
void ModStateType(std::string const &state_id, StateType new_state_type)
Modifies type of state.
Definition: modelManipulator.cpp:122
rtctk::componentFramework::StdComponent::InputStage::m_engine
StateMachineEngine & m_engine
Definition: stdComponent.hpp:126
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::StdComponent::InputStage
Definition: stdComponent.hpp:111
rtcCmdsImpl.hpp
Implementation of MAL commands for layer 'RtcComponent'.
rtctk::componentFramework::StateMachineEngine::RegisterGuard
void RegisterGuard(std::string const &id, GuardMethod guard)
Register guard.
Definition: stateMachineEngine.cpp:74
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::StdComponent
Basic life cycle for StdComponent.
Definition: stdComponent.hpp:91
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::RtcComponent::InputStage::Start
void Start() override
Definition: rtcComponent.hpp:48
rtctk::componentFramework::StateMachineEngine::RegisterAction
void RegisterAction(std::string const &id, ActionMethod action)
Register action.
Definition: stateMachineEngine.cpp:62
rtctk::componentFramework::RtcComponent::OutputStage::m_tmp_update_request
std::shared_ptr< rad::cii::Request< std::string, std::string > > m_tmp_update_request
Definition: rtcComponent.hpp:138
rtctk::componentFramework::RtcComponent::OutputStage::m_no_disable_in_states
std::list< std::string > m_no_disable_in_states
Definition: rtcComponent.hpp:142
rtctk::componentFramework::Payload
std::string Payload
Definition: payload.hpp:19
rtctk::componentFramework::RtcComponent::OutputStage::m_update_success_handler
std::function< void()> m_update_success_handler
Definition: rtcComponent.hpp:139
rtctk::componentFramework::StdComponent::InputStage::InputStage
InputStage(CommandReplier &replier, StateMachineEngine &engine)
Definition: stdComponent.hpp:113
rtctk::componentFramework::StdComponent::OutputStage::m_engine
StateMachineEngine & m_engine
Definition: stdComponent.hpp:382
rtctk::componentFramework::NestedExceptionPrinter::Str
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition: exceptions.hpp:175
rtctk::componentFramework::RtcComponent::BizLogicIf::ActivityUpdating
virtual void ActivityUpdating(StopToken st, Payload args)
Definition: rtcComponent.hpp:37
rtctk::componentFramework::StateMachineEngine::RegisterRejectHandler
void RegisterRejectHandler(std::string const &id, RejectMethod reject)
Register reject handler.
Definition: stateMachineEngine.cpp:95
rtctk::componentFramework::ModelManipulator::AddState
void AddState(StateType type, std::string const &id, std::string const &parent_id="", std::string const &activity_id="", std::string const &entry_action_id="", std::string const &exit_action_id="")
Adds a new state.
Definition: modelManipulator.cpp:29
rtctk::componentFramework::ModelBuilderBase::RegisterLayer
void RegisterLayer(Layer layer)
Definition: modelBuilderBase.hpp:74
stdComponent.hpp
Lifecycle of the 'Standard Component'.
stopToken.hpp
A simple Stop Token.
rtctk::componentFramework::Initial
@ Initial
Definition: model.hpp:26
rtctk::componentFramework::StdComponent::BizLogicIf
Definition: stdComponent.hpp:108
rtctk::componentFramework::RtcComponent::BizLogicIf::GuardUpdatingAllowed
virtual bool GuardUpdatingAllowed(Payload args)
Definition: rtcComponent.hpp:39
rtctk::componentFramework::StdComponent::ModelBuilder
Definition: stdComponent.hpp:389
rtctk::componentFramework::RtcComponentSuper
StdComponent< UseStarting< Enabled >, UseInitialising< Enabled >, UseEnabling< Enabled >, UseDisabling< Enabled > > RtcComponentSuper
Definition: rtcComponent.hpp:25
rtctk::componentFramework::RequestFailed
Definition: stdComponent.hpp:65
rtctk::componentFramework::RtcComponent::ModelBuilder
Definition: rtcComponent.hpp:145
rtctk::componentFramework::RtcComponent::OutputStage::OutputStage
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition: rtcComponent.hpp:57
rtctk::componentFramework::StateMachineEngine::GetState
std::string GetState()
Queries the current state.
Definition: stateMachineEngine.cpp:126
rtctk::componentFramework::RtcComponent::OutputStage::m_no_update_in_states
std::list< std::string > m_no_update_in_states
Definition: rtcComponent.hpp:141
rtctk::componentFramework::StdComponent::OutputStage
Definition: stdComponent.hpp:129
rtctk::componentFramework::RtcComponent::InputStage
Definition: rtcComponent.hpp:44
rtctk::componentFramework::StdComponent::OutputStage::m_logic
BizLogicIf & m_logic
Definition: stdComponent.hpp:383
payload.hpp
Defines an agnostic payload type based on std::string.
rtctk::componentFramework::UpdateCmdsImpl::Register
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition: rtcCmdsImpl.hpp:66
rtctk::componentFramework::StateMachineEngine::PostEvent
void PostEvent(rad::SharedEvent s)
Injects a new event into the state machine engine.
Definition: stateMachineEngine.cpp:121
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
rtctk::componentFramework::StdComponent::InputStage::Start
virtual void Start()
Definition: stdComponent.hpp:120