2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
/*
|
|
|
|
Note:
|
|
|
|
- this class is marked for destruction
|
|
|
|
- functionality to be absorbed into llarp/link/link_manager.hpp
|
|
|
|
- can be subdivided into multiple objects
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
#include "rc_lookup_handler.hpp"
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/thread/threading.hpp>
|
2023-09-13 19:57:18 +00:00
|
|
|
#include <llarp/util/types.hpp>
|
|
|
|
#include <llarp/util/formattable.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/profiling.hpp>
|
2020-01-06 13:20:02 +00:00
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct PendingSession;
|
|
|
|
|
2023-08-28 14:56:44 +00:00
|
|
|
struct LinkManager;
|
2019-06-26 21:39:29 +00:00
|
|
|
struct I_RCLookupHandler;
|
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
enum class SessionResult
|
|
|
|
{
|
|
|
|
Establish,
|
|
|
|
Timeout,
|
|
|
|
RouterNotFound,
|
|
|
|
InvalidRouter,
|
|
|
|
NoLink,
|
|
|
|
EstablishFail
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr std::string_view
|
|
|
|
ToString(SessionResult sr)
|
|
|
|
{
|
|
|
|
return sr == llarp::SessionResult::Establish ? "success"sv
|
|
|
|
: sr == llarp::SessionResult::Timeout ? "timeout"sv
|
|
|
|
: sr == llarp::SessionResult::NoLink ? "no link"sv
|
|
|
|
: sr == llarp::SessionResult::InvalidRouter ? "invalid router"sv
|
|
|
|
: sr == llarp::SessionResult::RouterNotFound ? "not found"sv
|
|
|
|
: sr == llarp::SessionResult::EstablishFail ? "establish failed"sv
|
|
|
|
: "???"sv;
|
|
|
|
}
|
|
|
|
template <>
|
|
|
|
constexpr inline bool IsToStringFormattable<SessionResult> = true;
|
|
|
|
|
|
|
|
using RouterCallback = std::function<void(const RouterID&, const SessionResult)>;
|
|
|
|
|
|
|
|
struct OutboundSessionMaker
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-06-11 11:44:02 +00:00
|
|
|
using Work_t = std::function<void(void)>;
|
|
|
|
using WorkerFunc_t = std::function<void(Work_t)>;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using CallbacksQueue = std::list<RouterCallback>;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
public:
|
2023-09-13 19:57:18 +00:00
|
|
|
~OutboundSessionMaker() = default;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-13 19:57:18 +00:00
|
|
|
OnSessionEstablished(AbstractLinkSession* session);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-13 19:57:18 +00:00
|
|
|
OnConnectTimeout(AbstractLinkSession* session);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-13 19:57:18 +00:00
|
|
|
CreateSessionTo(const RouterID& router, RouterCallback on_result) EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-13 19:57:18 +00:00
|
|
|
CreateSessionTo(const RouterContact& rc, RouterCallback on_result) EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-13 19:57:18 +00:00
|
|
|
HavePendingSessionTo(const RouterID& router) const EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-13 19:57:18 +00:00
|
|
|
ConnectToRandomRouters(int numDesired);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
util::StatusObject
|
2023-09-13 19:57:18 +00:00
|
|
|
ExtractStatus() const;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2019-08-12 12:40:38 +00:00
|
|
|
bool
|
2023-09-13 19:57:18 +00:00
|
|
|
ShouldConnectTo(const RouterID& router) const EXCLUDES(_mutex);
|
2019-08-12 12:40:38 +00:00
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Init(
|
2023-09-15 14:55:32 +00:00
|
|
|
Router* router,
|
2023-09-13 15:57:29 +00:00
|
|
|
LinkManager* linkManager,
|
2023-09-14 12:08:10 +00:00
|
|
|
RCLookupHandler* rcLookup,
|
2020-04-07 18:38:56 +00:00
|
|
|
Profiling* profiler,
|
2021-03-02 07:02:59 +00:00
|
|
|
EventLoop_ptr loop,
|
2020-06-11 11:44:02 +00:00
|
|
|
WorkerFunc_t work);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2019-08-23 11:32:52 +00:00
|
|
|
void
|
|
|
|
SetOurRouter(RouterID r)
|
|
|
|
{
|
|
|
|
us = std::move(r);
|
|
|
|
}
|
|
|
|
|
2019-08-12 12:40:38 +00:00
|
|
|
/// always maintain this many connections to other routers
|
|
|
|
size_t minConnectedRouters = 4;
|
|
|
|
/// hard upperbound limit on the number of router to router connections
|
|
|
|
size_t maxConnectedRouters = 6;
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
private:
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
GotRouterContact(const RouterID& router, const RouterContact& rc) EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
InvalidRouter(const RouterID& router);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
RouterNotFound(const RouterID& router);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
OnRouterContactResult(
|
|
|
|
const RouterID& router, const RouterContact* const rc, const RCRequestResult result);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
VerifyRC(const RouterContact rc);
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
CreatePendingSession(const RouterID& router) EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
FinalizeRequest(const RouterID& router, const SessionResult type) EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-08-28 14:56:44 +00:00
|
|
|
mutable util::Mutex _mutex; // protects pendingCallbacks
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-03-09 18:39:40 +00:00
|
|
|
std::unordered_map<RouterID, CallbacksQueue> pendingCallbacks GUARDED_BY(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
Router* _router = nullptr;
|
2023-08-28 14:56:44 +00:00
|
|
|
LinkManager* _linkManager = nullptr;
|
2023-09-14 12:08:10 +00:00
|
|
|
RCLookupHandler* _rcLookup = nullptr;
|
2020-04-07 18:38:56 +00:00
|
|
|
Profiling* _profiler = nullptr;
|
2021-02-02 14:35:40 +00:00
|
|
|
std::shared_ptr<NodeDB> _nodedb;
|
2021-03-02 07:02:59 +00:00
|
|
|
EventLoop_ptr _loop;
|
2020-06-11 11:44:02 +00:00
|
|
|
WorkerFunc_t work;
|
2019-08-23 11:32:52 +00:00
|
|
|
RouterID us;
|
2019-06-26 21:39:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp
|