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
18#include "sen/core/meta/var.h"
21#include "sen/core/obj/object.h"
24
25// kernel
26#include "sen/kernel/kernel.h"
28#include "sen/kernel/tracer.h"
30
31// generated code
32#include "stl/sen/kernel/basic_types.stl.h"
33
34// std
35#include <atomic>
36#include <cstdint>
37#include <cstring>
38#include <filesystem>
39#include <functional>
40#include <memory>
41#include <optional>
42#include <string>
43#include <string_view>
44#include <vector>
45
46namespace sen::kernel
47{
48
51
54
56class RunApi;
57class KernelApi;
58
59namespace impl
60{
61
62class Runner;
63class KernelImpl;
64
65[[nodiscard]] FuncResult execLoop(Runner* runner, Duration cycleTime, std::function<void()>&& workFunction);
66
67void installTransportFactory(KernelImpl* kernel, TransportFactory&& factory, uint32_t transportVersion);
68
69void remoteProcessDetected(RunApi& api, const ProcessInfo& processInfo);
70
71void remoteProcessLost(RunApi& api, const ProcessInfo& processInfo);
72
73[[nodiscard]] std::shared_ptr<ObjectSource> getSource(Runner* runner, const BusAddress& address);
74
75[[nodiscard]] SessionsDiscoverer& getSessionsDiscoverer(Runner* runner);
76
77[[nodiscard]] const ProcessInfo* fetchOwnerInfo(const Object* object);
78
79[[nodiscard]] ::sen::impl::WorkQueue* getWorkQueue(Runner* runner);
80
81} // namespace impl
82
85{
86 ComponentInfo info;
87 ComponentConfig config;
88 bool requiresRealTime = false;
89 std::optional<Duration> cycleTime;
90 std::size_t objectCount = 0;
91};
92
95{
96 RunMode runMode = RunMode::realTime;
98 std::vector<ComponentMonitoringInfo> components;
99};
100
103{
104public:
105 KernelApi(Kernel& kernel, impl::Runner* runner) noexcept;
106
107public:
109 [[nodiscard]] CustomTypeRegistry& getTypes() noexcept;
110
113 void requestKernelStop(int exitCode = 0);
114
116 [[nodiscard]] std::shared_ptr<ObjectSource> getSource(const BusAddress& address);
117
120 [[nodiscard]] std::shared_ptr<ObjectSource> getSource(const std::string& address);
121
123 [[nodiscard]] SessionsDiscoverer& getSessionsDiscoverer() noexcept;
124
127 [[nodiscard]] const ProcessInfo* fetchOwnerInfo(const Object* object) const noexcept;
128
130 [[nodiscard]] const std::string& getAppName() const noexcept;
131
133 [[nodiscard]] ::sen::impl::WorkQueue* getWorkQueue() const noexcept;
134
135 template <typename T, typename B>
136 [[nodiscard]] std::shared_ptr<Subscription<T>> selectAllFrom(const B& bus);
137
140 [[nodiscard]] std::filesystem::path getConfigFilePath() const noexcept { return kernel_.getConfigPath(); }
141
142private:
143 template <typename T>
144 [[nodiscard]] std::string buildQuery(const BusAddress& address) const;
145
146 template <typename T>
147 [[nodiscard]] std::string buildQuery(std::string_view bus) const;
148
149private:
150 Kernel& kernel_;
151 impl::Runner* runner_;
152};
153
156{
157public:
158 explicit ConfigGetter(const VarMap& config) noexcept;
159
160public:
162 [[nodiscard]] const VarMap& getConfig() const noexcept;
163
164private:
165 const VarMap& config_;
166};
167
170{
171public:
172 SEN_NOCOPY_NOMOVE(RegistrationApi)
173
174public:
175 RegistrationApi(Kernel& kernel, impl::Runner* runner, const VarMap& config) noexcept;
176 ~RegistrationApi() noexcept = default;
177};
178
180class PreloadApi: public ConfigGetter, public KernelApi
181{
182 SEN_NOCOPY_NOMOVE(PreloadApi)
183
184public:
185 PreloadApi(Kernel& kernel, impl::Runner* runner, const VarMap& config) noexcept;
186
187 ~PreloadApi() noexcept = default;
188
189public:
191 void installTransportFactory(TransportFactory&& factory, uint32_t transportVersion) const;
192
194 void installTracerFactory(TracerFactory&& factory) const;
195
196private:
197 Kernel& kernel_;
198};
199
201class LoadApi: public ConfigGetter, public KernelApi
202{
203 SEN_NOCOPY_NOMOVE(LoadApi)
204
205public:
206 LoadApi(Kernel& kernel, impl::Runner* runner, const VarMap& config) noexcept;
207
208 ~LoadApi() noexcept = default;
209};
210
212class InitApi: public ConfigGetter, public KernelApi
213{
214 SEN_NOCOPY_NOMOVE(InitApi)
215
216public:
217 InitApi(Kernel& kernel, impl::Runner* runner, const VarMap& config) noexcept;
218
219 ~InitApi() noexcept = default;
220};
221
223class RunApi: public ConfigGetter, public KernelApi
224{
225public:
226 SEN_NOCOPY_NOMOVE(RunApi)
227
228public:
230 impl::KernelImpl& kernelImpl,
231 impl::Runner* runner,
232 std::atomic_bool& stopRequested,
233 const VarMap& config,
234 Guarded<TimeStamp>& timePoint) noexcept;
235
236 ~RunApi() noexcept = default;
237
238public:
240 [[nodiscard]] const std::atomic_bool& stopRequested() const noexcept;
241
246
248 void update();
249
254 void commit();
255
258 [[nodiscard]] FuncResult execLoop(Duration cycleTime,
259 std::function<void()>&& func = nullptr,
260 bool logOverruns = true);
261
263 [[nodiscard]] TimeStamp getStartTime() const noexcept;
264
266 [[nodiscard]] TimeStamp getTime() const noexcept;
267
269 [[nodiscard]] std::optional<Duration> getTargetCycleTime() const noexcept;
270
273
275 [[nodiscard]] Tracer& getTracer() const noexcept;
276
277private:
278 friend void impl::remoteProcessDetected(RunApi& api, const ProcessInfo& processInfo);
279
280 friend void impl::remoteProcessLost(RunApi& api, const ProcessInfo& processInfo);
281
282private:
283 impl::KernelImpl& kernelImpl_;
284 impl::Runner* runner_;
285 std::atomic_bool& stopRequested_;
286 Guarded<TimeStamp>& timePoint_;
287};
288
290class UnloadApi: public ConfigGetter, public KernelApi
291{
292 SEN_NOCOPY_NOMOVE(UnloadApi)
293
294public:
295 UnloadApi(Kernel& kernel, const VarMap& config, impl::Runner* runner) noexcept;
296 ~UnloadApi() noexcept = default;
297};
298
299//----------------------------------------------------------------------------------------------------------------------
300// Inline implementation
301//----------------------------------------------------------------------------------------------------------------------
302
303template <typename T, typename B>
304inline std::shared_ptr<Subscription<T>> KernelApi::selectAllFrom(const B& bus)
305{
306 auto sub = std::make_shared<Subscription<T>>();
307 sub->source = getSource(bus);
308 sub->source->addSubscriber(Interest::make(buildQuery<T>(bus), getTypes()), &sub->list, true);
309 return sub;
310}
311
312template <typename T>
313inline std::string KernelApi::buildQuery(const BusAddress& address) const
314{
315 const ClassType* meta = nullptr;
316 if constexpr (!std::is_same_v<T, Object>)
317 {
318 meta = &T::meta();
319 }
320
321 std::string query = "SELECT ";
322 query.append(meta ? meta->getQualifiedName() : "*");
323 query.append(" FROM ");
324 query.append(address.sessionName);
325 query.append(".");
326 query.append(address.busName);
327
328 return query;
329}
330
331template <typename T>
332inline std::string KernelApi::buildQuery(std::string_view bus) const
333{
334 const ClassType* meta = nullptr;
335 if constexpr (!std::is_same_v<T, Object>)
336 {
337 meta = T::meta()->asClassType();
338 }
339
340 std::string query = "SELECT ";
341 query.append(meta ? meta->getQualifiedName() : "*");
342 query.append(" FROM ");
343 query.append(bus);
344
345 return query;
346}
347
348} // namespace sen::kernel
349
350#endif // SEN_KERNEL_COMPONENT_API_H
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
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
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:103
::sen::impl::WorkQueue * getWorkQueue() const noexcept
The work queue of this runner.
KernelApi(Kernel &kernel, impl::Runner *runner) noexcept
std::shared_ptr< ObjectSource > getSource(const BusAddress &address)
Gets an object source, where objects can be found and published.
std::shared_ptr< Subscription< T > > selectAllFrom(const B &bus)
Definition component_api.h:304
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:140
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 ...
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:224
TimeStamp getStartTime() const noexcept
The initial simulation time for the objects in the component.
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.
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
Result< OpState, ExecError > PassResult
The result of operations that may be called multiple times.
Definition component_api.h:53
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:50
ComponentConfig config
Definition component_api.h:87
std::optional< Duration > cycleTime
Definition component_api.h:89
std::vector< ComponentMonitoringInfo > components
Definition component_api.h:98
std::size_t objectCount
Definition component_api.h:90
RunMode runMode
Definition component_api.h:96
bool requiresRealTime
Definition component_api.h:88
TransportStats transportStats
Definition component_api.h:97
ComponentInfo info
Definition component_api.h:86
Monitoring information about components.
Definition component_api.h:85
Kernel monitoring information.
Definition component_api.h:95
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:24