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