mirror of
https://github.com/Dummi26/musicdb.git
synced 2025-12-14 11:56:16 +01:00
server can now send error messages to clients
This commit is contained in:
@@ -232,7 +232,14 @@ impl Database {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn apply_command(&mut self, command: Command) {
|
||||
pub fn apply_command(&mut self, mut command: Command) {
|
||||
if !self.is_client() {
|
||||
if let Command::ErrorInfo(t, _) = &mut command {
|
||||
// clients can send ErrorInfo to the server and it will show up on other clients,
|
||||
// BUT only the server can set the Title of the ErrorInfo.
|
||||
t.clear();
|
||||
}
|
||||
}
|
||||
// since db.update_endpoints is empty for clients, this won't cause unwanted back and forth
|
||||
self.broadcast_update(&command);
|
||||
match command {
|
||||
@@ -349,6 +356,7 @@ impl Database {
|
||||
Command::InitComplete => {
|
||||
self.client_is_init = true;
|
||||
}
|
||||
Command::ErrorInfo(..) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ impl Song {
|
||||
{
|
||||
Ok(data) => Some(data),
|
||||
Err(e) => {
|
||||
eprintln!("[info] error loading song {id}: {e}");
|
||||
eprintln!("[WARN] error loading song {id}: {e}");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,6 +139,13 @@ impl Player {
|
||||
db.apply_command(Command::NextSong);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// couldn't load song bytes
|
||||
db.broadcast_update(&Command::ErrorInfo(
|
||||
"NoSongData".to_owned(),
|
||||
format!("Couldn't load song #{}\n({})", song.id, song.title),
|
||||
));
|
||||
db.apply_command(Command::NextSong);
|
||||
}
|
||||
} else {
|
||||
self.source = None;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
pub mod get;
|
||||
|
||||
use std::{
|
||||
eprintln,
|
||||
io::{BufRead, BufReader, Read, Write},
|
||||
net::{SocketAddr, TcpListener},
|
||||
sync::{mpsc, Arc, Mutex},
|
||||
@@ -51,6 +50,7 @@ pub enum Command {
|
||||
RemoveArtist(ArtistId),
|
||||
ModifyArtist(Artist),
|
||||
InitComplete,
|
||||
ErrorInfo(String, String),
|
||||
}
|
||||
impl Command {
|
||||
pub fn send_to_server(self, db: &Database) -> Result<(), Self> {
|
||||
@@ -106,7 +106,6 @@ pub fn run_server(
|
||||
let command_sender = command_sender.clone();
|
||||
let db = Arc::clone(&db);
|
||||
thread::spawn(move || {
|
||||
eprintln!("[info] TCP connection accepted from {con_addr}.");
|
||||
// each connection first has to send one line to tell us what it wants
|
||||
let mut connection = BufReader::new(connection);
|
||||
let mut line = String::new();
|
||||
@@ -279,6 +278,11 @@ impl ToFromBytes for Command {
|
||||
Self::InitComplete => {
|
||||
s.write_all(&[0b00110001])?;
|
||||
}
|
||||
Self::ErrorInfo(t, d) => {
|
||||
s.write_all(&[0b11011011])?;
|
||||
t.to_bytes(s)?;
|
||||
d.to_bytes(s)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -324,6 +328,7 @@ impl ToFromBytes for Command {
|
||||
0b11011100 => Self::RemoveArtist(ToFromBytes::from_bytes(s)?),
|
||||
0b01011101 => Self::AddCover(ToFromBytes::from_bytes(s)?),
|
||||
0b00110001 => Self::InitComplete,
|
||||
0b11011011 => Self::ErrorInfo(ToFromBytes::from_bytes(s)?, ToFromBytes::from_bytes(s)?),
|
||||
_ => {
|
||||
eprintln!("unexpected byte when reading command; stopping playback.");
|
||||
Self::Stop
|
||||
|
||||
Reference in New Issue
Block a user