From 3ecfe8a05b854bd30f8897c8542c40791002d99e Mon Sep 17 00:00:00 2001 From: Takeru Ohta Date: Mon, 12 Feb 2024 21:39:55 +0900 Subject: [PATCH] Add `RpcClient::connect_with_port()` to support epmdless nodes --- examples/call-zero-arity-fun.rs | 10 +++++++++- src/lib.rs | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/examples/call-zero-arity-fun.rs b/examples/call-zero-arity-fun.rs index 7309884..4af5e12 100644 --- a/examples/call-zero-arity-fun.rs +++ b/examples/call-zero-arity-fun.rs @@ -9,6 +9,9 @@ struct Args { #[clap(long)] cookie: Option, + + #[clap(long, short)] + port: Option, } fn main() -> anyhow::Result<()> { @@ -22,7 +25,12 @@ fn main() -> anyhow::Result<()> { }; smol::block_on(async { - let client = erl_rpc::RpcClient::connect(&args.node_name.to_string(), &cookie).await?; + let client = if let Some(port) = args.port { + erl_rpc::RpcClient::connect_with_port(&args.node_name.to_string(), port, &cookie) + .await? + } else { + erl_rpc::RpcClient::connect(&args.node_name.to_string(), &cookie).await? + }; let mut handle = client.handle(); smol::spawn(async { if let Err(e) = client.run().await { diff --git a/src/lib.rs b/src/lib.rs index b4d13f4..3e963e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,12 +99,22 @@ pub struct RpcClient { impl RpcClient { /// Connects to a given Erlang node. pub async fn connect(server_node_name: &str, cookie: &str) -> Result { - let server_node_name: NodeName = server_node_name.parse()?; - let server_node_entry = get_node_entry(&server_node_name).await?; + let server_node_entry = get_node_entry(&server_node_name.parse()?).await?; if server_node_entry.highest_version < 6 { return Err(ConnectError::TooOldDistributionProtocolVersion); } + Self::connect_with_port(server_node_name, server_node_entry.port, cookie).await + } + + /// Directly connects to a given Erlang node without using EPMD. + /// + pub async fn connect_with_port( + server_node_name: &str, + server_listen_port: u16, + cookie: &str, + ) -> Result { + let server_node_name: NodeName = server_node_name.parse()?; let tentative_name = "nonode@localhost"; let mut local_node = LocalNode::new(tentative_name.parse()?, Creation::random()); local_node.flags |= DistributionFlags::NAME_ME; @@ -112,8 +122,7 @@ impl RpcClient { local_node.flags |= DistributionFlags::DIST_MONITOR; local_node.flags |= DistributionFlags::DIST_MONITOR_NAME; - let connection = - TcpStream::connect((server_node_name.host(), server_node_entry.port)).await?; + let connection = TcpStream::connect((server_node_name.host(), server_listen_port)).await?; let mut handshake = ClientSideHandshake::new(connection, local_node.clone(), cookie); let status = handshake.execute_send_name(6).await?; if let HandshakeStatus::Named { name, creation } = status {