Struct NetworkBuilder

Source
pub struct NetworkBuilder<T, I>(/* private fields */);
Expand description

Convenient network layer builder

Implementations§

Source§

impl NetworkBuilder<HNil, (Here, Here)>

Source

pub fn with_adnl<T>( addr: T, keystore: Keystore, options: NodeOptions, ) -> NetworkBuilder<HCons<Result<Arc<Node>>, HNil>, (Here, Here)>
where T: ToSocketAddrs,

Creates a basic network layer that is an ADNL node

See with_adnl_ext if you need a node with a peer filter

§Examples
#[tokio::main]
async fn main() -> Result<()> {
    let keystore = adnl::Keystore::builder()
        .with_tagged_key([0; 32], 0)?
        .build();

    let options = adnl::NodeOptions::default();

    let adnl = NetworkBuilder::with_adnl("127.0.0.1:10000", keystore, options).build()?;

    Ok(())
}
Source

pub fn with_adnl_ext<T>( addr: T, keystore: Keystore, options: NodeOptions, peer_filter: Arc<dyn PeerFilter>, ) -> NetworkBuilder<HCons<Result<Arc<Node>>, HNil>, (Here, Here)>
where T: ToSocketAddrs,

Creates a basic network layer that is an ADNL node with additional filter

§Examples
struct MyFilter;

impl adnl::PeerFilter for MyFilter {
    fn check(
        &self,
        ctx: adnl::NewPeerContext,
        addr: SocketAddrV4,
        peer_id: &adnl::NodeIdShort,
    ) -> bool {
        // Allow only non-loopback IPs
        !addr.ip().is_loopback()
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let keystore = adnl::Keystore::builder()
        .with_tagged_key([0; 32], 0)?
        .build();

    let options = adnl::NodeOptions::default();

    let peer_filter = Arc::new(MyFilter);

    let adnl = NetworkBuilder::with_adnl_ext("127.0.0.1:10000", keystore, options, peer_filter)
        .build()?;

    Ok(())
}
Source§

impl<L, A, R> NetworkBuilder<L, (A, R)>
where L: HList + Selector<Result<Arc<Node>>, A>,

Source

pub fn with_query_subscriber(self, subscriber: Arc<dyn QuerySubscriber>) -> Self

Adds query subscriber if ADNL was successfully initialized.

§Examples
struct Service;

#[async_trait::async_trait]
impl QuerySubscriber for Service {
    async fn try_consume_query<'a>(
        &self,
        _ctx: SubscriberContext<'a>,
        _constructor: u32,
        _query: Cow<'a, [u8]>,
    ) -> Result<QueryConsumingResult<'a>> {
        Ok(QueryConsumingResult::Consumed(None))
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let keystore = adnl::Keystore::builder()
        .with_tagged_key([0; 32], 0)?
        .build();

    let options = adnl::NodeOptions::default();

    let adnl = NetworkBuilder::with_adnl("127.0.0.1:10000", keystore, options)
        .with_query_subscriber(Arc::new(Service))
        .build()?;

    Ok(())
}
Source

pub fn with_message_subscriber( self, subscriber: Arc<dyn MessageSubscriber>, ) -> Self

Adds custom message subscriber if ADNL was successfully initialized.

§Examples
struct Service;

#[async_trait::async_trait]
impl MessageSubscriber for Service {
    async fn try_consume_custom<'a>(
        &self,
        _ctx: SubscriberContext<'a>,
        _constructor: u32,
        _data: &'a [u8],
    ) -> Result<bool> {
        Ok(true)
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let keystore = adnl::Keystore::builder()
        .with_tagged_key([0; 32], 0)?
        .build();

    let options = adnl::NodeOptions::default();

    let adnl = NetworkBuilder::with_adnl("127.0.0.1:10000", keystore, options)
        .with_message_subscriber(Arc::new(Service))
        .build()?;

    Ok(())
}
Source§

impl<L, A, R> NetworkBuilder<L, (A, R)>

Source

pub fn with_dht( self, key_tag: usize, options: NodeOptions, ) -> NetworkBuilder<HCons<Result<(Arc<Node>, usize, NodeOptions)>, L>, (There<A>, There<R>)>

Creates DHT network layer

§Examples

#[tokio::main]
async fn main() -> Result<()> {
    const DHT_KEY_TAG: usize = 0;

    let keystore = adnl::Keystore::builder()
        .with_tagged_key([0; 32], DHT_KEY_TAG)?
        .build();

    let adnl_options = adnl::NodeOptions::default();
    let dht_options = dht::NodeOptions::default();

    let (adnl, dht) = NetworkBuilder::with_adnl("127.0.0.1:10000", keystore, adnl_options)
        .with_dht(DHT_KEY_TAG, dht_options)
        .build()?;
    Ok(())
}
Source§

