SOLA
Loading...
Searching...
No Matches
minhcast_impl.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_MINHCAST_MINHCAST_IMPL_H_
8#define NATTER_MINHCAST_MINHCAST_IMPL_H_
9
10#include <cstdint>
11#include <optional>
12#include <set>
13#include <string>
14#include <tuple>
15#include <unordered_map>
16#include <vector>
17
18#include "broadcast_info.h"
19#include "core/network_facade.h"
20#include "logging/logger.h"
21#include "natter/logger_interface.h"
22#include "natter/minhcast_level_number.h"
23#include "natter/natter_minhcast.h"
24#include "solanet/uuid.h"
25#include "solanet/uuid_generator.h"
26
27namespace natter::minhcast {
28
29class MinhcastMessage;
30
32public:
33 Impl(MsgReceiveFct recv, MsgMissingFct miss, std::vector<logging::LoggerPtr> logger = {},
34 solanet::UUID uuid = solanet::generateUUID());
35
36 // Publish message, returns message_id if everything went successfull
37 solanet::UUID publish(const std::string &topic, const std::string &msg_content);
38
39 bool addPeer(const std::string &topic, const NodeInfo &info);
40
41 bool removePeer(const std::string &topic, const std::string &ip, uint16_t port);
42
43 void subscribeTopic(const std::string &topic, const NodeInfo &info);
44
45 bool isSubscribedToTopic(const std::string &topic) const;
46
47 void unsubscribeTopic(const std::string &topic);
48
49 NodeInfo getOwnNodeInfo(const std::string &topic) const { return own_node_info_.at(topic); }
50
51 solanet::UUID getUUID() const { return uuid_; }
52
53 NetworkInfoIPv4 getNetworkInfo() const { return network_.getNetworkInfo(); }
54
55private:
56 BroadcastInfo createBroadcastInfo(const MinhcastMessage &msg) const;
57
58#ifndef NDEBUG
59 // For debugging: Log forwarding limits and check that no message arrived multiple times
60 void logMinhcastBroadcastInfo(const BroadcastInfo &bc);
61#endif
62
68 std::tuple<bool, bool> sendToChildren(const BroadcastInfo &bc, Level child_down_border);
69
70 // Helper methods to send message to other nodes
71 void sendToInlevel(const BroadcastInfo &bc, bool other_forwarding_down, bool send_to_children);
72 void sendToParent(const BroadcastInfo &bc, Level parent_up_border);
73 std::tuple<uint32_t, uint32_t> sendToAdjacents(const BroadcastInfo &bc);
74
75 // Return nodes for inlevel forward for given broadcast info
76 std::tuple<std::vector<NodeInfo>, std::vector<NodeInfo>> getInlevel(const BroadcastInfo &bc);
77
83 static bool centeredNodeAddChildren(const BroadcastInfo &bc,
84 const std::set<NodeInfo> &other_peers,
85 std::vector<NodeInfo> &receiver);
86
92 static bool imperfectTreeAddChildren(const BroadcastInfo &bc,
93 const std::set<NodeInfo> &other_peers,
94 std::vector<NodeInfo> &receiver);
95
96 // Calculate intersection between lastNode LRT and ownNode RRT
97 static std::vector<LevelNumber> calculateRRTIntersection(const BroadcastInfo &bc);
98
99 // Get leftmost adjacent on deepest level and within BroadcastInfos forwarding limits
100 std::optional<NatterMinhcast::NodeInfo> getDeeperAdjacent(const BroadcastInfo &bc) const;
101
102 // Get rightmost adjacent on highest level and within BroadcastInfos forwarding limits
103 std::optional<NatterMinhcast::NodeInfo> getHigherAdjacent(const BroadcastInfo &bc) const;
104
105 // Helper method to send message to peer
106 void sendMessage(const BroadcastInfo &bc, const NodeInfo &peer, uint32_t up_limit,
107 uint32_t down_limit, bool inner_forward = false);
108
109 // Processing method for newly arrived messages
110 void processMessage(const MinhcastMessage &msg);
111
112 void broadcast(const BroadcastInfo &bc);
113
114 // Check if node has children
115 static bool hasChildren(LevelNumber node, const std::set<NodeInfo> &other_peers);
116
117 // Add all nodes from LRT and RRT of own_node to receiver
118 static void addRoutingTableToReceiver(LevelNumber own_node, const std::set<NodeInfo> &other_peers,
119 std::vector<NodeInfo> &receiver);
120 static void addLRT(LevelNumber own_node, const std::set<NodeInfo> &other_peers,
121 std::vector<NodeInfo> &receiver);
122 static void addRRT(LevelNumber own_node, const std::set<NodeInfo> &other_peers,
123 std::vector<NodeInfo> &receiver);
124
125 // Add all nodes in given direction to receiver which are in the remaining interval of nodes
126 // next to us which haven't received the message yet and are known by us
127 static void addLeftForwardNodes(LevelNumber own_node, LevelNumber last_node,
128 const std::set<NodeInfo> &other_peers,
129 std::vector<NodeInfo> &receiver);
130 static void addRightForwardNodes(LevelNumber own_node, LevelNumber last_node,
131 const std::set<NodeInfo> &other_peers,
132 std::vector<NodeInfo> &receiver);
133
134 // Add inner nodes (of two outer children with m > 2) to receiver
135 static void addInnerForwardNodes(const BroadcastInfo &bc, const std::set<NodeInfo> &other_peers,
136 std::vector<NodeInfo> &receiver);
137
138 static void addLeftAndRightmostChildren(LevelNumber node_info,
139 const std::set<NodeInfo> &other_peers,
140 std::vector<NodeInfo> &receiver);
141
142 // Helper function to add nodes in given interval by passing a comparison function object
143 template <typename Compare>
144 static void addFromRoutingTable(const std::set<LevelNumber> &rt,
145 const std::set<NodeInfo> &other_peers,
146 std::vector<NodeInfo> &receiver, uint32_t boundary, Compare comp);
147
148 using Topic = std::string;
149 std::unordered_map<Topic, NodeInfo> own_node_info_;
150 std::unordered_map<Topic, std::set<NodeInfo>> other_peers_;
151 solanet::UUID uuid_;
153 MsgReceiveFct msg_recv_callback_;
155
156#ifndef NDEBUG
157 // To keep track of possible received duplicate messages
158 std::set<solanet::UUID> received_messages_;
159#endif
160};
161} // namespace natter::minhcast
162
163#endif // DAISI_NATTER_IMPL_H_
Definition network_facade.h:18
Definition logger.h:26
Definition minhcast_message.h:21
Definition minhcast_impl.h:31
Definition network_info_ipv4.h:14
Definition broadcast_info.h:19