pub struct Node<N: NetworkProvider + 'static> { /* private fields */ }Expand description
The main truffle node — single public entry point for all functionality.
Generic over N: NetworkProvider so that tests can inject a mock provider
without Tailscale. In production, use the concrete type
Node<TailscaleProvider> (created via NodeBuilder).
§Lifecycle
- Create via
Node::builder()+.build().await - Use
peers(),send(),subscribe(),open_tcp(), etc. - Call
stop()to shut down
Implementations§
Source§impl<N: NetworkProvider + 'static> Node<N>
impl<N: NetworkProvider + 'static> Node<N>
Sourcepub fn builder() -> NodeBuilder
pub fn builder() -> NodeBuilder
Create a new NodeBuilder for configuring and constructing a node.
Sourcepub fn file_transfer(&self) -> FileTransfer<'_, N>
pub fn file_transfer(&self) -> FileTransfer<'_, N>
Access the file transfer subsystem.
Returns a FileTransfer handle
that provides methods for sending, receiving, and pulling files.
Sourcepub fn synced_store<T>(self: &Arc<Self>, store_id: &str) -> Arc<SyncedStore<T>>
pub fn synced_store<T>(self: &Arc<Self>, store_id: &str) -> Arc<SyncedStore<T>>
Create a synchronized store for device-owned state.
Returns an Arc<SyncedStore<T>> that syncs data across the mesh on
namespace "ss:{store_id}". The caller owns the returned Arc;
the background sync task also holds one.
Requires self to be wrapped in an Arc because the sync task
needs to outlive this call.
Sourcepub fn synced_store_with_backend<T>(
self: &Arc<Self>,
store_id: &str,
backend: Arc<dyn StoreBackend>,
) -> Arc<SyncedStore<T>>
pub fn synced_store_with_backend<T>( self: &Arc<Self>, store_id: &str, backend: Arc<dyn StoreBackend>, ) -> Arc<SyncedStore<T>>
Create a synchronized store with a custom persistence backend.
Same as synced_store but restores persisted
data on startup and writes through to the backend on every change.
Sourcepub async fn crdt_doc(
self: &Arc<Self>,
doc_id: &str,
) -> Result<Arc<CrdtDoc>, CrdtDocError>
pub async fn crdt_doc( self: &Arc<Self>, doc_id: &str, ) -> Result<Arc<CrdtDoc>, CrdtDocError>
Create a CRDT document synchronized across the mesh.
Returns an Arc<CrdtDoc> that syncs on namespace "crdt:{doc_id}".
Uses the default MemoryCrdtBackend
(no persistence).
Sourcepub async fn crdt_doc_with_backend(
self: &Arc<Self>,
doc_id: &str,
backend: Arc<dyn CrdtBackend>,
) -> Result<Arc<CrdtDoc>, CrdtDocError>
pub async fn crdt_doc_with_backend( self: &Arc<Self>, doc_id: &str, backend: Arc<dyn CrdtBackend>, ) -> Result<Arc<CrdtDoc>, CrdtDocError>
Create a CRDT document with a custom persistence backend.
Same as crdt_doc but restores persisted data on
startup and writes through to the backend on every change.
Sourcepub fn state_dir(&self) -> &Path
pub fn state_dir(&self) -> &Path
The state directory for persistence backends.
Returns an empty path for test nodes created via from_parts.
Sourcepub async fn stop(&self)
pub async fn stop(&self)
Stop the node and all underlying layers.
After calling stop(), the node should not be used for further
operations. Peer connections are closed and the network provider
is shut down.
Sourcepub fn local_info(&self) -> NodeIdentity
pub fn local_info(&self) -> NodeIdentity
Return the local node’s identity (stable ID, hostname, name).
Sourcepub async fn peers(&self) -> Vec<Peer>
pub async fn peers(&self) -> Vec<Peer>
Return all known peers.
Includes peers that are online but not yet connected (no active WS). This information comes from Layer 3 peer discovery.
Sourcepub fn on_peer_change(&self) -> Receiver<PeerEvent>
pub fn on_peer_change(&self) -> Receiver<PeerEvent>
Subscribe to peer change events (joined, left, connected, etc.).
Sourcepub async fn resolve_peer_id(&self, peer_id: &str) -> Result<String, NodeError>
pub async fn resolve_peer_id(&self, peer_id: &str) -> Result<String, NodeError>
Resolve a peer identifier to the canonical per-device ULID
(device_id) from the RFC 017 hello envelope.
Accepts any of:
- the stable
device_id(full ULID) - a unique prefix of the
device_id(at least 4 characters; must match exactly one known peer) - the human-readable
device_namefrom the hello - the Layer 3 Tailscale hostname (the sanitised slug) — legacy
- the Tailscale stable ID — escape hatch for diagnostics
The returned string is always a device_id, so it is safe to feed
back into Node::send or any other method that takes a peer
identifier.
Sourcepub async fn ping(&self, peer_id: &str) -> Result<PingResult, NodeError>
pub async fn ping(&self, peer_id: &str) -> Result<PingResult, NodeError>
Ping a peer via the network layer.
Resolves the peer ID to an IP address and pings via Layer 3. Accepts
the same identifier forms as resolve_peer_id.
Sourcepub async fn health(&self) -> HealthInfo
pub async fn health(&self) -> HealthInfo
Return health information from the network layer.
Sourcepub async fn send(
&self,
peer_id: &str,
namespace: &str,
data: &[u8],
) -> Result<(), NodeError>
pub async fn send( &self, peer_id: &str, namespace: &str, data: &[u8], ) -> Result<(), NodeError>
Send a namespaced message to a specific peer.
The data is wrapped in a Layer 6 Envelope with the given namespace
and a "message" type, then serialized and sent via the session layer.
If no WebSocket connection exists, one is lazily established.
Sourcepub async fn send_typed(
&self,
peer_id: &str,
namespace: &str,
msg_type: &str,
payload: &Value,
) -> Result<(), NodeError>
pub async fn send_typed( &self, peer_id: &str, namespace: &str, msg_type: &str, payload: &Value, ) -> Result<(), NodeError>
Send a namespaced message with an explicit msg_type and JSON payload.
Unlike send, this method takes a pre-built
serde_json::Value payload and a caller-chosen msg_type instead
of raw bytes with a hardcoded "message" type. Used by subsystems
(file transfer, synced store, request/reply) that define their own
wire protocol message types.
Sourcepub async fn broadcast_typed(
&self,
namespace: &str,
msg_type: &str,
payload: &Value,
)
pub async fn broadcast_typed( &self, namespace: &str, msg_type: &str, payload: &Value, )
Broadcast a namespaced message with an explicit msg_type and JSON
payload to all connected peers.
Sourcepub async fn broadcast(&self, namespace: &str, data: &[u8])
pub async fn broadcast(&self, namespace: &str, data: &[u8])
Broadcast a namespaced message to all connected peers.
Only peers with active WebSocket connections receive the broadcast. No lazy connections are established.
Sourcepub fn subscribe(&self, namespace: &str) -> Receiver<NamespacedMessage>
pub fn subscribe(&self, namespace: &str) -> Receiver<NamespacedMessage>
Subscribe to messages in a specific namespace.
Returns a broadcast receiver that yields NamespacedMessages
matching the given namespace. Multiple subscribers to the same
namespace share the same underlying channel.
Sourcepub async fn open_tcp(
&self,
peer_id: &str,
port: u16,
) -> Result<TcpStream, NodeError>
pub async fn open_tcp( &self, peer_id: &str, port: u16, ) -> Result<TcpStream, NodeError>
Open a raw TCP stream to a peer on the given port.
Resolves the peer ID to an IP address via the session’s peer list,
then dials via the network layer. Accepts the same identifier
forms as resolve_peer_id. Returns a
plain TcpStream for byte-oriented I/O.
Sourcepub async fn listen_tcp(&self, port: u16) -> Result<RawListener, NodeError>
pub async fn listen_tcp(&self, port: u16) -> Result<RawListener, NodeError>
Listen for incoming TCP connections on a port.
Returns a RawListener that yields raw TcpStreams. The caller
is responsible for accepting connections in a loop.
Auto Trait Implementations§
impl<N> !Freeze for Node<N>
impl<N> !RefUnwindSafe for Node<N>
impl<N> Send for Node<N>
impl<N> Sync for Node<N>
impl<N> Unpin for Node<N>
impl<N> UnsafeUnpin for Node<N>
impl<N> !UnwindSafe for Node<N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);