26using namespace swarm_protocol;
28class SwarmManager :
public std::enable_shared_from_this<SwarmManager>
30 using ChannelCb = std::function<
bool(
const std::shared_ptr<dhtnet::ChannelSocketInterface>&)>;
31 using NeedSocketCb = std::function<
void(
const std::string&, ChannelCb&&)>;
32 using ToConnectCb = std::function<
bool(
const NodeId&)>;
33 using OnConnectionChanged = std::function<
void(
bool ok)>;
66 void addChannel(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& channel);
101 void deleteNode(
const std::vector<NodeId>& nodes);
135 JAMI_DEBUG(
"SwarmManager {:s} has {:d} nodes in table [P = {}]",
141 for (
auto& node :
bucket.getNodes()) {
142 JAMI_DEBUG(
"Node {:s}", node.first.toString());
190 bool addKnownNode(
const NodeId& nodeId);
196 void addMobileNodes(
const NodeId& nodeId);
205 void sendRequest(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket,
215 void sendAnswer(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket,
const Message&
msg_);
221 void receiveMessage(
const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket);
229 void resetNodeExpiry(
const asio::error_code&
ec,
230 const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket,
237 void tryConnect(
const NodeId& nodeId);
243 void removeNodeInternal(
const NodeId& nodeId);
246 bool isMobile_ {
false};
248 mutable std::mutex mutex;
249 RoutingTable routing_table;
251 std::atomic_bool isShutdown_ {
false};
253 OnConnectionChanged onConnectionChanged_ {};
255 ToConnectCb toConnectCb_;
static constexpr int BUCKET_MAX_SIZE
unsigned getNodeCount() const
Returns number of total nodes in routing table.
std::list< Bucket > & getBuckets()
Returns buckets in routing table.
unsigned getActiveNodesCount() const
void onConnectionChanged(OnConnectionChanged cb)
unsigned getActiveNodesCount() const
bool isMobile() const
Get mobility of swarm manager.
NeedSocketCb needSocketCb_
std::list< Bucket > & getBuckets()
Get buckets of routing table.
bool isConnectedWith(const NodeId &deviceId)
Check if we're connected with a specific device.
void removeNode(const NodeId &nodeId)
Remove channel from routing table.
void shutdown()
Shutdown swarm manager.
bool isShutdown()
Check if swarm manager is shutdown.
void changeMobility(const NodeId &nodeId, bool isMobile)
Change mobility of specific node.
void maintainBuckets(const std::set< NodeId > &toConnect={})
Maintain/Update buckets.
void deleteNode(const std::vector< NodeId > &nodes)
Delete nodes from the different tables in bucket.
void setMobileNodes(const std::vector< NodeId > &mobile_nodes)
Set list of nodes to the routing table mobile_nodes.
const NodeId & getId() const
Get swarm manager id.
void restart()
Restart the swarm manager.
bool setKnownNodes(const std::vector< NodeId > &known_nodes)
Set list of nodes to the routing table known_nodes.
std::vector< NodeId > getConnectedNodes() const
std::vector< NodeId > getAllNodes() const
get all nodes from the different tables in bucket
std::vector< std::map< std::string, std::string > > getRoutingTableInfo() const
void setMobility(bool isMobile)
Set mobility of swarm manager.
void addChannel(const std::shared_ptr< dhtnet::ChannelSocketInterface > &channel)
Add channel to routing table.
std::weak_ptr< SwarmManager > weak()
void display()
Display swarm manager info.
RoutingTable & getRoutingTable()
Get routing table.
bool isConnected() const
Check if swarm manager is connected.
#define JAMI_DEBUG(formatstr,...)
void emitSignal(Args... args)