Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RpcClient::connect_with_port() to directly connect to an epmdless node #4

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading