My Project 3.2.0
C++ Distributed Hash Table
Loading...
Searching...
No Matches
Public Member Functions | List of all members
dht::Dht Class Referencefinal

#include <dht.h>

Inheritance diagram for dht::Dht:
Inheritance graph
[legend]
Collaboration diagram for dht::Dht:
Collaboration graph
[legend]

Public Member Functions

 Dht (std::unique_ptr< net::DatagramSocket > &&sock, const Config &config, const Sp< Logger > &l={}, std::unique_ptr< std::mt19937_64 > &&rd={})
 
const InfoHashgetNodeId () const override
 
void setOnPublicAddressChanged (PublicAddressChangedCb cb) override
 
NodeStatus updateStatus (sa_family_t af) override
 
NodeStatus getStatus (sa_family_t af) const override
 
NodeStatus getStatus () const override
 
net::DatagramSocketgetSocket () const override
 
void shutdown (ShutdownCallback cb, bool stop=false) override
 
bool isRunning (sa_family_t af=0) const override
 
virtual void registerType (const ValueType &type) override
 
const ValueTypegetType (ValueType::Id type_id) const override
 
void addBootstrap (const std::string &host, const std::string &service) override
 
void clearBootstrap () override
 
void insertNode (const InfoHash &id, const SockAddr &) override
 
void insertNode (const NodeExport &n) override
 
void pingNode (SockAddr, DoneCallbackSimple &&cb={}) override
 
time_point periodic (const uint8_t *buf, size_t buflen, SockAddr, const time_point &now) override
 
time_point periodic (const uint8_t *buf, size_t buflen, const sockaddr *from, socklen_t fromlen, const time_point &now) override
 
virtual void get (const InfoHash &key, GetCallback cb, DoneCallback donecb={}, Value::Filter &&f={}, Where &&w={}) override
 
virtual void get (const InfoHash &key, GetCallback cb, DoneCallbackSimple donecb={}, Value::Filter &&f={}, Where &&w={}) override
 
virtual void get (const InfoHash &key, GetCallbackSimple cb, DoneCallback donecb={}, Value::Filter &&f={}, Where &&w={}) override
 
virtual void get (const InfoHash &key, GetCallbackSimple cb, DoneCallbackSimple donecb, Value::Filter &&f={}, Where &&w={}) override
 
virtual void query (const InfoHash &key, QueryCallback cb, DoneCallback done_cb={}, Query &&q={}) override
 
virtual void query (const InfoHash &key, QueryCallback cb, DoneCallbackSimple done_cb={}, Query &&q={}) override
 
std::vector< Sp< Value > > getLocal (const InfoHash &key, const Value::Filter &f={}) const override
 
Sp< ValuegetLocalById (const InfoHash &key, Value::Id vid) const override
 
void put (const InfoHash &key, Sp< Value >, DoneCallback cb=nullptr, time_point created=time_point::max(), bool permanent=false) override
 
void put (const InfoHash &key, const Sp< Value > &v, DoneCallbackSimple cb, time_point created=time_point::max(), bool permanent=false) override
 
void put (const InfoHash &key, Value &&v, DoneCallback cb=nullptr, time_point created=time_point::max(), bool permanent=false) override
 
void put (const InfoHash &key, Value &&v, DoneCallbackSimple cb, time_point created=time_point::max(), bool permanent=false) override
 
std::vector< Sp< Value > > getPut (const InfoHash &) const override
 
Sp< ValuegetPut (const InfoHash &, const Value::Id &) const override
 
bool cancelPut (const InfoHash &, const Value::Id &) override
 
size_t listen (const InfoHash &, ValueCallback, Value::Filter={}, Where={}) override
 
size_t listen (const InfoHash &key, GetCallback cb, Value::Filter f={}, Where w={}) override
 
size_t listen (const InfoHash &key, GetCallbackSimple cb, Value::Filter f={}, Where w={}) override
 
bool cancelListen (const InfoHash &, size_t token) override
 
void connectivityChanged (sa_family_t) override
 
void connectivityChanged () override
 
std::vector< NodeExportexportNodes () const override
 
std::vector< ValuesExport > exportValues () const override
 
void importValues (const std::vector< ValuesExport > &) override
 
void saveState (const std::string &path) const
 
void loadState (const std::string &path)
 
NodeStats getNodesStats (sa_family_t af) const override
 
std::string getStorageLog () const override
 
std::string getStorageLog (const InfoHash &) const override
 
std::string getRoutingTablesLog (sa_family_t) const override
 
std::string getSearchesLog (sa_family_t) const override
 
std::string getSearchLog (const InfoHash &, sa_family_t af=AF_UNSPEC) const override
 
