Skip to content

Commit

Permalink
some polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
marci1175 committed Jan 14, 2024
1 parent c44d7a0 commit 2f8a555
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build_info.matthias_build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.01.14. 14:10
2024.01.14. 22:22
106 changes: 82 additions & 24 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use egui::{vec2, Align, Color32, Layout, RichText};
use std::fs::{self};
use std::sync::mpsc;
use windows_sys::w;
use windows_sys::Win32::UI::WindowsAndMessaging::{MessageBoxW, MB_ICONERROR};

Expand All @@ -16,7 +17,7 @@ use self::account_manager::{
append_to_file, decrypt_lines_from_vec, delete_line_from_file, generate_uuid, read_from_file,
};

use self::backend::{ClientConnection, ServerMaster};
use self::backend::{ClientConnection, ServerMaster, ConnectionState};
use self::input::keymap;

impl eframe::App for backend::TemplateApp {
Expand Down Expand Up @@ -111,49 +112,98 @@ impl eframe::App for backend::TemplateApp {
ui.text_edit_singleline(&mut self.client_ui.send_on_ip);
});

match self.client_connection.client.is_none() {
true => {
match self.client_connection.state {
ConnectionState::Connected => {
if ui
.button(RichText::from("Disconnect").color(Color32::RED))
.clicked()
{
//Reset client
self.client_connection.client = None;
self.client_ui.incoming_msg = ServerMaster::default();
self.autosync_should_run
.store(false, std::sync::atomic::Ordering::Relaxed);

self.client_connection.state = ConnectionState::Disconnected;
}
},
ConnectionState::Disconnected => {
if ui.button("Connect").clicked() {

let ip = self.client_ui.send_on_ip.clone();
match ClientConnection::connect(format!("http://{}", ip)) {
Ok(ok) => {
self.client_connection = ok;
}
Err(err) => {
std::thread::spawn(move || unsafe {
MessageBoxW(
0,
str::encode_utf16(err.to_string().as_str())
.chain(std::iter::once(0))
.collect::<Vec<_>>()
.as_ptr(),
w!("Error"),
MB_ICONERROR,
);
});
}
};

let sender = self.connection_sender.clone();

std::thread::spawn(move || {
match ClientConnection::connect(format!("http://{}", ip)) {
Ok(ok) => {
if let Err(err) = sender.send(ok){
dbg!(err);
};
}
Err(err) => {
std::thread::spawn(move || unsafe {
MessageBoxW(
0,
str::encode_utf16(err.to_string().as_str())
.chain(std::iter::once(0))
.collect::<Vec<_>>()
.as_ptr(),
w!("Error"),
MB_ICONERROR,
);
});
}
};
});

self.autosync_should_run
.store(true, std::sync::atomic::Ordering::Relaxed);

//reset autosync
self.autosync_sender = None;

self.client_connection.state = ConnectionState::Connecting;

}
}
false => {
},
ConnectionState::Connecting => {
if ui
.button(RichText::from("Disconnect").color(Color32::RED))
.button(RichText::from("Cancel connection").color(Color32::LIGHT_GRAY))
.clicked()
{
//Reset client
self.client_connection.client = None;
self.client_ui.incoming_msg = ServerMaster::default();
self.autosync_should_run
.store(false, std::sync::atomic::Ordering::Relaxed);

self.client_connection.state = ConnectionState::Disconnected;
}
},
}

match self.client_connection.client.is_none() {
true => {

}
false => {

}
}

match self.client_connection.state {
ConnectionState::Connected => {
ui.label(RichText::from("Connected").color(Color32::GREEN));
},
ConnectionState::Disconnected => {
ui.label(RichText::from("Disconnected").color(Color32::RED));
},
ConnectionState::Connecting => {
ui.label(RichText::from("Connecting").color(Color32::LIGHT_GREEN));
},
}

ui.allocate_ui(vec2(25., 25.), |ui| {
if ui
.add(egui::widgets::ImageButton::new(egui::include_image!(
Expand Down Expand Up @@ -240,5 +290,13 @@ impl eframe::App for backend::TemplateApp {
Err(err) => eprintln!("{err}"),
};
});

//Connection reciver
match self.connection_reciver.try_recv() {
Ok(connection) => self.client_connection = connection,
Err(err) => {
//dbg!(err);
},
}
}
}
45 changes: 43 additions & 2 deletions src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rand::rngs::ThreadRng;
use crate::app::input::Input;
use rodio::{OutputStream, OutputStreamHandle, Sink};
use std::collections::BTreeMap;
use std::fmt::Display;
use std::fmt::{Display, Debug};
use std::io;
use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;
Expand Down Expand Up @@ -115,6 +115,13 @@ pub struct TemplateApp {
#[serde(skip)]
pub dtx: mpsc::Sender<String>,

///Server connection
#[serde(skip)]
pub connection_reciver: mpsc::Receiver<ClientConnection>,
#[serde(skip)]
pub connection_sender: mpsc::Sender<ClientConnection>,


///Server - client syncing thread
#[serde(skip)]
pub autosync_sender: Option<mpsc::Receiver<String>>,
Expand All @@ -135,6 +142,7 @@ impl Default for TemplateApp {
let (ftx, frx) = mpsc::channel::<String>();
let (itx, irx) = mpsc::channel::<String>();
let (audio_save_tx, audio_save_rx) = mpsc::channel::<String>();
let (connection_sender, connection_reciver) = mpsc::channel::<ClientConnection>();
Self {
audio_file: Arc::new(Mutex::new(PathBuf::from(format!(
"{}\\Matthias\\Client\\voice_record.wav",
Expand Down Expand Up @@ -196,6 +204,10 @@ impl Default for TemplateApp {
rx,
tx,

//Server connection
connection_sender,
connection_reciver,

//data sync
drx,
dtx,
Expand Down Expand Up @@ -635,6 +647,8 @@ impl ClientMessage {
pub struct ClientConnection {
#[serde(skip)]
pub client: Option<MessageClient<Channel>>,
#[serde(skip)]
pub state: ConnectionState,
}

impl ClientConnection {
Expand Down Expand Up @@ -691,11 +705,38 @@ impl ClientConnection {
//Is the client valid?
reciver.recv().unwrap()
},
state: ConnectionState::Connected,
})
}
}

///Used to decide what type fo file i want to send client_actions/actions.rs
///Used to show state of the connection
#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub enum ConnectionState {
Connected,
Disconnected,
Connecting,
}

impl Default for ConnectionState {
fn default() -> Self {
Self::Disconnected
}
}

impl Debug for ConnectionState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(
match self {
ConnectionState::Connected => "Connected",
ConnectionState::Disconnected => "Disconnected",
ConnectionState::Connecting => "Connecting",
}
)
}
}

///Used to decide what type fo file i want to send client_actions/actions.rs TODO: include this in main
pub enum ClientSendType {
File,
Message,
Expand Down
1 change: 0 additions & 1 deletion src/app/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ pub async fn server_main(
let messages = &msg_service.messages.lock().unwrap().to_vec();

Server::builder()
.http2_keepalive_interval(Some(Duration::from_secs(10)))
.add_service(MessageServer::new(msg_service))
.serve(addr)
.await?;
Expand Down
1 change: 0 additions & 1 deletion src/app/ui/client_ui/client_actions/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl TemplateApp {
#[inline]
pub fn send_file(&mut self, file: std::path::PathBuf) {
let passw = self.client_ui.client_password.clone();
let ip = self.client_ui.send_on_ip.clone();
let author = self.login_username.clone();
let replying_to = self.client_ui.replying_to;

Expand Down
1 change: 0 additions & 1 deletion src/app/ui/client_ui/message_instances/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ impl TemplateApp {
//We dont have file on our local system so we have to ask the server to provide it
let passw = self.client_ui.client_password.clone();
let author = self.login_username.clone();
let send_on_ip = self.client_ui.send_on_ip.clone();
let sender = self.audio_save_tx.clone();

let message = ClientMessage::construct_audio_request_msg(
Expand Down
1 change: 0 additions & 1 deletion src/app/ui/client_ui/message_instances/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ impl TemplateApp {
{
let passw = self.client_ui.client_password.clone();
let author = self.login_username.clone();
let send_on_ip = self.client_ui.send_on_ip.clone();
let sender = self.ftx.clone();

let message = ClientMessage::construct_file_request_msg(file.index, passw, author);
Expand Down
1 change: 0 additions & 1 deletion src/app/ui/client_ui/message_instances/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ impl TemplateApp {
//We dont have file on our local system so we have to ask the server to provide it
let passw = self.client_ui.client_password.clone();
let author = self.login_username.clone();
let send_on_ip = self.client_ui.send_on_ip.clone();
let sender = self.itx.clone();

let message = ClientMessage::construct_image_request_msg(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl TemplateApp {
true => self.client_ui.client_password.clone(),
false => "".into(),
};
let temp_ip = self.client_ui.send_on_ip.clone();

let replying_to = self.client_ui.replying_to;
let connection = self.client_connection.clone();

Expand Down

0 comments on commit 2f8a555

Please sign in to comment.