|
|
|
@ -1,87 +1,23 @@ |
|
|
|
use http::HeaderMap;
|
|
|
|
use log::{error, info};
|
|
|
|
use tokio::select;
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
error::ClientActorError,
|
|
|
|
message::{ClientActorMessage, ClientActorMessageHandle},
|
|
|
|
ClientActor,
|
|
|
|
ClientTaskResult,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async fn process_next_result(mut actor: ClientActor, result: ClientTaskResult) -> ClientActor {
|
|
|
|
use ClientActorMessageHandle::{Download, GetData};
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Err(mut e) => {
|
|
|
|
info!("Retry failed download: {:?}", e);
|
|
|
|
// retry ... instead of responing here we could also respond
|
|
|
|
// with something that in turn would be used to retry...
|
|
|
|
let mut client = actor.client.clone();
|
|
|
|
actor.tasks.spawn(async move {
|
|
|
|
match e.action {
|
|
|
|
Download { .. } => {
|
|
|
|
let result = client.download( e.action.filename()
|
|
|
|
, &e.action.uri()
|
|
|
|
, &HeaderMap::new()).await;
|
|
|
|
match result {
|
|
|
|
Err(source) =>
|
|
|
|
Err(ClientActorError::new( &e.action
|
|
|
|
, source.into() )),
|
|
|
|
Ok(state) => {
|
|
|
|
e.action.set_state(state);
|
|
|
|
Ok(Some(e.action))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
GetData { .. } => {
|
|
|
|
let result = client.data(&e.action.uri(), &HeaderMap::new()).await;
|
|
|
|
match result {
|
|
|
|
Err(source) =>
|
|
|
|
Err(ClientActorError::new( &e.action
|
|
|
|
, source.into() )),
|
|
|
|
Ok(data) => {
|
|
|
|
*e.action.buffer_mut() = Some(data);
|
|
|
|
Ok(Some(e.action))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Err(e) => {
|
|
|
|
info!("Aborted download: {:?}", e);
|
|
|
|
actor.respond_action_err(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
// when the task finishes
|
|
|
|
Ok(Some(action)) => {
|
|
|
|
match action {
|
|
|
|
Download { ref uri, ref state, ref message, .. } => {
|
|
|
|
info!("Done download: {:?}", uri);
|
|
|
|
if let Some((_, message)) = actor.actions.remove_entry(message) {
|
|
|
|
match message {
|
|
|
|
ClientActorMessage::Download { respond_to, .. } => {
|
|
|
|
let _ = respond_to.send(Ok(state.clone()));
|
|
|
|
},
|
|
|
|
_ => panic!("Wrong variant ... this should never happen"),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic!("Lost a message");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
GetData { ref uri, ref buffer, ref message } => {
|
|
|
|
info!("Done get_data: {:?}", uri);
|
|
|
|
if let Some((_, message)) = actor.actions.remove_entry(message) {
|
|
|
|
match message {
|
|
|
|
ClientActorMessage::GetData { respond_to, .. } => {
|
|
|
|
let _ = respond_to.send(buffer.clone());
|
|
|
|
},
|
|
|
|
_ => panic!("Wrong variant ... this should never happen"),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic!("Lost a message");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
info!("Done download: {:?}", action);
|
|
|
|
actor.respond_action_ok(&action);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Got a stop message...here we still continue procession until the
|
|
|
|
|