Skip to content

Commit

Permalink
Implement JSON RPC net_getPort
Browse files Browse the repository at this point in the history
  • Loading branch information
foriequal0 authored and kseo committed Aug 4, 2018
1 parent 564c651 commit 480994f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
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>;
}
}

0 comments on commit 480994f

Please sign in to comment.