pub struct Node {
config: Arc<Config>,
key_pair: KeyPair,
peer_id: PeerID,
monitor: Arc<Monitor>,
discovery: Arc<dyn Discovery>,
peer_pool: Arc<PeerPool>,
connector: Arc<Connector<PeerNetMsgCodec>>,
listener: Arc<Listener<PeerNetMsgCodec>>,
task_group: TaskGroup,
}Expand description
Central entry point for the p2p network.
Manages peer connections, discovery, and protocol registration.
§Example
use karyon_core::async_runtime::global_executor;
use karyon_p2p::{Node, Config, keypair::{KeyPair, KeyPairType}};
let key_pair = KeyPair::generate(&KeyPairType::Ed25519);
let config = Config {
listen_endpoints: vec![
"tcp://0.0.0.0:8000".parse().unwrap(),
],
..Config::default()
};
let node = Node::new(&key_pair, config, global_executor());
// node.run().await.unwrap();
// node.attach_protocol(MyProto::new).await;
// node.shutdown().await;Fields§
§config: Arc<Config>The Configuration for the P2P network.
key_pair: KeyPairIdentity Key pair
peer_id: PeerIDPeer ID
monitor: Arc<Monitor>Responsible for network and system monitoring.
discovery: Arc<dyn Discovery>Discovery instance.
peer_pool: Arc<PeerPool>PeerPool instance.
connector: Arc<Connector<PeerNetMsgCodec>>Connector for outbound connections.
listener: Arc<Listener<PeerNetMsgCodec>>Listener for inbound connections.
task_group: TaskGroupManaging spawned tasks.
Implementations§
Source§impl Node
impl Node
Sourcepub fn new(key_pair: &KeyPair, config: Config, ex: Executor) -> Arc<Node>
pub fn new(key_pair: &KeyPair, config: Config, ex: Executor) -> Arc<Node>
Creates a new Node with the default Kademlia discovery.
Sourcepub fn with_discovery(
key_pair: &KeyPair,
config: Config,
discovery: Arc<dyn Discovery>,
ex: Executor,
) -> Arc<Node>
pub fn with_discovery( key_pair: &KeyPair, config: Config, discovery: Arc<dyn Discovery>, ex: Executor, ) -> Arc<Node>
Creates a new Node with a custom discovery implementation.
Sourcepub async fn run(self: &Arc<Self>) -> Result<()>
pub async fn run(self: &Arc<Self>) -> Result<()>
Run the Node, starting listeners, PeerPool, and Discovery.
Sourceasync fn forward_peer_events(self: Arc<Self>)
async fn forward_peer_events(self: Arc<Self>)
Forward peer-pool lifecycle events to discovery so it can update its routing state. Each registered listener (Node’s here, plus any external Swarm subscriber) gets every event independently - no MPMC stealing.
Sourceasync fn connect_discovered_peers(self: Arc<Self>)
async fn connect_discovered_peers(self: Arc<Self>)
Consume discovered peers from the discovery service and connect to them. Runs forever; the task_group cancels it on Node::shutdown.
Sourcepub async fn attach_protocol<P: ProtocolTrait + 'static>(
&self,
c: impl Fn(PeerConn) -> P + Send + Sync + 'static,
) -> Result<()>
pub async fn attach_protocol<P: ProtocolTrait + 'static>( &self, c: impl Fn(PeerConn) -> P + Send + Sync + 'static, ) -> Result<()>
Attach a custom protocol. karyon runs the constructor closure
once per connected peer with a typed PeerConn scoped to this
protocol. The protocol type is inferred from the closure’s
return value. The protocol id is advertised through discovery
according to P::flags().
node.attach_protocol(MyProtocol::new).await?;Sourceasync fn attach_core_protocols(self: &Arc<Self>) -> Result<()>
async fn attach_core_protocols(self: &Arc<Self>) -> Result<()>
Attach the core protocols (PING). Called once during run.
Sourcepub fn advertise(&self, item: impl AsRef<[u8]>, flags: ProtocolFlags)
pub fn advertise(&self, item: impl AsRef<[u8]>, flags: ProtocolFlags)
Advertise an item (protocol id, swarm key, …) through
discovery. See ProtocolFlags. Used by Swarm and other
layers.
Sourcepub fn find_peers_with(&self, item: impl AsRef<[u8]>) -> Vec<DiscoveredPeer>
pub fn find_peers_with(&self, item: impl AsRef<[u8]>) -> Vec<DiscoveredPeer>
Find peers in the routing table that may support item.
Useful for swarm-targeted lookups (e.g. “peers in this room”)
without changing handshake semantics.
Sourcepub async fn inbound_peers(&self) -> HashMap<PeerID, Endpoint>
pub async fn inbound_peers(&self) -> HashMap<PeerID, Endpoint>
Returns a map of inbound connected peers with their endpoints.
Sourcepub async fn outbound_peers(&self) -> HashMap<PeerID, Endpoint>
pub async fn outbound_peers(&self) -> HashMap<PeerID, Endpoint>
Returns a map of outbound connected peers with their endpoints.
Sourcepub fn register_peer_events(&self) -> EventListener<PeerEventTopic, PeerEvent>
pub fn register_peer_events(&self) -> EventListener<PeerEventTopic, PeerEvent>
Register a listener for peer lifecycle events. Each call returns a fresh listener that receives every event (true broadcast).
Sourcepub async fn broadcast_to(
&self,
proto_id: &ProtocolID,
msg: Vec<u8>,
targets: &HashSet<PeerID>,
)
pub async fn broadcast_to( &self, proto_id: &ProtocolID, msg: Vec<u8>, targets: &HashSet<PeerID>, )
Broadcast a message to a specific set of peers on a given protocol. Used by Swarm and other layers to scope broadcasts.
Sourcepub async fn send_to(
&self,
peer_id: &PeerID,
proto_id: &ProtocolID,
msg: Vec<u8>,
) -> Result<()>
pub async fn send_to( &self, peer_id: &PeerID, proto_id: &ProtocolID, msg: Vec<u8>, ) -> Result<()>
Send a message to a specific peer on the given protocol.
Returns PeerNotFound if the peer is not currently connected.
Sourcepub async fn peer_protocol_set(
&self,
pid: &PeerID,
) -> Option<HashSet<ProtocolID>>
pub async fn peer_protocol_set( &self, pid: &PeerID, ) -> Option<HashSet<ProtocolID>>
Returns the negotiated protocol set for a connected peer, or
None if no peer with that id is currently in the pool.