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