From e1822495a64f099d2f76508a8366e2522bb1ef8a Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 17 Dec 2019 15:04:25 +0000 Subject: [PATCH] socket clients: setting up with id and local parameters --- src/commands/run.rs | 16 +++------------- src/commands/tcpsocket.rs | 27 +++++++++++++++++++++++++-- src/commands/websocket.rs | 12 ++++++++++-- src/main.rs | 25 +++++++++++++++++++++++-- src/persistence/pemstore.rs | 7 +++++++ src/sockets/mod.rs | 1 + src/sockets/tcp.rs | 5 +++++ 7 files changed, 74 insertions(+), 19 deletions(-) diff --git a/src/commands/run.rs b/src/commands/run.rs index fa9d16728b..dc19422b96 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -1,26 +1,16 @@ use crate::banner; use crate::clients::NymClient; -use crate::identity::mixnet; -use crate::persistence::pathfinder::Pathfinder; -use crate::persistence::pemstore::PemStore; +use crate::persistence::pemstore; use clap::ArgMatches; pub fn execute(matches: &ArgMatches) { println!("{}", banner()); let is_local = matches.is_present("local"); - let id = matches.value_of("id").unwrap().to_string(); - println!("Starting client, local: {:?}", is_local); + println!("Starting client..."); - let keypair = read_keypair_from_disk(id); + let keypair = pemstore::read_keypair_from_disk(id); let client = NymClient::new(keypair.public_bytes(), is_local); client.start().unwrap(); } - -fn read_keypair_from_disk(id: String) -> mixnet::KeyPair { - let pathfinder = Pathfinder::new(id); - let pem_store = PemStore::new(pathfinder); - let keypair = pem_store.read(); - keypair -} diff --git a/src/commands/tcpsocket.rs b/src/commands/tcpsocket.rs index 79adfb29e6..1e3a21f1d1 100644 --- a/src/commands/tcpsocket.rs +++ b/src/commands/tcpsocket.rs @@ -1,10 +1,33 @@ +use crate::banner; +use crate::clients::NymClient; +use crate::persistence::pemstore; +use crate::sockets::tcp; + use clap::ArgMatches; +use std::net::ToSocketAddrs; pub fn execute(matches: &ArgMatches) { - let port = match matches.value_of("port").unwrap().parse::() { + println!("{}", banner()); + + let is_local = matches.is_present("local"); + let id = matches.value_of("id").unwrap().to_string(); + let port = match matches.value_of("port").unwrap_or("9001").parse::() { Ok(n) => n, Err(err) => panic!("Invalid port value provided - {:?}", err), }; - println!("On the following port: {:?}", port); + println!("Starting TCP socket on port: {:?}", port); + println!("Listening for messages..."); + + let socket_address = ("127.0.0.1", port) + .to_socket_addrs() + .expect("Failed to combine host and port") + .next() + .expect("Failed to extract the socket address from the iterator"); + + let keypair = pemstore::read_keypair_from_disk(id); + let _client = NymClient::new(keypair.public_bytes(), is_local); + // Question: should we be passing the client into the TCP socket somehow next? + + tcp::start(socket_address); } diff --git a/src/commands/websocket.rs b/src/commands/websocket.rs index dcf80afc06..cdad955d2a 100644 --- a/src/commands/websocket.rs +++ b/src/commands/websocket.rs @@ -1,17 +1,21 @@ use crate::banner; +use crate::clients::NymClient; +use crate::persistence::pemstore; use crate::sockets::ws; + use clap::ArgMatches; use std::net::ToSocketAddrs; pub fn execute(matches: &ArgMatches) { println!("{}", banner()); - let port = match matches.value_of("port").unwrap().parse::() { + let is_local = matches.is_present("local"); + let id = matches.value_of("id").unwrap().to_string(); + let port = match matches.value_of("port").unwrap_or("9001").parse::() { Ok(n) => n, Err(err) => panic!("Invalid port value provided - {:?}", err), }; - println!("{}", banner()); println!("Starting websocket on port: {:?}", port); println!("Listening for messages..."); @@ -21,5 +25,9 @@ pub fn execute(matches: &ArgMatches) { .next() .expect("Failed to extract the socket address from the iterator"); + let keypair = pemstore::read_keypair_from_disk(id); + let _client = NymClient::new(keypair.public_bytes(), is_local); + // Question: should we be passing the client into the websocket somehow next? + ws::start(socket_address); } diff --git a/src/main.rs b/src/main.rs index 2c45340ff3..c6ae48c2f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,9 +50,19 @@ fn main() { Arg::with_name("port") .short("p") .long("port") - .help("Port to listen on") + .help("Port for TCP socket to listen on") .takes_value(true) .required(true), + ).arg(Arg::with_name("local") + .long("local") + .help("Flag to indicate whether the client is expected to run on the local deployment.") + .takes_value(false) + ) + .arg(Arg::with_name("id") + .long("id") + .help("Id of the nym-mixnet-client we want to run.") + .takes_value(true) + .required(true) ) ) .subcommand( @@ -62,10 +72,21 @@ fn main() { Arg::with_name("port") .short("p") .long("port") - .help("Port to listen on") + .help("Port for websocket to listen on") .takes_value(true) .required(true), ) + .arg(Arg::with_name("local") + .long("local") + .help("Flag to indicate whether the client is expected to run on the local deployment.") + .takes_value(false) + ) + .arg(Arg::with_name("id") + .long("id") + .help("Id of the nym-mixnet-client we want to run.") + .takes_value(true) + .required(true) + ) ) .get_matches(); diff --git a/src/persistence/pemstore.rs b/src/persistence/pemstore.rs index 02c1f5db00..037c6ab58a 100644 --- a/src/persistence/pemstore.rs +++ b/src/persistence/pemstore.rs @@ -5,6 +5,13 @@ use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; +pub fn read_keypair_from_disk(id: String) -> KeyPair { + let pathfinder = Pathfinder::new(id); + let pem_store = PemStore::new(pathfinder); + let keypair = pem_store.read(); + keypair +} + pub struct PemStore { config_dir: PathBuf, private_mix_key: PathBuf, diff --git a/src/sockets/mod.rs b/src/sockets/mod.rs index 6757c99679..86dd43f872 100644 --- a/src/sockets/mod.rs +++ b/src/sockets/mod.rs @@ -1 +1,2 @@ +pub mod tcp; pub mod ws; diff --git a/src/sockets/tcp.rs b/src/sockets/tcp.rs index e69de29bb2..a9475ab1cb 100644 --- a/src/sockets/tcp.rs +++ b/src/sockets/tcp.rs @@ -0,0 +1,5 @@ +use std::net::SocketAddr; + +pub fn start(_socket_address: SocketAddr) { + println!("TCP server time!!!"); +}