pub struct DHT { /* private fields */ }
Expand description
This struct is the heart of the library - contains data structure and business logic to run a DHT node.
Implementations§
Source§impl DHT
impl DHT
Sourcepub fn get_info_hashes(
&self,
newer_than: Option<Instant>,
) -> Vec<(Id, Vec<PeerInfo>)>
pub fn get_info_hashes( &self, newer_than: Option<Instant>, ) -> Vec<(Id, Vec<PeerInfo>)>
Returns a full dump of all the info hashes and peers in storage.
Peers that haven’t announced since the provided newer_than
can be optionally filtered.
Sourcepub fn get_nodes(&self) -> Vec<NodeWrapper>
pub fn get_nodes(&self) -> Vec<NodeWrapper>
Returns information about all currently-verified DHT nodes that we’re “connected” with.
Sourcepub fn get_settings(&self) -> DHTSettings
pub fn get_settings(&self) -> DHTSettings
Return a copy of the settings used by the DHT
Sourcepub fn new(
shutdown: ShutdownReceiver,
id: Option<Id>,
socket_addr: SocketAddr,
ip4_source: Box<dyn IPV4AddrSource + Send>,
buckets: Box<dyn NodeStorage + Send>,
settings: DHTSettings,
) -> Result<DHT, RustyDHTError>
pub fn new( shutdown: ShutdownReceiver, id: Option<Id>, socket_addr: SocketAddr, ip4_source: Box<dyn IPV4AddrSource + Send>, buckets: Box<dyn NodeStorage + Send>, settings: DHTSettings, ) -> Result<DHT, RustyDHTError>
Creates a new DHT.
§Arguments
shutdown
- the DHT passes this to any sub-tasks that it spawns, and uses it to know when to stop its event own event loop.id
- an optional initial Id for the DHT. The DHT may change its Id if at some point its not valid for the external IPv4 address (as reported by ip4_source).listen_port
- the port that the DHT should bind its UDP socket on.ip4_source
- Some type that implements IPV4AddrSource. This object will be used by the DHT to keep up to date on its IPv4 address.buckets
- A function that takes an Id and returns a struct implementing NodeStorage. The NodeStorage-implementing type will be used to keep the nodes (or routing table) of the DHT.routers
- Array of string slices with hostname:port of DHT routers. These help us get bootstrapped onto the network.settings
- DHTSettings struct containing settings that DHT will use.
Sourcepub async fn run_event_loop(&self) -> Result<(), RustyDHTError>
pub async fn run_event_loop(&self) -> Result<(), RustyDHTError>
Runs the main event loop of the DHT.
It will only return if there’s an error or if the DHT’s ShutdownReceiver is signalled to stop the DHT.
Sourcepub async fn send_request(
&self,
req: Message,
dest: SocketAddr,
dest_id: Option<Id>,
timeout: Option<Duration>,
) -> Result<Message, RustyDHTError>
pub async fn send_request( &self, req: Message, dest: SocketAddr, dest_id: Option<Id>, timeout: Option<Duration>, ) -> Result<Message, RustyDHTError>
Sends a Message, awaits and returns a response.
Note that req
must be a request message (not a response or error message),
as this method awaits a reply. DHT automatically handles sending responses for
incoming requests.
§Arguments
req
- the message that should be sentdest
- the IP/port of the intended recipientdest_id
- the Id of the DHT node listening atdest
, if known. Otherwise,None
can be provided.timeout
- An optional timeout. If supplied, this function will return a RustyDHTError::TimeoutError ifdest
does not reply to the message within the allotted time.