RTC Toolkit  2.0.0
recordingUnit.hpp
Go to the documentation of this file.
1 
11 #ifndef RTCTK_COMPONENTFRAMEWORK_RECORDINGUNIT_HPP
12 #define RTCTK_COMPONENTFRAMEWORK_RECORDINGUNIT_HPP
13 
15 #include <boost/signals2.hpp>
16 #include <filesystem>
17 #include <optional>
18 #include <shared_mutex>
19 #include <atomic>
20 
21 namespace rtctk::componentFramework {
22 
23 class RecordingUnit;
24 class ServiceContainer;
25 
26 using RecUnitListType = std::list<std::unique_ptr<RecordingUnit>>;
27 using RecUnitFactoryType = std::function<RecUnitListType(std::string const&, ServiceContainer&)>;
28 
34 template <class T, class... Args>
35 void AddRecUnit(RecUnitListType& units, Args&&... args) {
36  units.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
37 }
38 
43  public:
45  explicit InvalidStateChange(const std::string& message) :
46  RtctkException("Invalid Operation: " + message) {}
47 };
48 
53  public:
55  explicit InvalidSetting(const std::string& message) :
56  RtctkException("Invalid Setting: " + message) {}
57 };
58 
63 // TODO: it makes sense to use ComponentMetrics to publish a datapoint with the recorded samples
64 // for each recording unit, this helps for debugging and performance analysis.
66 public:
68 
76  RecordingUnit( std::string const& comp_id,
77  std::string const& unit_id,
78  RepositoryIf& rtr,
79  RepositoryIf& oldb);
80 
81  RecordingUnit() = delete;
82 
83  virtual ~RecordingUnit() = default;
84 
86 
90  std::string GetId();
91 
95  State GetState();
96 
100  void CheckErrors();
105  virtual void Prepare(const std::filesystem::path& file_path) = 0;
109  virtual void Start() = 0;
114  virtual std::optional<std::filesystem::path> Stop() = 0;
118  virtual void Update();
119 
125  boost::signals2::connection
126  Connect(const std::function<void(std::string const&, State const&)>& function);
127 
128 protected:
135  bool SetState(State state, State precondition);
136 
144  void SetState(State state, State precondition, const std::string& error_message);
145 
149  void SetFailed(const std::exception_ptr& exception);
153  void SetStopped();
158  bool HasLeaders();
163  bool HasFirstLeaderStarted();
168  bool HasLastLeaderFinished();
169  void ResetLeaderStates();
170 
171  std::string m_comp_id;
172  std::string m_unit_id;
175  std::optional<std::filesystem::path> m_file_path;
176  boost::signals2::signal<void(std::string const&, State const&)> m_signal;
177 
178 private:
179  std::mutex m_leader_mutex;
180  std::atomic<State> m_state;
181  std::exception_ptr m_exception = nullptr;
182  std::shared_mutex m_exception_mutex;
183  std::vector<std::string> m_leaders;
184  std::map<std::string, State> m_leader_states;
185  bool m_first_leader_started = false;
186  bool m_last_leader_finished = false;
187 };
188 
189 } // namespace rtctk::componentFramework
190 
191 #endif // RTCTK_COMPONENTFRAMEWORK_RECORDINGUNIT_HPP
rtctk::componentFramework::RecordingUnit::Connect
boost::signals2::connection Connect(const std::function< void(std::string const &, State const &)> &function)
Connect to State change signal.
Definition: recordingUnit.cpp:162
rtctk::componentFramework::RecordingUnit::m_unit_id
std::string m_unit_id
Definition: recordingUnit.hpp:172
rtctk::componentFramework::RecordingUnit::Update
virtual void Update()
Update dynamic settings.
Definition: recordingUnit.cpp:157
rtctk::componentFramework::InvalidStateChange
This Exception is raised when the state change requested is invalid.
Definition: recordingUnit.hpp:42
rtctk::componentFramework::RecUnitListType
std::list< std::unique_ptr< RecordingUnit > > RecUnitListType
Definition: recordingUnit.hpp:26
rtctk::componentFramework::RecordingUnit::ConnectWithLeaders
void ConnectWithLeaders(RecUnitListType &units)
Definition: recordingUnit.cpp:51
rtctk::componentFramework::RecordingUnit::m_signal
boost::signals2::signal< void(std::string const &, State const &)> m_signal
Definition: recordingUnit.hpp:176
rtctk::componentFramework::RecordingUnit::HasLastLeaderFinished
bool HasLastLeaderFinished()
This function is used to determine if this unit should stop recording when waiting for leaders.
Definition: recordingUnit.cpp:101
rtctk::componentFramework::RtctkException::RtctkException
RtctkException() noexcept
Definition: exceptions.cpp:99
rtctk::componentFramework::InvalidSetting::InvalidSetting
InvalidSetting(const std::string &message)
Definition: recordingUnit.hpp:55
rtctk::componentFramework::RecordingUnit::m_comp_id
std::string m_comp_id
Definition: recordingUnit.hpp:171
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::RecordingUnit::Stop
virtual std::optional< std::filesystem::path > Stop()=0
Stop the recording and wait for it's termination.
rtctk::componentFramework::RecordingUnit::~RecordingUnit
virtual ~RecordingUnit()=default
rtctk::componentFramework::RecordingUnit::m_rtr
RepositoryIf & m_rtr
Definition: recordingUnit.hpp:173
rtctk::componentFramework::RecordingUnit::m_file_path
std::optional< std::filesystem::path > m_file_path
Definition: recordingUnit.hpp:175
rtctk::componentFramework::detail::Args
Definition: rtcComponent.hpp:38
rtctk::componentFramework::RtctkException
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
rtctk::componentFramework::RecordingUnit::GetState
State GetState()
Get the current state of the Recording Unit.
Definition: recordingUnit.cpp:146
rtctk::componentFramework::RecordingUnit::SetState
bool SetState(State state, State precondition)
Sets the new state, only goes to new state, if expected state matches.
Definition: recordingUnit.cpp:119
rtctk::componentFramework::RecordingUnit
Abstract base class for all sources that can be recorded by the MetadataCollector and TelemetryRecord...
Definition: recordingUnit.hpp:65
rtctk::componentFramework::RecordingUnit::RecordingUnit
RecordingUnit()=delete
repositoryIf.hpp
Header file for RepositoryIf and related base classes.
rtctk::componentFramework::RecordingUnit::State::FINISHED
@ FINISHED
rtctk::componentFramework::RecordingUnit::State::IDLE
@ IDLE
rtctk::componentFramework::RecordingUnit::HasLeaders
bool HasLeaders()
Check if this unit is following any leaders.
Definition: recordingUnit.cpp:92
rtctk::componentFramework::RecordingUnit::Start
virtual void Start()=0
Start the recording.
rtctk_config_tool.message
message
Definition: rtctk_config_tool.py:42
rtctk::componentFramework::ServiceContainer
Container class that holds services of any type.
Definition: serviceContainer.hpp:35
rtctk::componentFramework::RecordingUnit::CheckErrors
void CheckErrors()
Check for Errors, will rethrow errors thrown in the recorder.
Definition: recordingUnit.cpp:150
rtctk::componentFramework::RecordingUnit::SetFailed
void SetFailed(const std::exception_ptr &exception)
Set the unit into failed state, with the given exception.
Definition: recordingUnit.cpp:134
rtctk::componentFramework::RepositoryIf
Abstract interface providing basic read and write facilities to a repository.
Definition: repositoryIf.hpp:34
rtctk::componentFramework::RecordingUnit::State
State
Definition: recordingUnit.hpp:67
rtctk_ctrl_mon_tool.state
def state(ctx, str component)
Definition: rtctk_ctrl_mon_tool.py:158
rtctk::componentFramework::InvalidSetting
This Exception is raised when a invalid setting was used in the runtime repo.
Definition: recordingUnit.hpp:52
rtctk::componentFramework::RecordingUnit::HasFirstLeaderStarted
bool HasFirstLeaderStarted()
This function is used to determine if this unit should start recording when waiting for leaders.
Definition: recordingUnit.cpp:96
rtctk::componentFramework::RecordingUnit::State::WAITING
@ WAITING
rtctk::componentFramework::RecordingUnit::GetId
std::string GetId()
Get the unit_it of this RecordingUnit.
Definition: recordingUnit.cpp:115
rtctk::componentFramework::RecordingUnit::State::RUNNING
@ RUNNING
rtctk::componentFramework::RecordingUnit::m_oldb
RepositoryIf & m_oldb
Definition: recordingUnit.hpp:174
rtctk::componentFramework::InvalidStateChange::InvalidStateChange
InvalidStateChange(const std::string &message)
Definition: recordingUnit.hpp:45
rtctk::componentFramework::RecordingUnit::State::STOPPED
@ STOPPED
rtctk::componentFramework::RecordingUnit::SetStopped
void SetStopped()
Set the Unit state to STOPPED independent of the current State.
Definition: recordingUnit.cpp:141
rtctk::componentFramework::RecordingUnit::ResetLeaderStates
void ResetLeaderStates()
Definition: recordingUnit.cpp:106
rtctk::componentFramework::AddRecUnit
void AddRecUnit(RecUnitListType &units, Args &&... args)
Add a RecordingUnit to the RecordingUnit list of a TelemetryRecorder oder MetadataCollector.
Definition: recordingUnit.hpp:35
rtctk::componentFramework::RecordingUnit::State::FAILED
@ FAILED
rtctk::componentFramework::RecordingUnit::Prepare
virtual void Prepare(const std::filesystem::path &file_path)=0
Prepare the recording.
rtctk::componentFramework::RecUnitFactoryType
std::function< RecUnitListType(std::string const &, ServiceContainer &)> RecUnitFactoryType
Definition: recordingUnit.hpp:27
rtctk::componentFramework::RecordingUnit::State::PREPARING
@ PREPARING