From 2897141036d91afcc33aaba1832821018f8e2118 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 28 May 2019 07:35:26 -0400 Subject: [PATCH] make format and introduce new function EnsureRouter on router to check nodedb or do dht lookup --- llarp/path/path.cpp | 29 ++++++++++++++++------------- llarp/router/abstractrouter.cpp | 11 +++++++++++ llarp/router/abstractrouter.hpp | 8 ++++++-- llarp/router/router.cpp | 4 ++-- llarp/router/router.hpp | 2 -- llarp/service/context.cpp | 4 ++-- llarp/utp/linklayer.cpp | 3 +-- 7 files changed, 38 insertions(+), 23 deletions(-) diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index bb7a71461..d333d3ed6 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -99,25 +99,28 @@ namespace llarp PathContext::ForwardLRCM(const RouterID& nextHop, const std::array< EncryptedFrame, 8 >& frames) { - auto msg = std::make_shared(frames); + auto msg = std::make_shared< const LR_CommitMessage >(frames); LogDebug("forwarding LRCM to ", nextHop); if(m_Router->HasSessionTo(nextHop)) { return m_Router->SendToOrQueue(nextHop, msg.get()); } - const RouterID router = nextHop; - AbstractRouter * const r = m_Router; - m_Router->LookupRouter(nextHop, [msg, r, router](const std::vector & found) { - if(found.size()) - { - r->TryConnectAsync(found[0], 1); - r->SendToOrQueue(router, msg.get()); - } - else - LogError("dropped LRCM to ", router, " as we cannot find in via DHT"); - }); - LogInfo("we are not directly connected to ", router, " so we need to do a lookup"); + const RouterID router = nextHop; + AbstractRouter* const r = m_Router; + m_Router->EnsureRouter( + nextHop, [msg, r, router](const std::vector< RouterContact >& found) { + if(found.size()) + { + r->TryConnectAsync(found[0], 1); + r->SendToOrQueue(router, msg.get()); + } + else + LogError("dropped LRCM to ", router, + " as we cannot find in via DHT"); + }); + LogInfo("we are not directly connected to ", router, + " so we need to do a lookup"); return true; } template < typename Map_t, typename Key_t, typename CheckValue_t, diff --git a/llarp/router/abstractrouter.cpp b/llarp/router/abstractrouter.cpp index b0da46e73..9bb8be2f8 100644 --- a/llarp/router/abstractrouter.cpp +++ b/llarp/router/abstractrouter.cpp @@ -1,8 +1,19 @@ #include +#include namespace llarp { AbstractRouter::~AbstractRouter() { } + + void + AbstractRouter::EnsureRouter(RouterID router, RouterLookupHandler handler) + { + std::vector< RouterContact > found(1); + if(nodedb()->Get(router, found[0])) + handler(found); + else + LookupRouter(router, handler); + } } // namespace llarp diff --git a/llarp/router/abstractrouter.hpp b/llarp/router/abstractrouter.hpp index 883a65b6a..149adfbfe 100644 --- a/llarp/router/abstractrouter.hpp +++ b/llarp/router/abstractrouter.hpp @@ -218,12 +218,16 @@ namespace llarp virtual bool ConnectionToRouterAllowed(const RouterID &router) const = 0; - /// return true if we have at least 1 session to this router in either direction + /// return true if we have at least 1 session to this router in either + /// direction virtual bool - HasSessionTo(const RouterID & router) const = 0; + HasSessionTo(const RouterID &router) const = 0; virtual util::StatusObject ExtractStatus() const = 0; + + void + EnsureRouter(RouterID router, RouterLookupHandler handler); }; } // namespace llarp diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 037f8aad9..dd8ca77fd 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -360,8 +360,8 @@ namespace llarp // we don't have the RC locally so do a dht lookup _dht->impl->LookupRouter(remote, - std::bind(&Router::HandleDHTLookupForSendTo, this, - remote, std::placeholders::_1)); + std::bind(&Router::HandleDHTLookupForSendTo, this, + remote, std::placeholders::_1)); return true; } diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 178b341a7..4c6e8f387 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -535,8 +535,6 @@ namespace llarp bool HasSessionTo(const RouterID &remote) const override; - - void HandleDHTLookupForTryEstablishTo( RouterID remote, const std::vector< RouterContact > &results); diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index 9a29cd86a..3c0d00a66 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -123,9 +123,9 @@ namespace llarp } } // tick active endpoints - for(const auto & item : m_Endpoints) + for(const auto &item : m_Endpoints) { - item.second->Tick(now); + item.second->Tick(now); } /* std::vector< RouterID > expired; diff --git a/llarp/utp/linklayer.cpp b/llarp/utp/linklayer.cpp index e27b0a98f..e02403645 100644 --- a/llarp/utp/linklayer.cpp +++ b/llarp/utp/linklayer.cpp @@ -329,8 +329,7 @@ namespace llarp LinkLayer::NewOutboundSession(const RouterContact& rc, const AddressInfo& addr) { - return std::make_shared< OutboundSession >( - this, NewSocket(), rc, addr); + return std::make_shared< OutboundSession >(this, NewSocket(), rc, addr); } uint64