RTC Toolkit  2.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 
22 // High level excepiton types returned via MAL
23 
24 // somebody send a stop or abort command
25 class RequestAborted : public rtctkif::ExceptionErr {
26 public:
27  static constexpr int32_t ERROR_CODE = 11;
29  : rtctkif::ExceptionErr("Request aborted.", ERROR_CODE) {
30  }
31 };
32 
33 // command is not allowed in current state or guard
34 class RequestRejected : public rtctkif::ExceptionErr {
35 public:
36  static constexpr int32_t ERROR_CODE = 10;
37  RequestRejected(std::string const& request_id, std::string const& state_id)
38  : rtctkif::ExceptionErr("Request " + request_id + " rejected in state " + state_id,
39  ERROR_CODE) {
40  }
41 };
42 
43 // the command was accepted but the task to do failed
44 class RequestFailed : public rtctkif::ExceptionErr {
45 public:
49  static constexpr int32_t ERROR_CODE = 501;
50  RequestFailed(std::string const& message)
51  : rtctkif::ExceptionErr(message, ERROR_CODE) {
52  }
53 };
54 
56 
58  UseInitialising<Enabled>,
59  UseEnabling<Enabled>,
60  UseDisabling<Enabled>>;
61 
69 
70  class BizLogicIf : public Super::BizLogicIf {
71  public:
72  virtual void ActivityUpdating(StopToken st, Payload args) {
73  }
74  virtual bool GuardUpdatingAllowed(Payload args) {
75  return true;
76  }
77  };
78 
79  class InputStage : public Super::InputStage {
80  public:
82 
83  void Start() override {
85 
87  }
88  };
89 
91  public:
93  // Handlers ###################################################################
94 
95  engine.RegisterRejectHandler<events::Update, RequestRejected>();
96 
97  m_update_success_handler = [this] {
98  this->m_engine.PostEvent(std::make_unique<events::UpdateDone>());
99  };
100 
101  m_update_error_handler = [this](std::exception_ptr eptr) {
102  this->m_engine.PostEvent(std::make_unique<events::UpdateError>(eptr));
103  };
104 
105  // No Disable in states ###############################################################
106 
107  m_no_disable_in_states.push_back("On:Operational:UpdateBusy");
108 
109  // Actions ############################################################################
110 
111  engine.RegisterAction("ActionUpdatingEntry", [this](auto c) {
112  m_tmp_update_request = GetPayloadNothrow<events::Update>(c);
113  });
114 
115  engine.RegisterAction("ActionUpdatingDone", [this](auto c) {
116  if (m_tmp_update_request) {
117  m_tmp_update_request->SetReplyValue(STD_OK_REPLY);
118  m_tmp_update_request = nullptr;
119  }
120  });
121 
122  engine.RegisterAction("ActionUpdatingFailed", [this](auto c) {
123  if(auto eptr = GetPayloadNothrow<events::UpdateError>(c); eptr) {
124  if (m_tmp_update_request) {
125  m_tmp_update_request->SetException(
127  m_tmp_update_request = nullptr;
128  }
129  }
130  });
131 
132  // Guards ############################################################################
133 
134  engine.RegisterGuard("GuardUpdatingAllowed", [this](auto c) {
135  auto req = GetPayloadNothrow<events::Update>(c);
136  if (req == nullptr) {
137  // no event or request
138  return false;
139  }
140  std::string args = req->GetRequestPayload();
141  // TODO: parse the args and present them nicely to the BL
142 
143  auto act_state = this->m_engine.GetState();
144  for (auto& s : m_no_update_in_states) {
145  if (act_state.find(s) != std::string::npos) {
146  return false;
147  }
148  }
149  return static_cast<BizLogicIf&>(this->m_logic).GuardUpdatingAllowed(args);
150  });
151 
152  engine.RegisterGuard("GuardDisablingAllowed", [this](auto c) {
153  auto act_state = this->m_engine.GetState();
154  for (auto& s : m_no_disable_in_states) {
155  if (act_state.find(s) != std::string::npos) {
156  return false;
157  }
158  }
159  return true;
160  });
161 
162  // Activities #####################################################################
163 
164  engine.RegisterActivity("ActivityUpdating",
165  [this](StopToken stop_token) {
166  std::string args = m_tmp_update_request->GetRequestPayload();
167  // TODO: parse the args and present them nicely to the BL
168  static_cast<BizLogicIf&>(this->m_logic).ActivityUpdating(stop_token, args);
170  }
171 
172  protected:
173  std::shared_ptr<rad::cii::Request<std::string, std::string>> m_tmp_update_request;
174  std::function<void()> m_update_success_handler;
175  std::function<void(std::exception_ptr)> m_update_error_handler;
176  std::list<std::string> m_no_update_in_states;
177  std::list<std::string> m_no_disable_in_states;
178  };
179 
181  public:
183  this->RegisterLayer({"RtcComponent", {}});
184 
185  const std::string parent_region = "On:Operational:RegionUpdate";
186 
187  this->mm.ModStateType("On:Operational", Parallel);
188  this->mm.AddState(Composite, parent_region, "On:Operational");
189 
190  this->mm.AddState(Initial, "On:Operational:UpdateInitial", parent_region);
191  this->mm.AddState(Simple, "On:Operational:UpdateIdle", parent_region);
192  this->mm.AddState(Simple, "On:Operational:UpdateBusy", parent_region , "ActivityUpdating" ,"ActionUpdatingEntry");
193 
194  this->mm.AddTrans("On:Operational:UpdateInitial" , "On:Operational:UpdateIdle" );
195  this->mm.AddTrans("On:Operational:UpdateIdle" , "On:Operational:UpdateBusy" , "events.Update", "GuardUpdatingAllowed");
196  this->mm.AddTrans("On:Operational:UpdateBusy" , "On:Operational:UpdateIdle" , "events.UpdateDone", "" ,"ActionUpdatingDone" );
197  this->mm.AddTrans("On:Operational:UpdateBusy" , "On:Operational:UpdateIdle" , "events.UpdateError", "" ,"ActionUpdatingFailed" );
198 
199  this->mm.ModTransGuard("On:Operational", "On:NotOperational:Disabling", "events.Disable", "", "GuardDisablingAllowed");
200  }
201  };
202 };
203 
204 } // namespace rtctk::componentFramework
205 
206 #endif // RTCTK_COMPONENTFRAMEWORK_RTCCOMPONENT_HPP
rtctk::componentFramework::RtcComponent::BizLogicIf
Definition: rtcComponent.hpp:70
rtctk::componentFramework::Simple
@ Simple
Definition: model.hpp:23
rtctk::componentFramework::RequestRejected
Definition: rtcComponent.hpp:34
rtctk::componentFramework::RtcComponent::OutputStage
Definition: rtcComponent.hpp:90
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:67
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:182
rtctk::componentFramework::StdComponent::InputStage::m_replier
CommandReplier & m_replier
Definition: stdComponent.hpp:172
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:175
rtctk::componentFramework::RequestAborted::ERROR_CODE
static constexpr int32_t ERROR_CODE
Definition: rtcComponent.hpp:27
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:173
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::StdComponent::InputStage
Definition: stdComponent.hpp:158
rtcCmdsImpl.hpp
Implementation of MAL commands for layer 'RtcComponent'.
rtctk::componentFramework::RequestFailed::ERROR_CODE
static constexpr int32_t ERROR_CODE
Definition: rtcComponent.hpp:49
rtctk::componentFramework::StateMachineEngine::RegisterGuard
void RegisterGuard(std::string const &id, GuardMethod guard)
Register guard.
Definition: stateMachineEngine.cpp:77
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
Definition: stateMachineEngine.hpp:31
rtctk::componentFramework::StdComponent
Basic life cycle for StdComponent.
Definition: stdComponent.hpp:138
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::RequestRejected::RequestRejected
RequestRejected(std::string const &request_id, std::string const &state_id)
Definition: rtcComponent.hpp:37
rtctk::componentFramework::RtcComponent::InputStage::Start
void Start() override
Definition: rtcComponent.hpp:83
rtctk::componentFramework::StateMachineEngine::RegisterAction
void RegisterAction(std::string const &id, ActionMethod action)
Register action.
Definition: stateMachineEngine.cpp:65
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:173
rtctk_config_tool.message
message
Definition: rtctk_config_tool.py:42
rtctk::componentFramework::RtcComponent::OutputStage::m_no_disable_in_states
std::list< std::string > m_no_disable_in_states
Definition: rtcComponent.hpp:177
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:174
rtctk::componentFramework::StdComponent::InputStage::InputStage
InputStage(CommandReplier &replier, StateMachineEngine &engine)
Definition: stdComponent.hpp:160
rtctk::componentFramework::StdComponent::OutputStage::m_engine
StateMachineEngine & m_engine
Definition: stdComponent.hpp:447
rtctk::componentFramework::NestedExceptionPrinter::Str
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition: exceptions.hpp:175
rtctk::componentFramework::RequestFailed::RequestFailed
RequestFailed(std::string const &message)
Definition: rtcComponent.hpp:50
rtctk::componentFramework::RtcComponent::BizLogicIf::ActivityUpdating
virtual void ActivityUpdating(StopToken st, Payload args)
Definition: rtcComponent.hpp:72
rtctk::componentFramework::StateMachineEngine::RegisterRejectHandler
void RegisterRejectHandler(std::string const &id, RejectMethod reject)
Register reject handler.
Definition: stateMachineEngine.cpp:98
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:155
rtctk::componentFramework::RtcComponent::BizLogicIf::GuardUpdatingAllowed
virtual bool GuardUpdatingAllowed(Payload args)
Definition: rtcComponent.hpp:74
rtctk::componentFramework::StdComponent::ModelBuilder
Definition: stdComponent.hpp:454
rtctk::componentFramework::RtcComponentSuper
StdComponent< UseStarting< Enabled >, UseInitialising< Enabled >, UseEnabling< Enabled >, UseDisabling< Enabled > > RtcComponentSuper
Definition: rtcComponent.hpp:60
rtctk::componentFramework::RequestFailed
Definition: rtcComponent.hpp:44
rtctk::componentFramework::RtcComponent::ModelBuilder
Definition: rtcComponent.hpp:180
rtctk::componentFramework::RequestAborted::RequestAborted
RequestAborted()
Definition: rtcComponent.hpp:28
rtctk::componentFramework::RtcComponent::OutputStage::OutputStage
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition: rtcComponent.hpp:92
mudpi::int32_t
int int32_t
Definition: mudpi.h:17
rtctk::componentFramework::StateMachineEngine::GetState
std::string GetState()
Queries the current state.
Definition: stateMachineEngine.cpp:129
rtctk::componentFramework::RequestAborted
Definition: rtcComponent.hpp:25
rtctk::componentFramework::RtcComponent::OutputStage::m_no_update_in_states
std::list< std::string > m_no_update_in_states
Definition: rtcComponent.hpp:176
rtctk::componentFramework::StdComponent::OutputStage
Definition: stdComponent.hpp:176
rtctk::componentFramework::RtcComponent::InputStage
Definition: rtcComponent.hpp:79
rtctk::componentFramework::StdComponent::OutputStage::m_logic
BizLogicIf & m_logic
Definition: stdComponent.hpp:448
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:124
rtctk::componentFramework::Composite
@ Composite
Definition: model.hpp:25
rtctk::componentFramework::RequestRejected::ERROR_CODE
static constexpr int32_t ERROR_CODE
Definition: rtcComponent.hpp:36
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::StdComponent::InputStage::Start
virtual void Start()
Definition: stdComponent.hpp:167