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.
This commit is contained in:
parent
9f7b95ca98
commit
336ea18ad9
11 changed files with 88 additions and 41 deletions
|
|
@ -119,25 +119,39 @@ struct Context {
|
|||
/// appended to the room id to create a URL. The URL is then printed to the
|
||||
/// console using the qr2term library. Finally, the function prints a
|
||||
/// message to the console with the URL.
|
||||
fn on_create_room(context: &Context, id: String) -> Status {
|
||||
fn on_create_room(context: &Context, id: String, relay: String) -> Status {
|
||||
let base64 = general_purpose::STANDARD.encode(&context.hmac);
|
||||
let url = format!("{}-{}", id, base64);
|
||||
|
||||
let rand_name = generate_random_name();
|
||||
let hash_name = hash_random_name(rand_name.clone());
|
||||
|
||||
let res = send_info("http://localhost:8000", &hash_name, url.as_str());
|
||||
debug!("Got Result: {:#?}", res);
|
||||
let send_url = url.to_string();
|
||||
let h_name = hash_name.to_string();
|
||||
let server_url = String::from("http://") + relay.as_str();
|
||||
let res = std::thread::spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(send_info(&server_url, &h_name, send_url.as_str()))
|
||||
})
|
||||
.join()
|
||||
.unwrap();
|
||||
debug!("Got Result: {:?}", res);
|
||||
// Print a newline to the console to separate the output from the command
|
||||
// line.
|
||||
println!();
|
||||
|
||||
// Try to generate a QR code from the URL. If the function fails for some
|
||||
// reason, print an error message to the console.
|
||||
if let Err(error) = qr2term::print_qr(&url) {
|
||||
// if let Err(error) = qr2term::print_qr(&url) {
|
||||
// error!("Failed to generate QR code: {}", error);
|
||||
// }
|
||||
|
||||
if let Err(error) = qr2term::print_qr(&rand_name) {
|
||||
error!("Failed to generate QR code: {}", error);
|
||||
}
|
||||
|
||||
// Print a newline to the console to separate the output from the command
|
||||
// line.
|
||||
println!();
|
||||
|
|
@ -535,13 +549,13 @@ fn on_handshake(context: &mut Context, handshake_response: HandshakeResponsePack
|
|||
/// struct is then matched on to call the appropriate function.
|
||||
///
|
||||
/// If the message is invalid, an error is returned.
|
||||
fn on_message(context: &mut Context, message: WebSocketMessage) -> Status {
|
||||
fn on_message(context: &mut Context, message: WebSocketMessage, relay: String) -> Status {
|
||||
if message.is_text() {
|
||||
let text = message.into_text().unwrap();
|
||||
let packet = serde_json::from_str(&text).unwrap();
|
||||
|
||||
return match packet {
|
||||
JsonPacketResponse::Create { id } => on_create_room(context, id),
|
||||
JsonPacketResponse::Create { id } => on_create_room(context, id, relay),
|
||||
JsonPacketResponse::Join { size } => on_join_room(context, size),
|
||||
JsonPacketResponse::Leave { index } => on_leave_room(context, index),
|
||||
JsonPacketResponse::Error { message } => on_error(message),
|
||||
|
|
@ -588,7 +602,7 @@ fn on_message(context: &mut Context, message: WebSocketMessage) -> Status {
|
|||
/// When the function is finished, it will exit and the transfer will be complete. If there
|
||||
/// is an error during the transfer, the function will print an error message to stdout and
|
||||
/// exit.
|
||||
pub async fn start(socket: Socket, paths: Vec<String>) {
|
||||
pub async fn start(socket: Socket, paths: Vec<String>, room_id: Option<String>, relay: String) {
|
||||
// Create a vector to store metadata about each file that will be sent.
|
||||
let mut files = vec![];
|
||||
|
||||
|
|
@ -668,7 +682,11 @@ pub async fn start(socket: Socket, paths: Vec<String>) {
|
|||
debug!("Attempting to create room...");
|
||||
|
||||
// Send a JSON packet to the server to create a room with a size of 2.
|
||||
context.sender.send_json_packet(JsonPacket::Create);
|
||||
debug!("With Room-ID: {:?}", room_id);
|
||||
context.sender.send_json_packet(JsonPacket::Create {
|
||||
id: room_id.clone(),
|
||||
});
|
||||
// context.sender.send_json_packet(JsonPacket::Create);
|
||||
|
||||
// Create a future that handles the outgoing stream of messages from the client to the
|
||||
// server.
|
||||
|
|
@ -678,7 +696,7 @@ pub async fn start(socket: Socket, paths: Vec<String>) {
|
|||
// client.
|
||||
let incoming_handler = incoming.try_for_each(|message| {
|
||||
// Call the `on_message` function to handle the incoming message.
|
||||
match on_message(&mut context, message) {
|
||||
match on_message(&mut context, message, relay.clone()) {
|
||||
// If the status is `Status::Exit`, the transfer is complete. Print a message to
|
||||
// stdout and exit the function.
|
||||
Status::Exit() => {
|
||||
|
|
@ -800,7 +818,8 @@ mod tests {
|
|||
on_create_room(
|
||||
&context,
|
||||
"b531e87d-e51a-4507-94f4-335cbe2d32f3-Nc5skZReq7qJN7INwckyAZLWEEbxsrFfH/692tUNgkM="
|
||||
.to_string()
|
||||
.to_string(),
|
||||
String::from("0.0.0.0:8000")
|
||||
),
|
||||
Status::Continue()
|
||||
);
|
||||
|
|
@ -885,17 +904,19 @@ mod tests {
|
|||
assert_eq!(
|
||||
on_message(
|
||||
&mut context,
|
||||
WebSocketMessage::Text(r#"{"type":"leave","index":5}"#.to_string())
|
||||
WebSocketMessage::Text(r#"{"type":"leave","index":5}"#.to_string()),
|
||||
String::from("0.0.0.0:8000")
|
||||
),
|
||||
Status::Continue()
|
||||
);
|
||||
assert_eq!(on_message(&mut context, WebSocketMessage::Text(r#"{"type":"create","id":"b531e87d-e51a-4507-94f4-335cbe2d32f3-Nc5skZReq7qJN7INwckyAZLWEEbxsrFfH/692tUNgkM="}"#.to_string())), Status::Continue());
|
||||
assert_eq!(on_message(&mut context, WebSocketMessage::Text(r#"{"type":"create","id":"b531e87d-e51a-4507-94f4-335cbe2d32f3-Nc5skZReq7qJN7INwckyAZLWEEbxsrFfH/692tUNgkM="}"#.to_string()), String::from("0.0.0.0:8000")), Status::Continue());
|
||||
assert_eq!(
|
||||
on_message(
|
||||
&mut context,
|
||||
WebSocketMessage::Text(
|
||||
r#"{"type":"error","message":"Error Message: Test"}"#.to_string()
|
||||
)
|
||||
),
|
||||
String::from("0.0.0.0:8000")
|
||||
),
|
||||
Status::Err("Error Message: Test".to_string())
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue