diff --git a/src/db_operations.rs b/src/db_operations.rs index c60dcb9..2494578 100644 --- a/src/db_operations.rs +++ b/src/db_operations.rs @@ -1,7 +1,8 @@ use derive_more::From; -use rusqlite::{params, Connection, Params, Result}; +use rusqlite::{params, Connection, Result}; use crate::file_operations::ItemTag; +use crate::server_handling::PartialTag; /// Catch all Error for database creation errors #[derive(From, Debug)] @@ -20,56 +21,6 @@ pub enum SearchType { Like, } -pub struct PartialTag { - pub path: Option, - pub title: Option, - pub artist: Option, - pub album: Option, - pub album_artist: Option, -} - -impl Default for PartialTag { - fn default() -> Self { - PartialTag { - path: None, - title: None, - artist: None, - album: None, - album_artist: None, - } - } -} - -impl PartialTag { - pub fn has_path(self: &Self) -> bool { - self.path.is_some() - } - - pub fn has_title(self: &Self) -> bool { - self.title.is_some() - } - - pub fn has_artist(self: &Self) -> bool { - self.artist.is_some() - } - - pub fn has_album(self: &Self) -> bool { - self.album.is_some() - } - - pub fn has_album_artist(self: &Self) -> bool { - self.album_artist.is_some() - } - - pub fn is_empty(self: &Self) -> bool { - return self.path.is_none() - && self.title.is_none() - && self.artist.is_none() - && self.album.is_none() - && self.album_artist.is_none(); - } -} - /// The container object for the main database Connection /// /// # Examples diff --git a/src/main.rs b/src/main.rs index aecd05d..53a659d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ use server_handling::UIRequest; use std::io::BufReader; use std::net::TcpStream; use std::path::PathBuf; -use std::thread::spawn; use std::{fs::File, net::TcpListener}; use tungstenite::accept; use tungstenite::protocol::WebSocket; @@ -11,7 +10,8 @@ use tungstenite::protocol::WebSocket; use clap::Parser; use dirs_next; -use crate::db_operations::{DatabaseRequest, PartialTag}; +use crate::db_operations::DatabaseRequest; +use crate::server_handling::PartialTag; pub mod db_operations; pub mod file_operations; @@ -101,42 +101,21 @@ fn main() { let server = TcpListener::bind("127.0.0.1:9001").unwrap(); - /* - for stream in server.incoming() { - spawn(move || { - let mut websocket = accept(stream.unwrap()).unwrap(); - loop { - let msg = websocket.read_message().unwrap(); - - // We do not want to send back ping/pong messages. - if msg.is_binary() || msg.is_text() { - println!("is binary?: {:?}", msg.is_binary()); - println!("msg: {:?}", msg); - } - } - }); - } - */ - let mut sockets = Vec::>::new(); loop { if let Ok((stream, addr)) = server.accept() { println!("New socket connected from: {}", addr); //TODO: handle this error sockets.push(accept(stream).unwrap()); - println!("len = {}", sockets.len()); } if sockets.len() == 0 { std::thread::sleep(std::time::Duration::from_secs(1)); - println!("sleeping"); } for i in 0..sockets.len() { if let Ok(mess) = sockets[i].read_message() { - println!("got a message from a socket"); if mess.is_text() { - println!("It was a text message!"); match server_handling::handle_request(mess.into_text().unwrap()) { Err(error) => { println!("There was an error decoding the message: {:?}", error) @@ -145,8 +124,8 @@ fn main() { UIRequest::Play => sink.play(), UIRequest::Pause => sink.pause(), UIRequest::Skip(skip_direction) => todo!(), - UIRequest::GetList => todo!(), - UIRequest::SwitchTo(partia_tag) => todo!(), + UIRequest::GetList(request) => todo!(), + UIRequest::SwitchTo(partial_tag) => todo!(), UIRequest::GetStatus => todo!(), }, } diff --git a/src/server_handling.rs b/src/server_handling.rs index 319303f..9a0074b 100644 --- a/src/server_handling.rs +++ b/src/server_handling.rs @@ -1,20 +1,74 @@ -use crate::db_operations::PartialTag; +use serde::{Deserialize, Serialize}; +#[derive(Deserialize, Serialize)] +pub struct PartialTag { + pub path: Option, + pub title: Option, + pub artist: Option, + pub album: Option, + pub album_artist: Option, +} + +impl Default for PartialTag { + fn default() -> Self { + PartialTag { + path: None, + title: None, + artist: None, + album: None, + album_artist: None, + } + } +} + +impl PartialTag { + pub fn has_path(self: &Self) -> bool { + self.path.is_some() + } + + pub fn has_title(self: &Self) -> bool { + self.title.is_some() + } + + pub fn has_artist(self: &Self) -> bool { + self.artist.is_some() + } + + pub fn has_album(self: &Self) -> bool { + self.album.is_some() + } + + pub fn has_album_artist(self: &Self) -> bool { + self.album_artist.is_some() + } + + pub fn is_empty(self: &Self) -> bool { + return self.path.is_none() + && self.title.is_none() + && self.artist.is_none() + && self.album.is_none() + && self.album_artist.is_none(); + } +} + +#[derive(Serialize, Deserialize)] pub enum SkipDirection { Forward, Backward, } +#[derive(Serialize, Deserialize)] pub enum UIRequest { Play, Pause, Skip(SkipDirection), - GetList, + GetList(String), SwitchTo(PartialTag), GetStatus, } -pub fn handle_request(socket_message: String) -> Result { +pub fn handle_request(socket_message: String) -> Result { println!("Recieved a socket message: {}", socket_message); - Ok(UIRequest::Play) + let request: UIRequest = serde_json::from_str(&socket_message)?; + Ok(request) }