Go to the documentation of this file.
9 #ifndef RTCTK_COMPONENTFRAMEWORK_RTCCOMPONENT_HPP
10 #define RTCTK_COMPONENTFRAMEWORK_RTCCOMPONENT_HPP
23 #include <mal/Cii.hpp>
27 #include <type_traits>
38 template <
class BusinessLogic>
42 template<
typename BusinessLogicFactory>
46 LOG4CPLUS_INFO(
GetLogger(),
"RtcComponent '" << m_component_name <<
"' started.");
50 m_services.
Add<
ServiceDiscovery>(std::make_unique<ServiceDiscovery>(svc_disc_endpoint, m_component_name));
58 m_services.
Add<
OldbApiIf>(std::make_unique<FakeOldbAdapter>(oldb_endpoint));
61 m_replier = std::make_unique<CommandReplier>(req_rep_endpoint);
64 m_publisher = std::make_unique<StatePublisher>(pub_sub_endpoint, m_component_name);
67 static_assert(std::is_base_of_v<RunnableStateMachineLogicIf, BusinessLogic>,
68 "BusinessLogic must implement RunnableStateMachineLogicIf");
70 static_assert(std::is_invocable_v<BusinessLogicFactory, std::string const&, ServiceContainer&>,
71 "Factory must be invocable with 'std::string const&' and 'ServiceContainer&'");
73 static_assert(std::is_same_v<std::invoke_result_t<BusinessLogicFactory, std::string const&, ServiceContainer&>, std::unique_ptr<BusinessLogic>>,
74 "Factory must return type 'std::unique_ptr<BusinessLogic>'");
76 m_business_logic = std::invoke(factory, m_component_name, m_services);
77 m_state_machine = std::make_unique<RunnableStateMachine>(*m_business_logic, [
this](std::string
const& state){OnStateChanged(state);});
78 m_command_handler = std::make_unique<CommandHandler>(*m_replier, *m_state_machine);
84 static_assert(std::is_constructible_v<BusinessLogic, std::string const&, ServiceContainer&>,
85 "BusinessLogic must be constructible with 'std::string const&' and 'ServiceContainer&'");
95 LOG4CPLUS_INFO(
GetLogger(),
"RtcComponent '" << m_component_name <<
"' terminated.");
100 m_command_handler->Start();
101 m_state_machine->Work();
106 void OnStateChanged(
const std::string& state) {
107 m_publisher->PublishState(state);
117 std::string m_component_name;
118 ServiceContainer m_services;
119 std::unique_ptr<CommandReplier> m_replier;
120 std::unique_ptr<StatePublisher> m_publisher;
121 std::unique_ptr<BusinessLogic> m_business_logic;
122 std::unique_ptr<RunnableStateMachine> m_state_machine;
123 std::unique_ptr<CommandHandler> m_command_handler;
139 template <
class BusinessLogic,
class BusinessLogicFactory>
156 template <
class BusinessLogic>
159 static_assert(std::is_constructible_v<BusinessLogic, std::string const&, ServiceContainer&>,
160 "BusinessLogic must be constructible with 'std::string const&' and 'ServiceContainer&'");
161 RunAsRtcComponent<BusinessLogic>(args, std::make_unique<BusinessLogic, std::string const&, ServiceContainer&>);
180 int main(
int argc,
char *argv[])
188 Args args{argc, argv};
191 auto log_props_file = args.GetLogPropsFile();
193 if(log_props_file.has_value()) {
194 auto props_file = log_props_file.value().path().to_string();
197 LOG4CPLUS_INFO(
GetLogger(),
"Custom log properties file loaded: '" << props_file);
200 MakeLogger(component_name, log4cplus::INFO_LOG_LEVEL);
209 std::cout << e.what() << std::endl;
212 std::cout << e.what() << std::endl;
213 }
catch(std::exception& e) {
216 LOG4CPLUS_FATAL(
GetLogger(),
"Unknown exception occured!");
A container that can hold any type of service.
std::string GetTypeName(const std::type_info &tid)
Definition: rtcComponent.hpp:105
Receive commands via MAL.
void SetDataPoint(const DataPointPath &path, const T value)
Sets a datapoint in the repository.
Definition: repositoryIf.hpp:407
void RtcComponentMain(rtctk::componentFramework::Args const &args)
Definition: main.cpp:18
Definition: rtcComponent.hpp:39
Definition: rtcComponent.hpp:20
void MakeLogger(const std::string &name, log4cplus::LogLevel ll, bool log_to_file=true, bool additive=false)
Service & Get(const std::string &name="")
Definition: serviceContainer.hpp:82
Definition: rtcComponent.hpp:27
void LogConfigure(const std::string &cfg_file_name="")
void Add(Service &&service, const std::string &name="")
Definition: serviceContainer.hpp:45
Publishes the stdif state topic via MAL.
detail::Args Args
Definition: rtcComponent.hpp:31
Definition: rtcComponent.hpp:17
Class that implements a very basic service discover mechanism.
static const DataPointPath RUNTIME_REPO_ENDPOINT
Definition: serviceDiscovery.hpp:32
virtual bool DataPointExists(const DataPointPath &path) const =0
Checks for the existence of a datapoint in the repository.
Definition: serviceDiscovery.hpp:25
Definition: rtcComponent.hpp:34
Provides details for core functionality of an RTC Component.
RtcComponent(RtcComponent &&other)=delete
::elt::mal::Uri Get(const std::string &service_name)
virtual void CreateDataPoint(const DataPointPath &path, const std::string &type)=0
Creates a new datapoint in the repository with a specified type.
int main(int argc, char *argv[])
Definition: rtcComponent.hpp:180
Header file needed to instantiate FakeRuntimeRepoAdapter in test components.
Receive commands via MAL and inject events to state machine.
Definition: logger.hpp:20
log4cplus::Logger & GetLogger(const std::string &name="")
~RtcComponent()
Definition: rtcComponent.hpp:93
static const DataPointPath REQ_REP_ENDPOINT
Definition: serviceDiscovery.hpp:30
Definition: oldbApiIf.hpp:18
Definition: serviceContainer.hpp:31
RtcComponent(int argc, char *argv[])
Definition: rtcComponent.hpp:81
RtcComponent(Args const &args, BusinessLogicFactory factory)
Definition: rtcComponent.hpp:43
Definition: runtimeRepoApiIf.hpp:59
std::string GetComponentName() const
Definition: rtcComponent.hpp:43
void Run()
Definition: rtcComponent.hpp:98
elt::mal::Uri GetServiceDiscEndpoint() const
Definition: rtcComponent.hpp:47
Header file needed to instantiate FakeOldbAdapter in test components.
Definition: mudpiProcessingError.hpp:109
RtcComponent(const RtcComponent &other)=delete
Logging Support Library based on log4cplus.
void SetDefaultLogger(const std::string &name)
Definition: logger.cpp:32
Definition: dataPointPath.hpp:30
static const DataPointPath PUB_SUB_ENDPOINT
Definition: serviceDiscovery.hpp:31
void RunAsRtcComponent(Args const &args, BusinessLogicFactory factory)
Definition: rtcComponent.hpp:140
static const DataPointPath OLDB_ENDPOINT
Definition: serviceDiscovery.hpp:34
RtcComponent & operator=(const RtcComponent &other)=delete
The Runnable Statemachine.