Kaspersky Neuromorphic Platform  1.0.0
API Reference
Loading...
Searching...
No Matches
subscription.h
Go to the documentation of this file.
1
22#pragma once
23#include <knp/core/uid.h>
24
25#include <algorithm>
26#include <string>
27#include <unordered_set>
28#include <vector>
29
30#include <boost/functional/hash.hpp>
31
32
33namespace knp::core
34{
35
40template <class MessageT>
41class Subscription final
42{
43public:
47 using MessageType = MessageT;
51 using MessageContainerType = std::vector<MessageType>;
52
56 using UidSet = std::unordered_set<::boost::uuids::uuid, boost::hash<boost::uuids::uuid>>;
57 // Subscription(const Subscription &) = delete;
58
59public:
65 Subscription(const UID &receiver, const std::vector<UID> &senders) : receiver_(receiver) { add_senders(senders); }
66
71 [[nodiscard]] const UidSet &get_senders() const { return senders_; }
72
77 [[nodiscard]] UID get_receiver_uid() const { return receiver_; }
78
85 size_t remove_sender(const UID &uid) { return senders_.erase(static_cast<boost::uuids::uuid>(uid)); }
86
93 size_t add_sender(const UID &uid) { return senders_.insert(static_cast<boost::uuids::uuid>(uid)).second; }
94
100 size_t add_senders(const std::vector<UID> &senders)
101 {
102 size_t size_before = senders_.size();
103 std::copy(senders.begin(), senders.end(), std::inserter(senders_, senders_.end()));
104 return senders_.size() - size_before;
105 }
106
112 [[nodiscard]] bool has_sender(const UID &uid) const
113 {
114 return senders_.find(static_cast<boost::uuids::uuid>(uid)) != senders_.end();
115 }
116
117public:
122 void add_message(MessageType &&message) { messages_.push_back(message); }
127 void add_message(const MessageType &message) { messages_.push_back(message); }
128
133 MessageContainerType &get_messages() { return messages_; }
138 const MessageContainerType &get_messages() const { return messages_; }
139
143 void clear_messages() { messages_.clear(); }
144
145private:
149 const UID receiver_;
150
154 std::unordered_set<::boost::uuids::uuid, boost::hash<boost::uuids::uuid>> senders_;
158 MessageContainerType messages_;
159};
160
161} // namespace knp::core
The Subscription class is used for message exchange between the network entities.
UID get_receiver_uid() const
Get UID of the entity that receives messages via the subscription.
std::vector< MessageType > MessageContainerType
Internal container for messages of the specified message type.
const MessageContainerType & get_messages() const
Get all messages.
std::unordered_set<::boost::uuids::uuid, boost::hash< boost::uuids::uuid > > UidSet
Internal container for UIDs.
Subscription(const UID &receiver, const std::vector< UID > &senders)
Subscription constructor.
const UidSet & get_senders() const
Get list of sender UIDs.
size_t remove_sender(const UID &uid)
Unsubscribe from a sender.
MessageContainerType & get_messages()
Get all messages.
void add_message(MessageType &&message)
Add a message to the subscription.
size_t add_sender(const UID &uid)
Add a sender with the given UID to the subscription.
size_t add_senders(const std::vector< UID > &senders)
Add several senders to the subscription.
void clear_messages()
Remove all stored messages.
bool has_sender(const UID &uid) const
Check if a sender with the given UID exists.
MessageT MessageType
Message type.
void add_message(const MessageType &message)
Add a message to the subscription.
Core library namespace.
Definition backend.h:50
UID class and routines.