7#ifndef NATTER_NATTER_H_
8#define NATTER_NATTER_H_
15#include "network_info_ipv4.h"
16#include "solanet/uuid.h"
20using MsgReceiveFct = std::function<void(
const Message &)>;
21using MsgMissingFct = std::function<void(
const std::string &)>;
26template <
class T,
class NodeInfoT>
class Natter {
29 using NodeInfo = NodeInfoT;
45 solanet::UUID
publish(
const std::string &topic,
const std::string &msg_content) {
46 return static_cast<T *
>(
this)->publishImpl(topic, msg_content);
59 bool addPeer(
const std::string &topic,
const NodeInfo &info) {
60 return static_cast<T *
>(
this)->addPeerImpl(topic, info);
68 static_cast<T *
>(
this)->subscribeTopicImpl(topic, info);
77 return static_cast<const T *
>(
this)->isSubscribedToTopicImpl(topic);
85 static_cast<T *
>(
this)->unsubscribeTopicImpl(topic);
94 bool removePeer(
const std::string &topic,
const std::string &ip, uint16_t port) {
95 return static_cast<T *
>(
this)->removePeerImpl(topic, ip, port);
102 solanet::UUID
getUUID()
const {
return static_cast<const T *
>(
this)->getUUIDImpl(); }
110 return static_cast<const T *
>(
this)->getOwnNodeInfoImpl(topic);
118 return static_cast<const T *
>(
this)->getNetworkInfoImpl();
122#define DECLARE_CRTP_METHODS \
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;
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); \
148 bool C::addPeerImpl(const std::string &topic, const C::NodeInfo &info) { \
149 return pimpl_->addPeer(topic, info); \
151 bool C::removePeerImpl(const std::string &topic, const std::string &ip, uint16_t port) { \
152 return pimpl_->removePeer(topic, ip, port); \
154 void C::subscribeTopicImpl(const std::string &topic, const C::NodeInfo &info) { \
155 pimpl_->subscribeTopic(topic, info); \
157 bool C::isSubscribedToTopicImpl(const std::string &topic) const { \
158 return pimpl_->isSubscribedToTopic(topic); \
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); \
165 solanet::UUID C::getUUIDImpl() const { return pimpl_->getUUID(); } \
166 NetworkInfoIPv4 C::getNetworkInfoImpl() const { return pimpl_->getNetworkInfo(); }
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