RTC Toolkit  2.0.0
main.hpp
Go to the documentation of this file.
1 
11 #ifndef RTCTK_TELSUB_MAIN_HPP
12 #define RTCTK_TELSUB_MAIN_HPP
13 
14 #include <memory>
15 #include <memory_resource>
16 
22 
23 namespace rtctk::telSub {
24 
44 template <class UserTopicType, class DataBlender>
45 void Main(const rtctk::componentFramework::Args& args, DataBlender&& blender) noexcept {
46  auto factory =
47  [blender = std::forward<DataBlender>(blender)](
48  std::string const& name,
49  componentFramework::ServiceContainer& services) -> std::unique_ptr<BusinessLogic> {
50  // Operational Logic factory.
51  // This takes ownership of `blender` and instances created from factory will only receive a
52  // reference. (TBD) (this requires a stateless DataBlender).
53  // If DataBlender is stateful with configuration params then this means that new instances
54  // should be created for each new instance of OperationalLogic?
55  auto op_logic_factory =
56  [&, blender = std::move(blender)](OperationalLogicFactoryParams const& params)
57  -> std::unique_ptr<OperationalLogicIf> {
58  // Allocate a block of memory used by Correlator and DdsWaitSet
59  // The resource and memory ownership is then transferred to OperationalLogic which is
60  // responsible for destroying them last.
61  // clang-format off
62  size_t size = (params.dds_params.m_topics.size() + 1u) * (
63  /* AgnosticDataSamples contains two vectors of dds sequences
64  owned by DdsWaitSet */
65  sizeof(DdsSampleSeq) +
66  sizeof(DdsInfoSeq) +
67  /* DataSamplesView contains vector of views owned by Correlator */
68  sizeof(DataSampleView) +
69  /* Vector of indices in Correlator */
70  sizeof(size_t));
71  // clang-format on
72  std::unique_ptr<std::byte[]> mem(new std::byte[size]);
73  // Both DdsWaitSet and Correlator only allocates once so we can use a simple
74  // monotonic_buffer_resource.
75  auto resource = std::make_unique<std::pmr::monotonic_buffer_resource>(mem.get(), size);
76  // Required by OperationalLogic
77  auto& metrics = services.Get<componentFramework::ComponentMetricsIf>();
78  auto& alerts = services.Get<componentFramework::AlertServiceIf>();
79 
80  auto dds_subscriber =
81  std::make_unique<DdsWaitSet>(params.dds_params, resource.get());
82  auto correlator = std::make_unique<Correlator>(
83  params.correlator_params, std::move(dds_subscriber), resource.get());
84  auto writer = ipcq::Writer<UserTopicType>(params.shm_params.topic_name.c_str(),
85  params.shm_params.capacity,
86  params.shm_params.mem_policy);
87  auto shm_publisher = MakeShmPublisher<UserTopicType>(std::move(writer), blender);
88  return std::make_unique<OperationalLogic>(params.operational_params,
89  std::move(correlator),
90  std::move(shm_publisher),
91  metrics,
92  alerts,
93  std::move(resource),
94  std::move(mem));
95  };
96 
97  return std::make_unique<BusinessLogic>(name, services, std::move(op_logic_factory));
98  };
99  componentFramework::RunAsRtcComponent<BusinessLogic>(args, std::move(factory));
100 }
101 
102 } // namespace rtctk::telSub
103 #endif // #ifndef RTCTK_TELSUB_MAIN_HPP
rtctk::telSub::OperationalLogicFactoryParams
Set of all parameters needed when constructing the OperationalLogic object.
Definition: businessLogic.hpp:45
wscript.name
name
Definition: wscript:15
rtctk::telSub::DdsSampleSeq
rtctk::componentFramework::AgnosticTopicSeq DdsSampleSeq
Definition: agnosticDataSamples.hpp:27
rtctk::telSub::DataSampleView
Agnostic data sample non-owning reference type.
Definition: dataSampleView.hpp:24
rtctk::componentFramework::detail::Args
Definition: rtcComponent.hpp:38
rtctk::telSub::Main
void Main(const rtctk::componentFramework::Args &args, DataBlender &&blender) noexcept
Main entrypoint for running telemetry subscriber.
Definition: main.hpp:45
shmPublisher.hpp
Declares ShmPublisher.
rtctk::componentFramework::ComponentMetricsIf
Component metrics interface.
Definition: componentMetricsIf.hpp:177
rtctk::componentFramework::ServiceContainer
Container class that holds services of any type.
Definition: serviceContainer.hpp:35
ddsWaitSet.hpp
Declares the DdsWaitSet implementation.
rtctk::componentFramework::AlertServiceIf
Alert Service interface.
Definition: alertServiceIf.hpp:303
businessLogic.hpp
Implements the business logic for telSub.
rtctk::telSub
Definition: main.cpp:6
correlator.hpp
Declares Correlator.
rtctk::telSub::DdsInfoSeq
rtctk::componentFramework::SampleInfoSeq DdsInfoSeq
Definition: agnosticDataSamples.hpp:29
rtcComponentMain.hpp
Provides core functionality of an RTC Component.