void dumpTables () const override
 
std::vector< unsigned > getNodeMessageStats (bool in=false) override
 
void setStorageLimit (size_t limit=DEFAULT_STORAGE_LIMIT) override
 
size_t getStorageLimit () const override
 
std::pair< size_t, size_t > getStoreSize () const override
 
std::vector< SockAddrgetPublicAddress (sa_family_t family=0) override
 
void pushNotificationReceived (const std::map< std::string, std::string > &) override
 
void resubscribe (unsigned)
 
- Public Member Functions inherited from dht::DhtInterface
 DhtInterface (const Logger &l)
 
 DhtInterface (const std::shared_ptr< Logger > &l)
 
void addOnConnectedCallback (std::function< void()> cb)
 
virtual void setLogger (const Logger &l)
 
virtual void setLogger (const std::shared_ptr< Logger > &l)
 
virtual void setLogFilter (const InfoHash &f)
 
virtual void setPushNotificationToken (const std::string &)
 
virtual void setPushNotificationTopic (const std::string &)
 
virtual void setPushNotificationPlatform (const std::string &)
 

Additional Inherited Members

- Public Types inherited from dht::DhtInterface
using Status = NodeStatus
 
using NodeExport = dht::NodeExport
 
- Protected Attributes inherited from dht::DhtInterface
std::shared_ptr< Loggerlogger_ {}
 
std::queue< std::function< void()> > onConnectCallbacks_ {}
 

Detailed Description

Main Dht class. Provides a Distributed Hash Table node.

Must be given open UDP sockets and periodic must be called regularly.

Definition at line 62 of file dht.h.

Constructor & Destructor Documentation

◆ Dht()

dht::Dht::Dht ( std::unique_ptr< net::DatagramSocket > && sock,
const Config & config,
const Sp< Logger > & l = {},
std::unique_ptr< std::mt19937_64 > && rd = {} )

Initialise the Dht with two open sockets (for IPv4 and IP6) and an ID for the node.

Member Function Documentation

◆ addBootstrap()

void dht::Dht::addBootstrap ( const std::string & host,
const std::string & service )
inlineoverridevirtual

Reimplemented from dht::DhtInterface.

Definition at line 115 of file dht.h.

◆ cancelListen()

bool dht::Dht::cancelListen ( const InfoHash & ,
size_t token )
overridevirtual

Implements dht::DhtInterface.

◆ cancelPut()

bool dht::Dht::cancelPut ( const InfoHash & ,
const Value::Id &  )
overridevirtual

Stop any put/announce operation at the given location, for the value with the given id.

Implements dht::DhtInterface.

◆ clearBootstrap()

void dht::Dht::clearBootstrap ( )
inlineoverridevirtual

Reimplemented from dht::DhtInterface.

Definition at line 120 of file dht.h.

◆ connectivityChanged() [1/2]

void dht::Dht::connectivityChanged ( )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 267 of file dht.h.

◆ connectivityChanged() [2/2]

void dht::Dht::connectivityChanged ( sa_family_t )
overridevirtual

Inform the DHT of lower-layer connectivity changes. This will cause the DHT to assume a public IP address change. The DHT will recontact neighbor nodes, re-register for listen ops etc.

Implements dht::DhtInterface.

◆ dumpTables()

void dht::Dht::dumpTables ( ) const
overridevirtual

Implements dht::DhtInterface.

◆ exportNodes()

std::vector< NodeExport > dht::Dht::exportNodes ( ) const
overridevirtual

Get the list of good nodes for local storage saving purposes The list is ordered to minimize the back-to-work delay.

Implements dht::DhtInterface.

◆ exportValues()

std::vector< ValuesExport > dht::Dht::exportValues ( ) const
overridevirtual

Implements dht::DhtInterface.

◆ get() [1/4]

virtual void dht::Dht::get ( const InfoHash & key,
GetCallback cb,
DoneCallback donecb = {},
Value::Filter && f = {},
Where && w = {} )
overridevirtual

Get a value by searching on all available protocols (IPv4, IPv6), and call the provided get callback when values are found at key. The operation will start as soon as the node is connected to the network.

Parameters
cba function called when new values are found on the network. It should return false to stop the operation.
donecba function called when the operation is complete. cb and donecb won't be called again afterward.
fa filter function used to prefilter values.

Implements dht::DhtInterface.

◆ get() [2/4]

virtual void dht::Dht::get ( const InfoHash & key,
GetCallback cb,
DoneCallbackSimple donecb = {},
Value::Filter && f = {},
Where && w = {} )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 152 of file dht.h.

◆ get() [3/4]

