diff --git a/iroh/examples/custom-protocol.rs b/iroh/examples/custom-protocol.rs index ded75add1a..2516eef8d7 100644 --- a/iroh/examples/custom-protocol.rs +++ b/iroh/examples/custom-protocol.rs @@ -13,8 +13,6 @@ use iroh::{ }; use tracing_subscriber::{prelude::*, EnvFilter}; -const EXAMPLE_ALPN: &'static [u8] = b"example-proto/0"; - #[derive(Debug, Parser)] pub struct Cli { #[clap(subcommand)] @@ -33,7 +31,7 @@ async fn main() -> Result<()> { let args = Cli::parse(); // create a new node let node = iroh::node::Node::memory() - .accept(EXAMPLE_ALPN, |node| ExampleProtocol::build(node)) + .accept(ExampleProto::ALPN, |node| ExampleProto::build(node)) .spawn() .await?; @@ -46,7 +44,7 @@ async fn main() -> Result<()> { tokio::signal::ctrl_c().await?; } Command::Connect { node: node_id } => { - let proto = ExampleProtocol::from_node(&node, EXAMPLE_ALPN).expect("it is registered"); + let proto = ExampleProto::get_from_node(&node, EXAMPLE_ALPN).expect("it is registered"); proto.connect(node_id).await?; } } @@ -57,11 +55,11 @@ async fn main() -> Result<()> { } #[derive(Debug)] -struct ExampleProtocol { +struct ExampleProto { node: Node, } -impl Protocol for ExampleProtocol { +impl Protocol for ExampleProto { fn as_arc_any(self: Arc) -> Arc { self } @@ -71,13 +69,15 @@ impl Protocol for ExampleProtocol { } } -impl ExampleProtocol { +impl ExampleProto { + const ALPN: &'static [u8] = b"example-proto/0"; + fn build(node: Node) -> Arc { Arc::new(Self { node }) } - fn from_node(node: &Node, alpn: &'static [u8]) -> Option> { - node.get_protocol::>(alpn) + fn get_from_node(node: &Node, alpn: &'static [u8]) -> Option> { + node.get_protocol::>(alpn) } async fn handle_connection(&self, conn: Connection) -> Result<()> { @@ -100,7 +100,7 @@ impl ExampleProtocol { Ok(()) } - pub async fn connect(&self, remote_node_id: NodeId) -> Result<()> { + async fn connect(&self, remote_node_id: NodeId) -> Result<()> { println!("our node id: {}", self.node.node_id()); println!("connecting to {remote_node_id}"); let conn = self