2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "i_link_manager.hpp"
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/compare_ptr.hpp>
|
|
|
|
#include "server.hpp"
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <set>
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct IRouterContactManager;
|
|
|
|
|
|
|
|
struct LinkManager final : public ILinkManager
|
|
|
|
{
|
|
|
|
public:
|
2019-07-30 23:42:13 +00:00
|
|
|
~LinkManager() override = default;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
LinkLayer_ptr
|
2020-04-07 18:38:56 +00:00
|
|
|
GetCompatibleLink(const RouterContact& rc) const override;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
IOutboundSessionMaker*
|
2019-06-26 21:39:29 +00:00
|
|
|
GetSessionMaker() const override;
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
SendTo(
|
|
|
|
const RouterID& remote,
|
|
|
|
const llarp_buffer_t& buf,
|
|
|
|
ILinkSession::CompletionHandler completed) override;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
HasSessionTo(const RouterID& remote) const override;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-06-07 22:31:57 +00:00
|
|
|
bool
|
|
|
|
HasOutboundSessionTo(const RouterID& remote) const override;
|
|
|
|
|
2020-11-10 14:24:58 +00:00
|
|
|
std::optional<bool>
|
|
|
|
SessionIsClient(RouterID remote) const override;
|
|
|
|
|
|
|
|
void
|
|
|
|
DeregisterPeer(RouterID remote) override;
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
|
|
|
PumpLinks() override;
|
|
|
|
|
|
|
|
void
|
|
|
|
AddLink(LinkLayer_ptr link, bool inbound = false) override;
|
|
|
|
|
|
|
|
bool
|
2021-03-02 22:27:35 +00:00
|
|
|
StartLinks() override;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Stop() override;
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
PersistSessionUntil(const RouterID& remote, llarp_time_t until) override;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ForEachPeer(std::function<void(const ILinkSession*, bool)> visit, bool randomize = false)
|
|
|
|
const override;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ForEachPeer(std::function<void(ILinkSession*)> visit) override;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ForEachInboundLink(std::function<void(LinkLayer_ptr)> visit) const override;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-02-08 11:35:24 +00:00
|
|
|
void
|
|
|
|
ForEachOutboundLink(std::function<void(LinkLayer_ptr)> visit) const override;
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
size_t
|
|
|
|
NumberOfConnectedRouters() const override;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
NumberOfConnectedClients() const override;
|
|
|
|
|
2019-12-03 17:03:19 +00:00
|
|
|
size_t
|
|
|
|
NumberOfPendingConnections() const override;
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
GetRandomConnectedRouter(RouterContact& router) const override;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
CheckPersistingSessions(llarp_time_t now) override;
|
|
|
|
|
2020-06-04 16:00:30 +00:00
|
|
|
void
|
|
|
|
updatePeerDb(std::shared_ptr<PeerDb> peerDb) override;
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
util::StatusObject
|
2019-06-26 21:39:29 +00:00
|
|
|
ExtractStatus() const override;
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Init(IOutboundSessionMaker* sessionMaker);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
LinkLayer_ptr
|
2020-04-07 18:38:56 +00:00
|
|
|
GetLinkWithSessionTo(const RouterID& remote) const;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::atomic<bool> stopping;
|
2019-06-26 21:39:29 +00:00
|
|
|
mutable util::Mutex _mutex; // protects m_PersistingSessions
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using LinkSet = std::set<LinkLayer_ptr, ComparePtr<LinkLayer_ptr>>;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
LinkSet outboundLinks;
|
|
|
|
LinkSet inboundLinks;
|
|
|
|
|
|
|
|
// sessions to persist -> timestamp to end persist at
|
2021-03-09 18:39:40 +00:00
|
|
|
std::unordered_map<RouterID, llarp_time_t> m_PersistingSessions GUARDED_BY(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-03-09 18:39:40 +00:00
|
|
|
std::unordered_map<RouterID, SessionStats> m_lastRouterStats;
|
2020-06-04 16:00:30 +00:00
|
|
|
|
2021-06-07 12:39:38 +00:00
|
|
|
util::DecayingHashSet<RouterID> m_Clients{path::default_lifetime};
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
IOutboundSessionMaker* _sessionMaker;
|
2019-06-26 21:39:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp
|