mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-17 15:25:35 +00:00
1a9f977208
- tons of surrounding stupid refactoring required
106 lines
2.5 KiB
C++
106 lines
2.5 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
|
|
{
|
|
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.IsExpired(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::extract_status() const
|
|
{
|
|
util::StatusObject obj{
|
|
{"nodes", _rc_nodes->ExtractStatus()},
|
|
{"services", _introset_nodes->ExtractStatus()},
|
|
{"local_key", _local_key.ToHex()}};
|
|
return obj;
|
|
}
|
|
|
|
bool
|
|
Contacts::lookup_router(
|
|
const RouterID& rid, std::function<void(const std::vector<RouterContact>&)> func)
|
|
{
|
|
dht::Key_t ask_peer;
|
|
|
|
if (not _rc_nodes->FindClosest(dht::Key_t{rid}, ask_peer))
|
|
return false;
|
|
|
|
_router.loop()->call([this, rid, func]() {
|
|
pending_lookups[rid] = [](const std::vector<RouterContact>&) {
|
|
// TODO: combine this concept with service::Endpoint introset lookups?
|
|
};
|
|
|
|
_router.send_control_message(
|
|
rid, "find_router", FindRouterMessage::serialize(rid, false, false, 0));
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
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
|