21#include <dhtnet/multiplexed_socket.h>
23#include <opendht/infohash.h>
26#include <asio/detail/deadline_timer_service.hpp>
43 std::shared_ptr<dhtnet::ChannelSocketInterface>
socket {};
50 NodeInfo(
bool mobile, std::shared_ptr<dhtnet::ChannelSocketInterface> socket_)
70 bool addNode(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket);
91 const std::map<NodeId, NodeInfo>&
getNodes()
const {
return nodes; }
92 std::map<NodeId, NodeInfo>&
getNodes() {
return nodes; }
138 bool hasKnownNode(
const NodeId& nodeId)
const {
return known_nodes.find(nodeId) != known_nodes.end(); }
192 return connecting_nodes.find(nodeId) != connecting_nodes.end();
195 bool isEmpty()
const {
return nodes.empty(); }
219 return *node.begin();
227 asio::steady_timer&
getNodeTimer(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket);
288 std::set<std::shared_ptr<dhtnet::ChannelSocketInterface>>
getNodeSockets()
const;
292 std::map<NodeId, NodeInfo> nodes;
293 std::set<NodeId> known_nodes;
294 std::set<NodeId> connecting_nodes;
295 std::set<NodeId> mobile_nodes;
296 mutable std::mutex mutex;
313 bool addNode(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket);
321 bool addNode(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& channel, std::list<Bucket>::iterator&
bucket);
353 return bucket->hasKnownNode(nodeId);
399 return bucket->hasConnectingNode(nodeId);
431 unsigned size()
const {
return buckets.size(); }
440 for (
const auto&
b : buckets)
441 count +=
b.getNodesSize();
448 for (
const auto&
b : buckets)
449 count +=
b.getNodesSize() +
b.getConnectingNodesSize();
469 for (
auto&
bucket : buckets)
470 bucket.shutdownAllNodes();
495 std::vector<NodeId>
getNodes()
const;
529 bool contains(
const std::list<Bucket>::iterator&
it,
const NodeId& nodeId)
const;
560 NodeId middle(std::list<Bucket>::iterator&
it)
const;
567 unsigned depth(std::list<Bucket>::iterator&
bucket)
const;
574 bool split(std::list<Bucket>::iterator&
bucket);
578 std::list<Bucket> buckets;
580 mutable std::mutex mutex_;
void setLowerLimit(const NodeId &nodeId)
Set bucket's lower limit.
void shutdownAllNodes()
Shutdowns all sockets in nodes through shutdownNode.
unsigned getNodesSize() const
Returns number of nodes in bucket.
bool addNode(const std::shared_ptr< dhtnet::ChannelSocketInterface > &socket)
Add Node socket to bucket.
bool removeNode(const NodeId &nodeId)
Remove NodeId socket from bucket and insert it in known_nodes or mobile_nodes depending on its type.
void removeMobileNode(const NodeId &nodeId)
Remove NodeId from mobile_nodes.
const std::set< NodeId > & getMobileNodes() const
Get NodeIds from mobile_nodes.
bool shutdownNode(const NodeId &nodeId)
Shutdowns socket and removes it from nodes.
unsigned getKnownNodesSize() const
Returns number of knwon_nodes in bucket.
NodeId getLowerLimit() const
Returns bucket lower limit.
NodeId randomId(std::mt19937_64 &rd) const
Returns random NodeId from known_nodes.
Bucket(const Bucket &)=delete
bool addMobileNode(const NodeId &nodeId)
Add NodeId to mobile_nodes if it doesn't exist in nodes.
void removeConnectingNode(const NodeId &nodeId)
Remove NodeId from connecting_nodes.
const std::set< NodeId > & getConnectingNodes() const
Get NodeIds of connecting_nodes.
bool hasNode(const NodeId &nodeId) const
Test if socket exists in nodes.
std::set< NodeId > getNodeIds() const
Get NodeIds from bucket.
static constexpr int BUCKET_MAX_SIZE
bool hasKnownNode(const NodeId &nodeId) const
Test if NodeId exist in known_nodes.
bool addConnectingNode(const NodeId &nodeId)
Add NodeId to connecting_nodes if it doesn't exist in nodes.
bool hasMobileNode(const NodeId &nodeId)
Test if NodeId exist in mobile_nodes.
NodeId getKnownNode(unsigned index) const
Returns NodeId from known_nodes at index.
void printBucket(unsigned number) const
Prints bucket and bucket's number.
bool addKnownNode(const NodeId &nodeId)
Add NodeId to known_nodes if it doesn't exist in nodes.
bool isFull() const
Indicate if bucket is full.
const std::map< NodeId, NodeInfo > & getNodes() const
Get connected nodes from bucket.
std::set< std::shared_ptr< dhtnet::ChannelSocketInterface > > getNodeSockets() const
Get sockets from bucket.
std::map< NodeId, NodeInfo > & getNodes()
bool hasConnectingNode(const NodeId &nodeId) const
Test if NodeId exist in connecting_nodes.
asio::steady_timer & getNodeTimer(const std::shared_ptr< dhtnet::ChannelSocketInterface > &socket)
Returns socket's timer.
const std::set< NodeId > & getKnownNodes() const
Get NodeIds from known_nodes.
unsigned getConnectingNodesSize() const
Returns number of mobile_nodes in bucket.
std::set< NodeId > getKnownNodesRandom(unsigned numberNodes, std::mt19937_64 &rd) const
Returns random numberNodes NodeId from known_nodes.
void changeMobility(const NodeId &nodeId, bool isMobile)
Change mobility of specific node, mobile or not.
void removeKnownNode(const NodeId &nodeId)
Remove NodeId from known_nodes.
static LIBJAMI_TEST_EXPORT Manager & instance()
std::shared_ptr< asio::io_context > ioContext() const
bool hasMobileNode(const NodeId &nodeId)
Check if mobile node exists in routing table.
std::vector< NodeId > getAllNodes() const
Return every node from each bucket.
std::vector< NodeId > getConnectingNodes() const
Returns all routing table's connecting nodes.
void deleteNode(const NodeId &nodeId)
Delete node from every table in bucket.
unsigned getNodeCount() const
Returns number of total nodes in routing table.
bool addKnownNode(const NodeId &nodeId)
Add known node to routing table.
std::vector< NodeId > closestNodes(const NodeId &nodeId, unsigned count)
Returns the count closest nodes to a specific nodeId.
std::vector< NodeId > getBucketMobileNodes() const
Returns mobile nodes corresponding to the swarm's id.
void shutdownAllNodes()
Shutdowns all nodes in routing table and add them to known_nodes or mobile_nodes.
void shutdownNode(const NodeId &nodeId)
Shutdowns a node.
bool addMobileNode(const NodeId &nodeId)
Add mobile node to routing table.
std::list< Bucket >::iterator findBucket(const NodeId &nodeId)
Returns bucket iterator containing nodeId.
std::vector< NodeId > getMobileNodes() const
Returns all routing table's mobile nodes.
unsigned size() const
Returns number of buckets in routing table.
bool hasKnownNode(const NodeId &nodeId) const
Checks if known node exists in routing table.
bool removeNode(const NodeId &nodeId)
Removes node from routing table Adds it to known_nodes or mobile_nodes depending on mobility.
void removeConnectingNode(const NodeId &nodeId)
Remove connecting connecting node to routing table.
bool hasNode(const NodeId &nodeId)
Check if connected node exsits in routing table.
bool hasConnectingNode(const NodeId &nodeId) const
Check if Connecting node exists in routing table.
std::vector< NodeId > getConnectedNodes() const
std::vector< NodeId > getKnownNodes() const
Returns all routing table's known nodes.
std::vector< NodeId > getNodes() const
Returns all routing table's connected nodes.
bool contains(const std::list< Bucket >::iterator &it, const NodeId &nodeId) const
Test if connected nodeId is in specific bucket.
void printRoutingTable() const
Prints routing table.
std::list< Bucket > & getBuckets()
Returns buckets in routing table.
bool addConnectingNode(const NodeId &nodeId)
Add connecting node to routing table.
const std::list< Bucket >::const_iterator findBucket(const NodeId &nodeId) const
Returns bucket iterator containing nodeId.
unsigned getActiveNodesCount() const
NodeId getId() const
Returns id for routing table.
void removeMobileNode(const NodeId &nodeId)
Remove mobile node to routing table.
bool addNode(const std::shared_ptr< dhtnet::ChannelSocketInterface > &socket)
Add socket to bucket.
std::vector< NodeStats > getRoutingTableStats() const
void setId(const NodeId &node)
Sets id for routing table.
static constexpr const std::chrono::minutes FIND_PERIOD
void emitSignal(Args... args)
std::shared_ptr< dhtnet::ChannelSocketInterface > socket
asio::steady_timer refresh_timer
NodeInfo(NodeInfo &&) noexcept=default
NodeInfo(bool mobile, std::shared_ptr< dhtnet::ChannelSocketInterface > socket_)
std::string remoteAddress
std::chrono::system_clock::time_point connectionTime