Struct MessageBuilder

Source
pub struct MessageBuilder { /* private fields */ }
Expand description

Allows building packets::Message structs in a more human-friendly way.

§Example

use rustydht_lib::common::Id;
use rustydht_lib::packets::MessageBuilder;

let client_id = Id::from_hex("0000000000000000000000000000000000000001").unwrap();
let server_id = Id::from_hex("0000000000000000000000000000000000000002").unwrap();

// To build a ping request
let ping_req = MessageBuilder::new_ping_request()
    .sender_id(client_id)
    .build()
    .unwrap();

// To build a ping response
let ping_res = MessageBuilder::new_ping_response()
    .sender_id(server_id)
    .transaction_id(ping_req.transaction_id.clone())
    .build()
    .unwrap();

Implementations§

Source§

impl MessageBuilder

Source

pub fn new_ping_request() -> MessageBuilder

Create a new MessageBuilder for a ping request

Source

pub fn new_ping_response() -> MessageBuilder

Create a new MessageBuilder for a ping response

Source

pub fn new_find_node_request() -> MessageBuilder

Create a new MessageBuilder for a find_node request

Source

pub fn new_find_node_response() -> MessageBuilder

Create a new MessageBuilder for a find_node response

Source

pub fn new_get_peers_request() -> MessageBuilder

Create a new MessageBuilder for a get_peers request

Source

pub fn new_get_peers_response() -> MessageBuilder

Create a new MessageBuilder for a get_peers response

Source

pub fn new_announce_peer_request() -> MessageBuilder

Create a new MessageBuilder for an announce_peer request

Source

pub fn new_announce_peer_response() -> MessageBuilder

Create a new MessageBuilder for an announce_peer response

Source

pub fn new_sample_infohashes_request() -> MessageBuilder

Create a new MessageBuilder for a sample_infohashes request

Source

pub fn new_sample_infohashes_response() -> MessageBuilder

Create a new MessageBuilder for a sample_infohashes response

Source

pub fn new_error() -> MessageBuilder

Create a new MessageBuilder for an error

Source

pub fn transaction_id(self, transaction_id: Vec<u8>) -> Self

Set the transaction id of the packet. If one is not specified, generated requests will get a random transaction id and responses will receive an error.

Source

pub fn version(self, version: Vec<u8>) -> Self

Set the string of bytes that should be included in the packet to identify the version of the software participating on the DHT.

If one is not specified, the builder will omit the version field from the generated packet (it is optional).

Source

pub fn requester_ip(self, remote: SocketAddr) -> Self

For response packets, set the IP address and port that we saw the request come from. This is used to help other nodes on the DHT know what their external IPv4 address is.

Has no effect for request packets. If not specified on response packets, the builder will omit it from the generated response packet.

Source

pub fn read_only(self, read_only: bool) -> Self

For request packets, specifies whether the read only flag should be set.

Has no effect on response packets.

Source

pub fn sender_id(self, sender_id: Id) -> Self

Set the Id of the DHT node sending the packet (whether it’s a request or response).

Source

pub fn target(self, target: Id) -> Self

Set the Id of the target node or info_hash (for get_peers, find_node, sample_infohashes, announce_peer)

Source

pub fn port(self, port: u16) -> Self

Set the port field for announce_peer requests.

If not specified, 0 will be used and implied_port will automatically be implicitly set to true (unless explicitly set to false, in which case an error will occur).

Source

pub fn implied_port(self, implied_port: bool) -> Self

Set the true/false value of implied port for announce_peer requests.

Source

pub fn token(self, token: Vec<u8>) -> Self

Set the token byte string. Used for announce_peer requests and get_peers responses.

Source

pub fn nodes(self, nodes: Vec<Node>) -> Self

Set the list of Nodes used in get_peers, find_node, and sample_infohashes responses.

nodes will be ignored for get_peers packets if peers are specified.

Source

pub fn peers(self, peers: Vec<SocketAddr>) -> Self

Set the list of peers used in get_peers responses.

nodes will be ignored for get_peers packets if peers are specified.

Source

pub fn interval(self, interval: Duration) -> Self

Set the interval used in sample_infohashes responses.

Source

pub fn samples(self, samples: Vec<Id>) -> Self

Set the list of info_hashes used in sample_infohashes responses.

Source

pub fn num_infohashes(self, num: usize) -> Self

Set the number of info_hashes as reported in sample_infohashes responses.

Source

pub fn code(self, code: i32) -> Self

Set the error code .for error messages.

Source

pub fn description(self, description: String) -> Self

Set the description for error messages.

Source

pub fn build(self) -> Result<Message, RustyDHTError>

Build the Message, consuming this MessageBuilder in the process

Trait Implementations§

Source§

impl Clone for MessageBuilder

Source§

fn clone(&self) -> MessageBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V