feature(sender,receiver): enabled local and relay transfer

actually a clean close of the sender application is still missing and
has to be included
This commit is contained in:
Patryk Hegenberg 2024-05-01 17:46:04 +02:00
parent 300f688111
commit 2262fd9f75
9 changed files with 144 additions and 61 deletions

View file

@ -47,17 +47,29 @@ use tokio_tungstenite::{
use tracing::{debug, error};
pub async fn start_receiver(relay: &str, name: &str) {
let room_id = http_client::download_info(relay, name).await.unwrap();
debug!("Got Room_ID from Server: {room_id}");
let res = http_client::download_info(relay, name).await.unwrap();
debug!("Got room_id from Server: {:?}", res);
let res_ip = res.ip + ":9000";
start_ws_com(relay, room_id.as_str()).await;
if let Err(e) = start_ws_com(res_ip.as_str(), res.room_id[0].as_str()).await {
debug!("Failed to connect local with first room_id: {e}");
if let Err(e) = start_ws_com(res_ip.as_str(), res.room_id[1].as_str()).await {
debug!("Failed to connect local with second room_id: {e}");
if let Err(e) = start_ws_com(relay, res.room_id[0].as_str()).await {
debug!("Failed to connect remote with first room_id: {e}");
if let Err(e) = start_ws_com(relay, res.room_id[1].as_str()).await {
error!("Failed to accomplish transfer with error: {e}");
}
}
}
}
}
pub async fn start_ws_com(relay: &str, name: &str) {
pub async fn start_ws_com(relay: &str, name: &str) -> Result<(), Box<dyn std::error::Error>> {
let url = String::from("ws://") + relay + "/ws";
let Ok(mut request) = url.into_client_request() else {
println!("Error: Failed to create request.");
return;
return Err("Failed to create request".into());
};
// Insert the origin into the request headers to prevent
@ -68,11 +80,6 @@ pub async fn start_ws_com(relay: &str, name: &str) {
println!("Attempting to connect...");
// 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:?}"),
@ -81,4 +88,5 @@ pub async fn start_ws_com(relay: &str, name: &str) {
// receiver::client module and is the function that interacts with
// the server to receive files.
// receiver::start(socket, name).await
Ok(())
}