added proper definition of traits for classes

This commit is contained in:
michael-bailey 2020-08-09 11:36:14 +01:00
parent a1474d01de
commit ef86b89042
6 changed files with 42 additions and 19 deletions

3
.gitignore vendored
View File

@ -11,4 +11,5 @@ Cargo.lock
.DS_Store
.idea
*.properties
*.properties
.vscode/launch.json

View File

@ -17,12 +17,12 @@ dashmap = "3.11.4"
async-std = "1.6.2"
lazy_static = "1.4.0"
rayon = "1.3.1"
diesel = { version = "1.4.5", features = ["sqlite"] }
zeroize = "1.1.0"
cursive = { version = "0.15.0", default-features = false, features = ["crossterm-backend"]}
crossterm = "0.17.7"
log = "0.4"
clap = "3.0.0-beta.1"
rust-bert = "0.7.11"

View File

@ -4,8 +4,6 @@ use crate::{
commands::Commands,
};
use std::time::Duration;
use std::str::FromStr;
use std::net::SocketAddr;
use zeroize::Zeroize;
@ -46,7 +44,7 @@ impl ClientApi {
let addr = host.parse().unwrap();
let mut stream = TcpStream::connect_timeout(&addr, Duration::from_millis(10000))?;
stream.read(&mut buffer)?;
let _ = stream.read(&mut buffer)?;
match Commands::from(&buffer) {
Commands::Request(None) => {

View File

@ -1,5 +1,6 @@
use std::string::ToString;
use std::collections::HashMap;
use std::str::FromStr;
use std::borrow::Borrow;
use regex::Regex;
@ -25,6 +26,11 @@ pub enum Commands {
Error(Option<HashMap<String, String>>),
}
pub enum CommandParseError {
UnknownCommand,
NoString,
}
impl ToString for Commands {
fn to_string(&self) -> std::string::String {
@ -58,14 +64,20 @@ impl ToString for Commands {
} else {
out_string.push_str(v.as_str());
}
}
}
out_string
}
}
impl FromStr for Commands {
type Err = CommandParseError;
fn from_str(_: &str) -> std::result::Result<Self, Self::Err> {
todo!();
}
}
impl From<&str> for Commands {
fn from(data: &str) -> Self {
let regex = Regex::new(r###"(\?|!)([a-zA-z0-9]*):|([a-zA-z]*):([a-zA-Z0-9@\-\+\[\]{}_=/.]+|("(.*?)")+)"###).unwrap();

View File

@ -108,7 +108,7 @@ impl Client {
if self.stream_arc.lock().unwrap().peek(&mut buffer).is_ok() {
let mut stream = self.stream_arc.lock().unwrap();
stream.read(&mut buffer).unwrap();
let _ = stream.read(&mut buffer).unwrap();
let command = Commands::from(&buffer);
@ -123,7 +123,7 @@ impl Client {
let _ = stream.write_all(Commands::Success(None).to_string().as_bytes());
},
Commands::ClientUpdate(None) => {
let _ = self.server_sender.send(ServerMessages::RequestUpdate(self.uuid.clone()));
let _ = self.server_sender.send(ServerMessages::RequestUpdate(self.stream_arc.clone()));
let _ = stream.write_all(Commands::Success(None).to_string().as_bytes());
}
_ => {
@ -156,6 +156,10 @@ impl Client {
}
}
impl ToString for Client {
fn to_string(&self) -> std::string::String { todo!() }
}
impl Drop for Client {
fn drop(&mut self) {
let _ = self.stream_arc.lock().unwrap().write_all(Commands::Disconnect(None).to_string().as_bytes());

View File

@ -14,8 +14,6 @@ use std::{
io
};
use regex::Regex;
use rayon::prelude::*;
use log::info;
@ -27,9 +25,9 @@ use std::time::Duration;
#[derive(Debug)]
pub enum ServerMessages {
#[allow(dead_code)]
RequestUpdate(String),
RequestUpdate(Arc<Mutex<TcpStream>>),
#[allow(dead_code)]
RequestInfo(String, String),
RequestInfo(String, Arc<Mutex<TcpStream>>),
#[allow(dead_code)]
Disconnect(String),
#[allow(dead_code)]
@ -74,7 +72,7 @@ impl Server {
self.address.to_string()
}
pub fn start(&self) -> Result<(), io::Error>{
pub fn start<'a>(&self) -> Result<(), io::Error>{
info!("server: starting server...");
// clone elements for thread
let client_map = self.connected_clients.clone();
@ -100,9 +98,15 @@ impl Server {
// TODO: implement disconnecting all clients and shutting down the server
info!("server: shutting down...");
break 'outer;
},
ServerMessages::RequestUpdate(stream_arc) => {
let mut stream = stream_arc.lock().unwrap();
for (_k, v) in client_map.lock().unwrap().iter() {
let _ = &stream.write_all(v.to_string().as_bytes());
let _ = &stream.flush();
}
}
_ => {}
}
}
@ -149,8 +153,8 @@ impl Server {
let command = Commands::Info(Some(params));
let _ = stream.write_all(&command.to_string().as_bytes());
let _ = stream.flush();
stream.write_all(command.to_string().as_bytes()).unwrap();
stream.flush().unwrap();
},
_ => {
info!("Server: Invalid command sent");
@ -167,7 +171,7 @@ impl Server {
v.handle_connection();
}
}
info!("server: stopped")
info!("server: stopped");
});
info!("server: started");
Ok(())
@ -205,11 +209,15 @@ impl Server {
* the connection is lost before transmitting. Maybe change to handle any exceptions
* that may occur.
*/
stream.write(data.to_string().as_bytes()).unwrap();
let _ = stream.write(data.to_string().as_bytes()).unwrap();
stream.flush().unwrap();
}
}
impl ToString for Server {
fn to_string(&self) -> std::string::String { todo!() }
}
impl Drop for Server {
fn drop(&mut self) {
println!("server dropped");