feature(receiver): adapted receiver to work with a simple generated name
This commit is contained in:
parent
8b62159762
commit
9f7b95ca98
3 changed files with 49 additions and 9 deletions
|
|
@ -17,7 +17,9 @@ path = "src/shuttle.rs"
|
||||||
futures-util = "0.3"
|
futures-util = "0.3"
|
||||||
tungstenite = "0.21.0"
|
tungstenite = "0.21.0"
|
||||||
tokio = { version = "1.28.1", features = ["full"] }
|
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_json = { version = "1.0" }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
uuid = { version = "1.3.2", features = ["v4"] }
|
uuid = { version = "1.3.2", features = ["v4"] }
|
||||||
|
|
@ -47,8 +49,8 @@ tower = { version = "0.4", features = ["util"] }
|
||||||
shuttle-axum = { version = "0.44.0" }
|
shuttle-axum = { version = "0.44.0" }
|
||||||
shuttle-runtime = { version = "0.44.0" }
|
shuttle-runtime = { version = "0.44.0" }
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
|
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
reqwest = { version = "0.12.4", features = ["json", "blocking"] }
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
prost-build = "0.12.4"
|
prost-build = "0.12.4"
|
||||||
|
|
|
||||||
25
src/receiver/http_client.rs
Normal file
25
src/receiver/http_client.rs
Normal 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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -36,6 +36,7 @@
|
||||||
/// The `start` function takes ownership of the WebSocket stream and
|
/// The `start` function takes ownership of the WebSocket stream and
|
||||||
/// the file paths, so we pass them by value.
|
/// the file paths, so we pass them by value.
|
||||||
pub mod client;
|
pub mod client;
|
||||||
|
pub mod http_client;
|
||||||
|
|
||||||
use crate::receiver::client as receiver;
|
use crate::receiver::client as receiver;
|
||||||
|
|
||||||
|
|
@ -43,10 +44,18 @@ use tokio_tungstenite::{
|
||||||
connect_async,
|
connect_async,
|
||||||
tungstenite::{client::IntoClientRequest, http::HeaderValue},
|
tungstenite::{client::IntoClientRequest, http::HeaderValue},
|
||||||
};
|
};
|
||||||
use tracing::error;
|
use tracing::{debug, error};
|
||||||
|
|
||||||
pub async fn start_receiver(relay: &str, name: &str) {
|
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.");
|
println!("Error: Failed to create request.");
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
@ -59,13 +68,17 @@ pub async fn start_receiver(relay: &str, name: &str) {
|
||||||
|
|
||||||
println!("Attempting to connect...");
|
println!("Attempting to connect...");
|
||||||
|
|
||||||
let Ok((socket, _)) = connect_async(request).await else {
|
// let Ok((socket, _)) = connect_async(request).await else {
|
||||||
error!("Error: Failed to connect.");
|
// error!("Error: Failed to connect.");
|
||||||
return;
|
// 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
|
// The start function is defined in the
|
||||||
// receiver::client module and is the function that interacts with
|
// receiver::client module and is the function that interacts with
|
||||||
// the server to receive files.
|
// the server to receive files.
|
||||||
receiver::start(socket, name).await
|
// receiver::start(socket, name).await
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue