2018-07-11 13:20:14 +00:00
|
|
|
#ifndef LLARP_DHT_BUCKET_HPP
|
|
|
|
#define LLARP_DHT_BUCKET_HPP
|
|
|
|
|
|
|
|
#include <llarp/dht/kademlia.hpp>
|
|
|
|
#include <llarp/dht/key.hpp>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2018-08-04 02:59:32 +00:00
|
|
|
#include <vector>
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
template < typename Val_t >
|
|
|
|
struct Bucket
|
|
|
|
{
|
|
|
|
typedef std::map< Key_t, Val_t, XorMetric > BucketStorage_t;
|
|
|
|
|
|
|
|
Bucket(const Key_t& us) : nodes(XorMetric(us)){};
|
|
|
|
|
2018-09-09 12:27:56 +00:00
|
|
|
size_t
|
|
|
|
Size() const
|
|
|
|
{
|
|
|
|
return nodes.size();
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:59:32 +00:00
|
|
|
bool
|
2018-08-29 20:40:26 +00:00
|
|
|
GetRandomNodeExcluding(Key_t& result,
|
|
|
|
const std::set< Key_t >& exclude) const
|
2018-08-04 02:59:32 +00:00
|
|
|
{
|
|
|
|
std::vector< Key_t > candidates;
|
|
|
|
for(const auto& item : nodes)
|
|
|
|
{
|
|
|
|
if(exclude.find(item.first) == exclude.end())
|
|
|
|
candidates.push_back(item.first);
|
|
|
|
}
|
|
|
|
if(candidates.size() == 0)
|
|
|
|
return false;
|
|
|
|
result = candidates[llarp_randint() % candidates.size()];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:20:14 +00:00
|
|
|
bool
|
|
|
|
FindClosest(const Key_t& target, Key_t& result) const
|
|
|
|
{
|
|
|
|
Key_t mindist;
|
|
|
|
mindist.Fill(0xff);
|
|
|
|
for(const auto& item : nodes)
|
|
|
|
{
|
|
|
|
auto curDist = item.first ^ target;
|
|
|
|
if(curDist < mindist)
|
|
|
|
{
|
|
|
|
mindist = curDist;
|
|
|
|
result = item.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes.size() > 0;
|
|
|
|
}
|
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
bool
|
|
|
|
GetManyRandom(std::set< Key_t >& result, size_t N) const
|
|
|
|
{
|
|
|
|
if(nodes.size() < N)
|
|
|
|
return false;
|
|
|
|
if(nodes.size() == N)
|
|
|
|
{
|
|
|
|
for(const auto& node : nodes)
|
|
|
|
{
|
|
|
|
result.insert(node.first);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
size_t expecting = N;
|
|
|
|
size_t sz = nodes.size();
|
|
|
|
while(N)
|
|
|
|
{
|
|
|
|
auto itr = nodes.begin();
|
|
|
|
std::advance(itr, llarp_randint() % sz);
|
|
|
|
if(result.insert(itr->first).second)
|
|
|
|
--N;
|
|
|
|
}
|
|
|
|
return result.size() == expecting;
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:44:16 +00:00
|
|
|
bool
|
|
|
|
GetManyNearExcluding(const Key_t& target, std::set< Key_t >& result,
|
|
|
|
size_t N, const std::set< Key_t >& exclude) const
|
|
|
|
{
|
|
|
|
std::set< Key_t > s;
|
|
|
|
for(const auto& k : exclude)
|
|
|
|
s.insert(k);
|
|
|
|
Key_t peer;
|
|
|
|
while(N--)
|
|
|
|
{
|
|
|
|
if(!FindCloseExcluding(target, peer, s))
|
|
|
|
return false;
|
|
|
|
s.insert(peer);
|
|
|
|
result.insert(peer);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:20:14 +00:00
|
|
|
bool
|
|
|
|
FindCloseExcluding(const Key_t& target, Key_t& result,
|
2018-08-29 20:40:26 +00:00
|
|
|
const std::set< Key_t >& exclude) const
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
|
|
|
Key_t maxdist;
|
|
|
|
maxdist.Fill(0xff);
|
|
|
|
Key_t mindist;
|
|
|
|
mindist.Fill(0xff);
|
|
|
|
for(const auto& item : nodes)
|
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
if(exclude.count(item.first))
|
2018-07-11 13:20:14 +00:00
|
|
|
continue;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2018-07-11 13:20:14 +00:00
|
|
|
auto curDist = item.first ^ target;
|
|
|
|
if(curDist < mindist)
|
|
|
|
{
|
|
|
|
mindist = curDist;
|
|
|
|
result = item.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mindist < maxdist;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PutNode(const Val_t& val)
|
|
|
|
{
|
2018-09-21 14:50:07 +00:00
|
|
|
auto itr = nodes.find(val.ID);
|
|
|
|
if(itr == nodes.end() || itr->second < val)
|
|
|
|
nodes[val.ID] = val;
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelNode(const Key_t& key)
|
|
|
|
{
|
|
|
|
auto itr = nodes.find(key);
|
|
|
|
if(itr != nodes.end())
|
|
|
|
nodes.erase(itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
BucketStorage_t nodes;
|
|
|
|
};
|
2018-07-17 04:37:50 +00:00
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|
2018-08-27 13:44:16 +00:00
|
|
|
#endif
|