RTC Toolkit 6.0.0-pre2
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
16#include <boost/interprocess/exceptions.hpp>
17#include <memory>
18#include <memory_resource>
19
25
26namespace rtctk::telSub {
27
47template <class UserTopicType, class DataBlender>
48void Main(const rtctk::componentFramework::Args& args, DataBlender&& blender) {
49 auto factory =
50 [blender = std::forward<DataBlender>(blender)](
51 const std::string& name,
52 componentFramework::ServiceContainer& services) -> std::unique_ptr<BusinessLogic> {
53 // Operational Logic factory.
54 // This takes ownership of `blender` and instances created from factory will only receive a
55 // reference. (TBD) (this requires a stateless DataBlender).
56 // If DataBlender is stateful with configuration params then this means that new instances
57 // should be created for each new instance of OperationalLogic?
58 auto op_logic_factory =
59 [&, blender = std::move(blender)](const OperationalLogicFactoryParams& params)
60 -> std::unique_ptr<OperationalLogicIf> {
61 // Allocate a block of memory used by Correlator and DdsWaitSet
62 // The resource and memory ownership is then transferred to OperationalLogic which is
63 // responsible for destroying them last.
64 // clang-format off
65 size_t size = (params.dds_params.m_topics.size() + 1u) * (
66 /* AgnosticDataSamples contains two vectors of dds sequences
67 owned by DdsWaitSet */
68 sizeof(DdsSampleSeq) +
69 sizeof(DdsInfoSeq) +
70 /* DataSamplesView contains vector of views owned by Correlator */
71 sizeof(DataSampleView) +
72 /* Vector of indices in Correlator */
73 sizeof(size_t));
74 // clang-format on
75 std::unique_ptr<std::byte[]> mem = std::make_unique_for_overwrite<std::byte[]>(size);
76 // Both DdsWaitSet and Correlator only allocates once so we can use a simple
77 // monotonic_buffer_resource.
78 auto resource = std::make_unique<std::pmr::monotonic_buffer_resource>(mem.get(), size);
79 // Required by OperationalLogic
80 auto& metrics = services.Get<componentFramework::ComponentMetricsIf>();
81 auto& alerts = services.Get<componentFramework::AlertServiceIf>();
82
83 auto dds_subscriber = std::make_unique<DdsWaitSet>(params.dds_params, resource.get());
84 auto correlator = std::make_unique<Correlator>(
85 params.correlator_params, std::move(dds_subscriber), metrics, resource.get());
86 try {
87 auto writer = ipcq::Writer<UserTopicType>(params.shm_params.topic_name.c_str(),
88 params.shm_params.capacity,
89 params.shm_params.mem_policy);
90 auto shm_publisher = MakeShmPublisher<UserTopicType>(std::move(writer), blender);
91 return std::make_unique<OperationalLogic>(params.operational_params,
92 std::move(correlator),
93 std::move(shm_publisher),
94 metrics,
95 alerts,
96 std::move(resource),
97 std::move(mem));
98 } catch (boost::interprocess::interprocess_exception& e) {
99 throw CII_MAKE_EXCEPTION_WITH_NESTED(
101 e,
102 fmt::format("Failed to create SHM queue {}, does it already exist?",
103 params.shm_params.topic_name.c_str()));
104 }
105 };
106
107 return std::make_unique<BusinessLogic>(name, services, std::move(op_logic_factory));
108 };
110}
111
112} // namespace rtctk::telSub
113
114#endif // RTCTK_TELSUB_MAIN_HPP
Alert Service interface.
Definition alertServiceIf.hpp:138
Class used to parse default command line arguments.
Definition rtcComponentArgs.hpp:33
Component metrics interface.
Definition componentMetricsIf.hpp:164
The RtctkException class is the base class for all Rtctk exceptions.
Definition exceptions.hpp:213
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.
void RunAsRtcComponent(const Args &args, BLF factory)
RTC Component runner function, needed to run custom BusinessLogic as RTC Component.
Definition rtcComponentMain.hpp:74
int Main(int argc, char *argv[])
Main function implementation.
Definition rtcComponentMain.hpp:249
Definition main.cpp:24
rtctk::componentFramework::AgnosticTopicSeq DdsSampleSeq
Definition agnosticDataSamples.hpp:30
auto MakeShmPublisher(ShmWriter &&shm_writer, DataBlender &blender) -> std::unique_ptr< ShmPublisher< UserTopicType, DataBlender, ShmWriter > >
Helper that can deduce DataBlender class template argument.
Definition shmPublisher.hpp:59
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