Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marci1175 committed Aug 16, 2024
1 parent 57805d6 commit 8048277
Show file tree
Hide file tree
Showing 7 changed files with 611 additions and 568 deletions.
2 changes: 1 addition & 1 deletion build_info.matthias_build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.08.15. 21:40
2024.08.16. 12:08
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl eframe::App for backend::Application
self.server_shutdown_token.cancel();
self.autosync_shutdown_token.cancel();
self.voip_shutdown_token.cancel();
self.voip_video_shutdown_token.cancel();

//Signal the voice recorder function to stop
let _ = self.record_audio_interrupter.send(());
Expand Down
13 changes: 9 additions & 4 deletions src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub struct Application

///Webcam recorder shutdown token
#[serde(skip)]
pub webcam_recording_shutdown: CancellationToken,
pub voip_video_shutdown_token: CancellationToken,

///What is the server's password set to
pub server_password: String,
Expand Down Expand Up @@ -202,6 +202,10 @@ pub struct Application
#[serde(skip)]
pub voip_thread: Option<()>,

///Voip image sender thread
#[serde(skip)]
pub voip_video_thread: Option<()>,

#[serde(skip)]
/// This is what the main thread uses to recive messages from the sync thread
pub server_output_reciver: Arc<Receiver<Option<String>>>,
Expand Down Expand Up @@ -239,7 +243,8 @@ impl Default for Application
let (voip_connection_sender, voip_connection_reciver) = mpsc::channel::<Voip>();

Self {
webcam_recording_shutdown: CancellationToken::new(),
voip_video_thread: None,
voip_video_shutdown_token: CancellationToken::new(),
startup_args: None,
record_audio_interrupter: mpsc::channel::<()>().0,
toasts: Arc::new(Mutex::new(Toasts::new())),
Expand Down Expand Up @@ -2293,7 +2298,7 @@ impl Voip
//This thread modifies the camera_handle directly
tokio::spawn(async move {
//Send an empty image indicating video shutdown
match Voip::send_image(&voip, uuid, &vec![0], &encryption_key).await {
match Voip::send_image(&voip, uuid, &[0], &encryption_key).await {
Ok(_) => (),
Err(err) => {
tracing::error!("{err}");
Expand Down Expand Up @@ -3444,7 +3449,7 @@ pub fn get_image_header(

//Try getting the uuid's ImageHeaders
//Insert IndexMap into the ```image_buffer```
if let Some(mut image_headers) = dbg!(image_buffer.get_mut(&image_header.uuid)) {
if let Some(mut image_headers) = image_buffer.get_mut(&image_header.uuid) {
image_headers.insert(
image_header.identificator.clone(),
HashMap::from_iter(
Expand Down
630 changes: 571 additions & 59 deletions src/app/client.rs

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions src/app/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
time::Duration,
};

use crate::app::client::{HASH_BYTE_OFFSET, IDENTIFICATOR_BYTE_OFFSET, UUID_BYTE_OFFSET};

use anyhow::{bail, Error, Result};
use chrono::Utc;
use dashmap::DashMap;
Expand All @@ -24,7 +26,7 @@ use super::backend::{
ServerReplyType, ServerSync, ServerVoip, ServerVoipReply, ServerVoipState,
};

use crate::app::backend::{
use super::backend::{
decrypt_aes256_bytes, encrypt_aes256_bytes, get_image_header,
ClientFileRequestType as ClientRequestTypeStruct, ClientFileUpload as ClientFileUploadStruct,
ClientMessage,
Expand All @@ -34,6 +36,7 @@ use crate::app::backend::{
},
ImageHeader, ServerFileReply, ServerImageReply, ServerMaster, UdpMessageType,
};

use tokio::{
io::AsyncWrite,
net::{tcp::OwnedReadHalf, UdpSocket},
Expand Down Expand Up @@ -446,21 +449,21 @@ pub fn create_client_voip_manager(
let message_bytes = decrypted_bytes.to_vec();

//Get the identificator of the image part in bytes
let indetificator_bytes = message_bytes[message_bytes.len() - 64..].to_vec();
let indetificator_bytes = message_bytes[message_bytes.len() - IDENTIFICATOR_BYTE_OFFSET..].to_vec();

let identificator = String::from_utf8(indetificator_bytes).unwrap();

//Get the identificator of the image part in bytes
let hash_bytes = message_bytes[message_bytes.len() - 64 - 64 - 36..message_bytes.len() - 64 - 36].to_vec();
let hash_bytes = message_bytes[message_bytes.len() - HASH_BYTE_OFFSET..message_bytes.len() - UUID_BYTE_OFFSET].to_vec();

let hash = String::from_utf8(hash_bytes).unwrap();

//Get the image part bytes
//We subtract 164 bytes to only get the image part
let image = message_bytes[..message_bytes.len() - 64 - 64 - 36].to_vec();
let image = message_bytes[..message_bytes.len() - HASH_BYTE_OFFSET].to_vec();

//THIS IS UNUSED AND SHOULD BE REMOVED
let _uuid_bytes = message_bytes[message_bytes.len() - 64 - 36..message_bytes.len() - 64].to_vec();
let _uuid_bytes = message_bytes[message_bytes.len() - UUID_BYTE_OFFSET..message_bytes.len() - IDENTIFICATOR_BYTE_OFFSET].to_vec();

if let Some(mut image_header) = image_buffer.get_mut(&uuid) {
if let Some((index, _, contents)) = image_header.get_full_mut(&identificator) {
Expand All @@ -475,6 +478,8 @@ pub fn create_client_voip_manager(
//If all the parts of the image header had arrived send the image to all the clients
if contents.iter().all(|(_, value)| value.is_some()) {
let contents_clone = contents.clone();
let image_buffer = image_buffer.clone();

tokio::spawn(async move {
for connected_client in voip_connected_clients.iter() {
let uuid = connected_client.key();
Expand Down Expand Up @@ -505,6 +510,11 @@ pub fn create_client_voip_manager(
let header_message =
ImageHeader::new(uuid.clone(), image_parts.clone(), identificator.clone());

if image_bytes == vec![0] {
println!("asd");
image_buffer.remove(&uuid.clone());
}

// Send image header
send_bytes(
serde_json::to_string(&header_message).unwrap().as_bytes().to_vec(),
Expand All @@ -524,15 +534,15 @@ pub fn create_client_voip_manager(

//Drain earlier ImageHeaders (and the current one), because a new one has arrived
image_header.drain(index..=index);
}
};
}
else {
tracing::error!("Image header not found: {identificator}");
}
}
else {
tracing::error!("User not found in the image header list: {uuid}");
}
};
}
}
},
Expand Down
Loading

0 comments on commit 8048277

Please sign in to comment.