From 2ed55b3aa02d4389c148598c437e1c35dfd12300 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sun, 18 Dec 2022 18:26:13 -0800 Subject: [PATCH] feat: Basic websocket communication added Started parrallel development of SousaCli to work over websockets to control this app --- Cargo.toml | 3 ++ src/main.rs | 71 +++++++++++++++++++++++++++++++++++++----- src/server_handling.rs | 20 ++++++++++++ 3 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 src/server_handling.rs diff --git a/Cargo.toml b/Cargo.toml index db2e311..09a94d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,6 @@ scan_dir = "0.3.3" derive_more = "0.99.17" id3 = "1.5.1" rodio = "0.16.0" +tungstenite = "0.18.0" +workctl = "0.2.0" +crossbeam-channel = "0.5.6" diff --git a/src/main.rs b/src/main.rs index af371a4..aecd05d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,12 @@ -use rodio::{source::Source, Decoder, OutputStream, Sink}; -use std::fs::File; +use rodio::{Decoder, OutputStream, Sink}; +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; use clap::Parser; use dirs_next; @@ -10,6 +15,7 @@ use crate::db_operations::{DatabaseRequest, PartialTag}; pub mod db_operations; pub mod file_operations; +pub mod server_handling; #[derive(Parser, Debug)] #[command(author, version, about, long_about=None)] @@ -90,11 +96,62 @@ fn main() { let source = Decoder::new(file).unwrap(); sink.append(source); // Play the sound directly on the device - std::thread::sleep(std::time::Duration::from_secs(5)); sink.pause(); - std::thread::sleep(std::time::Duration::from_secs(5)); - sink.play(); - sink.sleep_until_end(); + std::thread::sleep(std::time::Duration::from_secs(2)); - println!("{:?}", cli); + 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) + } + Ok(req) => match req { + UIRequest::Play => sink.play(), + UIRequest::Pause => sink.pause(), + UIRequest::Skip(skip_direction) => todo!(), + UIRequest::GetList => todo!(), + UIRequest::SwitchTo(partia_tag) => todo!(), + UIRequest::GetStatus => todo!(), + }, + } + } + } + } + } } diff --git a/src/server_handling.rs b/src/server_handling.rs new file mode 100644 index 0000000..319303f --- /dev/null +++ b/src/server_handling.rs @@ -0,0 +1,20 @@ +use crate::db_operations::PartialTag; + +pub enum SkipDirection { + Forward, + Backward, +} + +pub enum UIRequest { + Play, + Pause, + Skip(SkipDirection), + GetList, + SwitchTo(PartialTag), + GetStatus, +} + +pub fn handle_request(socket_message: String) -> Result { + println!("Recieved a socket message: {}", socket_message); + Ok(UIRequest::Play) +}