From 5aacf6ea3871b72042ac7ca840dc83c9a3a752e8 Mon Sep 17 00:00:00 2001 From: debris Date: Fri, 30 Sep 2016 16:25:28 +0200 Subject: [PATCH] cli port option and parsing to config --- p2p/src/connect.rs | 1 + pbtc/cli.yml | 5 +++++ pbtc/main.rs | 33 ++++++++++++++++++++++++++------- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/p2p/src/connect.rs b/p2p/src/connect.rs index 14f4b4bf..18ecb9c8 100644 --- a/p2p/src/connect.rs +++ b/p2p/src/connect.rs @@ -16,6 +16,7 @@ pub struct Connection where A: io::Read + io::Write { pub struct Config { magic: Magic, + port: u16, services: ServiceFlags, user_agent: String, start_height: i32, diff --git a/pbtc/cli.yml b/pbtc/cli.yml index 740ca6c6..3ca9eab5 100644 --- a/pbtc/cli.yml +++ b/pbtc/cli.yml @@ -18,4 +18,9 @@ args: value_name: IP help: Connect to a node to retrieve peer addresses, and disconnect takes_value: true + - port: + long: port + value_name: PORT + help: Listen for connections on PORT + takes_value: true diff --git a/pbtc/main.rs b/pbtc/main.rs index e0124393..1f218c2f 100644 --- a/pbtc/main.rs +++ b/pbtc/main.rs @@ -16,17 +16,36 @@ use net::common::Magic; fn main() { let yaml = load_yaml!("cli.yml"); let matches = clap::App::from_yaml(yaml).get_matches(); + let _cfg = parse(&matches); +} - let _magic = match matches.is_present("testnet") { +struct Config<'a> { + magic: Magic, + port: u16, + connect: Option<&'a str>, + seednode: Option<&'a str>, +} + +fn parse<'a>(matches: &'a clap::ArgMatches<'a>) -> Result, String> { + let magic = match matches.is_present("testnet") { true => Magic::Testnet, false => Magic::Mainnet, }; - if let Some(ip) = matches.value_of("connect") { - println!("Connecting to ip: {}", ip); - } + let port = match matches.value_of("port") { + Some(port) => try!(port.parse().map_err(|_| "Invalid port".to_owned())), + None => match magic { + Magic::Mainnet => 8333, + Magic::Testnet => 18333, + }, + }; - if let Some(ip) = matches.value_of("seednode") { - println!("Seednode ip: {}", ip); - } + let config = Config { + magic: magic, + port: port, + connect: matches.value_of("connect"), + seednode: matches.value_of("seednode"), + }; + + Ok(config) }