Kaspersky Neuromorphic Platform  1.0.0
API Reference
Loading...
Searching...
No Matches
message_endpoint.h
Go to the documentation of this file.
1
22#pragma once
23
27#include <knp/core/uid.h>
28
29#include <any>
30#include <chrono>
31#include <functional>
32#include <map>
33#include <memory>
34#include <numeric>
35#include <string>
36#include <utility>
37#include <variant>
38#include <vector>
39
40#include <boost/mp11.hpp>
41#include <boost/noncopyable.hpp>
42
43
47namespace knp::core::messaging::impl
48{
52class MessageEndpointImpl;
53} // namespace knp::core::messaging::impl
54
55
56namespace knp::core
57{
68class MessageEndpoint : private boost::noncopyable
69{
70public:
74 using AllSubscriptions = boost::mp11::mp_transform<Subscription, messaging::AllMessages>;
75
85 using SubscriptionVariant = boost::mp11::mp_rename<AllSubscriptions, std::variant>;
86
87public:
93 static UID get_receiver_uid(const SubscriptionVariant &subscription);
99 static std::pair<size_t, UID> get_subscription_key(const SubscriptionVariant &subscription);
100
108 template <typename Variant, typename Type>
109 static constexpr size_t get_type_index = boost::mp11::mp_find<Variant, Type>::value;
110
111public:
116 MessageEndpoint(MessageEndpoint &&endpoint) noexcept;
117
122
127
128public:
139 template <typename MessageType>
140 Subscription<MessageType> &subscribe(const UID &receiver, const std::vector<UID> &senders);
141
148 template <typename MessageType>
149 bool unsubscribe(const UID &receiver);
150
155 void remove_receiver(const UID &receiver);
156
162
168
174 size_t receive_all_messages(const std::chrono::milliseconds &sleep_duration = std::chrono::milliseconds(0));
175
183 template <class MessageType>
184 std::vector<MessageType> unload_messages(const knp::core::UID &receiver_uid);
185
186public:
190 using SubscriptionContainer = std::map<std::pair<size_t, UID>, SubscriptionVariant>;
191
196 const SubscriptionContainer &get_endpoint_subscriptions() const { return subscriptions_; }
197
198protected:
202 std::shared_ptr<messaging::impl::MessageEndpointImpl> impl_;
203
204protected:
208 MessageEndpoint() = default;
209
210private:
214 SubscriptionContainer subscriptions_;
215};
216
217} // namespace knp::core
The MessageEndpoint class is a definition of message endpoints.
std::vector< MessageType > unload_messages(const knp::core::UID &receiver_uid)
Read messages of the specified type received via subscription.
const SubscriptionContainer & get_endpoint_subscriptions() const
Get access to subscription container of the endpoint.
MessageEndpoint & operator=(MessageEndpoint &)=delete
Avoid copy assignment of an endpoint.
static constexpr size_t get_type_index
Find index of an entity type in its variant.
boost::mp11::mp_transform< Subscription, messaging::AllMessages > AllSubscriptions
List of subscription types based on message types specified in messaging::AllMessages.
MessageEndpoint(MessageEndpoint &&endpoint) noexcept
Move constructor for message endpoints.
MessageEndpoint()=default
Message endpoint default constructor.
Subscription< MessageType > & subscribe(const UID &receiver, const std::vector< UID > &senders)
Add a subscription to messages of the specified type from senders with given UIDs.
std::map< std::pair< size_t, UID >, SubscriptionVariant > SubscriptionContainer
Type of subscription container.
void send_message(const knp::core::messaging::MessageVariant &message)
Send a message to the message bus.
boost::mp11::mp_rename< AllSubscriptions, std::variant > SubscriptionVariant
Subscription variant that contains any subscription type specified in AllSubscriptions.
virtual ~MessageEndpoint()
Message endpoint destructor.
static std::pair< size_t, UID > get_subscription_key(const SubscriptionVariant &subscription)
Get subscription key from a subscription variant.
static UID get_receiver_uid(const SubscriptionVariant &subscription)
Get receiver UID from a subscription variant.
size_t receive_all_messages(const std::chrono::milliseconds &sleep_duration=std::chrono::milliseconds(0))
Receive all messages that were sent to the endpoint.
void remove_receiver(const UID &receiver)
Remove all subscriptions for a receiver with given UID.
bool receive_message()
Receive a message from the message bus.
bool unsubscribe(const UID &receiver)
Unsubscribe from messages of a specified type.
Message envelope routines.
Common header for messaging.
boost::mp11::mp_rename< AllMessages, std::variant > MessageVariant
Message variant that contains any message type specified in AllMessages.
Core library namespace.
Definition backend.h:50
Subscription class that determines message exchange between entities in the network.
UID class and routines.