Created
June 19, 2018 05:53
-
-
Save daschl/c8c0230f4760937bdd7e1cd5947b3825 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use actix::prelude::*; | |
use futures::stream::once; | |
use futures::sync::mpsc; | |
use message::RawLineMessage; | |
use std::fs::File; | |
use std::io; | |
use std::io::{BufRead, BufReader}; | |
use std::thread; | |
pub struct FileReader<T: StreamHandler<RawLineMessage, io::Error>> { | |
target: T, | |
path: String, | |
} | |
impl<T: StreamHandler<RawLineMessage, io::Error>> FileReader<T> { | |
pub fn new(path: &str, target: T) -> Self { | |
FileReader { | |
path: path.into(), | |
target, | |
} | |
} | |
} | |
impl<T: StreamHandler<RawLineMessage, io::Error>> Actor for FileReader<T> { | |
type Context = Context<Self>; | |
fn started(&mut self, ctx: &mut Self::Context) { | |
let file = File::open(&self.path).unwrap(); | |
let reader = BufReader::new(file); | |
let (tx, rx) = mpsc::unbounded(); | |
let child = thread::spawn(move || { | |
for line in reader.lines() { | |
tx.unbounded_send(RawLineMessage(line.expect("error decoding line"))) | |
.expect("error doing unbounded send"); | |
} | |
}); | |
StreamHandler::add_stream(rx, ctx); // <-- this should actuall be T target? | |
//self.target.add_stream(rx, ctx); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment