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

Implement JSON RPC net_getPort #536

Merged
merged 2 commits into from
Aug 4, 2018
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
4 changes: 4 additions & 0 deletions codechain/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ impl NetworkControl for DummyNetworkService {
fn is_connected(&self, _addr: &SocketAddr) -> Result<bool, NetworkControlError> {
Err(NetworkControlError::Disabled)
}

fn get_port(&self) -> Result<u16, NetworkControlError> {
Err(NetworkControlError::Disabled)
}
}

fn run_node(matches: ArgMatches) -> Result<(), String> {
Expand Down
1 change: 1 addition & 0 deletions network/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub trait Control: Send + Sync {
fn connect(&self, addr: SocketAddr) -> Result<(), Error>;
fn disconnect(&self, addr: SocketAddr) -> Result<(), Error>;
fn is_connected(&self, addr: &SocketAddr) -> Result<bool, Error>;
fn get_port(&self) -> Result<u16, Error>;
}

#[derive(Clone, Debug)]
Expand Down
6 changes: 6 additions & 0 deletions network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Service {
timer: IoService<timer::Message>,
client: Arc<Client>,
routing_table: Arc<RoutingTable>,
socket_address: SocketAddr,
}

impl Service {
Expand Down Expand Up @@ -68,6 +69,7 @@ impl Service {
timer,
client,
routing_table,
socket_address: address,
}))
}

Expand Down Expand Up @@ -126,6 +128,10 @@ impl Control for Service {
fn is_connected(&self, addr: &SocketAddr) -> Result<bool, ControlError> {
Ok(self.routing_table.is_connected(addr))
}

fn get_port(&self) -> Result<u16, ControlError> {
Ok(self.socket_address.port())
}
}

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions rpc/src/v1/impls/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ impl Net for NetClient {
fn is_connected(&self, address: ::std::net::IpAddr, port: u16) -> Result<bool> {
Ok(self.network_control.is_connected(&SocketAddr::new(address, port)).map_err(errors::network_control)?)
}

fn get_port(&self) -> Result<u16> {
Ok(self.network_control.get_port().map_err(errors::network_control)?)
}
}
3 changes: 3 additions & 0 deletions rpc/src/v1/traits/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ build_rpc_trait! {

# [rpc(name = "net_isConnected")]
fn is_connected(&self, ::std::net::IpAddr, u16) -> Result<bool>;

# [rpc(name = "net_getPort")]
fn get_port(&self) -> Result<u16>;
}
}