pub struct Node { /* private fields */ }
Expand description
Represents a node on the circuit network.
A node can connect to one or more other nodes, and a stream can be established between any two nodes on the entire connected graph of nodes.
Implementations§
Source§impl Node
impl Node
Sourcepub fn new(
node_id: &str,
protocol: &str,
new_peer_sender: Sender<String>,
incoming_stream_sender: Sender<(Reader, Writer, String)>,
) -> Result<Node>
pub fn new( node_id: &str, protocol: &str, new_peer_sender: Sender<String>, incoming_stream_sender: Sender<(Reader, Writer, String)>, ) -> Result<Node>
Establish a new node.
Any time a new peer becomes visible to this node, the peer’s node ID will be sent to
new_peer_sender
.
Any time a peer wants to establish a stream with this node, a reader and writer for the new
stream as well as the peer’s node ID will be sent to incoming_stream_sender
.
Sourcepub fn new_with_router(
node_id: &str,
protocol: &str,
interval: Duration,
new_peer_sender: Sender<String>,
incoming_stream_sender: Sender<(Reader, Writer, String)>,
) -> Result<(Node, impl Future<Output = ()> + Send)>
pub fn new_with_router( node_id: &str, protocol: &str, interval: Duration, new_peer_sender: Sender<String>, incoming_stream_sender: Sender<(Reader, Writer, String)>, ) -> Result<(Node, impl Future<Output = ()> + Send)>
Establish a new node which will forward streams between its peers. The router process provided must be polled continuously to provide this forwarding.
Sourcepub async fn connect_to_peer(
&self,
connection_reader: Reader,
connection_writer: Writer,
node_id: &str,
) -> Result<()>
pub async fn connect_to_peer( &self, connection_reader: Reader, connection_writer: Writer, node_id: &str, ) -> Result<()>
Establish a stream with another node. Data will be sent to the peer with
connection_writer
, and received with connection_reader
.
Sourcepub fn link_node(
&self,
control_stream: Option<(Reader, Writer)>,
new_stream_sender: Sender<(Reader, Writer)>,
new_stream_receiver: impl Stream<Item = (Reader, Writer, Sender<Result<()>>)> + Unpin + Send,
quality: Quality,
) -> impl Future<Output = Result<()>> + Send
pub fn link_node( &self, control_stream: Option<(Reader, Writer)>, new_stream_sender: Sender<(Reader, Writer)>, new_stream_receiver: impl Stream<Item = (Reader, Writer, Sender<Result<()>>)> + Unpin + Send, quality: Quality, ) -> impl Future<Output = Result<()>> + Send
Connect to another node.
This establishes the internal state to link this node directly to another one, there by joining it to the circuit network. To actually perform the networking necessary to create such a link, a back end will have to service the streams given to this function via its arguments. To keep the link running, the returned future must also be polled to completion. Depending on configuration, it may complete swiftly or may poll for the entire lifetime of the link.
When we link to another node, we immediately start a “control stream” that performs a
handshake and sends routing messages. The reader and writer passed in through
control_stream
will be used to service this stream. If control_stream
is None
the
first stream emitted from new_stream_receiver
will be used.
When the local node needs to create a new stream to the linked node, it will send a reader
and writer to new_stream_sender
.
When the linked node wants to create a new stream to this node, the back end may send a
reader and writer through new_stream_receiver
, as well as a oneshot::Sender
which will
be used to report if the link is established successfully or if an error occurs.
The returned future will continue to poll for the lifetime of the link and return the error that terminated it.