caesar-transfer/src/sender/mod.rs
Patryk Hegenberg 336ea18ad9 feature(sender): adapted relay and sender component to work with a simple transfer name
In order to be able to use a simple transfer name, the relay server and
the sender were adapted. On the sender, the on_create_room method was
adapted so that a call is made to the http_client to register the
transfer with the relay server. The handle_create_room method on the
server has been adapted so that when a room is created, the name is
transmitted by the receiver and is no longer generated on the
relayserver.This was also done in preparation for integrating a local
relayserver for the direct connection between sender and receiver.
2024-04-30 23:31:47 +02:00

80 lines
3.3 KiB
Rust

/// Connects to the WebSocket server at `ws://0.0.0.0:8000/ws` with an
/// `Origin` header of `ws://0.0.0.0:8000/ws`. This is the URL that the
/// sender and receiver clients will connect to.
///
/// The `start_sender` function takes a reference to a vector of strings,
/// which are the paths to the files that the sender will send over the
/// WebSocket connection.
///
/// The function first creates a WebSocket request using the `IntoClientRequest`
/// trait from `tungstenite`, which is defined on the `IntoClientRequest` struct.
/// This struct is a type that represents a request to a WebSocket server.
///
/// The `into_client_request` function returns a `Result` because it may fail
/// to create the request. In this case, we do not handle the error, so we just
/// return if the result is an error.
///
/// Once we have a request, we insert the `Origin` header into the headers of
/// the request. This is necessary because the WebSocket protocol requires the
/// `Origin` header to be present in the handshake.
///
/// After that, we print out a message to the console indicating that we are
/// attempting to connect to the server.
///
/// Next, we call the `connect_async` function from `tokio_tungstenite` which
/// takes our request and attempts to connect to the server. This function
/// returns a `Future` that resolves to a tuple of a `WebSocketStream` and a
/// `Response` from the server. The `WebSocketStream` is a stream of
/// WebSocket messages from the server, and the `Response` is the response
/// from the server to our handshake request.
///
/// If connecting to the server fails, we print out an error message and
/// return.
///
/// If connecting to the server succeeds, we pass the `WebSocketStream` and
/// the paths to the files to the `start` function from the `sender` module.
/// The `start` function is defined in the `sender` module, and it is the
/// function that sends the files over the WebSocket connection.
///
/// The `start` function takes ownership of the `WebSocketStream` and the file
/// paths, so we pass it the `paths` vector by value.
pub mod client;
pub mod http_client;
pub mod util;
use crate::sender::client as sender;
use tokio_tungstenite::{
connect_async,
tungstenite::{client::IntoClientRequest, http::HeaderValue},
};
use tracing::{debug, error};
use uuid::Uuid;
pub async fn start_sender(relay: &str, files: &[String]) {
debug!("Got relay: {relay}");
let url = String::from("ws://") + relay + "/ws";
match url.clone().into_client_request() {
Ok(mut request) => {
request
.headers_mut()
.insert("Origin", HeaderValue::from_str(relay).unwrap());
debug!("Attempting to connect to {url}...");
let room_id = Uuid::new_v4().to_string();
match connect_async(request).await {
Ok((socket, _)) => {
let paths = files.to_vec();
sender::start(socket, paths, Some(room_id), relay.to_string()).await;
// sender::start(socket, paths).await;
}
Err(e) => {
error!("Error: Failed to connect with error: {e}");
}
}
}
Err(e) => {
error!("Error: failed to create request with reason: {e:?}");
}
}
}