Skip to content

Commit

Permalink
Add RpcClient::connect_with_port() to support epmdless nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Feb 12, 2024
1 parent 818733e commit 3ecfe8a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
10 changes: 9 additions & 1 deletion examples/call-zero-arity-fun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ struct Args {

#[clap(long)]
cookie: Option<String>,

#[clap(long, short)]
port: Option<u16>,
}

fn main() -> anyhow::Result<()> {
Expand All @@ -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 {
Expand Down
17 changes: 13 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,30 @@ pub struct RpcClient {
impl RpcClient {
/// Connects to a given Erlang node.
pub async fn connect(server_node_name: &str, cookie: &str) -> Result<Self, ConnectError> {
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<Self, ConnectError> {
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;
local_node.flags |= DistributionFlags::SPAWN;
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 {
Expand Down

0 comments on commit 3ecfe8a

Please sign in to comment.