2020-10-27 03:27:25 +00:00
|
|
|
use std::{
|
2020-10-27 03:58:50 +00:00
|
|
|
convert::Infallible,
|
2020-10-27 03:27:25 +00:00
|
|
|
error::Error,
|
2020-10-28 01:39:02 +00:00
|
|
|
path::PathBuf,
|
2020-10-27 03:27:25 +00:00
|
|
|
sync::Arc,
|
2020-10-27 03:58:50 +00:00
|
|
|
time::Duration,
|
2020-10-27 03:27:25 +00:00
|
|
|
};
|
2020-10-27 02:49:12 +00:00
|
|
|
|
|
|
|
use hyper::{
|
|
|
|
StatusCode,
|
|
|
|
Uri,
|
|
|
|
};
|
2020-10-27 03:58:50 +00:00
|
|
|
use reqwest::{
|
|
|
|
Body,
|
|
|
|
Client,
|
|
|
|
};
|
|
|
|
use tokio::{
|
|
|
|
fs::File,
|
|
|
|
io::AsyncReadExt,
|
|
|
|
sync::mpsc::{
|
|
|
|
channel,
|
|
|
|
},
|
|
|
|
time::delay_for,
|
|
|
|
};
|
2020-10-27 02:49:12 +00:00
|
|
|
|
2020-10-28 01:20:06 +00:00
|
|
|
use ptth::http_serde::*;
|
|
|
|
|
2020-10-27 02:49:12 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main () -> Result <(), Box <dyn Error>> {
|
2020-10-27 03:27:25 +00:00
|
|
|
let client = Arc::new (Client::new ());
|
2020-10-27 02:49:12 +00:00
|
|
|
|
2020-10-27 03:58:50 +00:00
|
|
|
let mut backoff_delay = 0;
|
2020-10-27 02:49:12 +00:00
|
|
|
|
|
|
|
loop {
|
2020-10-27 03:58:50 +00:00
|
|
|
if backoff_delay > 0 {
|
|
|
|
delay_for (Duration::from_millis (backoff_delay)).await;
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:49:12 +00:00
|
|
|
let _uri = Uri::builder ()
|
|
|
|
.scheme ("http")
|
|
|
|
.authority ("127.0.0.1:4000")
|
2020-10-27 03:27:25 +00:00
|
|
|
.path_and_query ("/http_listen/alien_wildlands")
|
2020-10-27 02:49:12 +00:00
|
|
|
.build ().unwrap ();
|
|
|
|
|
2020-10-27 03:27:25 +00:00
|
|
|
let req_req = client.get ("http://127.0.0.1:4000/http_listen/alien_wildlands");
|
|
|
|
|
2020-10-28 01:20:06 +00:00
|
|
|
//println! ("Step 1");
|
2020-10-27 03:27:25 +00:00
|
|
|
let req_resp = match req_req.send ().await {
|
2020-10-27 02:49:12 +00:00
|
|
|
Err (e) => {
|
|
|
|
println! ("Err: {:?}", e);
|
2020-10-27 03:58:50 +00:00
|
|
|
backoff_delay = backoff_delay * 2 + 500;
|
2020-10-27 02:49:12 +00:00
|
|
|
continue;
|
|
|
|
},
|
2020-10-27 03:58:50 +00:00
|
|
|
Ok (r) => {
|
|
|
|
backoff_delay = 0;
|
|
|
|
r
|
|
|
|
},
|
2020-10-27 02:49:12 +00:00
|
|
|
};
|
|
|
|
|
2020-10-27 03:27:25 +00:00
|
|
|
if req_resp.status () != StatusCode::OK {
|
2020-10-27 02:49:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-27 03:27:25 +00:00
|
|
|
println! ("Step 3");
|
|
|
|
|
|
|
|
let body = req_resp.bytes ().await?;
|
2020-10-28 01:20:06 +00:00
|
|
|
let parts: RequestParts = match rmp_serde::from_read_ref (&body)
|
|
|
|
{
|
|
|
|
Ok (x) => x,
|
|
|
|
_ => continue,
|
|
|
|
};
|
2020-10-27 02:49:12 +00:00
|
|
|
|
2020-10-28 01:39:02 +00:00
|
|
|
let (req_id, uri) = (parts.id, parts.uri);
|
|
|
|
|
|
|
|
println! ("Client requested {}", uri);
|
2020-10-27 03:27:25 +00:00
|
|
|
|
|
|
|
println! ("Step 4/5");
|
|
|
|
|
|
|
|
println! ("Step 6");
|
|
|
|
let client = client.clone ();
|
|
|
|
tokio::spawn (async move {
|
2020-10-27 03:58:50 +00:00
|
|
|
let (tx, rx) = channel (2);
|
|
|
|
//let rx: Receiver <Vec <u8>> = rx;
|
|
|
|
|
|
|
|
tokio::spawn (async move {
|
2020-10-28 01:39:02 +00:00
|
|
|
//let path = "/home/user/projects/2020/ptth/README.md";
|
|
|
|
|
|
|
|
let mut path = PathBuf::from ("/home/user");
|
|
|
|
path.push (&uri [1..]);
|
|
|
|
|
|
|
|
println! ("Opening file {:?}", path);
|
2020-10-28 01:20:06 +00:00
|
|
|
|
2020-10-27 03:58:50 +00:00
|
|
|
let mut f = File::open (path).await.unwrap ();
|
|
|
|
let mut tx = tx;
|
2020-10-27 13:25:21 +00:00
|
|
|
let mut bytes_sent = 0;
|
2020-10-27 03:58:50 +00:00
|
|
|
|
|
|
|
loop {
|
2020-10-27 13:25:21 +00:00
|
|
|
let mut buffer = vec! [0u8; 4096];
|
2020-10-27 03:58:50 +00:00
|
|
|
let bytes_read = f.read (&mut buffer).await.unwrap ();
|
|
|
|
|
|
|
|
buffer.truncate (bytes_read);
|
|
|
|
|
|
|
|
if bytes_read == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-28 01:20:06 +00:00
|
|
|
tx.send (Ok::<_, Infallible> (buffer)).await.unwrap ();
|
2020-10-27 13:25:21 +00:00
|
|
|
bytes_sent += bytes_read;
|
|
|
|
|
|
|
|
println! ("Sent {} bytes", bytes_sent);
|
|
|
|
|
|
|
|
delay_for (Duration::from_millis (50)).await;
|
2020-10-27 03:58:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-28 01:39:02 +00:00
|
|
|
let resp_req = client.post (&format! ("http://127.0.0.1:4000/http_response/{}", req_id)).body (Body::wrap_stream (rx));
|
2020-10-27 03:27:25 +00:00
|
|
|
|
|
|
|
println! ("Step 6");
|
2020-10-28 01:39:02 +00:00
|
|
|
if let Err (e) = resp_req.send ().await {
|
|
|
|
println! ("Err: {:?}", e);
|
2020-10-27 03:27:25 +00:00
|
|
|
}
|
|
|
|
});
|
2020-10-27 02:49:12 +00:00
|
|
|
}
|
|
|
|
}
|