Impl enough to get "OK MPD ..." from mpd server

This commit is contained in:
Stephen Seo 2021-09-15 18:16:22 +09:00
parent df6844c487
commit d9f8e75915
3 changed files with 447 additions and 2 deletions

225
Cargo.lock generated Normal file
View file

@ -0,0 +1,225 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cb00336871be5ed2c8ed44b60ae9959dc5b9f08539422ed43f09e34ecaeba21"
[[package]]
name = "mpd_info_screen"
version = "0.1.0"
dependencies = [
"structopt",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9f5105d4fdaab20335ca9565e106a5d9b82b6219b5ba735731124ac6711d23d"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf9d950ef167e25e0bdb073cf1d68e9ad2795ac826f2f3f59647817cf23c0bfa"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "134d838a2c9943ac3125cf6df165eda53493451b719f3255b2a26b85f772d0ba"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-segmentation"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View file

@ -6,3 +6,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
structopt = "0.3"

View file

@ -1,3 +1,222 @@
fn main() { use std::io::Read;
println!("Hello, world!"); use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "mpd_info_screen")]
struct Opt {
host: Ipv4Addr,
#[structopt(default_value = "6600")]
port: u16,
}
struct Shared {
art_data: Vec<u8>,
current_song: String,
current_song_length: u64,
current_song_position: u64,
thread_running: bool,
stream: TcpStream,
}
impl Shared {
fn new(stream: TcpStream) -> Self {
Self {
art_data: Vec::new(),
current_song: String::new(),
current_song_length: 0,
current_song_position: 0,
thread_running: true,
stream,
}
}
}
fn get_connection(host: Ipv4Addr, port: u16) -> Result<TcpStream, String> {
let stream = TcpStream::connect_timeout(
&SocketAddr::new(IpAddr::V4(host), port),
Duration::from_secs(5),
)
.map_err(|_| String::from("Failed to get connection"))?;
Ok(stream)
}
fn check_next_chars(
buf: &[u8],
idx: usize,
saved: &mut Vec<u8>,
) -> Result<(char, u8), (String, u8)> {
if idx >= buf.len() {
return Err((String::from("idx out of bounds"), 0u8));
}
if buf[idx] & 0b10000000 == 0 {
Ok((
char::from_u32(buf[idx] as u32)
.ok_or_else(|| (String::from("Not one-byte UTF-8"), 0u8))?,
1u8,
))
} else if buf[idx] & 0b11100000 == 0b11000000 {
if idx + 1 >= buf.len() {
saved.push(buf[idx]);
return Err((
String::from("Is two byte UTF-8, but not enough bytes provided"),
1u8,
));
}
Ok((
char::from_u32((buf[idx] as u32) | ((buf[idx + 1] as u32) << 8))
.ok_or_else(|| (String::from("Not two-byte UTF-8"), 0u8))?,
2u8,
))
} else if buf[idx] & 0b11110000 == 0b11100000 {
if idx + 2 >= buf.len() {
for tidx in idx..buf.len() {
saved.push(buf[tidx]);
}
return Err((
String::from("Is three byte UTF-8, but not enough bytes provided"),
(idx + 3 - buf.len()) as u8,
));
}
Ok((
char::from_u32(
(buf[idx] as u32) | ((buf[idx + 1] as u32) << 8) | ((buf[idx + 2] as u32) << 16),
)
.ok_or_else(|| (String::from("Not three-byte UTF-8"), 0u8))?,
3u8,
))
} else if buf[idx] & 0b11111000 == 0b11110000 {
if idx + 2 >= buf.len() {
for tidx in idx..buf.len() {
saved.push(buf[tidx]);
}
return Err((
String::from("Is four byte UTF-8, but not enough bytes provided"),
(idx + 4 - buf.len()) as u8,
));
}
Ok((
char::from_u32(
(buf[idx] as u32)
| ((buf[idx + 1] as u32) << 8)
| ((buf[idx + 2] as u32) << 16)
| ((buf[idx + 3] as u32) << 24),
)
.ok_or_else(|| (String::from("Not four-byte UTF-8"), 0u8))?,
4u8,
))
} else {
Err((String::from("Invalid UTF-8 char"), 0u8))
}
}
fn read_line(buf: &[u8], count: usize, saved: &mut Vec<u8>) -> Result<String, (String, String)> {
let mut result = String::new();
if !saved.is_empty() {
// TODO
}
saved.clear();
let mut skip_count = 0;
for idx in 0..count {
if skip_count > 0 {
skip_count -= 1;
continue;
}
let next_char_result = check_next_chars(buf, idx, saved);
if let Ok((c, s)) = next_char_result {
if c == '\n' {
return Ok(result);
}
result.push(c);
skip_count = s - 1;
} else if let Err((msg, count)) = next_char_result {
for i in 0..count {
saved.push(buf[idx + i as usize]);
}
return Err((String::from("Not enough bytes"), result));
} else {
unreachable!();
}
}
Err((String::from("Newline not reached"), result))
}
fn info_loop(shared_data: Arc<Mutex<Shared>>) -> Result<(), String> {
let mut buf: [u8; 4192] = [0; 4192];
let mut init: bool = true;
let mut saved: Vec<u8> = Vec::new();
let mut saved_str: String = String::new();
loop {
if !shared_data.lock().map_err(|_| String::from("Failed to get shared_data.thread_running"))?.thread_running {
break;
}
{
let lock_result = shared_data.try_lock();
if let Ok(mut lock) = lock_result {
let read_result = lock.stream.read(&mut buf);
if let Ok(count) = read_result {
let read_line_result = read_line(&buf, count, &mut saved);
if let Ok(mut line) = read_line_result {
line = saved_str + &line;
saved_str = String::new();
if init {
if line.starts_with("OK MPD ") {
init = false;
println!("Got initial \"OK\" from MPD");
} else {
return Err(String::from("Did not get expected init message from MPD"));
}
} else {
// TODO handling of other messages
}
} else if let Err((msg, read_line_in_progress)) = read_line_result {
println!("Error during \"read_line\": {}", msg);
saved_str = read_line_in_progress;
} else {
unreachable!();
}
}
}
}
// TODO send messages to get info
thread::sleep(Duration::from_millis(100));
}
Ok(())
}
fn main() -> Result<(), String> {
let opt = Opt::from_args();
println!("Got host addr == {}, port == {}", opt.host, opt.port);
let connection = get_connection(opt.host, opt.port)?;
connection.set_read_timeout(Some(Duration::from_millis(100))).expect("Should be able to set timeout for TcpStream reads");
let shared_data = Arc::new(Mutex::new(Shared::new(connection)));
let thread_shared_data = shared_data.clone();
let child = thread::spawn(move || {
info_loop(thread_shared_data).expect("Failure during info_loop");
});
thread::sleep(Duration::from_secs(5));
println!("Stopping thread...");
shared_data.lock().map_err(|_| String::from("Failed to get shared_data.thread_running in main"))?.thread_running = false;
println!("Waiting on thread...");
thread::sleep(Duration::from_secs(5));
println!("Joining on thread...");
child.join().expect("Should be able to join on thread");
Ok(())
} }