Send and receive channels

This commit is contained in:
Aditya Kulkarni
2019-09-15 15:38:02 -07:00
committed by adityapk00
parent 84f21dcd5b
commit 194c6b2780
3 changed files with 43 additions and 19 deletions

View File

@@ -48,7 +48,7 @@ impl LightClient {
}
pub fn new(seed_phrase: Option<&str>) -> io::Result<Self> {
pub fn new(seed_phrase: Option<String>) -> io::Result<Self> {
let mut lc = if Path::new("wallet.dat").exists() {
// Make sure that if a wallet exists, there is no seed phrase being attempted
if !seed_phrase.is_none() {
@@ -150,7 +150,7 @@ impl LightClient {
println!("ERR = {:?}", e);
});
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap()
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap();
}
pub fn do_seed_phrase(&self) -> String {
@@ -668,7 +668,7 @@ impl LightClient {
println!("ERR = {:?}", e);
});
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap()
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap();
}
pub fn broadcast_raw_tx(&self, tx_bytes: Box<[u8]>) {
@@ -686,7 +686,7 @@ impl LightClient {
println!("ERR = {:?}", e);
});
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap()
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap();
}
pub fn fetch_latest_block<F : 'static + std::marker::Send>(&self, mut c : F)
@@ -705,7 +705,7 @@ impl LightClient {
println!("ERR = {:?}", e);
});
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap()
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap();
}
fn make_grpc_client(&self, uri: http::Uri) -> Result<Box<dyn Future<Item=Client, Error=tower_grpc::Status> + Send>, Box<dyn Error>> {

View File

@@ -566,7 +566,7 @@ impl LightWallet {
(extsk, extfvk, address)
}
pub fn new(seed_phrase: Option<&str>) -> io::Result<Self> {
pub fn new(seed_phrase: Option<String>) -> io::Result<Self> {
use rand::{FromEntropy, ChaChaRng, Rng};
let mut seed_bytes = [0u8; 32];

View File

@@ -28,15 +28,32 @@ pub fn main() {
.takes_value(true))
.get_matches();
let mut lightclient = match LightClient::new(matches.value_of("seed")) {
Ok(lc) => lc,
Err(e) => {
eprintln!("Failed to start wallet. Error was:\n{}", e);
return;
}
};
let seed: Option<String> = matches.value_of("seed").map(|s| s.to_string());
println!("Starting Light Client");
let (command_tx, command_rx) = std::sync::mpsc::channel::<(String, Vec<String>)>();
let (resp_tx, resp_rx) = std::sync::mpsc::channel::<String>();
std::thread::spawn(move || {
let mut lightclient = match LightClient::new(seed) {
Ok(lc) => lc,
Err(e) => { eprintln!("Failed to start wallet. Error was:\n{}", e); return; }
};
println!("Starting Light Client");
loop {
match command_rx.recv() {
Ok((cmd, args)) => {
let args = args.iter().map(|s| s.as_ref()).collect();
commands::do_user_command(&cmd, &args, &mut lightclient);
resp_tx.send("Finished command".to_string()).unwrap();
},
_ => {}
}
}
println!("finished running");
});
// `()` can be used when no completer is required
let mut rl = Editor::<()>::new();
@@ -45,7 +62,7 @@ pub fn main() {
println!("Ready!");
loop {
let readline = rl.readline(&format!("Block:{} (type 'help') >> ", lightclient.last_scanned_height()));
let readline = rl.readline(">>"); //&format!("Block:{} (type 'help') >> ", lightclient.last_scanned_height()));
match readline {
Ok(line) => {
rl.add_history_entry(line.as_str());
@@ -63,8 +80,15 @@ pub fn main() {
}
let cmd = cmd_args.remove(0);
let args: Vec<&str> = cmd_args.iter().map(|s| s.as_ref()).collect();
commands::do_user_command(&cmd, &args, &mut lightclient);
let args: Vec<String> = cmd_args;
// commands::do_user_command(&cmd, &args, &mut lightclient);
command_tx.send((cmd, args)).unwrap();
// Wait for the response
match resp_rx.recv() {
Ok(response) => println!("{}", response),
_ => { eprintln!("Error receiveing response");}
}
// Special check for Quit command.
if line == "quit" {
@@ -73,12 +97,12 @@ pub fn main() {
},
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
lightclient.do_save();
//lightclient.do_save();
break
},
Err(ReadlineError::Eof) => {
println!("CTRL-D");
lightclient.do_save();
//lightclient.do_save();
break
},
Err(err) => {