implemented receive and reset in sender

This commit is contained in:
ikeen0807 2024-05-15 23:40:45 +02:00
parent 96340622d8
commit 88abc6d3de
24 changed files with 240 additions and 73 deletions

View file

@ -40,6 +40,7 @@ headers = "0.4"
tower = { version = "0.4", features = ["util"] }
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
hex = "0.4.3"
anyhow = "1.0.83"
[build-dependencies]
prost-build = "0.12.4"

View file

@ -1,27 +1,22 @@
use anyhow::{anyhow, Result};
use hex;
use reqwest::{self, Client};
use sha2::{Digest, Sha256};
use tracing::error;
use crate::relay::transfer::TransferResponse;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub async fn download_info(relay: &str, name: &str) -> Result<TransferResponse> {
let url = String::from(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::<TransferResponse>().await {
Ok(res) => Ok(res),
Err(e) => Err(Box::new(e)),
},
Err(err) => {
error!("Error: {err}");
Err(Box::new(err))
}
}
let resp = reqwest::get(format!("{}/download/{}", url, hashed_string))
.await
.map_err(|e| anyhow!("Failed to send GET request: {}", e))?;
resp.json::<TransferResponse>()
.await
.map_err(|e| anyhow!("Failed to parse JSON response: {}", e))
}
pub async fn download_success(relay: &str, name: &str) -> Result<()> {
@ -33,6 +28,8 @@ pub async fn download_success(relay: &str, name: &str) -> Result<()> {
let _ = client
.post(format!("{}/download_success/{}", url, hashed_string))
.send()
.await?;
.await
.map_err(|e| anyhow!("Failed to send POST request: {}", e))?;
Ok(())
}
}

View file

@ -1,6 +1,7 @@
pub mod client;
pub mod http_client;
use anyhow::{Result, anyhow};
use crate::{receiver::client as receiver, sender::util::replace_protocol};
use tokio_tungstenite::{
@ -9,7 +10,7 @@ use tokio_tungstenite::{
};
use tracing::{debug, error};
pub async fn start_receiver(relay: &str, name: &str) {
pub async fn start_receiver(relay: &str, name: &str) -> Result<()> {
let http_url = replace_protocol(relay);
let res = http_client::download_info(http_url.as_str(), name)
.await
@ -23,19 +24,19 @@ pub async fn start_receiver(relay: &str, name: &str) {
debug!("Failed to connect remote: {relay_err}");
}
}
let success = http_client::download_success(http_url.as_str(), name).await;
match success {
Ok(()) => debug!("Success"),
Err(e) => error!("Error: {e:?}"),
};
let _success = http_client::download_success(http_url.as_str(), name)
.await
.map_err(|e| anyhow!("Failed to download success: {}", e))?;
debug!("Success");
Ok(())
}
pub async fn start_ws_com(relay: &str, name: &str) -> Result<(), Box<dyn std::error::Error>> {
pub async fn start_ws_com(relay: &str, name: &str) -> Result<()> {
let url = String::from(relay) + "/ws";
let Ok(mut request) = url.into_client_request() else {
println!("Error: Failed to create request.");
return Err("Failed to create request".into());
};
let mut request = url
.into_client_request()
.map_err(|e| anyhow!("Failed to create request: {}", e))?;
request
.headers_mut()
@ -60,4 +61,4 @@ pub async fn start_ws_com(relay: &str, name: &str) -> Result<(), Box<dyn std::er
}?,
};
Ok(())
}
}