2021-03-19 14:09:06 +00:00
|
|
|
#include "lns_tracker.hpp"
|
|
|
|
|
|
|
|
namespace llarp::service
|
|
|
|
{
|
2021-03-20 15:47:36 +00:00
|
|
|
std::function<void(std::optional<LNSLookupTracker::Addr_t>)>
|
2021-03-19 14:09:06 +00:00
|
|
|
LNSLookupTracker::MakeResultHandler(
|
|
|
|
std::string name,
|
|
|
|
std::size_t numPeers,
|
2021-03-20 15:47:36 +00:00
|
|
|
std::function<void(std::optional<Addr_t>)> resultHandler)
|
2021-03-19 14:09:06 +00:00
|
|
|
{
|
2021-03-19 14:18:36 +00:00
|
|
|
m_PendingLookups.emplace(name, LookupInfo{numPeers, resultHandler});
|
2021-03-20 15:47:36 +00:00
|
|
|
return [name, this](std::optional<Addr_t> found) {
|
2021-03-19 14:18:36 +00:00
|
|
|
auto itr = m_PendingLookups.find(name);
|
2021-03-19 20:13:09 +00:00
|
|
|
if (itr == m_PendingLookups.end())
|
|
|
|
return;
|
|
|
|
itr->second.HandleOneResult(found);
|
|
|
|
if (itr->second.IsDone())
|
2021-03-19 14:18:36 +00:00
|
|
|
m_PendingLookups.erase(itr);
|
2021-03-19 14:09:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-03-19 20:13:09 +00:00
|
|
|
LNSLookupTracker::LookupInfo::IsDone() const
|
|
|
|
{
|
|
|
|
return m_ResultsGotten == m_ResultsNeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-20 15:47:36 +00:00
|
|
|
LNSLookupTracker::LookupInfo::HandleOneResult(std::optional<Addr_t> result)
|
2021-03-19 14:09:06 +00:00
|
|
|
{
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
m_CurrentValues.insert(*result);
|
|
|
|
}
|
|
|
|
m_ResultsGotten++;
|
2021-03-19 20:13:09 +00:00
|
|
|
if (IsDone())
|
2021-03-19 14:09:06 +00:00
|
|
|
{
|
|
|
|
if (m_CurrentValues.size() == 1)
|
|
|
|
{
|
|
|
|
m_HandleResult(*m_CurrentValues.begin());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_HandleResult(std::nullopt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace llarp::service
|