You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/link/contacts.cpp

87 lines
2.1 KiB
C++

#include "contacts.hpp"
#include <llarp/messages/dht.hpp>
#include <llarp/router/router.hpp>
namespace llarp
{
Contacts::Contacts(const dht::Key_t& k, Router& r) : _local_key{k}, _router{r}
{
timer_keepalive = std::make_shared<int>(0);
_router.loop()->call_every(1s, timer_keepalive, [this]() { on_clean_contacts(); });
_rc_nodes = std::make_unique<dht::Bucket<dht::RCNode>>(_local_key, llarp::randint);
_introset_nodes = std::make_unique<dht::Bucket<dht::ISNode>>(_local_key, llarp::randint);
}
std::optional<service::EncryptedIntroSet>
Contacts::get_introset_by_location(const dht::Key_t& key) const
{
return _router.loop()->call_get([this, key]() -> std::optional<service::EncryptedIntroSet> {
auto& introsets = _introset_nodes->nodes;
if (auto itr = introsets.find(key); itr != introsets.end())
return itr->second.introset;
return std::nullopt;
});
}
void
Contacts::on_clean_contacts()
{
const auto now = llarp::time_now_ms();
if (_rc_nodes)
{
auto& nodes = _rc_nodes->nodes;
auto itr = nodes.begin();
while (itr != nodes.end())
{
if (itr->second.rc.is_expired(now))
itr = nodes.erase(itr);
else
++itr;
}
}
if (_introset_nodes)
{
auto& svcs = _introset_nodes->nodes;
auto itr = svcs.begin();
while (itr != svcs.end())
{
if (itr->second.introset.IsExpired(now))
itr = svcs.erase(itr);
else
++itr;
}
}
}
util::StatusObject
Contacts::ExtractStatus() const
{
util::StatusObject obj{
{"nodes", _rc_nodes->ExtractStatus()},
{"services", _introset_nodes->ExtractStatus()},
{"local_key", _local_key.ToHex()}};
return obj;
}
void
Contacts::put_rc_node_async(const dht::RCNode& val)
{
_router.loop()->call([this, val]() { _rc_nodes->PutNode(val); });
}
void
Contacts::delete_rc_node_async(const dht::Key_t& val)
{
_router.loop()->call([this, val]() { _rc_nodes->DelNode(val); });
}
} // namespace llarp