SOLA
Loading...
Searching...
No Matches
natter.h
1// Copyright The SOLA Contributors
2//
3// Licensed under the MIT License.
4// For details on the licensing terms, see the LICENSE file.
5// SPDX-License-Identifier: MIT
6
7#ifndef NATTER_NATTER_H_
8#define NATTER_NATTER_H_
9
10#include <functional>
11#include <memory>
12#include <string>
13
14#include "message.h"
15#include "network_info_ipv4.h"
16#include "solanet/uuid.h"
17
18namespace natter {
19
20using MsgReceiveFct = std::function<void(const Message &)>;
21using MsgMissingFct = std::function<void(const std::string &)>;
22
26template <class T, class NodeInfoT> class Natter {
27public:
28 Natter() = default;
29 using NodeInfo = NodeInfoT;
30
31 // Disallow copy & move
32 Natter(const Natter &) = delete;
33 Natter &operator=(const Natter &other) = delete;
34 Natter(Natter &&) = delete;
35 Natter &operator=(Natter &&) = delete;
36 ~Natter() = default;
37
45 solanet::UUID publish(const std::string &topic, const std::string &msg_content) {
46 return static_cast<T *>(this)->publishImpl(topic, msg_content);
47 }
48
52
59 bool addPeer(const std::string &topic, const NodeInfo &info) {
60 return static_cast<T *>(this)->addPeerImpl(topic, info);
61 }
62
67 void subscribeTopic(const std::string &topic, const NodeInfo &info) {
68 static_cast<T *>(this)->subscribeTopicImpl(topic, info);
69 }
70
76 bool isSubscribedToTopic(const std::string &topic) const {
77 return static_cast<const T *>(this)->isSubscribedToTopicImpl(topic);
78 }
79
84 void unsubscribeTopic(const std::string &topic) {
85 static_cast<T *>(this)->unsubscribeTopicImpl(topic);
86 }
87
94 bool removePeer(const std::string &topic, const std::string &ip, uint16_t port) {
95 return static_cast<T *>(this)->removePeerImpl(topic, ip, port);
96 }
97
102 solanet::UUID getUUID() const { return static_cast<const T *>(this)->getUUIDImpl(); }
103
109 NodeInfo getOwnNodeInfo(const std::string &topic) const {
110 return static_cast<const T *>(this)->getOwnNodeInfoImpl(topic);
111 }
112
118 return static_cast<const T *>(this)->getNetworkInfoImpl();
119 }
120};
121
122#define DECLARE_CRTP_METHODS \
123 friend Natter; \
124 class Impl; \
125 std::unique_ptr<Impl> pimpl_; \
126 solanet::UUID publishImpl(const std::string &topic, const std::string &msg_content); \
127 bool addPeerImpl(const std::string &topic, const NodeInfo &info); \
128 bool removePeerImpl(const std::string &topic, const std::string &ip, uint16_t port); \
129 void subscribeTopicImpl(const std::string &topic, const NodeInfo &info); \
130 bool isSubscribedToTopicImpl(const std::string &topic) const; \
131 void unsubscribeTopicImpl(const std::string &topic); \
132 NodeInfo getOwnNodeInfoImpl(const std::string &topic) const; \
133 solanet::UUID getUUIDImpl() const; \
134 NetworkInfoIPv4 getNetworkInfoImpl() const;
135
136#define DEFINE_CRTP_METHODS(C) \
137 C::C(MsgReceiveFct recv_callback, MsgMissingFct missing_callback) \
138 : pimpl_(std::make_unique<Impl>(recv_callback, missing_callback)) {} \
139 C::C(MsgReceiveFct recv_callback, MsgMissingFct missing_callback, \
140 const std::vector<logging::LoggerPtr> &logger) \
141 : pimpl_(std::make_unique<Impl>(recv_callback, missing_callback, logger)) {} \
142 C::C(MsgReceiveFct recv_callback, MsgMissingFct missing_callback, \
143 const std::vector<logging::LoggerPtr> &logger, solanet::UUID uuid) \
144 : pimpl_(std::make_unique<Impl>(recv_callback, missing_callback, logger, uuid)) {} \
145 solanet::UUID C::publishImpl(const std::string &topic, const std::string &msg_content) { \
146 return pimpl_->publish(topic, msg_content); \
147 } \
148 bool C::addPeerImpl(const std::string &topic, const C::NodeInfo &info) { \
149 return pimpl_->addPeer(topic, info); \
150 } \
151 bool C::removePeerImpl(const std::string &topic, const std::string &ip, uint16_t port) { \
152 return pimpl_->removePeer(topic, ip, port); \
153 } \
154 void C::subscribeTopicImpl(const std::string &topic, const C::NodeInfo &info) { \
155 pimpl_->subscribeTopic(topic, info); \
156 } \
157 bool C::isSubscribedToTopicImpl(const std::string &topic) const { \
158 return pimpl_->isSubscribedToTopic(topic); \
159 } \
160 void C::unsubscribeTopicImpl(const std::string &topic) { pimpl_->unsubscribeTopic(topic); } \
161 C::NodeInfo C::getOwnNodeInfoImpl(const std::string &topic) const { \
162 return pimpl_->getOwnNodeInfo(topic); \
163 } \
164 C::~C() = default; \
165 solanet::UUID C::getUUIDImpl() const { return pimpl_->getUUID(); } \
166 NetworkInfoIPv4 C::getNetworkInfoImpl() const { return pimpl_->getNetworkInfo(); }
167
168} // namespace natter
169
170#endif // NATTER_NATTER_H_
Definition natter.h:26
bool isSubscribedToTopic(const std::string &topic) const
Definition natter.h:76
NodeInfo getOwnNodeInfo(const std::string &topic) const
Definition natter.h:109
bool removePeer(const std::string &topic, const std::string &ip, uint16_t port)
Definition natter.h:94
void unsubscribeTopic(const std::string &topic)
Definition natter.h:84
solanet::UUID publish(const std::string &topic, const std::string &msg_content)
Definition natter.h:45
NetworkInfoIPv4 getNetworkInfo() const
Definition natter.h:117
void subscribeTopic(const std::string &topic, const NodeInfo &info)
Definition natter.h:67
solanet::UUID getUUID() const
Definition natter.h:102
bool addPeer(const std::string &topic, const NodeInfo &info)
Definition natter.h:59
Definition network_info_ipv4.h:14