@ -6,50 +6,50 @@
namespace llarp : : path
{
PathSet : : PathSet ( size_t num ) : num DesiredPaths ( num )
PathSet : : PathSet ( size_t num ) : num _paths_desired ( num )
{ }
bool
PathSet : : ShouldBuildMore ( llarp_time_t now ) const
{
( void ) now ;
const auto building = NumInStatus ( BUILDING) ;
if ( building > = num DesiredPaths )
const auto building = NumInStatus ( PathStatus: : BUILDING) ;
if ( building > = num _paths_desired )
return false ;
const auto established = NumInStatus ( ESTABLISHED) ;
return established < num DesiredPaths ;
}
bool
PathSet : : ShouldBuildMoreForRoles ( llarp_time_t now , PathRole roles ) const
{
Lock_t l ( m_PathsMutex ) ;
const size_t required = MinRequiredForRoles ( roles ) ;
size_t has = 0 ;
for ( const auto & item : m_Paths )
{
if ( item . second - > SupportsAnyRoles ( roles ) )
{
if ( ! item . second - > ExpiresSoon ( now ) )
+ + has ;
}
}
return has < required ;
}
size_t
PathSet : : MinRequiredForRoles ( PathRole roles ) const
{
( void ) roles ;
return 0 ;
}
const auto established = NumInStatus ( PathStatus: : ESTABLISHED) ;
return established < num _paths_desired ;
}
// bool
// PathSet::ShouldBuildMoreForRoles(llarp_time_t now, PathRole roles) const
// {
// Lock_t l(paths_mutex);
// const size_t required = MinRequiredForRoles(roles);
// size_t has = 0;
// for (const auto& item : _paths)
// {
// if (item.second->SupportsAnyRoles(roles))
// {
// if (!item.second->ExpiresSoon(now))
// ++has;
// }
// }
// return has < required;
// }
// size_t
// PathSet::MinRequiredForRoles(PathRole roles) const
// {
// (void)roles;
// return 0;
// }
size_t
PathSet : : NumPathsExistingAt ( llarp_time_t futureTime ) const
{
size_t num = 0 ;
Lock_t l ( m_PathsM utex) ;
for ( const auto & item : m_P aths)
Lock_t l ( paths_m utex) ;
for ( const auto & item : _p aths)
{
if ( item . second - > IsReady ( ) & & ! item . second - > Expired ( futureTime ) )
+ + num ;
@ -61,8 +61,8 @@ namespace llarp::path
PathSet : : TickPaths ( Router * r )
{
const auto now = llarp : : time_now_ms ( ) ;
Lock_t l { m_PathsM utex} ;
for ( auto & item : m_P aths)
Lock_t l { paths_m utex} ;
for ( auto & item : _p aths)
{
item . second - > Tick ( now , r ) ;
}
@ -72,17 +72,17 @@ namespace llarp::path
PathSet : : Tick ( llarp_time_t )
{
std : : unordered_set < RouterID > endpoints ;
for ( auto & item : m_P aths)
for ( auto & item : _p aths)
{
endpoints . emplace ( item . second - > Endpoint ( ) ) ;
}
m_PathC ache. clear ( ) ;
path_c ache. clear ( ) ;
for ( const auto & ep : endpoints )
{
if ( auto path = GetPathByRouter ( ep ) )
{
m_PathC ache[ ep ] = path - > weak_from_this ( ) ;
path_c ache[ ep ] = path - > weak_from_this ( ) ;
}
}
}
@ -90,11 +90,11 @@ namespace llarp::path
void
PathSet : : ExpirePaths ( llarp_time_t now , [[maybe_unused]] Router * router )
{
Lock_t l ( m_PathsM utex) ;
if ( m_P aths. size ( ) = = 0 )
Lock_t l ( paths_m utex) ;
if ( _p aths. size ( ) = = 0 )
return ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > Expired ( now ) )
{
@ -103,23 +103,23 @@ namespace llarp::path
// router->outboundMessageHandler().RemovePath(std::move(txid));
PathID_t rxid = itr - > second - > RXID ( ) ;
// router->outboundMessageHandler().RemovePath(std::move(rxid));
itr = m_P aths. erase ( itr ) ;
itr = _p aths. erase ( itr ) ;
}
else
+ + itr ;
}
}
Path_ptr
std: : shared_ptr < Path >
PathSet : : GetEstablishedPathClosestTo (
RouterID id , std : : unordered_set < RouterID > excluding , PathRole roles ) const
{
Lock_t l { m_PathsM utex} ;
Path_ptr path = nullptr ;
Lock_t l { paths_m utex} ;
std: : shared_ptr < Path > path = nullptr ;
AlignedBuffer < 32 > dist ;
AlignedBuffer < 32 > to = id ;
dist . Fill ( 0xff ) ;
for ( const auto & item : m_P aths)
for ( const auto & item : _p aths)
{
if ( ! item . second - > IsReady ( ) )
continue ;
@ -137,13 +137,13 @@ namespace llarp::path
return path ;
}
Path_ptr
std: : shared_ptr < Path >
PathSet : : GetNewestPathByRouter ( RouterID id , PathRole roles ) const
{
Lock_t l ( m_PathsM utex) ;
Path_ptr chosen = nullptr ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
Lock_t l ( paths_m utex) ;
std: : shared_ptr < Path > chosen = nullptr ;
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > IsReady ( ) & & itr - > second - > SupportsAnyRoles ( roles ) )
{
@ -162,20 +162,20 @@ namespace llarp::path
return chosen ;
}
Path_ptr
std: : shared_ptr < Path >
PathSet : : GetPathByRouter ( RouterID id , PathRole roles ) const
{
Lock_t l ( m_PathsM utex) ;
Path_ptr chosen = nullptr ;
Lock_t l ( paths_m utex) ;
std: : shared_ptr < Path > chosen = nullptr ;
if ( roles = = ePathRoleAny )
{
if ( auto itr = m_PathC ache. find ( id ) ; itr ! = m_PathC ache. end ( ) )
if ( auto itr = path_c ache. find ( id ) ; itr ! = path_c ache. end ( ) )
{
return itr - > second . lock ( ) ;
}
}
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > IsReady ( ) & & itr - > second - > SupportsAnyRoles ( roles ) )
{
@ -195,13 +195,13 @@ namespace llarp::path
return chosen ;
}
Path_ptr
std: : shared_ptr < Path >
PathSet : : GetRandomPathByRouter ( RouterID id , PathRole roles ) const
{
Lock_t l ( m_PathsM utex) ;
std : : vector < Path_ptr > chosen ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
Lock_t l ( paths_m utex) ;
std : : vector < std: : shared_ptr < Path > > chosen ;
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > IsReady ( ) & & itr - > second - > SupportsAnyRoles ( roles ) )
{
@ -217,12 +217,12 @@ namespace llarp::path
return chosen [ std : : uniform_int_distribution < size_t > { 0 , chosen . size ( ) - 1 } ( llarp : : csrng ) ] ;
}
Path_ptr
std: : shared_ptr < Path >
PathSet : : GetByEndpointWithID ( RouterID ep , PathID_t id ) const
{
Lock_t l ( m_PathsM utex) ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
Lock_t l ( paths_m utex) ;
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > is_endpoint ( ep , id ) )
{
@ -233,12 +233,12 @@ namespace llarp::path
return nullptr ;
}
Path_ptr
std: : shared_ptr < Path >
PathSet : : GetPathByID ( PathID_t id ) const
{
Lock_t l ( m_PathsM utex) ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
Lock_t l ( paths_m utex) ;
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > RXID ( ) = = id )
return itr - > second ;
@ -250,12 +250,12 @@ namespace llarp::path
size_t
PathSet : : AvailablePaths ( PathRole roles ) const
{
Lock_t l ( m_PathsM utex) ;
Lock_t l ( paths_m utex) ;
size_t count = 0 ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > Status ( ) = = ESTABLISHED & & itr - > second - > SupportsAnyRoles ( roles ) )
if ( itr - > second - > Status ( ) = = PathStatus: : ESTABLISHED & & itr - > second - > SupportsAnyRoles ( roles ) )
+ + count ;
+ + itr ;
}
@ -265,10 +265,10 @@ namespace llarp::path
size_t
PathSet : : NumInStatus ( PathStatus st ) const
{
Lock_t l ( m_PathsM utex) ;
Lock_t l ( paths_m utex) ;
size_t count = 0 ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > Status ( ) = = st )
+ + count ;
@ -278,12 +278,12 @@ namespace llarp::path
}
void
PathSet : : AddPath ( Path_ptr path )
PathSet : : AddPath ( std: : shared_ptr < Path > path )
{
Lock_t l ( m_PathsM utex) ;
Lock_t l ( paths_m utex) ;
const auto upstream = path - > upstream ( ) ; // RouterID
const auto RXID = path - > RXID ( ) ; // PathID
if ( not m_P aths. emplace ( std : : make_pair ( upstream , RXID ) , path ) . second )
if ( not _p aths. emplace ( std : : make_pair ( upstream , RXID ) , path ) . second )
{
LogError (
Name ( ) ,
@ -294,12 +294,12 @@ namespace llarp::path
}
}
Path_ptr
std: : shared_ptr < Path >
PathSet : : GetByUpstream ( RouterID remote , PathID_t rxid ) const
{
Lock_t l ( m_PathsM utex) ;
auto itr = m_P aths. find ( { remote , rxid } ) ;
if ( itr = = m_P aths. end ( ) )
Lock_t l ( paths_m utex) ;
auto itr = _p aths. find ( { remote , rxid } ) ;
if ( itr = = _p aths. end ( ) )
return nullptr ;
return itr - > second ;
}
@ -309,9 +309,9 @@ namespace llarp::path
std : : function < bool ( const service : : Introduction & ) > filter ) const
{
std : : set < service : : Introduction > intros ;
Lock_t l { m_PathsM utex} ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
Lock_t l { paths_m utex} ;
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > IsReady ( ) and filter ( itr - > second - > intro ) )
{
@ -325,30 +325,30 @@ namespace llarp::path
}
void
PathSet : : HandlePathBuildTimeout ( Path_ptr p )
PathSet : : HandlePathBuildTimeout ( std: : shared_ptr < Path > p )
{
LogWarn ( Name ( ) , " path build " , p - > ShortN ame( ) , " timed out " ) ;
m_BuildS tats. timeouts + + ;
LogWarn ( Name ( ) , " path build " , p - > short_n ame( ) , " timed out " ) ;
build_s tats. timeouts + + ;
}
void
PathSet : : HandlePathBuildFailedAt ( Path_ptr p , RouterID hop )
PathSet : : HandlePathBuildFailedAt ( std: : shared_ptr < Path > p , RouterID hop )
{
LogWarn ( Name ( ) , " path build " , p - > ShortN ame( ) , " failed at " , hop ) ;
m_BuildS tats. fails + + ;
LogWarn ( Name ( ) , " path build " , p - > short_n ame( ) , " failed at " , hop ) ;
build_s tats. fails + + ;
}
void
PathSet : : HandlePathDied ( Path_ptr p )
PathSet : : HandlePathDied ( std: : shared_ptr < Path > p )
{
LogWarn ( Name ( ) , " path " , p - > ShortN ame( ) , " died " ) ;
LogWarn ( Name ( ) , " path " , p - > short_n ame( ) , " died " ) ;
}
void
PathSet : : PathBuildStarted ( Path_ptr p )
PathSet : : PathBuildStarted ( std: : shared_ptr < Path > p )
{
LogInfo ( Name ( ) , " path build " , p - > ShortN ame( ) , " started " ) ;
m_BuildS tats. attempts + + ;
LogInfo ( Name ( ) , " path build " , p - > short_n ame( ) , " started " ) ;
build_s tats. attempts + + ;
}
util : : StatusObject
@ -383,9 +383,9 @@ namespace llarp::path
{
intro . Clear ( ) ;
bool found = false ;
Lock_t l ( m_PathsM utex) ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
Lock_t l ( paths_m utex) ;
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > IsReady ( ) & & itr - > second - > intro . expiry > intro . expiry )
{
@ -397,13 +397,13 @@ namespace llarp::path
return found ;
}
Path_ptr
std: : shared_ptr < Path >
PathSet : : PickRandomEstablishedPath ( PathRole roles ) const
{
std : : vector < Path_ptr > established ;
Lock_t l ( m_PathsM utex) ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
std : : vector < std: : shared_ptr < Path > > established ;
Lock_t l ( paths_m utex) ;
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > IsReady ( ) & & itr - > second - > SupportsAnyRoles ( roles ) )
established . push_back ( itr - > second ) ;
@ -418,19 +418,19 @@ namespace llarp::path
return nullptr ;
}
Path_ptr
std: : shared_ptr < Path >
PathSet : : PickEstablishedPath ( PathRole roles ) const
{
std : : vector < Path_ptr > established ;
Lock_t l ( m_PathsM utex) ;
auto itr = m_P aths. begin ( ) ;
while ( itr ! = m_P aths. end ( ) )
std : : vector < std: : shared_ptr < Path > > established ;
Lock_t l ( paths_m utex) ;
auto itr = _p aths. begin ( ) ;
while ( itr ! = _p aths. end ( ) )
{
if ( itr - > second - > IsReady ( ) & & itr - > second - > SupportsAnyRoles ( roles ) )
established . push_back ( itr - > second ) ;
+ + itr ;
}
Path_ptr chosen = nullptr ;
std: : shared_ptr < Path > chosen = nullptr ;
llarp_time_t minLatency = 30 s ;
for ( const auto & path : established )
{