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 {};
47 NodeInfo(std::shared_ptr<dhtnet::ChannelSocketInterface> socket_)
50 NodeInfo(
bool mobile, std::shared_ptr<dhtnet::ChannelSocketInterface> socket_)
71 bool addNode(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket);
92 std::map<NodeId, NodeInfo>&
getNodes() {
return nodes; }
140 return known_nodes.find(nodeId) != known_nodes.end();
163 return mobile_nodes.find(nodeId) != mobile_nodes.end();
198 return connecting_nodes.find(nodeId) != connecting_nodes.end();
201 bool isEmpty()
const {
return nodes.empty(); }
225 return *node.begin();
233 asio::steady_timer&
getNodeTimer(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket);
294 std::set<std::shared_ptr<dhtnet::ChannelSocketInterface>>
getNodeSockets()
const;
298 std::map<NodeId, NodeInfo> nodes;
299 std::set<NodeId> known_nodes;
300 std::set<NodeId> connecting_nodes;
301 std::set<NodeId> mobile_nodes;
302 mutable std::mutex mutex;
317 bool addNode(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket);
325 bool addNode(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& channel,
326 std::list<Bucket>::iterator&
bucket);
358 return bucket->hasKnownNode(nodeId);
404 return bucket->hasConnectingNode(nodeId);
421 return std::list<Bucket>::const_iterator(
446 for (
const auto&
b : buckets)
447 count +=
b.getNodesSize();
467 for (
auto&
bucket : buckets)
468 bucket.shutdownAllNodes();
493 std::vector<NodeId>
getNodes()
const;
525 bool contains(
const std::list<Bucket>::iterator&
it,
const NodeId& nodeId)
const;
546 NodeId middle(std::list<Bucket>::iterator&
it)
const;
553 unsigned depth(std::list<Bucket>::iterator&
bucket)
const;
560 bool split(std::list<Bucket>::iterator&
bucket);
564 std::list<Bucket> buckets;
566 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.
std::set< std::shared_ptr< dhtnet::ChannelSocketInterface > > getNodeSockets() const
Get sockets from bucket.
std::map< NodeId, NodeInfo > & getNodes()
Get connected nodes from bucket.
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.
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.
unsigned getRoutingTableNodeCount() const
Returns number of total nodes in routing table.
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.
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.
unsigned getRoutingTableSize() const
Returns number of buckets in routing table.
bool hasConnectingNode(const NodeId &nodeId) const
Check if Connecting node exists in routing table.
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.
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.
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
NodeInfo(NodeInfo &&)=default
NodeInfo(std::shared_ptr< dhtnet::ChannelSocketInterface > socket_)
asio::steady_timer refresh_timer
NodeInfo(bool mobile, std::shared_ptr< dhtnet::ChannelSocketInterface > socket_)