2019-06-26 21:39:29 +00:00
|
|
|
#ifndef LLARP_ROUTER_OUTBOUND_SESSION_MAKER_HPP
|
|
|
|
#define LLARP_ROUTER_OUTBOUND_SESSION_MAKER_HPP
|
|
|
|
|
|
|
|
#include <router/i_outbound_session_maker.hpp>
|
|
|
|
|
|
|
|
#include <router/i_rc_lookup_handler.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
|
|
|
#include <util/thread/threading.hpp>
|
|
|
|
#include <util/thread/thread_pool.hpp>
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
struct llarp_nodedb;
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct PendingSession;
|
|
|
|
|
|
|
|
struct ILinkManager;
|
|
|
|
struct I_RCLookupHandler;
|
|
|
|
|
|
|
|
struct OutboundSessionMaker final : public IOutboundSessionMaker
|
|
|
|
{
|
|
|
|
using CallbacksQueue = std::list< RouterCallback >;
|
|
|
|
|
|
|
|
public:
|
2019-07-30 23:42:13 +00:00
|
|
|
~OutboundSessionMaker() override = default;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
OnSessionEstablished(ILinkSession *session) override;
|
|
|
|
|
|
|
|
void
|
|
|
|
OnConnectTimeout(ILinkSession *session) override;
|
|
|
|
|
|
|
|
void
|
2019-08-07 23:18:56 +00:00
|
|
|
CreateSessionTo(const RouterID &router, RouterCallback on_result) override
|
|
|
|
LOCKS_EXCLUDED(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2019-08-07 23:18:56 +00:00
|
|
|
CreateSessionTo(const RouterContact &rc, RouterCallback on_result) override
|
|
|
|
LOCKS_EXCLUDED(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2019-08-07 23:18:56 +00:00
|
|
|
HavePendingSessionTo(const RouterID &router) const override
|
|
|
|
LOCKS_EXCLUDED(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ConnectToRandomRouters(int numDesired, llarp_time_t now) override;
|
|
|
|
|
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const override;
|
|
|
|
|
2019-08-12 12:40:38 +00:00
|
|
|
bool
|
|
|
|
ShouldConnectTo(const RouterID &router) const override
|
|
|
|
LOCKS_EXCLUDED(_mutex);
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
|
|
|
Init(ILinkManager *linkManager, I_RCLookupHandler *rcLookup,
|
|
|
|
std::shared_ptr< Logic > logic, llarp_nodedb *nodedb,
|
|
|
|
std::shared_ptr< llarp::thread::ThreadPool > threadpool);
|
|
|
|
|
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
|
2019-08-07 23:18:56 +00:00
|
|
|
DoEstablish(const RouterID &router) LOCKS_EXCLUDED(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2019-08-07 23:18:56 +00:00
|
|
|
GotRouterContact(const RouterID &router, const RouterContact &rc)
|
|
|
|
LOCKS_EXCLUDED(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
InvalidRouter(const RouterID &router);
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterNotFound(const RouterID &router);
|
|
|
|
|
|
|
|
void
|
|
|
|
OnRouterContactResult(const RouterID &router, const RouterContact *const rc,
|
|
|
|
const RCRequestResult result);
|
|
|
|
|
|
|
|
void
|
|
|
|
VerifyRC(const RouterContact rc);
|
|
|
|
|
|
|
|
void
|
2019-08-07 23:18:56 +00:00
|
|
|
CreatePendingSession(const RouterID &router) LOCKS_EXCLUDED(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2019-08-07 23:18:56 +00:00
|
|
|
FinalizeRequest(const RouterID &router, const SessionResult type)
|
|
|
|
LOCKS_EXCLUDED(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
mutable util::Mutex _mutex; // protects pendingSessions, pendingCallbacks
|
|
|
|
|
|
|
|
std::unordered_map< RouterID, std::shared_ptr< PendingSession >,
|
|
|
|
RouterID::Hash >
|
|
|
|
pendingSessions GUARDED_BY(_mutex);
|
|
|
|
|
|
|
|
std::unordered_map< RouterID, CallbacksQueue, RouterID::Hash >
|
|
|
|
pendingCallbacks GUARDED_BY(_mutex);
|
|
|
|
|
|
|
|
ILinkManager *_linkManager;
|
|
|
|
I_RCLookupHandler *_rcLookup;
|
|
|
|
std::shared_ptr< Logic > _logic;
|
|
|
|
llarp_nodedb *_nodedb;
|
|
|
|
std::shared_ptr< llarp::thread::ThreadPool > _threadpool;
|
2019-08-23 11:32:52 +00:00
|
|
|
RouterID us;
|
2019-06-26 21:39:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif // LLARP_ROUTER_OUTBOUND_SESSION_MAKER_HPP
|