RTC Toolkit 5.0.0
Loading...
Searching...
No Matches
main.hpp
Go to the documentation of this file.
1
12#ifndef RTCTK_TELSUB_MAIN_HPP
13#define RTCTK_TELSUB_MAIN_HPP
14
15#include "ciiException.hpp"
17#include <boost/interprocess/exceptions.hpp>
18#include <memory>
19#include <memory_resource>
20
26
27namespace rtctk::telSub {
28
48template <class UserTopicType, class DataBlender>
50 auto factory =
51 [blender = std::forward<DataBlender>(blender)](
52 const std::string& name,
53 componentFramework::ServiceContainer& services) -> std::unique_ptr<BusinessLogic> {
54 // Operational Logic factory.
55 // This takes ownership of `blender` and instances created from factory will only receive a
56 // reference. (TBD) (this requires a stateless DataBlender).
57 // If DataBlender is stateful with configuration params then this means that new instances
58 // should be created for each new instance of OperationalLogic?
59 auto op_logic_factory =
60 [&, blender = std::move(blender)](const OperationalLogicFactoryParams& params)
61 -> std::unique_ptr<OperationalLogicIf> {
62 // Allocate a block of memory used by Correlator and DdsWaitSet
63 // The resource and memory ownership is then transferred to OperationalLogic which is
64 // responsible for destroying them last.
65 // clang-format off
66 size_t size = (params.dds_params.m_topics.size() + 1u) * (
67 /* AgnosticDataSamples contains two vectors of dds sequences
68 owned by DdsWaitSet */
69 sizeof(DdsSampleSeq) +
70 sizeof(DdsInfoSeq) +
71 /* DataSamplesView contains vector of views owned by Correlator */
72 sizeof(DataSampleView) +
73 /* Vector of indices in Correlator */
74 sizeof(size_t));
75 // clang-format on
76 std::unique_ptr<std::byte[]> mem(new std::byte[size]);
77 // Both DdsWaitSet and Correlator only allocates once so we can use a simple
78 // monotonic_buffer_resource.
79 auto resource = std::make_unique<std::pmr::monotonic_buffer_resource>(mem.get(), size);
80 // Required by OperationalLogic
81 auto& metrics = services.Get<componentFramework::ComponentMetricsIf>();
82 auto& alerts = services.Get<componentFramework::AlertServiceIf>();
83
84 auto dds_subscriber = std::make_unique<DdsWaitSet>(params.dds_params, resource.get());
85 auto correlator = std::make_unique<Correlator>(
86 params.correlator_params, std::move(dds_subscriber), metrics, resource.get());
87 try {
88 auto writer = ipcq::Writer<UserTopicType>(params.shm_params.topic_name.c_str(),
89 params.shm_params.capacity,
90 params.shm_params.mem_policy);
92 return std::make_unique<OperationalLogic>(params.operational_params,
93 std::move(correlator),
94 std::move(shm_publisher),
95 metrics,
96 alerts,
97 std::move(resource),
98 std::move(mem));
99 } catch (boost::interprocess::interprocess_exception& e) {
102 e,
103 fmt::format("Failed to create SHM queue {}, does it already exist?",
104 params.shm_params.topic_name.c_str()));
105 }
106 };
107
108 return std::make_unique<BusinessLogic>(name, services, std::move(op_logic_factory));
109 };
111}
112
113} // namespace rtctk::telSub
114
115#endif // RTCTK_TELSUB_MAIN_HPP
Alert Service interface.
Definition alertServiceIf.hpp:128
Class used to parse default command line arguments.
Definition rtcComponentArgs.hpp:33
Component metrics interface.
Definition componentMetricsIf.hpp:85
The RtctkException class is the base class for all Rtctk exceptions.
Definition exceptions.hpp:211
Container class that holds services of any type.
Definition serviceContainer.hpp:39
Declares Correlator.
Declares the DdsWaitSet implementation.
Provides macros and utilities for exception handling.
int Main(int argc, char *argv[])
Main function implementation.
Definition rtcComponentMain.hpp:231
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
Definition main.cpp:24
rtctk::componentFramework::AgnosticTopicSeq DdsSampleSeq
Definition agnosticDataSamples.hpp:30
rtctk::componentFramework::SampleInfoSeq DdsInfoSeq
Definition agnosticDataSamples.hpp:32
Implements the business logic for telSub.
Provides core functionality of an RTC Component.
Declares ShmPublisher.
Agnostic data sample non-owning reference type.
Definition dataSampleView.hpp:29
Set of all parameters needed when constructing the OperationalLogic object.
Definition businessLogic.hpp:50