Ring Daemon 16.0.0
Loading...
Searching...
No Matches
swarm_manager.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include "routing_table.h"
20#include "swarm_protocol.h"
21
22#include <iostream>
23#include <memory>
24
25namespace jami {
26
27using namespace swarm_protocol;
28
29class SwarmManager : public std::enable_shared_from_this<SwarmManager>
30{
31 using ChannelCb = std::function<bool(const std::shared_ptr<dhtnet::ChannelSocketInterface>&)>;
32 using NeedSocketCb = std::function<void(const std::string&, ChannelCb&&)>;
33 using ToConnectCb = std::function<bool(const NodeId&)>;
34 using OnConnectionChanged = std::function<void(bool ok)>;
35
36public:
37 explicit SwarmManager(const NodeId&, const std::mt19937_64& rand, ToConnectCb&& toConnectCb);
39
40 NeedSocketCb needSocketCb_;
41
42 std::weak_ptr<SwarmManager> weak() { return weak_from_this(); }
43
48 const NodeId& getId() const { return id_; }
49
55 bool setKnownNodes(const std::vector<NodeId>& known_nodes);
56
61 void setMobileNodes(const std::vector<NodeId>& mobile_nodes);
62
67 void addChannel(const std::shared_ptr<dhtnet::ChannelSocketInterface>& channel);
68
73 void removeNode(const NodeId& nodeId);
74
80 void changeMobility(const NodeId& nodeId, bool isMobile);
81
85 std::vector<NodeId> getAllNodes() const;
86
90 void deleteNode(std::vector<NodeId> nodes);
91
92 // For tests
93
98 RoutingTable& getRoutingTable() { return routing_table; };
99
104 std::list<Bucket>& getBuckets() { return routing_table.getBuckets(); };
105
109 void shutdown();
110
117 void restart();
118
122 void display()
123 {
124 JAMI_DEBUG("SwarmManager {:s} has {:d} nodes in table [P = {}]",
125 getId().to_c_str(),
126 routing_table.getRoutingTableNodeCount(),
127 isMobile_);
128 // print nodes of routingtable
129 for (auto& bucket : routing_table.getBuckets()) {
130 for (auto& node : bucket.getNodes()) {
131 JAMI_DEBUG("Node {:s}", node.first.toString());
132 }
133 }
134 }
135
136 /*
137 * Callback for connection changed
138 * @param cb
139 */
140 void onConnectionChanged(OnConnectionChanged cb) { onConnectionChanged_ = std::move(cb); }
141
146 void setMobility(bool isMobile) { isMobile_ = isMobile; }
147
152 bool isMobile() const { return isMobile_; }
153
158 void maintainBuckets(const std::set<NodeId>& toConnect = {});
159
165 bool isConnectedWith(const NodeId& deviceId);
166
171 bool isShutdown() { return isShutdown_; };
172
173private:
179 bool addKnownNode(const NodeId& nodeId);
180
185 void addMobileNodes(const NodeId& nodeId);
186
194 void sendRequest(const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket,
195 NodeId& nodeId,
196 Query q,
198
204 void sendAnswer(const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket, const Message& msg_);
205
210 void receiveMessage(const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket);
211
218 void resetNodeExpiry(const asio::error_code& ec,
219 const std::shared_ptr<dhtnet::ChannelSocketInterface>& socket,
220 NodeId node = {});
221
226 void tryConnect(const NodeId& nodeId);
227
232 void removeNodeInternal(const NodeId& nodeId);
233
234 const NodeId id_;
235 bool isMobile_ {false};
236 std::mt19937_64 rd;
237 mutable std::mutex mutex;
238 RoutingTable routing_table;
239
240 std::atomic_bool isShutdown_ {false};
241
242 OnConnectionChanged onConnectionChanged_ {};
243
244 ToConnectCb toConnectCb_;
245};
246
247} // namespace jami
static constexpr int BUCKET_MAX_SIZE
unsigned getRoutingTableNodeCount() const
Returns number of total nodes in routing table.
std::list< Bucket > & getBuckets()
Returns buckets in routing table.
void onConnectionChanged(OnConnectionChanged cb)
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 deleteNode(std::vector< NodeId > nodes)
Delete nodes from the different tables in bucket.
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 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 > getAllNodes() const
get all nodes from the different tables in bucket
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.
#define JAMI_DEBUG(formatstr,...)
Definition logger.h:226
dht::h256 NodeId
void emitSignal(Args... args)
Definition ring_signal.h:64
dht::PkId NodeId