|
| 1 | +extern crate tempdir; |
| 2 | + |
| 3 | +use self::tempdir::TempDir; |
| 4 | +use super::mpd; |
| 5 | +use std::fs::{File, create_dir}; |
| 6 | +use std::io::Write; |
| 7 | +use std::os::unix::net::UnixStream; |
| 8 | +use std::path::{Path, PathBuf}; |
| 9 | +use std::process::{Command, Child}; |
| 10 | + |
| 11 | +struct MpdConfig { |
| 12 | + db_file: PathBuf, |
| 13 | + music_directory: PathBuf, |
| 14 | + playlist_directory: PathBuf, |
| 15 | + config_path: PathBuf, |
| 16 | + sock_path: PathBuf, |
| 17 | +} |
| 18 | + |
| 19 | +impl MpdConfig { |
| 20 | + pub fn new<P>(base: P) -> MpdConfig |
| 21 | + where P: AsRef<Path> |
| 22 | + { |
| 23 | + let base = base.as_ref(); |
| 24 | + MpdConfig { |
| 25 | + db_file: base.join("db"), |
| 26 | + music_directory: base.join("music"), |
| 27 | + playlist_directory: base.join("playlists"), |
| 28 | + config_path: base.join("config"), |
| 29 | + sock_path: base.join("sock"), |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +fn write_config(config: &MpdConfig) { |
| 35 | + let config_text = format!(r#" |
| 36 | +db_file "{db_file}" |
| 37 | +log_file "/dev/null" |
| 38 | +music_directory "{music_directory}" |
| 39 | +playlist_directory "{playlist_directory}" |
| 40 | +bind_to_address "{sock_path}" |
| 41 | +audio_output {{ |
| 42 | + type "null" |
| 43 | + name "null" |
| 44 | +}} |
| 45 | +"#, |
| 46 | +db_file=config.db_file.display(), |
| 47 | +music_directory=config.music_directory.display(), |
| 48 | +playlist_directory=config.playlist_directory.display(), |
| 49 | +sock_path=config.sock_path.display(), |
| 50 | +); |
| 51 | + create_dir(&config.music_directory).expect("Could not create music directory."); |
| 52 | + create_dir(&config.playlist_directory).expect("Could not create playlist directory."); |
| 53 | + let mut file = File::create(&config.config_path).expect("Could not create config file."); |
| 54 | + file.write_all(config_text.as_bytes()).expect("Could not write config file."); |
| 55 | +} |
| 56 | + |
| 57 | +pub struct Daemon { |
| 58 | + // Saved here so it gets dropped when this does. |
| 59 | + _temp_dir: TempDir, |
| 60 | + config: MpdConfig, |
| 61 | + process: Child, |
| 62 | +} |
| 63 | + |
| 64 | +impl Drop for Daemon { |
| 65 | + fn drop(&mut self) { |
| 66 | + self.process.kill().expect("Could not kill mpd daemon."); |
| 67 | + self.process.wait().expect("Could not wait for mpd daemon to shutdown."); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fn sleep() { |
| 72 | + use std::{thread, time}; |
| 73 | + let ten_millis = time::Duration::from_millis(100); |
| 74 | + thread::sleep(ten_millis); |
| 75 | +} |
| 76 | + |
| 77 | +impl Daemon { |
| 78 | + #[cfg_attr(feature="cargo-clippy", allow(new_without_default))] |
| 79 | + pub fn new() -> Daemon { |
| 80 | + let temp_dir = TempDir::new("mpd-test").unwrap(); |
| 81 | + let config = MpdConfig::new(&temp_dir); |
| 82 | + write_config(&config); |
| 83 | + let process = Command::new("mpd") |
| 84 | + .arg("--no-daemon") |
| 85 | + .arg(&config.config_path) |
| 86 | + .spawn() |
| 87 | + .expect("Could not create mpd daemon."); |
| 88 | + while !config.sock_path.exists() {} |
| 89 | + |
| 90 | + // FIXME: Wait for mpd to finish updating the database. |
| 91 | + sleep(); |
| 92 | + |
| 93 | + Daemon { |
| 94 | + _temp_dir: temp_dir, |
| 95 | + config: config, |
| 96 | + process: process, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + pub fn connect(&self) -> mpd::Client<UnixStream> { |
| 101 | + let stream = UnixStream::connect(&self.config.sock_path).unwrap(); |
| 102 | + mpd::Client::new(stream).unwrap() |
| 103 | + } |
| 104 | +} |
0 commit comments