virtual void dht::Dht::get ( const InfoHash & key,
GetCallbackSimple cb,
DoneCallback donecb = {},
Value::Filter && f = {},
Where && w = {} )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 155 of file dht.h.

◆ get() [4/4]

virtual void dht::Dht::get ( const InfoHash & key,
GetCallbackSimple cb,
DoneCallbackSimple donecb,
Value::Filter && f = {},
Where && w = {} )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 158 of file dht.h.

◆ getLocal()

std::vector< Sp< Value > > dht::Dht::getLocal ( const InfoHash & key,
const Value::Filter & f = {} ) const
overridevirtual

Get locally stored data for the given hash.

Implements dht::DhtInterface.

◆ getLocalById()

Sp< Value > dht::Dht::getLocalById ( const InfoHash & key,
Value::Id vid ) const
overridevirtual

Get locally stored data for the given key and value id.

Implements dht::DhtInterface.

◆ getNodeId()

const InfoHash & dht::Dht::getNodeId ( ) const
inlineoverridevirtual

Get the ID of the node.

Implements dht::DhtInterface.

Definition at line 75 of file dht.h.

◆ getNodeMessageStats()

std::vector< unsigned > dht::Dht::getNodeMessageStats ( bool in = false)
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 294 of file dht.h.

◆ getNodesStats()

NodeStats dht::Dht::getNodesStats ( sa_family_t af) const
overridevirtual

Implements dht::DhtInterface.

◆ getPublicAddress()

std::vector< SockAddr > dht::Dht::getPublicAddress ( sa_family_t family = 0)
overridevirtual

Implements dht::DhtInterface.

◆ getPut() [1/2]

std::vector< Sp< Value > > dht::Dht::getPut ( const InfoHash & ) const
overridevirtual

Get data currently being put at the given hash.

Implements dht::DhtInterface.

◆ getPut() [2/2]

Sp< Value > dht::Dht::getPut ( const InfoHash & ,
const Value::Id &  ) const
overridevirtual

Get data currently being put at the given hash with the given id.

Implements dht::DhtInterface.

◆ getRoutingTablesLog()

std::string dht::Dht::getRoutingTablesLog ( sa_family_t ) const
overridevirtual

Implements dht::DhtInterface.

◆ getSearchesLog()

std::string dht::Dht::getSearchesLog ( sa_family_t ) const
overridevirtual

Implements dht::DhtInterface.

◆ getSearchLog()

std::string dht::Dht::getSearchLog ( const InfoHash & ,
sa_family_t af = AF_UNSPEC ) const
overridevirtual

Implements dht::DhtInterface.

◆ getSocket()

net::DatagramSocket * dht::Dht::getSocket ( ) const
inlineoverridevirtual

Reimplemented from dht::DhtInterface.

Definition at line 93 of file dht.h.

◆ getStatus() [1/2]

NodeStatus dht::Dht::getStatus ( ) const
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 89 of file dht.h.

◆ getStatus() [2/2]

NodeStatus dht::Dht::getStatus ( sa_family_t af) const
inlineoverridevirtual

Get the current status of the node for the given family.

Implements dht::DhtInterface.

Definition at line 85 of file dht.h.

◆ getStorageLimit()

size_t dht::Dht::getStorageLimit ( ) const
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 304 of file dht.h.

◆ getStorageLog() [1/2]

std::string dht::Dht::getStorageLog ( ) const
overridevirtual

Implements dht::DhtInterface.

◆ getStorageLog() [2/2]

std::string dht::Dht::getStorageLog ( const InfoHash & ) const
overridevirtual

Implements dht::DhtInterface.

◆ getStoreSize()

std::pair< size_t, size_t > dht::Dht::getStoreSize ( ) const
inlineoverridevirtual

Returns the total memory usage of stored values and the number of stored values.

Implements dht::DhtInterface.

Definition at line 312 of file dht.h.

◆ getType()

const ValueType & dht::Dht::getType ( ValueType::Id type_id) const
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 111 of file dht.h.

◆ importValues()

void dht::Dht::importValues ( const std::vector< ValuesExport > & )
overridevirtual

Implements dht::DhtInterface.

◆ insertNode() [1/2]

void dht::Dht::insertNode ( const InfoHash & id,
const SockAddr &  )
overridevirtual

Insert a node in the main routing table. The node is not pinged, so this should be used to bootstrap efficiently from previously known nodes.

Implements dht::DhtInterface.

◆ insertNode() [2/2]

void dht::Dht::insertNode ( const NodeExport & n)
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 130 of file dht.h.

◆ isRunning()

bool dht::Dht::isRunning ( sa_family_t af = 0) const
overridevirtual

Returns true if the node is running (have access to an open socket).

