support connect to given port via command line

This commit is contained in:
Svyatoslav Nikolsky 2016-11-02 10:25:09 +03:00
parent a611ac8c62
commit 36e30ef5ce
3 changed files with 11 additions and 7 deletions

View File

@ -1,4 +1,4 @@
use std::net::IpAddr; use std::net::SocketAddr;
use net::Config as NetConfig; use net::Config as NetConfig;
#[derive(Debug)] #[derive(Debug)]
@ -16,7 +16,7 @@ pub struct Config {
/// Configuration for every connection. /// Configuration for every connection.
pub connection: NetConfig, pub connection: NetConfig,
/// Connect only ot these nodes. /// Connect only ot these nodes.
pub peers: Vec<IpAddr>, pub peers: Vec<SocketAddr>,
/// Connect to these nodes to retrieve peer addresses, and disconnect. /// Connect to these nodes to retrieve peer addresses, and disconnect.
pub seeds: Vec<String>, pub seeds: Vec<String>,
} }

View File

@ -323,9 +323,8 @@ impl P2P {
Ok(()) Ok(())
} }
pub fn connect<T>(&self, ip: net::IpAddr) where T: SessionFactory { pub fn connect<T>(&self, addr: net::SocketAddr) where T: SessionFactory {
let socket = net::SocketAddr::new(ip, self.config.connection.magic.port()); Context::connect::<T>(self.context.clone(), addr, self.config.connection.clone());
Context::connect::<T>(self.context.clone(), socket, self.config.connection.clone());
} }
pub fn connect_to_seednode(&self, resolver: &Resolver, seednode: &str) { pub fn connect_to_seednode(&self, resolver: &Resolver, seednode: &str) {

View File

@ -5,7 +5,7 @@ use message::common::Magic;
pub struct Config { pub struct Config {
pub magic: Magic, pub magic: Magic,
pub port: u16, pub port: u16,
pub connect: Option<net::IpAddr>, pub connect: Option<net::SocketAddr>,
pub seednode: Option<String>, pub seednode: Option<String>,
pub print_to_console: bool, pub print_to_console: bool,
pub use_disk_database: bool, pub use_disk_database: bool,
@ -26,7 +26,12 @@ pub fn parse(matches: &clap::ArgMatches) -> Result<Config, String> {
}; };
let connect = match matches.value_of("connect") { let connect = match matches.value_of("connect") {
Some(s) => Some(try!(s.parse().map_err(|_| "Invalid connect".to_owned()))), Some(s) => Some(try!(match s.parse::<net::SocketAddr>() {
Err(_) => s.parse::<net::IpAddr>()
.map(|ip| net::SocketAddr::new(ip, magic.port()))
.map_err(|_| "Invalid connect".to_owned()),
Ok(a) => Ok(a),
})),
None => None, None => None,
}; };