Skip to main content

Builder

Struct Builder 

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

Builder for creating a P2P endpoint with custom configuration.

The Builder uses the builder pattern to provide a fluent API for configuring all aspects of a P2P endpoint before creating it.

§Examples

§Basic Configuration

use rustp2p::Builder;
use std::net::Ipv4Addr;

let endpoint = Builder::new()
    .node_id(Ipv4Addr::new(10, 0, 0, 1).into())
    .udp_port(8080)
    .build()
    .await?;

§Full Configuration

use rustp2p::{Builder, PeerNodeAddress, GroupCode};
use rustp2p::cipher::Algorithm;
use std::net::Ipv4Addr;
use std::str::FromStr;

let endpoint = Builder::new()
    .node_id(Ipv4Addr::new(10, 0, 0, 1).into())
    .udp_port(8080)
    .tcp_port(8080)
    .peers(vec![
        PeerNodeAddress::from_str("udp://192.168.1.100:9090").unwrap()
    ])
    .group_code(GroupCode::try_from("12345").unwrap())
    .encryption(Algorithm::AesGcm("password".to_string()))
    .build()
    .await?;

Implementations§

Source§

impl Builder

Source

pub fn new() -> Self

Creates a new builder with default settings.

§Examples
use rustp2p::Builder;

let builder = Builder::new();
Source

pub fn from_config(config: Config) -> Self

Creates a builder from an existing configuration.

§Arguments
  • config - A pre-configured Config object
§Examples
use rustp2p::{Builder, Config};

let config = Config::empty();
let builder = Builder::from_config(config);
Source

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

Sets the UDP port for this endpoint.

§Arguments
  • port - The port number to bind to (0 for random)
§Examples
use rustp2p::Builder;

let builder = Builder::new().udp_port(8080);
Source

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

Sets the TCP port for this endpoint.

§Arguments
  • port - The port number to bind to (0 for random)
§Examples
use rustp2p::Builder;

let builder = Builder::new().tcp_port(8080);
Source

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

Sets the initial list of peers to connect to.

These peers will be contacted when the endpoint starts to help with peer discovery and NAT traversal.

§Arguments
  • peers - A vector of peer addresses
§Examples
use rustp2p::{Builder, PeerNodeAddress};
use std::str::FromStr;

let builder = Builder::new()
    .peers(vec![
        PeerNodeAddress::from_str("udp://192.168.1.100:9090").unwrap(),
        PeerNodeAddress::from_str("tcp://10.0.0.1:8080").unwrap(),
    ]);
Source

pub fn group_code(self, group_code: GroupCode) -> Self

Sets the group code for this endpoint.

Endpoints with different group codes will not be able to communicate. This allows you to create isolated P2P networks.

§Arguments
  • group_code - A unique group identifier
§Examples
use rustp2p::Builder;
use rustp2p::GroupCode;

let builder = Builder::new()
    .group_code(GroupCode::try_from("12345").unwrap());
Source

pub fn encryption(self, encryption: Algorithm) -> Self

Sets the encryption algorithm for this endpoint.

When encryption is enabled, all communication will be encrypted with the specified algorithm and password. All peers must use the same encryption settings to communicate.

§Arguments
  • encryption - The encryption algorithm and password
§Examples
use rustp2p::{Builder, cipher::Algorithm};

let builder = Builder::new()
    .encryption(Algorithm::AesGcm("my-secret-password".to_string()));
Source

pub fn node_id(self, node_id: NodeID) -> Self

Sets the node ID for this endpoint (required).

Each peer in the network must have a unique node ID. The node ID is used to identify and route messages to the correct destination.

§Arguments
  • node_id - A unique identifier for this node
§Examples
use rustp2p::Builder;
use std::net::Ipv4Addr;

let builder = Builder::new()
    .node_id(Ipv4Addr::new(10, 0, 0, 1).into());
Source

pub fn interceptor<I: DataInterceptor + 'static>(self, interceptor: I) -> Self

Sets a custom data interceptor.

The interceptor can inspect and modify data before it is processed.

§Arguments
  • interceptor - An implementation of the DataInterceptor trait
Source

pub fn punching_policy<I: PunchingPolicy + 'static>( self, punching_policy: I, ) -> Self

Sets a custom punching policy.

The punching policy determines how NAT traversal is attempted.

§Arguments
  • punching_policy - An implementation of the PunchingPolicy trait
Source

pub fn load_balance(self, load_balance: LoadBalance) -> Self

Sets the load balancing strategy.

Determines how traffic is distributed across multiple available routes.

§Arguments
  • load_balance - The load balancing strategy
Source

pub fn bind_interface(self, interface: LocalInterface) -> Self

Binds to a specific network interface.

§Arguments
  • interface - The network interface to bind to
Source

pub async fn build(self) -> Result<EndPoint>

Builds and returns the configured endpoint.

This method consumes the builder and creates an EndPoint with all the specified configuration options.

§Errors

Returns an error if:

  • The node ID was not set
  • Unable to bind to the specified ports
  • Other configuration errors
§Examples
use rustp2p::Builder;
use std::net::Ipv4Addr;

let endpoint = Builder::new()
    .node_id(Ipv4Addr::new(10, 0, 0, 1).into())
    .udp_port(8080)
    .build()
    .await?;

Trait Implementations§

Source§

impl Default for Builder

Source§

fn default() -> Self

Returns the “default value” for a type. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> FromRef<T> for U
where T: Clone, U: From<T>,

Source§

fn from_ref(val_ref: &T) -> U

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, 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