diff --git a/src/accountant_skel.rs b/src/accountant_skel.rs index 2f82f6441..6819a1223 100644 --- a/src/accountant_skel.rs +++ b/src/accountant_skel.rs @@ -13,12 +13,14 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; use std::thread::{spawn, JoinHandle}; use std::default::Default; +use std::io::Write; use serde_json; -pub struct AccountantSkel { +pub struct AccountantSkel { pub acc: Accountant, pub last_id: Hash, pub ledger: Vec, + writer: W, } #[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))] @@ -37,20 +39,21 @@ pub enum Response { Id { id: Hash, is_last: bool }, } -impl AccountantSkel { - pub fn new(acc: Accountant) -> Self { +impl AccountantSkel { + pub fn new(acc: Accountant, w: W) -> Self { let last_id = acc.first_id; AccountantSkel { acc, last_id, ledger: vec![], + writer: w, } } - pub fn sync(self: &mut Self) -> Hash { + pub fn sync(&mut self) -> Hash { while let Ok(entry) = self.acc.historian.receiver.try_recv() { self.last_id = entry.id; - println!("{}", serde_json::to_string(&entry).unwrap()); + write!(self.writer, "{}", serde_json::to_string(&entry).unwrap()).unwrap(); self.ledger.push(entry); } self.last_id @@ -131,7 +134,7 @@ impl AccountantSkel { /// UDP Server that forwards messages to Accountant methods. pub fn serve( - obj: Arc>, + obj: Arc>>, addr: &str, exit: Arc, ) -> Result>> { diff --git a/src/accountant_stub.rs b/src/accountant_stub.rs index 8bed5a20c..5bef39445 100644 --- a/src/accountant_stub.rs +++ b/src/accountant_stub.rs @@ -128,6 +128,7 @@ mod tests { use signature::{KeyPair, KeyPairUtil}; use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicBool, Ordering}; + use std::io::sink; #[test] fn test_accountant_stub() { @@ -137,7 +138,7 @@ mod tests { let acc = Accountant::new(&alice, Some(30)); let bob_pubkey = KeyPair::new().pubkey(); let exit = Arc::new(AtomicBool::new(false)); - let acc = Arc::new(Mutex::new(AccountantSkel::new(acc))); + let acc = Arc::new(Mutex::new(AccountantSkel::new(acc, sink()))); let threads = AccountantSkel::serve(acc, addr, exit.clone()).unwrap(); sleep(Duration::from_millis(30)); diff --git a/src/bin/testnode.rs b/src/bin/testnode.rs index 43b397d19..c8b2b51b6 100644 --- a/src/bin/testnode.rs +++ b/src/bin/testnode.rs @@ -3,7 +3,7 @@ extern crate silk; use silk::accountant_skel::AccountantSkel; use silk::accountant::Accountant; -use std::io::{self, BufRead}; +use std::io::{self, stdout, BufRead}; use std::sync::{Arc, Mutex}; use std::sync::atomic::AtomicBool; @@ -16,7 +16,7 @@ fn main() { .map(|line| serde_json::from_str(&line.unwrap()).unwrap()); let acc = Accountant::new_from_entries(entries, Some(1000)); let exit = Arc::new(AtomicBool::new(false)); - let skel = Arc::new(Mutex::new(AccountantSkel::new(acc))); + let skel = Arc::new(Mutex::new(AccountantSkel::new(acc, stdout()))); eprintln!("Listening on {}", addr); let threads = AccountantSkel::serve(skel, addr, exit.clone()).unwrap(); for t in threads {