pub struct NetApp {
    pub version_tag: [u8; 16],
    pub netid: Key,
    pub id: PublicKey,
    pub privkey: SecretKey,
    /* private fields */
}
Expand description

NetApp is the main class that handles incoming and outgoing connections.

NetApp can be used in a stand-alone fashion or together with a peering strategy. If using it alone, you will want to set on_connect and on_disconnect events in order to manage information about the current peer list.

Fields§

§version_tag: [u8; 16]

Version tag, 8 bytes for netapp version, 8 bytes for app version

§netid: Key

Network secret key

§id: PublicKey

Our peer ID

§privkey: SecretKey

Private key associated with our peer ID

Implementations§

source§

impl NetApp

source

pub fn new( app_version_tag: u64, netid: Key, privkey: SecretKey, bind_outgoing_to: Option<IpAddr> ) -> Arc<NetApp>

Creates a new instance of NetApp, which can serve either as a full p2p node, or just as a passive client. To upgrade to a full p2p node, spawn a listener using .listen()

Our Peer ID is the public key associated to the secret key given here.

source

pub fn on_connected<F>(&self, handler: F)
where F: Fn(PublicKey, SocketAddr, bool) + Send + Sync + 'static,

Set the handler to be called when a new connection (incoming or outgoing) has been successfully established. Do not set this if using a peering strategy, as the peering strategy will need to set this itself.

source

pub fn on_disconnected<F>(&self, handler: F)
where F: Fn(PublicKey, bool) + Send + Sync + 'static,

Set the handler to be called when an existing connection (incoming or outgoing) has been closed by either party. Do not set this if using a peering strategy, as the peering strategy will need to set this itself.

source

pub fn endpoint<M, H>(self: &Arc<NetApp>, path: String) -> Arc<Endpoint<M, H>>
where M: Message + 'static, H: StreamingEndpointHandler<M> + 'static,

Create a new endpoint with path path, that handles messages of type M. H is the type of the object that should handle requests to this endpoint on the local node. If you don’t want to handle request on the local node (e.g. if this node is only a client in the network), define the type H to be (). This function will panic if the endpoint has already been created.

source

pub async fn listen( self: Arc<NetApp>, listen_addr: SocketAddr, public_addr: Option<SocketAddr>, must_exit: Receiver<bool> )

Main listening process for our app. This future runs during the whole run time of our application. If this is not called, the NetApp instance remains a passive client.

source

pub fn drop_all_handlers(&self)

Drop all endpoint handlers, as well as handlers for connection/disconnection events. (This disables the peering strategy)

Use this when terminating to break reference cycles

source

pub async fn try_connect( self: Arc<NetApp>, ip: SocketAddr, id: PublicKey ) -> Result<(), Error>

Attempt to connect to a peer, given by its ip:port and its public key. The public key will be checked during the secret handshake process. This function returns once the connection has been established and a successfull handshake was made. At this point we can send messages to the other node with Netapp::request

source

pub fn disconnect(self: &Arc<NetApp>, id: &PublicKey)

Close the outgoing connection we have to a node specified by its public key, if such a connection is currently open.

Trait Implementations§

source§

impl EndpointHandler<HelloMessage> for NetApp

source§

fn handle<'life0, 'life1, 'async_trait>( self: &'life0 Arc<NetApp>, msg: &'life1 HelloMessage, from: PublicKey ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, NetApp: 'async_trait,

Auto Trait Implementations§

§

impl !Freeze for NetApp

§

impl !RefUnwindSafe for NetApp

§

impl Send for NetApp

§

impl Sync for NetApp

§

impl Unpin for NetApp

§

impl !UnwindSafe for NetApp

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> FutureExt for T

source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> Same for T

§

type Output = T

Should always be Self
source§

impl<T, M> StreamingEndpointHandler<M> for T
where T: EndpointHandler<M>, M: Message,

source§

fn handle<'life0, 'async_trait>( self: &'life0 Arc<T>, m: Req<M>, from: PublicKey ) -> Pin<Box<dyn Future<Output = Resp<M>> + Send + 'async_trait>>
where 'life0: 'async_trait, T: 'async_trait,

source§

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

§

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

§

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