feat: Added basic protocol support for play/pause

This commit is contained in:
Nickiel12 2022-12-18 21:07:33 -08:00
parent 2ed55b3aa0
commit 7257db9c9a
3 changed files with 64 additions and 80 deletions

View file

@ -1,7 +1,8 @@
use derive_more::From; use derive_more::From;
use rusqlite::{params, Connection, Params, Result}; use rusqlite::{params, Connection, Result};
use crate::file_operations::ItemTag; use crate::file_operations::ItemTag;
use crate::server_handling::PartialTag;
/// Catch all Error for database creation errors /// Catch all Error for database creation errors
#[derive(From, Debug)] #[derive(From, Debug)]
@ -20,56 +21,6 @@ pub enum SearchType {
Like, Like,
} }
pub struct PartialTag {
pub path: Option<String>,
pub title: Option<String>,
pub artist: Option<String>,
pub album: Option<String>,
pub album_artist: Option<String>,
}
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 /// The container object for the main database Connection
/// ///
/// # Examples /// # Examples

View file

@ -3,7 +3,6 @@ use server_handling::UIRequest;
use std::io::BufReader; use std::io::BufReader;
use std::net::TcpStream; use std::net::TcpStream;
use std::path::PathBuf; use std::path::PathBuf;
use std::thread::spawn;
use std::{fs::File, net::TcpListener}; use std::{fs::File, net::TcpListener};
use tungstenite::accept; use tungstenite::accept;
use tungstenite::protocol::WebSocket; use tungstenite::protocol::WebSocket;
@ -11,7 +10,8 @@ use tungstenite::protocol::WebSocket;
use clap::Parser; use clap::Parser;
use dirs_next; 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 db_operations;
pub mod file_operations; pub mod file_operations;
@ -101,42 +101,21 @@ fn main() {
let server = TcpListener::bind("127.0.0.1:9001").unwrap(); 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::<WebSocket<TcpStream>>::new(); let mut sockets = Vec::<WebSocket<TcpStream>>::new();
loop { loop {
if let Ok((stream, addr)) = server.accept() { if let Ok((stream, addr)) = server.accept() {
println!("New socket connected from: {}", addr); println!("New socket connected from: {}", addr);
//TODO: handle this error //TODO: handle this error
sockets.push(accept(stream).unwrap()); sockets.push(accept(stream).unwrap());
println!("len = {}", sockets.len());
} }
if sockets.len() == 0 { if sockets.len() == 0 {
std::thread::sleep(std::time::Duration::from_secs(1)); std::thread::sleep(std::time::Duration::from_secs(1));
println!("sleeping");
} }
for i in 0..sockets.len() { for i in 0..sockets.len() {
if let Ok(mess) = sockets[i].read_message() { if let Ok(mess) = sockets[i].read_message() {
println!("got a message from a socket");
if mess.is_text() { if mess.is_text() {
println!("It was a text message!");
match server_handling::handle_request(mess.into_text().unwrap()) { match server_handling::handle_request(mess.into_text().unwrap()) {
Err(error) => { Err(error) => {
println!("There was an error decoding the message: {:?}", error) println!("There was an error decoding the message: {:?}", error)
@ -145,8 +124,8 @@ fn main() {
UIRequest::Play => sink.play(), UIRequest::Play => sink.play(),
UIRequest::Pause => sink.pause(), UIRequest::Pause => sink.pause(),
UIRequest::Skip(skip_direction) => todo!(), UIRequest::Skip(skip_direction) => todo!(),
UIRequest::GetList => todo!(), UIRequest::GetList(request) => todo!(),
UIRequest::SwitchTo(partia_tag) => todo!(), UIRequest::SwitchTo(partial_tag) => todo!(),
UIRequest::GetStatus => todo!(), UIRequest::GetStatus => todo!(),
}, },
} }

View file

@ -1,20 +1,74 @@
use crate::db_operations::PartialTag; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct PartialTag {
pub path: Option<String>,
pub title: Option<String>,
pub artist: Option<String>,
pub album: Option<String>,
pub album_artist: Option<String>,
}
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 { pub enum SkipDirection {
Forward, Forward,
Backward, Backward,
} }
#[derive(Serialize, Deserialize)]
pub enum UIRequest { pub enum UIRequest {
Play, Play,
Pause, Pause,
Skip(SkipDirection), Skip(SkipDirection),
GetList, GetList(String),
SwitchTo(PartialTag), SwitchTo(PartialTag),
GetStatus, GetStatus,
} }
pub fn handle_request(socket_message: String) -> Result<UIRequest, ()> { pub fn handle_request(socket_message: String) -> Result<UIRequest, serde_json::Error> {
println!("Recieved a socket message: {}", socket_message); println!("Recieved a socket message: {}", socket_message);
Ok(UIRequest::Play) let request: UIRequest = serde_json::from_str(&socket_message)?;
Ok(request)
} }