mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 13:27:17 +01:00
65 lines
1.2 KiB
C
65 lines
1.2 KiB
C
|
/*
|
||
|
* Copyright (c) 2023, The PurpleI2P Project
|
||
|
*
|
||
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||
|
*
|
||
|
* See full license text in LICENSE file at top of project tree
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#ifndef KADDHT_H__
|
||
|
#define KADDHT_H__
|
||
|
|
||
|
#include <memory>
|
||
|
#include <sstream>
|
||
|
#include "Identity.h"
|
||
|
|
||
|
// Kademlia DHT (XOR distance)
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace data
|
||
|
{
|
||
|
struct DHTNode
|
||
|
{
|
||
|
DHTNode * zero, * one;
|
||
|
IdentHash * hash;
|
||
|
|
||
|
DHTNode ();
|
||
|
~DHTNode ();
|
||
|
|
||
|
bool IsEmpty () const { return !zero && !one && !hash; };
|
||
|
void MoveHashUp (bool fromOne);
|
||
|
};
|
||
|
|
||
|
class DHTTable
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
DHTTable ();
|
||
|
~DHTTable ();
|
||
|
|
||
|
DHTNode * Insert (const IdentHash& h);
|
||
|
bool Remove (const IdentHash& h);
|
||
|
IdentHash * FindClosest (const IdentHash& h);
|
||
|
|
||
|
void Print (std::stringstream& s);
|
||
|
size_t GetSize () const { return m_Size; };
|
||
|
|
||
|
private:
|
||
|
|
||
|
DHTNode * Insert (IdentHash * h, DHTNode * root, int level); // recursive
|
||
|
bool Remove (const IdentHash& h, DHTNode * root, int level);
|
||
|
IdentHash * FindClosest (const IdentHash& h, DHTNode * root, int level);
|
||
|
void Print (std::stringstream& s, DHTNode * root, int level);
|
||
|
|
||
|
private:
|
||
|
|
||
|
DHTNode * m_Root;
|
||
|
size_t m_Size;
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|