Kaspersky Neuromorphic Platform  1.0.0
API Reference
Loading...
Searching...
No Matches
observer.h
Go to the documentation of this file.
1
21#pragma once
22#include <knp/core/impexp.h>
25
26#include <filesystem>
27#include <fstream>
28#include <functional>
29#include <string>
30#include <unordered_map>
31#include <utility>
32#include <vector>
33
34#include <boost/mp11.hpp>
35
36
41{
46template <class Message>
47using MessageProcessor = std::function<void(const std::vector<Message>)>;
48
49
55template <class Message>
57{
58public:
66 core::MessageEndpoint &&endpoint, MessageProcessor<Message> &&processor, core::UID uid = core::UID{true})
67 : endpoint_(std::move(endpoint)), process_messages_(processor), base_data_{uid}
68 {
69 }
70
75 MessageObserver(MessageObserver<Message> &&other) noexcept = default;
76
81 void subscribe(const std::vector<core::UID> &entities) { endpoint_.subscribe<Message>(base_data_.uid_, entities); }
82
86 void update()
87 {
88 endpoint_.receive_all_messages();
89 auto messages_raw = endpoint_.unload_messages<Message>(base_data_.uid_);
90 process_messages_(messages_raw);
91 }
92
97 [[nodiscard]] knp::core::UID get_uid() const { return base_data_.uid_; }
98
99private:
100 core::MessageEndpoint endpoint_;
101 MessageProcessor<Message> process_messages_;
102 core::BaseData base_data_;
103};
104
108using AllObservers = boost::mp11::mp_transform<MessageObserver, core::messaging::AllMessages>;
109
113using AnyObserverVariant = boost::mp11::mp_rename<AllObservers, std::variant>;
114
115} // namespace knp::framework::monitoring
The MessageObserver class is a definition of an observer that receives messages and processes them.
Definition observer.h:57
void subscribe(const std::vector< core::UID > &entities)
Subscribe to messages.
Definition observer.h:81
MessageObserver(core::MessageEndpoint &&endpoint, MessageProcessor< Message > &&processor, core::UID uid=core::UID{true})
Constructor.
Definition observer.h:65
knp::core::UID get_uid() const
Get observer UID.
Definition observer.h:97
void update()
Receive and process messages.
Definition observer.h:86
MessageObserver(MessageObserver< Message > &&other) noexcept=default
Move constructor for observer.
Message endpoint interface.
Common header for messaging.
Monitoring namespace.
Definition observer.h:41
boost::mp11::mp_transform< MessageObserver, core::messaging::AllMessages > AllObservers
List of all possible observers.
Definition observer.h:108
std::function< void(const std::vector< Message >)> MessageProcessor
Functor for message processing.
Definition observer.h:47
boost::mp11::mp_rename< AllObservers, std::variant > AnyObserverVariant
Observer variant that contains all possible observers.
Definition observer.h:113