feature(receiver): adapted receiver to work with a simple generated name

This commit is contained in:
Patryk Hegenberg 2024-04-28 23:55:55 +02:00
parent 8b62159762
commit 9f7b95ca98
3 changed files with 49 additions and 9 deletions

View file

@ -17,7 +17,9 @@ path = "src/shuttle.rs"
futures-util = "0.3"
tungstenite = "0.21.0"
tokio = { version = "1.28.1", features = ["full"] }
tokio-tungstenite = { version = "0.21.0", features =["rustls-tls-webpki-roots"] }
tokio-tungstenite = { version = "0.21.0", features = [
"rustls-tls-webpki-roots",
] }
serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
uuid = { version = "1.3.2", features = ["v4"] }
@ -47,8 +49,8 @@ tower = { version = "0.4", features = ["util"] }
shuttle-axum = { version = "0.44.0" }
shuttle-runtime = { version = "0.44.0" }
dotenvy = "0.15.7"
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
hex = "0.4.3"
reqwest = { version = "0.12.4", features = ["json", "blocking"] }
[build-dependencies]
prost-build = "0.12.4"

View file

@ -0,0 +1,25 @@
use hex;
use reqwest;
use sha2::{Digest, Sha256};
use tracing::error;
use crate::relay::transfer::Transfer;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub async fn download_info(relay: &str, name: &str) -> Result<String> {
let url = String::from("http://") + relay;
let hashed_name = Sha256::digest(name.as_bytes());
let hashed_string = hex::encode(hashed_name);
match reqwest::get(format!("{}/download/{}", url, hashed_string)).await {
Ok(resp) => match resp.json::<Transfer>().await {
Ok(res) => Ok(res.room_id),
Err(e) => Err(Box::new(e)),
},
Err(err) => {
error!("Error: {err}");
Err(Box::new(err))
}
}
}

View file

@ -36,6 +36,7 @@
/// The `start` function takes ownership of the WebSocket stream and
/// the file paths, so we pass them by value.
pub mod client;
pub mod http_client;
use crate::receiver::client as receiver;
@ -43,10 +44,18 @@ use tokio_tungstenite::{
connect_async,
tungstenite::{client::IntoClientRequest, http::HeaderValue},
};
use tracing::error;
use tracing::{debug, error};
pub async fn start_receiver(relay: &str, name: &str) {
let Ok(mut request) = relay.into_client_request() else {
let room_id = http_client::download_info(relay, name).await.unwrap();
debug!("Got Room_ID from Server: {room_id}");
start_ws_com(relay, room_id.as_str()).await;
}
pub async fn start_ws_com(relay: &str, name: &str) {
let url = String::from("ws://") + relay + "/ws";
let Ok(mut request) = url.into_client_request() else {
println!("Error: Failed to create request.");
return;
};
@ -59,13 +68,17 @@ pub async fn start_receiver(relay: &str, name: &str) {
println!("Attempting to connect...");
let Ok((socket, _)) = connect_async(request).await else {
error!("Error: Failed to connect.");
return;
};
// let Ok((socket, _)) = connect_async(request).await else {
// error!("Error: Failed to connect.");
// return;
// };
match connect_async(request).await {
Ok((socket, _)) => receiver::start(socket, name).await,
Err(e) => error!("Error: Failed to connect: {e:?}"),
};
// The start function is defined in the
// receiver::client module and is the function that interacts with
// the server to receive files.
receiver::start(socket, name).await
// receiver::start(socket, name).await
}