mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 13:27:17 +01:00
GetClosestFloodfills added
This commit is contained in:
parent
2741e94a72
commit
2a997d94bf
44
NetDb.cpp
44
NetDb.cpp
|
@ -922,6 +922,50 @@ namespace data
|
|||
return r;
|
||||
}
|
||||
|
||||
std::vector<IdentHash> NetDb::GetClosestFloodfills (const IdentHash& destination, size_t num) const
|
||||
{
|
||||
struct Sorted
|
||||
{
|
||||
std::shared_ptr<const RouterInfo> r;
|
||||
XORMetric metric;
|
||||
bool operator< (const Sorted& other) const { return metric < other.metric; };
|
||||
};
|
||||
|
||||
std::set<Sorted> sorted;
|
||||
IdentHash destKey = CreateRoutingKey (destination);
|
||||
{
|
||||
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
|
||||
for (auto it: m_Floodfills)
|
||||
{
|
||||
if (!it->IsUnreachable ())
|
||||
{
|
||||
XORMetric m = destKey ^ it->GetIdentHash ();
|
||||
if (sorted.size () < num)
|
||||
sorted.insert ({it, m});
|
||||
else if (m < sorted.rbegin ()->metric)
|
||||
{
|
||||
sorted.insert ({it, m});
|
||||
sorted.erase (std::prev (sorted.end ()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<IdentHash> res;
|
||||
size_t i = 0;
|
||||
for (auto it: sorted)
|
||||
{
|
||||
if (i < num)
|
||||
{
|
||||
res.push_back (it.r->GetIdentHash ());
|
||||
i++;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
std::shared_ptr<const RouterInfo> NetDb::GetClosestNonFloodfill (const IdentHash& destination,
|
||||
const std::set<IdentHash>& excluded) const
|
||||
{
|
||||
|
|
1
NetDb.h
1
NetDb.h
|
@ -51,6 +51,7 @@ namespace data
|
|||
std::shared_ptr<const RouterInfo> GetRandomPeerTestRouter () const;
|
||||
std::shared_ptr<const RouterInfo> GetRandomIntroducer () const;
|
||||
std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
|
||||
std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num) const;
|
||||
std::shared_ptr<const RouterInfo> GetClosestNonFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
|
||||
void SetUnreachable (const IdentHash& ident, bool unreachable);
|
||||
|
||||
|
|
Loading…
Reference in a new issue