RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
ddtForwarder.hpp
Go to the documentation of this file.
1
11
12#ifndef DDT_FORWARDER_HPP
13#define DDT_FORWARDER_HPP
14
19
20#include <fmt/format.h>
21
22#include <atomic>
23#include <functional>
24#include <list>
25#include <memory>
26#include <set>
27#include <string>
28
29namespace rtctk::ddtServer {
30
31class DdtForwarder;
32
33using DdtForwarderListType = std::list<std::unique_ptr<DdtForwarder>>;
36
37template <class T, class... Args>
38void AddDdtForwarder(DdtForwarderListType& units, Args&&... args) {
39 units.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
40}
41
46public:
49
53 enum class State : uint8_t { STOPPED, STARTING, IDLE, RUNNING, ERROR };
54
55 DdtForwarder(const std::string& comp_id,
56 const std::string& fwd_type,
57 const std::string& fwd_id,
58 ServiceContainer& services)
60 , m_oldb(services.Get<OldbIf>())
61 , m_comp_id(comp_id)
62 , m_fwd_id(fwd_id)
63 , m_state(State::STOPPED)
64 , m_oldb_prefix(fmt::format("/{}/forwarders/{}", comp_id, fwd_id)) {
65 using namespace rtctk::componentFramework;
66
67 LOG4CPLUS_INFO(m_logger, fmt::format("Creating {} '{}'", fwd_type, fwd_id));
68
69 auto state_dp_path = DataPointPath(m_oldb_prefix + "/state");
70 m_oldb.CreateDataPoint<std::string>(state_dp_path);
71 m_oldb.SetDataPoint(state_dp_path, m_state_text.at(m_state));
72
73 auto type_dp_path = DataPointPath(m_oldb_prefix + "/type");
74 m_oldb.CreateDataPoint<std::string>(type_dp_path);
75 m_oldb.SetDataPoint(type_dp_path, fwd_type);
76 }
77
78 virtual ~DdtForwarder() = default;
79
83 const std::string& GetId() const {
84 return m_fwd_id;
85 }
86
90 State GetState() const {
91 return m_state;
92 }
93
97 virtual void Start() = 0;
98
102 virtual void Run() = 0;
103
107 virtual void Idle() = 0;
108
112 virtual void Stop() = 0;
113
117 virtual void Recover() = 0;
118
122 virtual void Update() = 0;
123
127 virtual void CheckErrors() = 0;
128
129protected:
130 virtual void SetState(State state) {
131 using namespace rtctk::componentFramework;
132
133 m_state = state;
134
135 try {
136 auto state_dp_path = DataPointPath(m_oldb_prefix + "/state");
137 m_oldb.SetDataPoint<std::string>(state_dp_path, m_state_text.at(state));
138 } catch (...) {
139 LOG4CPLUS_ERROR(m_logger, "DdtForwarder: Failed to publish state to OLDB.");
140 }
141 }
142
143 void AssertState(const std::set<State>& states) {
144 using namespace rtctk::componentFramework;
145
146 if (states.find(m_state) == states.end()) {
147 std::string state_texts;
148 for (auto s : states) {
149 state_texts += m_state_text.at(s) + " ";
150 }
151 CII_THROW(RtctkException,
152 fmt::format("State '{}' does not fulfill precondition '{}'.",
153 m_state_text.at(m_state),
154 state_texts));
155 }
156 }
157
158 const std::map<State, std::string> m_state_text = {
159 {State::STOPPED, "Stopped"},
160 {State::STARTING, "Starting"},
161 {State::IDLE, "Idle"},
162 {State::RUNNING, "Running"},
163 {State::ERROR, "Error"},
164 }; //< States names
165
166 log4cplus::Logger& m_logger;
167
168private:
169 OldbIf& m_oldb;
170
171 std::string m_comp_id;
172 std::string m_fwd_id;
173 std::atomic<State> m_state;
174
175 std::string m_oldb_prefix;
176};
177
178} // namespace rtctk::ddtServer
179
180#endif // DDT_FORWARDER
Class used to parse default command line arguments.
Definition rtcComponentArgs.hpp:32
This class provides a wrapper for a data point path.
Definition dataPointPath.hpp:76
Base interface for all OLDB adapters.
Definition oldbIf.hpp:24
The RtctkException class is the base class for all Rtctk exceptions.
Definition exceptions.hpp:220
Container class that holds services of any type.
Definition serviceContainer.hpp:38
Base class defining common interface for all DDT forwarders.
Definition ddtForwarder.hpp:45
const std::map< State, std::string > m_state_text
Definition ddtForwarder.hpp:158
DdtForwarder(const std::string &comp_id, const std::string &fwd_type, const std::string &fwd_id, ServiceContainer &services)
Definition ddtForwarder.hpp:55
virtual void Stop()=0
Stop the processing thread of the forwarder unit.
virtual void Update()=0
Reload dynamic configuration of the forwarder unit.
virtual void Start()=0
Start the processing thread of the forwarder unit.
void AssertState(const std::set< State > &states)
Definition ddtForwarder.hpp:143
virtual void Idle()=0
Stop publishing DDT streams.
virtual void SetState(State state)
Definition ddtForwarder.hpp:130
State
States a forwarder unit can be in.
Definition ddtForwarder.hpp:53
@ STARTING
Definition ddtForwarder.hpp:53
@ STOPPED
Definition ddtForwarder.hpp:53
@ RUNNING
Definition ddtForwarder.hpp:53
@ IDLE
Definition ddtForwarder.hpp:53
@ ERROR
Definition ddtForwarder.hpp:53
log4cplus::Logger & m_logger
Definition ddtForwarder.hpp:166
State GetState() const
Get the state of the forwarder unit.
Definition ddtForwarder.hpp:90
rtctk::componentFramework::ServiceContainer ServiceContainer
Definition ddtForwarder.hpp:47
virtual ~DdtForwarder()=default
virtual void Recover()=0
Stop the processing thread of the forwarder unit and clear errors.
rtctk::componentFramework::OldbIf OldbIf
Definition ddtForwarder.hpp:48
const std::string & GetId() const
Get identifier of the forwarder unit.
Definition ddtForwarder.hpp:83
virtual void CheckErrors()=0
Check for Errors, will rethrow errors thrown in the forwarder.
virtual void Run()=0
Start publishing DDT streams.
Provides macros and utilities for exception handling.
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:191
Logging Support Library based on log4cplus.
Definition dataPointPath.hpp:464
Definition commandReplier.cpp:21
Definition businessLogic.cpp:23
void AddDdtForwarder(DdtForwarderListType &units, Args &&... args)
Definition ddtForwarder.hpp:38
std::function< DdtForwarderListType( const std::string &, rtctk::componentFramework::ServiceContainer &)> DdtForwarderFactoryType
Definition ddtForwarder.hpp:34
std::list< std::unique_ptr< DdtForwarder > > DdtForwarderListType
Definition ddtForwarder.hpp:33
Header file for OldbIf, which defines the API for OldbAdapters.
A container that can hold any type of service.