Sen API
Sen Libraries
Loading...
Searching...
No Matches
component_api.h
Go to the documentation of this file.
1// === component_api.h =================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_KERNEL_COMPONENT_API_H
9#define SEN_KERNEL_COMPONENT_API_H
10
11// sen
16#include "sen/core/base/span.h"
19#include "sen/core/meta/var.h"
22#include "sen/core/obj/object.h"
26
27// kernel
28#include "sen/kernel/kernel.h"
30#include "sen/kernel/tracer.h"
32
33// generated code
34#include "stl/sen/kernel/basic_types.stl.h"
35
36// std
37#include <atomic>
38#include <cstdint>
39#include <cstring>
40#include <filesystem>
41#include <functional>
42#include <memory>
43#include <optional>
44#include <string>
45#include <string_view>
46#include <utility>
47#include <vector>
48
49namespace sen::kernel
50{
51
54
57
59class RunApi;
60class KernelApi;
61
62namespace impl
63{
64
65class Runner;
66class KernelImpl;
67
68[[nodiscard]] FuncResult execLoop(Runner* runner, Duration cycleTime, std::function<void()>&& workFunction);
69
70void installTransportFactory(KernelImpl* kernel, TransportFactory&& factory, uint32_t transportVersion);
71
72void remoteProcessDetected(RunApi& api, const ProcessInfo& processInfo);
73
74void remoteProcessLost(RunApi& api, const ProcessInfo& processInfo);
75
76[[nodiscard]] std::shared_ptr<ObjectSource> getSource(Runner* runner, const BusAddress& address);
77
78[[nodiscard]] SessionsDiscoverer& getSessionsDiscoverer(Runner* runner);
79
80[[nodiscard]] const ProcessInfo* fetchOwnerInfo(const Object* object);
81
82[[nodiscard]] ::sen::impl::WorkQueue* getWorkQueue(Runner* runner);
83
84} // namespace impl
85
88{
89 std::string name;
90 uint32_t group = 0;
91 bool requiresRealTime = false;
92 std::optional<Duration> cycleTime;
93 std::size_t objectCount = 0;
94};
95
98{
99 RunMode runMode = RunMode::realTime;
101 std::vector<ComponentMonitoringInfo> components;
102};
103
106{
107public:
108 KernelApi(Kernel& kernel, impl::Runner* runner) noexcept;
109
110public:
112 [[nodiscard]] CustomTypeRegistry& getTypes() noexcept;
113
116 void requestKernelStop(int exitCode = 0);
117
119 [[nodiscard]] std::shared_ptr<ObjectSource> getSource(const BusAddress& address);
120
123 [[nodiscard]] std::shared_ptr<ObjectSource> getSource(const std::string& address);
124
126 [[nodiscard]] SessionsDiscoverer& getSessionsDiscoverer() noexcept;
127
130 [[nodiscard]] const ProcessInfo* fetchOwnerInfo(const Object* object) const noexcept;
131
133 [[nodiscard]] const std::string& getAppName() const noexcept;
134
136 [[nodiscard]] ::sen::impl::WorkQueue* getWorkQueue() const noexcept;
137
138 template <typename T, typename Bus>
139 [[nodiscard]] std::shared_ptr<Subscription<T>> selectAllFrom(const Bus& bus);
140
144 template <typename T, typename Bus>
145 [[nodiscard]] std::shared_ptr<Subscription<T>> selectAllFrom(
146 const Bus& bus,
147 typename sen::ObjectList<T>::Callback onAdded,
148 typename sen::ObjectList<T>::Callback onRemoved = nullptr);
149
154 template <typename T, typename Bus>
155 [[nodiscard]] std::shared_ptr<Subscription<T>> selectFrom(const Bus& bus,
156 const std::string& query,
157 typename sen::ObjectList<T>::Callback onAdded = nullptr,
158 typename sen::ObjectList<T>::Callback onRemoved = nullptr);
159
162 [[nodiscard]] std::filesystem::path getConfigFilePath() const noexcept { return kernel_.getConfigPath(); }
163
164private:
165 template <typename T>
166 [[nodiscard]] std::string buildQuery(const BusAddress& address) const;
167
168 template <typename T>
169 [[nodiscard]] std::string buildQuery(std::string_view bus) const;
170
171private:
172 Kernel& kernel_;
173 impl::Runner* runner_;
174};
175
178{
179public:
180 explicit ConfigGetter(const VarMap& config) noexcept;
181
182public:
184 [[nodiscard]] const VarMap& getConfig() const noexcept;
185
186private:
187 const VarMap& config_;
188};
189
192{
193public:
194 SEN_NOCOPY_NOMOVE(RegistrationApi)
195
196public:
197 RegistrationApi(Kernel& kernel, impl::Runner* runner, const VarMap& config) noexcept;
198 ~RegistrationApi() noexcept = default;
199};
200
202class PreloadApi: public ConfigGetter, public KernelApi
203{
204 SEN_NOCOPY_NOMOVE(PreloadApi)
205
206public:
207 PreloadApi(Kernel& kernel, impl::Runner* runner, const VarMap& config) noexcept;
208
209 ~PreloadApi() noexcept = default;
210
211public:
213 void installTransportFactory(TransportFactory&& factory, uint32_t transportVersion) const;
214
216 void installTracerFactory(TracerFactory&& factory) const;
217
218private:
219 Kernel& kernel_;
220};
221
223class LoadApi: public ConfigGetter, public KernelApi
224{
225 SEN_NOCOPY_NOMOVE(LoadApi)
226
227public:
228 LoadApi(Kernel& kernel, impl::Runner* runner, const VarMap& config) noexcept;
229
230 ~LoadApi() noexcept = default;
231};
232
234class InitApi: public ConfigGetter, public KernelApi
235{
236 SEN_NOCOPY_NOMOVE(InitApi)
237
238public:
239 InitApi(Kernel& kernel, impl::Runner* runner, const VarMap& config) noexcept;
240
241 ~InitApi() noexcept = default;
242};
243
245class RunApi: public ConfigGetter, public KernelApi
246{
247public:
248 SEN_NOCOPY_NOMOVE(RunApi)
249
250public:
252 impl::KernelImpl& kernelImpl,
253 impl::Runner* runner,
254 std::atomic_bool& stopRequested,
255 const VarMap& config,
256 Guarded<TimeStamp>& timePoint) noexcept;
257
258 ~RunApi() noexcept = default;
259
260public:
262 [[nodiscard]] const std::atomic_bool& stopRequested() const noexcept;
263
268
270 void update();
271
276 void commit();
277
280 [[nodiscard]] FuncResult execLoop(Duration cycleTime,
281 std::function<void()>&& func = nullptr,
282 bool logOverruns = true);
283
285 [[nodiscard]] TimeStamp getStartTime() const noexcept;
286
288 [[nodiscard]] TimeStamp getTime() const noexcept;
289
291 [[nodiscard]] std::optional<Duration> getTargetCycleTime() const noexcept;
292
295
299 [[nodiscard]] Span<const ComponentInfo> getImportedPackages() const noexcept;
300
306 [[nodiscard]] Span<const ComponentInfo> getLoadedComponents() const noexcept;
307
310 [[nodiscard]] std::optional<uint32_t> getTransportProtocolVersion() const noexcept;
311
313 [[nodiscard]] Tracer& getTracer() const noexcept;
314
315private:
316 friend void impl::remoteProcessDetected(RunApi& api, const ProcessInfo& processInfo);
317
318 friend void impl::remoteProcessLost(RunApi& api, const ProcessInfo& processInfo);
319
320private:
321 impl::KernelImpl& kernelImpl_;
322 impl::Runner* runner_;
323 std::atomic_bool& stopRequested_;
324 Guarded<TimeStamp>& timePoint_;
325};
326
328class UnloadApi: public ConfigGetter, public KernelApi
329{
330 SEN_NOCOPY_NOMOVE(UnloadApi)
331
332public:
333 UnloadApi(Kernel& kernel, const VarMap& config, impl::Runner* runner) noexcept;
334 ~UnloadApi() noexcept = default;
335};
336
337//----------------------------------------------------------------------------------------------------------------------
338// Inline implementation
339//----------------------------------------------------------------------------------------------------------------------
340
341template <typename T, typename Bus>
342inline std::shared_ptr<Subscription<T>> KernelApi::selectAllFrom(const Bus& bus)
343{
344 auto sub = std::make_shared<Subscription<T>>();
345 sub->attachTo(getSource(bus), Interest::make(buildQuery<T>(bus), getTypes()), true);
346 return sub;
347}
348
349template <typename T, typename Bus>
350inline std::shared_ptr<Subscription<T>> KernelApi::selectAllFrom(const Bus& bus,
351 typename ObjectList<T>::Callback onAdded,
352 typename ObjectList<T>::Callback onRemoved)
353{
354 auto sub = std::make_shared<Subscription<T>>();
355 // Install callbacks before subscribing so they fire for objects already present.
356 if (onAdded)
357 {
358 std::ignore = sub->list.onAdded(std::move(onAdded));
359 }
360 if (onRemoved)
361 {
362 std::ignore = sub->list.onRemoved(std::move(onRemoved));
363 }
364 sub->attachTo(getSource(bus), Interest::make(buildQuery<T>(bus), getTypes()), true);
365 return sub;
366}
367
368template <typename T, typename Bus>
369inline std::shared_ptr<Subscription<T>> KernelApi::selectFrom(const Bus& bus,
370 const std::string& query,
371 typename sen::ObjectList<T>::Callback onAdded,
372 typename sen::ObjectList<T>::Callback onRemoved)
373{
374 auto sub = std::make_shared<Subscription<T>>();
375
376 // Install callbacks before subscribing so they fire for objects already present.
377 if (onAdded)
378 {
379 std::ignore = sub->list.onAdded(std::move(onAdded));
380 }
381 if (onRemoved)
382 {
383 std::ignore = sub->list.onRemoved(std::move(onRemoved));
384 }
385 sub->attachTo(getSource(bus), Interest::make(query, getTypes()), true);
386 return sub;
387}
388
389template <typename T>
390inline std::string KernelApi::buildQuery(const BusAddress& address) const
391{
392 const ClassType* meta = nullptr;
393 if constexpr (!std::is_same_v<T, Object>)
394 {
395 meta = &T::meta();
396 }
397
398 std::string query = "SELECT ";
399 query.append(meta ? meta->getQualifiedName() : "*");
400 query.append(" FROM ");
401 query.append(address.sessionName);
402 query.append(".");
403 query.append(address.busName);
404
405 return query;
406}
407
408template <typename T>
409inline std::string KernelApi::buildQuery(std::string_view bus) const
410{
411 const ClassType* meta = nullptr;
412 if constexpr (!std::is_same_v<T, Object>)
413 {
414 meta = T::meta()->asClassType();
415 }
416
417 std::string query = "SELECT ";
418 query.append(meta ? meta->getQualifiedName() : "*");
419 query.append(" FROM ");
420 query.append(bus);
421
422 return query;
423}
424
425} // namespace sen::kernel
426
427#endif // SEN_KERNEL_COMPONENT_API_H
Base class for event or method callbacks. It stores the queue where to push the response....
Definition callback.h:146
A registry of custom types.
Definition type_registry.h:36
A time duration.
Definition duration.h:25
static std::shared_ptr< Interest > make(std::string_view query, const CustomTypeRegistry &typeRegistry)
Make an interest from a query. Throws std::exception if not well formed.
A sen object.
Definition object.h:76
A list of objects that is managed by some source based on user-expressed interests.
Definition object_list.h:30
std::function< void(const Iterators &iterators)> Callback
Definition object_list.h:65
Allows adding and receiving objects.
Definition object_source.h:35
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to rep...
Definition result.h:135
Contiguous view of elements of type T. Inspired by http://www.open-std.org/jtc1/sc22/wg21/docs/papers...
Definition span.h:34
A point in time.
Definition timestamp.h:26
ConfigGetter(const VarMap &config) noexcept
const VarMap & getConfig() const noexcept
Gets the configuration associated with this component.
~InitApi() noexcept=default
InitApi(Kernel &kernel, impl::Runner *runner, const VarMap &config) noexcept
User-facing kernel functions.
Definition component_api.h:106
::sen::impl::WorkQueue * getWorkQueue() const noexcept
The work queue of this runner.
KernelApi(Kernel &kernel, impl::Runner *runner) noexcept
std::shared_ptr< Subscription< T > > selectFrom(const Bus &bus, const std::string &query, typename sen::ObjectList< T >::Callback onAdded=nullptr, typename sen::ObjectList< T >::Callback onRemoved=nullptr)
Creates a subscription for objects matching the given Sen query string. Unlike selectAllFrom,...
Definition component_api.h:369
std::shared_ptr< ObjectSource > getSource(const BusAddress &address)
Gets an object source, where objects can be found and published.
std::filesystem::path getConfigFilePath() const noexcept
Gets the path to the configuration file used to construct the kernel. It might be empty if the kernel...
Definition component_api.h:162
CustomTypeRegistry & getTypes() noexcept
The types registered into the kernel.
SessionsDiscoverer & getSessionsDiscoverer() noexcept
Object that allows discovering sessions and buses.
const ProcessInfo * fetchOwnerInfo(const Object *object) const noexcept
Gets information about the process where an object is. Returns nullptr if the object resides in the c...
const std::string & getAppName() const noexcept
Gets the (optional) application name passed to the kernel as a configuration parameter.
void requestKernelStop(int exitCode=0)
Issues an asynchronous request to stop the kernel. The request is ignored if a previous stop request ...
std::shared_ptr< Subscription< T > > selectAllFrom(const Bus &bus)
Definition component_api.h:342
Main entry point of a sen microkernel.
Definition kernel.h:40
~LoadApi() noexcept=default
LoadApi(Kernel &kernel, impl::Runner *runner, const VarMap &config) noexcept
PreloadApi(Kernel &kernel, impl::Runner *runner, const VarMap &config) noexcept
~PreloadApi() noexcept=default
void installTracerFactory(TracerFactory &&factory) const
Installs a tracer factory.
void installTransportFactory(TransportFactory &&factory, uint32_t transportVersion) const
Installs a transport factory for the kernel to use for sessions.
RegistrationApi(Kernel &kernel, impl::Runner *runner, const VarMap &config) noexcept
~RegistrationApi() noexcept=default
What can be done while a component is running.
Definition component_api.h:246
Span< const ComponentInfo > getLoadedComponents() const noexcept
Build information for every component loaded into the kernel, excluding pipeline components (which ar...
TimeStamp getStartTime() const noexcept
The initial simulation time for the objects in the component.
Span< const ComponentInfo > getImportedPackages() const noexcept
Build information for all imported packages (from pipeline components). The returned span references ...
void commit()
Send changes, so that they become visible to other participants. This includes object additions and r...
Tracer & getTracer() const noexcept
Create a scoped zone used for tracing runtime performance.
void drainInputs()
Perform any request coming from the outside and drainInputs all the local data structures with their ...
~RunApi() noexcept=default
std::optional< Duration > getTargetCycleTime() const noexcept
If present, it returns the configured cycle time for iterations.
std::optional< uint32_t > getTransportProtocolVersion() const noexcept
Version of the currently installed transport protocol. Empty when no transport is installed....
FuncResult execLoop(Duration cycleTime, std::function< void()> &&func=nullptr, bool logOverruns=true)
A basic execution loop. Func is an optional callback that will be invoked on each cycle.
RunApi(Kernel &kernel, impl::KernelImpl &kernelImpl, impl::Runner *runner, std::atomic_bool &stopRequested, const VarMap &config, Guarded< TimeStamp > &timePoint) noexcept
KernelMonitoringInfo fetchMonitoringInfo() const
Monitoring information.
friend void impl::remoteProcessLost(RunApi &api, const ProcessInfo &processInfo)
const std::atomic_bool & stopRequested() const noexcept
True if stop has been requested by the runtime.
void update()
This calls update() on all the objects registered by the component.
friend void impl::remoteProcessDetected(RunApi &api, const ProcessInfo &processInfo)
TimeStamp getTime() const noexcept
The (potentially virtualized) time.
Definition source_info.h:99
Interface implemented by tracers. You can use it to trace the behavior of your code....
Definition tracer.h:28
UnloadApi(Kernel &kernel, const VarMap &config, impl::Runner *runner) noexcept
~UnloadApi() noexcept=default
std::function< std::unique_ptr< Transport >(const std::string &, std::unique_ptr< Tracer > tracer)> TransportFactory
A function that creates a transport given a session name.
Definition include/sen/kernel/transport.h:203
Global transport statistics.
Definition include/sen/kernel/transport.h:146
std::map< std::string, Var, std::less<> > VarMap
A map of vars to represent structures.
Definition var.h:107
Strong type for the unique identifier for a transport timer.
Definition native_object_impl.h:39
uint32_t group
Definition component_api.h:90
Result< OpState, ExecError > PassResult
The result of operations that may be called multiple times.
Definition component_api.h:56
std::function< std::unique_ptr< Tracer >(std::string_view)> TracerFactory
A factory function for tracers.
Definition tracer.h:119
Result< void, ExecError > FuncResult
The result of operations that are called once.
Definition component_api.h:53
std::optional< Duration > cycleTime
Definition component_api.h:92
std::vector< ComponentMonitoringInfo > components
Definition component_api.h:101
std::size_t objectCount
Definition component_api.h:93
std::string name
Definition component_api.h:89
RunMode runMode
Definition component_api.h:99
bool requiresRealTime
Definition component_api.h:91
TransportStats transportStats
Definition component_api.h:100
Runtime monitoring information about a single component runner.
Definition component_api.h:88
Kernel runtime monitoring information.
Definition component_api.h:98
Definition assert.h:17
STL namespace.
Guarded is a lightweight wrapper class that can be used to protect an objet of type T from concurrent...
Definition mutex_utils.h:49
A list of objects and a reference to its source (to keep it alive).
Definition subscription.h:29