impl<L, A, R> NetworkBuilder<L, (A, R)>

Source

pub fn with_overlay( self, key_tag: usize, ) -> NetworkBuilder<HCons<Result<Arc<Node>>, L>, (There<A>, There<R>)>

Creates overlay network layer.

NOTE: RLDP network layer must be present before calling this method.

§Examples
#[tokio::main]
async fn main() -> Result<()> {
    const OVERLAY_KEY_TAG: usize = 0;

    let keystore = adnl::Keystore::builder()
        .with_tagged_key([0; 32], OVERLAY_KEY_TAG)?
        .build();

    let adnl_options = adnl::NodeOptions::default();
    let rldp_options = rldp::NodeOptions::default();

    let (adnl, rldp, overlay) =
        NetworkBuilder::with_adnl("127.0.0.1:10000", keystore, adnl_options)
            .with_rldp(rldp_options)
            .with_overlay(OVERLAY_KEY_TAG)
            .build()?;
    Ok(())
}
Source§

impl<L, A, R> NetworkBuilder<L, (A, R)>

Source

pub fn with_rldp( self, options: NodeOptions, ) -> NetworkBuilder<HCons<Result<(Arc<Node>, Vec<Arc<dyn QuerySubscriber>>, NodeOptions)>, L>, (There<A>, Here)>

Creates RLDP network layer

See with_rldp_ext if you need an RLDP node with additional subscribers

§Examples
#[tokio::main]
async fn main() -> Result<()> {
    let keystore = adnl::Keystore::builder()
        .with_tagged_key([0; 32], 0)?
        .build();

    let adnl_options = adnl::NodeOptions::default();
    let rldp_options = rldp::NodeOptions::default();

    let (adnl, rldp) = NetworkBuilder::with_adnl("127.0.0.1:10000", keystore, adnl_options)
        .with_rldp(rldp_options)
        .build()?;
    Ok(())
}
Source

pub fn with_rldp_ext( self, options: NodeOptions, subscribers: Vec<Arc<dyn QuerySubscriber>>, ) -> NetworkBuilder<HCons<Result<(Arc<Node>, Vec<Arc<dyn QuerySubscriber>>, NodeOptions)>, L>, (There<A>, Here)>

Creates RLDP network layer with additional RLDP query subscribers

§Examples
struct LoggerSubscriber;

#[async_trait::async_trait]
impl QuerySubscriber for LoggerSubscriber {
    async fn try_consume_query<'a>(
        &self,
        ctx: SubscriberContext<'a>,
        constructor: u32,
        query: Cow<'a, [u8]>,
    ) -> Result<QueryConsumingResult<'a>> {
        println!("received {constructor}");
        Ok(QueryConsumingResult::Rejected(query))
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let keystore = adnl::Keystore::builder()
        .with_tagged_key([0; 32], 0)?
        .build();

    let adnl_options = adnl::NodeOptions::default();
    let rldp_options = rldp::NodeOptions::default();

    let subscriber = Arc::new(LoggerSubscriber);

    let (adnl, rldp) = NetworkBuilder::with_adnl("127.0.0.1:10000", keystore, adnl_options)
        .with_rldp_ext(rldp_options, vec![subscriber])
        .build()?;
    Ok(())
}
Source§

impl<T, I> NetworkBuilder<T, I>

Source

pub fn build(self) -> Result<T::Output>

Initializes all layers into a tuple of components

Auto Trait Implementations§

§

impl<T, I> Freeze for NetworkBuilder<T, I>
where T: Freeze,

§

impl<T, I> RefUnwindSafe for NetworkBuilder<T, I>

§

impl<T, I> Send for NetworkBuilder<T, I>
where T: Send, I: Send,

§

impl<T, I> Sync for NetworkBuilder<T, I>
where T: Sync, I: Sync,

§

impl<T, I> Unpin for NetworkBuilder<T, I>
where T: Unpin, I: Unpin,

§

impl<T, I> UnwindSafe for NetworkBuilder<T, I>
where T: UnwindSafe, I: UnwindSafe,

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<Choices> CoproductSubsetter<CNil, HNil> for Choices

Source§

type Remainder = Choices

Source§

fn subset( self, ) -> Result<CNil, <Choices as CoproductSubsetter<CNil, HNil>>::Remainder>

Extract a subset of the possible types in a coproduct (or get the remaining possibilities) Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, I> LiftInto<U, I> for T
where U: LiftFrom<T, I>,

Source§

fn lift_into(self) -> U

Performs the indexed conversion.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<Source> Sculptor<HNil, HNil> for Source

Source§

type Remainder = Source

Source§

fn sculpt(self) -> (HNil, <Source as Sculptor<HNil, HNil>>::Remainder)

Consumes the current HList and returns an HList with the requested shape. 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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more