Skip to content

Instantly share code, notes, and snippets.

@daschl
Created June 19, 2018 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daschl/c8c0230f4760937bdd7e1cd5947b3825 to your computer and use it in GitHub Desktop.
Save daschl/c8c0230f4760937bdd7e1cd5947b3825 to your computer and use it in GitHub Desktop.
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