fix writer
This commit is contained in:
		
				
					committed by
					
						
						Greg Fitzgerald
					
				
			
			
				
	
			
			
			
						parent
						
							112aecf6eb
						
					
				
				
					commit
					14239e584f
				
			@@ -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<W: Write + Send + 'static> {
 | 
			
		||||
    pub acc: Accountant,
 | 
			
		||||
    pub last_id: Hash,
 | 
			
		||||
    pub ledger: Vec<Entry>,
 | 
			
		||||
    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<W: Write + Send + 'static> AccountantSkel<W> {
 | 
			
		||||
    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<Mutex<AccountantSkel>>,
 | 
			
		||||
        obj: Arc<Mutex<AccountantSkel<W>>>,
 | 
			
		||||
        addr: &str,
 | 
			
		||||
        exit: Arc<AtomicBool>,
 | 
			
		||||
    ) -> Result<Vec<JoinHandle<()>>> {
 | 
			
		||||
 
 | 
			
		||||
@@ -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));
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user