Struct msgpacknet::Node [] [src]

pub struct Node<M: Message, N: NodeId, I: InitMessage>(_);

The node struct

This is a reference counted wrapper around the node data. This struct can be parametrized with the following types:

There are several different constructors:

Methods

impl<M: Message, N: NodeId, I: InitMessage> Node<M, N, I>

fn new(node_id: N, init_message: I) -> CloseGuard<M, N, I>

Create a new node

The parameter node_id must be a unique identifier for this node and init_message is the first message sent automatically to other nodes when connected. The result of this call is a guard that closes the node if dropped.

fn set_connection_timeout(&self, dur: Duration)

Set the connection timeout

Whenever a connection is idle longer than this timeout, it will be closed. The default value is 60 seconds.

Note: Changes to this value only affect connections created after the change.

fn connection_timeout(&self) -> Duration

The connection timeout

fn set_stats_halflife_time(&self, dur: Duration)

Set the statistics half-life time

This value is used as a parameter for the rolling average statistics of sending and receiving rates. Shorter durations react faster to changes and longer durations result in smoother behavior. The default value is 60 seconds.

Note: Changes to this value only affect connections created after the change.

fn stats_halflife_time(&self) -> Duration

The statistics half-life time

fn set_init_message(&self, init: I)

Set the initialization message

This message is sent as a first message on all new connections and must identify the node.

fn init_message(&self) -> I

The initialization message

fn listen<A: ToSocketAddrs>(&self, addr: A) -> Result<SocketAddr, Error<N>>

Listen on the specified address

This method will open a new node listening to the given address. A dedicated thread will be started to handle incoming connections. The node can listen on multiple addresses. The result of this method, if successful, is the actual address used.

Note: A port of 0 has the special meaning of taking a random free port. If the host part of the address is "0.0.0.0" or "[::0]" the socket will be opened listening on all available IPv4 or IPv6 addresses.

fn listen_defaults(&self) -> Result<(), Error<N>>

Listen on the addresses "0.0.0.0:0" and "[::0]:0"

This will open sockets for IPv4 and IPv6 (see listen(...) for details).

Note: This method will always open two new sockets, regardless of whether sockets are already open.

fn addresses(&self) -> Vec<SocketAddr>

The addresses of this node

fn receive(&self) -> Event<M, N, I>

Receive an event

This method returns the next event received. If events are queued, the oldest queued event will be returned. Otherwise, the call will block until an event is received.

If the node has been closed, Event::Closed will be returned immediately.

fn node_id(&self) -> N

The id of this node

fn is_connected(&self, id: &N) -> bool

Whether this node is connected to the given node

fn connection_count(&self) -> usize

The number of fully established connections

fn stats(&self) -> NodeStats<N>

The statistics of this node

fn send(&self, dst: &N, msg: &M) -> Result<(), Error<N>>

Send a message

This method sends a message to the given destination. The message must be encodable using serde and the node must be connected to the destination. It is possible to send messages to the node itself by using its address. If no connection to the destination exists, an error is returned.

fn connect_request<A: ToSocketAddrs>(&self, addr: A) -> Result<ConnectionRequest<M, N, I>, Error<N>>

Open a connection and return the request

This method opens a connection, exchanges initialization messages with the peer and returns a connection request. This request object can then be used to inspect the node id and the initialization message and then decide whether to accept() or reject() the connection. This method blocks until the underlying connection has been established and the initialization messages have been exchanged.

For more details see connect(...).

fn connect<A: ToSocketAddrs>(&self, addr: A) -> Result<N, Error<N>>

Open a connection and accept it automatically

This method opens a connection, exchanges initialization messages with the peer, automatically accepts the peer and returns its node_id. This method blocks until the underlying connection has been established and the initialization messages have been exchanged.

Note: This connection will automatically be closed when it becomes idle for longer than the specified timeout.

Note 2: It is possible to connect the node to itself. However, messages will just be short-circuited and the connection will not be used and closed after the timeout.

fn lookup_connection<A: ToSocketAddrs>(&self, addr: A) -> Option<N>

Returns the node id for an address if connected

This method retrns the node id for an address if a connection to this address exists.

fn close(&self) -> Result<(), Error<N>>

impl<M: Message, N: NodeId, I: InitMessage> Node<M, N, I> where N: Rand

fn with_random_id(init: I) -> CloseGuard<M, N, I>

Create a new node with a random id

impl<M: Message, N: NodeId> Node<M, N, ()>

fn without_init(node_id: N) -> CloseGuard<M, N, ()>

Create a new node with a random id

impl<M: Message, N: NodeId> Node<M, N, ()> where N: Rand

fn create_default() -> CloseGuard<M, N, ()>

Create a new node with a random id and without init message

Trait Implementations

impl<M: Message, N: NodeId, I: InitMessage> Deref for Node<M, N, I>

type Target = NodeInner<M, N, I>

fn deref(&self) -> &Self::Target

Derived Implementations

impl<M: Clone + Message, N: Clone + NodeId, I: Clone + InitMessage> Clone for Node<M, N, I>

fn clone(&self) -> Node<M, N, I>

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