Removing on_done

This commit is contained in:
Tomasz Drwięga 2016-12-15 11:38:05 +01:00
parent 03db4d0373
commit c6912c8e0a
2 changed files with 90 additions and 97 deletions

View File

@ -120,7 +120,7 @@ impl<R: URLHint> ContentFetcher<R> {
// Content is already being fetched // Content is already being fetched
Some(&mut ContentStatus::Fetching(ref fetch_control)) => { Some(&mut ContentStatus::Fetching(ref fetch_control)) => {
trace!(target: "dapps", "Content fetching in progress. Waiting..."); trace!(target: "dapps", "Content fetching in progress. Waiting...");
(None, fetch_control.to_handler(control)) (None, fetch_control.to_async_handler(path, control))
}, },
// We need to start fetching the content // We need to start fetching the content
None => { None => {
@ -129,11 +129,12 @@ impl<R: URLHint> ContentFetcher<R> {
let content = self.resolver.resolve(content_hex); let content = self.resolver.resolve(content_hex);
let cache = self.cache.clone(); let cache = self.cache.clone();
let on_done = move |id: String, result: Option<LocalPageEndpoint>| { let id = content_id.clone();
let on_done = move |result: Option<LocalPageEndpoint>| {
let mut cache = cache.lock(); let mut cache = cache.lock();
match result { match result {
Some(endpoint) => { Some(endpoint) => {
cache.insert(id, ContentStatus::Ready(endpoint)); cache.insert(id.clone(), ContentStatus::Ready(endpoint));
}, },
// In case of error // In case of error
None => { None => {
@ -248,13 +249,14 @@ struct ContentInstaller {
id: String, id: String,
mime: String, mime: String,
content_path: PathBuf, content_path: PathBuf,
on_done: Box<Fn(String, Option<LocalPageEndpoint>) + Send>, on_done: Box<Fn(Option<LocalPageEndpoint>) + Send>,
} }
impl ContentValidator for ContentInstaller { impl ContentValidator for ContentInstaller {
type Error = ValidationError; type Error = ValidationError;
fn validate_and_install(&self, path: PathBuf) -> Result<(String, LocalPageEndpoint), ValidationError> { fn validate_and_install(&self, path: PathBuf) -> Result<(String, LocalPageEndpoint), ValidationError> {
let validate = || {
// Create dir // Create dir
try!(fs::create_dir_all(&self.content_path)); try!(fs::create_dir_all(&self.content_path));
@ -279,12 +281,13 @@ impl ContentValidator for ContentInstaller {
} }
try!(fs::copy(&path, &content_path)); try!(fs::copy(&path, &content_path));
Ok(LocalPageEndpoint::single_file(content_path, self.mime.clone(), PageCache::Enabled))
};
Ok((self.id.clone(), LocalPageEndpoint::single_file(content_path, self.mime.clone(), PageCache::Enabled))) // Make sure to always call on_done (even in case of errors)!
} let result = validate();
(self.on_done)(result.as_ref().ok().cloned());
fn done(&self, endpoint: Option<LocalPageEndpoint>) { result.map(|endpoint| (self.id.clone(), endpoint))
(self.on_done)(self.id.clone(), endpoint)
} }
} }
@ -292,7 +295,7 @@ impl ContentValidator for ContentInstaller {
struct DappInstaller { struct DappInstaller {
id: String, id: String,
dapps_path: PathBuf, dapps_path: PathBuf,
on_done: Box<Fn(String, Option<LocalPageEndpoint>) + Send>, on_done: Box<Fn(Option<LocalPageEndpoint>) + Send>,
embeddable_on: Option<(String, u16)>, embeddable_on: Option<(String, u16)>,
} }
@ -333,6 +336,7 @@ impl ContentValidator for DappInstaller {
fn validate_and_install(&self, app_path: PathBuf) -> Result<(String, LocalPageEndpoint), ValidationError> { fn validate_and_install(&self, app_path: PathBuf) -> Result<(String, LocalPageEndpoint), ValidationError> {
trace!(target: "dapps", "Opening dapp bundle at {:?}", app_path); trace!(target: "dapps", "Opening dapp bundle at {:?}", app_path);
let validate = || {
let mut file_reader = io::BufReader::new(try!(fs::File::open(app_path))); let mut file_reader = io::BufReader::new(try!(fs::File::open(app_path)));
let hash = try!(sha3(&mut file_reader)); let hash = try!(sha3(&mut file_reader));
let id = try!(self.id.as_str().parse().map_err(|_| ValidationError::InvalidContentId)); let id = try!(self.id.as_str().parse().map_err(|_| ValidationError::InvalidContentId));
@ -384,16 +388,15 @@ impl ContentValidator for DappInstaller {
let manifest_path = target.join(MANIFEST_FILENAME); let manifest_path = target.join(MANIFEST_FILENAME);
let mut manifest_file = try!(fs::File::create(manifest_path)); let mut manifest_file = try!(fs::File::create(manifest_path));
try!(manifest_file.write_all(manifest_str.as_bytes())); try!(manifest_file.write_all(manifest_str.as_bytes()));
// Create endpoint // Create endpoint
let app = LocalPageEndpoint::new(target, manifest.clone().into(), PageCache::Enabled, self.embeddable_on.clone()); let endpoint = LocalPageEndpoint::new(target, manifest.clone().into(), PageCache::Enabled, self.embeddable_on.clone());
// Return modified app manifest // Return modified app manifest
Ok((manifest.id.clone(), app)) Ok(endpoint)
} };
fn done(&self, endpoint: Option<LocalPageEndpoint>) { let result = validate();
(self.on_done)(self.id.clone(), endpoint) (self.on_done)(result.as_ref().ok().cloned());
result.map(|endpoint| (self.id.clone(), endpoint))
} }
} }

View File

@ -29,6 +29,7 @@ use hyper::{server, Decoder, Encoder, Next, Method, Control};
use hyper::net::HttpStream; use hyper::net::HttpStream;
use hyper::status::StatusCode; use hyper::status::StatusCode;
use endpoint::EndpointPath;
use handlers::{ContentHandler, Redirection, extract_url}; use handlers::{ContentHandler, Redirection, extract_url};
use page::LocalPageEndpoint; use page::LocalPageEndpoint;
@ -45,7 +46,6 @@ pub trait ContentValidator {
type Error: fmt::Debug + fmt::Display; type Error: fmt::Debug + fmt::Display;
fn validate_and_install(&self, app: PathBuf) -> Result<(String, LocalPageEndpoint), Self::Error>; fn validate_and_install(&self, app: PathBuf) -> Result<(String, LocalPageEndpoint), Self::Error>;
fn done(&self, Option<LocalPageEndpoint>);
} }
pub struct FetchControl { pub struct FetchControl {
@ -88,7 +88,9 @@ impl FetchControl {
self.abort.store(true, Ordering::SeqCst); self.abort.store(true, Ordering::SeqCst);
} }
pub fn to_handler(&self, control: Control) -> Box<server::Handler<HttpStream> + Send> { pub fn to_async_handler(&self, path: EndpointPath, control: Control) -> Box<server::Handler<HttpStream> + Send> {
// TODO [ToDr] We should be able to pass EndpointPath to handler as well
// (request may be coming from different domain, etc)
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
self.listeners.lock().push((control, tx)); self.listeners.lock().push((control, tx));
@ -141,25 +143,13 @@ pub struct ContentFetcherHandler<H: ContentValidator> {
embeddable_on: Option<(String, u16)>, embeddable_on: Option<(String, u16)>,
} }
impl<H: ContentValidator> Drop for ContentFetcherHandler<H> {
fn drop(&mut self) {
let result = match self.status {
FetchState::Done(_, ref result, _) => Some(result.clone()),
_ => None,
};
self.installer.done(result);
}
}
impl<H: ContentValidator> ContentFetcherHandler<H> { impl<H: ContentValidator> ContentFetcherHandler<H> {
pub fn new( pub fn new(
url: String, url: String,
control: Control, control: Control,
handler: H, handler: H,
embeddable_on: Option<(String, u16)>, embeddable_on: Option<(String, u16)>,
) -> (Self, Arc<FetchControl>) { ) -> (Self, Arc<FetchControl>) {
let fetch_control = Arc::new(FetchControl::default()); let fetch_control = Arc::new(FetchControl::default());
let client = Client::default(); let client = Client::default();
let handler = ContentFetcherHandler { let handler = ContentFetcherHandler {