af: address family. If non-zero, will return true if the node is running for the provided family.

Implements dht::DhtInterface.

◆ listen() [1/3]

size_t dht::Dht::listen ( const InfoHash & ,
ValueCallback ,
Value::Filter = {},
Where = {} )
overridevirtual

Listen on the network for any changes involving a specified hash. The node will register to receive updates from relevent nodes when new values are added or removed.

Returns
a token to cancel the listener later.

Implements dht::DhtInterface.

◆ listen() [2/3]

size_t dht::Dht::listen ( const InfoHash & ,
GetCallback ,
Value::Filter = {},
Where w = {} )
inlineoverridevirtual

Listen on the network for any changes involving a specified hash. The node will register to receive updates from relevent nodes when new values are added or removed.

Returns
a token to cancel the listener later.

Implements dht::DhtInterface.

Definition at line 248 of file dht.h.

◆ listen() [3/3]

size_t dht::Dht::listen ( const InfoHash & key,
GetCallbackSimple cb,
Value::Filter f = {},
Where w = {} )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 255 of file dht.h.

◆ periodic() [1/2]

time_point dht::Dht::periodic ( const uint8_t * buf,
size_t buflen,
const sockaddr * from,
socklen_t fromlen,
const time_point & now )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 137 of file dht.h.

◆ periodic() [2/2]

time_point dht::Dht::periodic ( const uint8_t * buf,
size_t buflen,
SockAddr ,
const time_point & now )
overridevirtual

Implements dht::DhtInterface.

◆ pingNode()

void dht::Dht::pingNode ( SockAddr ,
DoneCallbackSimple && cb = {} )
overridevirtual

Implements dht::DhtInterface.

◆ pushNotificationReceived()

void dht::Dht::pushNotificationReceived ( const std::map< std::string, std::string > & data)
inlineoverridevirtual

Call linked callback with a push notification

Parameters
notificationto process

Implements dht::DhtInterface.

Definition at line 318 of file dht.h.

◆ put() [1/4]

void dht::Dht::put ( const InfoHash & key,
const Sp< Value > & v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 197 of file dht.h.

◆ put() [2/4]

void dht::Dht::put ( const InfoHash & key,
Sp< Value > ,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false )
overridevirtual

Announce a value on all available protocols (IPv4, IPv6).

The operation will start as soon as the node is connected to the network. The done callback will be called once, when the first announce succeeds, or fails.

Implements dht::DhtInterface.

◆ put() [3/4]

void dht::Dht::put ( const InfoHash & key,
Value && v,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 206 of file dht.h.

◆ put() [4/4]

void dht::Dht::put ( const InfoHash & key,
Value && v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 214 of file dht.h.

◆ query() [1/2]

virtual void dht::Dht::query ( const InfoHash & key,
QueryCallback cb,
DoneCallback done_cb = {},
Query && q = {} )
overridevirtual

Similar to Dht::get, but sends a Query to filter data remotely.

Parameters
keythe key for which to query data for.
cba function called when new values are found on the network. It should return false to stop the operation.
done_cba function called when the operation is complete. cb and done_cb won't be called again afterward.
qa query used to filter values on the remotes before they send a response.

Implements dht::DhtInterface.

◆ query() [2/2]

virtual void dht::Dht::query ( const InfoHash & key,
QueryCallback cb,
DoneCallbackSimple done_cb = {},
Query && q = {} )
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 172 of file dht.h.

◆ registerType()

virtual void dht::Dht::registerType ( const ValueType & type)
inlineoverridevirtual

Implements dht::DhtInterface.

Definition at line 108 of file dht.h.

◆ resubscribe()

void dht::Dht::resubscribe ( unsigned )
inline

Definition at line 319 of file dht.h.

◆ setOnPublicAddressChanged()

void dht::Dht::setOnPublicAddressChanged ( PublicAddressChangedCb cb)
inlineoverridevirtual

Reimplemented from dht::DhtInterface.

Definition at line 76 of file dht.h.

◆ setStorageLimit()

void dht::Dht::setStorageLimit ( size_t limit = DEFAULT_STORAGE_LIMIT)
inlineoverridevirtual

Set the in-memory storage limit in bytes

Implements dht::DhtInterface.

Definition at line 301 of file dht.h.

◆ shutdown()

void dht::Dht::shutdown ( ShutdownCallback cb,
bool stop = false )
overridevirtual

Performs final operations before quitting.

Implements dht::DhtInterface.

◆ updateStatus()

NodeStatus dht::Dht::updateStatus ( sa_family_t af)
overridevirtual

Get the current status of the node for the given family.

Reimplemented from dht::DhtInterface.


The documentation for this class was generated from the following file: