RTC Toolkit  1.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 DdsSubscriber
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 DdsSubscriber */
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 DdsSubscriber 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 
77  auto dds_subscriber =
78  std::make_unique<DdsSubscriber>(params.dds_params, resource.get());
79  auto correlator = std::make_unique<Correlator>(
80  params.correlator_params, std::move(dds_subscriber), resource.get());
81  auto writer = ipcq::Writer<UserTopicType>(params.shm_params.topic_name.c_str(),
82  params.shm_params.capacity,
83  params.shm_params.mem_policy);
84  auto shm_publisher = MakeShmPublisher<UserTopicType>(std::move(writer), blender);
85  return std::make_unique<OperationalLogic>(params.operational_params,
86  std::move(correlator),
87  std::move(shm_publisher),
88  std::move(resource),
89  std::move(mem));
90  };
91 
92  return std::make_unique<BusinessLogic>(name, services, std::move(op_logic_factory));
93  };
94  componentFramework::RunAsRtcComponent<BusinessLogic>(args, std::move(factory));
95 }
96 
97 } // namespace rtctk::telSub
98 #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::DataSampleView
Agnostic data sample non-owning reference type.
Definition: dataSampleView.hpp:24
rtctk::telSub::DdsInfoSeq
SampleInfoSeq DdsInfoSeq
Definition: agnosticDataSamples.hpp:29
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.
ddsSubscriber.hpp
Declares the DdsSubscriber implementation.
rtctk::componentFramework::ServiceContainer
Container class that holds services of any type.
Definition: serviceContainer.hpp:35
rtctk::telSub::DdsSampleSeq
rtctk::AgnosticTopicSeq DdsSampleSeq
Definition: agnosticDataSamples.hpp:27
businessLogic.hpp
Implements the business logic for telSub.
rtctk::telSub
Definition: main.cpp:6
correlator.hpp
Declares Correlator.
rtcComponentMain.hpp
Provides core functionality of an RTC Component.