From a8bb696be89476470d90cdc832c8a109b284afe6 Mon Sep 17 00:00:00 2001 From: awiouy Date: Sat, 10 Feb 2018 08:23:46 +0100 Subject: [PATCH 1/8] core API: MercurySender::new --- core/src/mercury/sender.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/mercury/sender.rs b/core/src/mercury/sender.rs index 67b4dc08..359e6907 100644 --- a/core/src/mercury/sender.rs +++ b/core/src/mercury/sender.rs @@ -11,7 +11,7 @@ pub struct MercurySender { impl MercurySender { // TODO: pub(super) when stable - pub fn new(mercury: MercuryManager, uri: String) -> MercurySender { + pub(crate) fn new(mercury: MercuryManager, uri: String) -> MercurySender { MercurySender { mercury: mercury, uri: uri, From 72cef9a10c331f4abbcfa0f9fa951276619deaae Mon Sep 17 00:00:00 2001 From: awiouy Date: Sat, 10 Feb 2018 09:52:30 +0100 Subject: [PATCH 2/8] core API: Session.config() --- core/src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/session.rs b/core/src/session.rs index e3b474bf..37a4aba0 100644 --- a/core/src/session.rs +++ b/core/src/session.rs @@ -184,7 +184,7 @@ impl Session { self.0.cache.as_ref() } - pub fn config(&self) -> &SessionConfig { + fn config(&self) -> &SessionConfig { &self.0.config } From ae85e69aca4ab5eedd3ebbf63164ad30c3202ec9 Mon Sep 17 00:00:00 2001 From: awiouy Date: Sat, 10 Feb 2018 10:10:26 +0100 Subject: [PATCH 3/8] core API: Session.weak() --- core/src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/session.rs b/core/src/session.rs index 37a4aba0..4eecbe95 100644 --- a/core/src/session.rs +++ b/core/src/session.rs @@ -200,7 +200,7 @@ impl Session { &self.config().device_id } - pub fn weak(&self) -> SessionWeak { + fn weak(&self) -> SessionWeak { SessionWeak(Arc::downgrade(&self.0)) } From 55f27a9e0a26256e725d1c93805c2359ba027fa0 Mon Sep 17 00:00:00 2001 From: awiouy Date: Sat, 10 Feb 2018 10:22:03 +0100 Subject: [PATCH 4/8] core API: SessionWeak.try_upgrade(), SessionWeak.upgrade() --- core/src/session.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/session.rs b/core/src/session.rs index 4eecbe95..9fe785ed 100644 --- a/core/src/session.rs +++ b/core/src/session.rs @@ -213,11 +213,11 @@ impl Session { pub struct SessionWeak(pub Weak); impl SessionWeak { - pub fn try_upgrade(&self) -> Option { + fn try_upgrade(&self) -> Option { self.0.upgrade().map(Session) } - pub fn upgrade(&self) -> Session { + pub(crate) fn upgrade(&self) -> Session { self.try_upgrade().expect("Session died") } } From 77882836ce32bd475f8fec7d11c476153e551f9d Mon Sep 17 00:00:00 2001 From: awiouy Date: Sat, 10 Feb 2018 11:01:19 +0100 Subject: [PATCH 5/8] core API: move now_ms to spirc.rs --- core/src/util/mod.rs | 9 --------- src/spirc.rs | 11 ++++++++++- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/src/util/mod.rs b/core/src/util/mod.rs index 01a3a506..f9304f0b 100644 --- a/core/src/util/mod.rs +++ b/core/src/util/mod.rs @@ -8,7 +8,6 @@ use std::ops::{Mul, Rem, Shr}; use std::fs; use std::path::Path; use std::process::Command; -use std::time::{UNIX_EPOCH, SystemTime}; mod int128; mod spotify_id; @@ -22,14 +21,6 @@ pub fn rand_vec(rng: &mut G, size: usize) -> Vec { rng.gen_iter().take(size).collect() } -pub fn now_ms() -> i64 { - let dur = match SystemTime::now().duration_since(UNIX_EPOCH) { - Ok(dur) => dur, - Err(err) => err.duration(), - }; - (dur.as_secs() * 1000 + (dur.subsec_nanos() / 1000_000) as u64) as i64 -} - pub fn mkdir_existing(path: &Path) -> io::Result<()> { fs::create_dir(path).or_else(|err| { if err.kind() == io::ErrorKind::AlreadyExists { diff --git a/src/spirc.rs b/src/spirc.rs index 59b6b841..4e9e2991 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -6,7 +6,7 @@ use protobuf::{self, Message}; use core::config::ConnectConfig; use core::mercury::MercuryError; use core::session::Session; -use core::util::{now_ms, SpotifyId, SeqGenerator}; +use core::util::{SpotifyId, SeqGenerator}; use core::version; use protocol; @@ -18,6 +18,7 @@ use playback::player::Player; use std; use rand; use rand::Rng; +use std::time::{UNIX_EPOCH, SystemTime}; pub struct SpircTask { player: Player, @@ -53,6 +54,14 @@ pub struct Spirc { commands: mpsc::UnboundedSender, } +fn now_ms() -> i64 { + let dur = match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(dur) => dur, + Err(err) => err.duration(), + }; + (dur.as_secs() * 1000 + (dur.subsec_nanos() / 1000_000) as u64) as i64 +} + fn initial_state() -> State { protobuf_init!(protocol::spirc::State::new(), { repeat: false, From d7fa1464ffd3fc0dc4cabb1442f27be2d5adf17c Mon Sep 17 00:00:00 2001 From: awiouy Date: Sat, 10 Feb 2018 11:26:26 +0100 Subject: [PATCH 6/8] core API: move mkdir_existing to cache.rs --- core/src/cache.rs | 19 ++++++++++++++++--- core/src/util/mod.rs | 13 ------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/core/src/cache.rs b/core/src/cache.rs index 28b787ff..1a58bdc0 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -1,8 +1,11 @@ -use std::path::PathBuf; -use std::io::Read; +use std::fs; use std::fs::File; +use std::io; +use std::io::Read; +use std::path::Path; +use std::path::PathBuf; -use util::{FileId, mkdir_existing}; +use util::FileId; use authentication::Credentials; #[derive(Clone)] @@ -11,6 +14,16 @@ pub struct Cache { use_audio_cache: bool, } +fn mkdir_existing(path: &Path) -> io::Result<()> { + fs::create_dir(path).or_else(|err| { + if err.kind() == io::ErrorKind::AlreadyExists { + Ok(()) + } else { + Err(err) + } + }) +} + impl Cache { pub fn new(location: PathBuf, use_audio_cache: bool) -> Cache { mkdir_existing(&location).unwrap(); diff --git a/core/src/util/mod.rs b/core/src/util/mod.rs index f9304f0b..f96a5431 100644 --- a/core/src/util/mod.rs +++ b/core/src/util/mod.rs @@ -2,11 +2,8 @@ use num_bigint::BigUint; use num_traits::{Zero, One}; use num_integer::Integer; use rand::{Rng, Rand}; -use std::io; use std::mem; use std::ops::{Mul, Rem, Shr}; -use std::fs; -use std::path::Path; use std::process::Command; mod int128; @@ -21,16 +18,6 @@ pub fn rand_vec(rng: &mut G, size: usize) -> Vec { rng.gen_iter().take(size).collect() } -pub fn mkdir_existing(path: &Path) -> io::Result<()> { - fs::create_dir(path).or_else(|err| { - if err.kind() == io::ErrorKind::AlreadyExists { - Ok(()) - } else { - Err(err) - } - }) -} - pub fn run_program(program: &str) { info!("Running {}", program); let mut v: Vec<&str> = program.split_whitespace().collect(); From 496a802248af48c1c3ba88e68f9f5bf81a0846ed Mon Sep 17 00:00:00 2001 From: awiouy Date: Sat, 10 Feb 2018 16:26:08 +0100 Subject: [PATCH 7/8] core API: move subfile.rs to player.rs --- core/src/util/mod.rs | 2 -- core/src/util/subfile.rs | 38 ------------------------------------ playback/src/player.rs | 42 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 42 deletions(-) delete mode 100644 core/src/util/subfile.rs diff --git a/core/src/util/mod.rs b/core/src/util/mod.rs index f96a5431..d6cbdd68 100644 --- a/core/src/util/mod.rs +++ b/core/src/util/mod.rs @@ -8,11 +8,9 @@ use std::process::Command; mod int128; mod spotify_id; -mod subfile; pub use util::int128::u128; pub use util::spotify_id::{SpotifyId, FileId}; -pub use util::subfile::Subfile; pub fn rand_vec(rng: &mut G, size: usize) -> Vec { rng.gen_iter().take(size).collect() diff --git a/core/src/util/subfile.rs b/core/src/util/subfile.rs deleted file mode 100644 index 81d5d916..00000000 --- a/core/src/util/subfile.rs +++ /dev/null @@ -1,38 +0,0 @@ -use std::io::{Read, Seek, SeekFrom, Result}; - -pub struct Subfile { - stream: T, - offset: u64, -} - -impl Subfile { - pub fn new(mut stream: T, offset: u64) -> Subfile { - stream.seek(SeekFrom::Start(offset)).unwrap(); - Subfile { - stream: stream, - offset: offset, - } - } -} - -impl Read for Subfile { - fn read(&mut self, buf: &mut [u8]) -> Result { - self.stream.read(buf) - } -} - -impl Seek for Subfile { - fn seek(&mut self, mut pos: SeekFrom) -> Result { - pos = match pos { - SeekFrom::Start(offset) => SeekFrom::Start(offset + self.offset), - x => x, - }; - - let newpos = try!(self.stream.seek(pos)); - if newpos > self.offset { - Ok(newpos - self.offset) - } else { - Ok(0) - } - } -} diff --git a/playback/src/player.rs b/playback/src/player.rs index ffffa130..3958ab8a 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -1,15 +1,16 @@ use futures::sync::oneshot; use futures::{future, Future}; +use std; use std::borrow::Cow; +use std::io::{Read, Seek, SeekFrom, Result}; use std::mem; use std::sync::mpsc::{RecvError, TryRecvError, RecvTimeoutError}; use std::thread; use std::time::Duration; -use std; use core::config::{Bitrate, PlayerConfig}; use core::session::Session; -use core::util::{self, SpotifyId, Subfile}; +use core::util::{self, SpotifyId}; use audio_backend::Sink; use audio::{AudioFile, AudioDecrypt}; @@ -478,3 +479,40 @@ impl ::std::fmt::Debug for PlayerCommand { } } } + +struct Subfile { + stream: T, + offset: u64, +} + +impl Subfile { + pub fn new(mut stream: T, offset: u64) -> Subfile { + stream.seek(SeekFrom::Start(offset)).unwrap(); + Subfile { + stream: stream, + offset: offset, + } + } +} + +impl Read for Subfile { + fn read(&mut self, buf: &mut [u8]) -> Result { + self.stream.read(buf) + } +} + +impl Seek for Subfile { + fn seek(&mut self, mut pos: SeekFrom) -> Result { + pos = match pos { + SeekFrom::Start(offset) => SeekFrom::Start(offset + self.offset), + x => x, + }; + + let newpos = try!(self.stream.seek(pos)); + if newpos > self.offset { + Ok(newpos - self.offset) + } else { + Ok(0) + } + } +} From a35edc6af46a558d1f3325c9d056209713b5ce58 Mon Sep 17 00:00:00 2001 From: awiouy Date: Sat, 10 Feb 2018 17:18:54 +0100 Subject: [PATCH 8/8] core API: move run_program to player.rs --- core/src/util/mod.rs | 11 ----------- playback/src/player.rs | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/core/src/util/mod.rs b/core/src/util/mod.rs index d6cbdd68..6e21a0f2 100644 --- a/core/src/util/mod.rs +++ b/core/src/util/mod.rs @@ -4,7 +4,6 @@ use num_integer::Integer; use rand::{Rng, Rand}; use std::mem; use std::ops::{Mul, Rem, Shr}; -use std::process::Command; mod int128; mod spotify_id; @@ -16,16 +15,6 @@ pub fn rand_vec(rng: &mut G, size: usize) -> Vec { rng.gen_iter().take(size).collect() } -pub fn run_program(program: &str) { - info!("Running {}", program); - let mut v: Vec<&str> = program.split_whitespace().collect(); - let status = Command::new(&v.remove(0)) - .args(&v) - .status() - .expect("program failed to start"); - info!("Exit status: {}", status); -} - pub fn powm(base: &BigUint, exp: &BigUint, modulus: &BigUint) -> BigUint { let mut base = base.clone(); let mut exp = exp.clone(); diff --git a/playback/src/player.rs b/playback/src/player.rs index 3958ab8a..f0ee5d22 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -4,13 +4,14 @@ use std; use std::borrow::Cow; use std::io::{Read, Seek, SeekFrom, Result}; use std::mem; +use std::process::Command; use std::sync::mpsc::{RecvError, TryRecvError, RecvTimeoutError}; use std::thread; use std::time::Duration; use core::config::{Bitrate, PlayerConfig}; use core::session::Session; -use core::util::{self, SpotifyId}; +use core::util::SpotifyId; use audio_backend::Sink; use audio::{AudioFile, AudioDecrypt}; @@ -376,13 +377,13 @@ impl PlayerInternal { fn run_onstart(&self) { if let Some(ref program) = self.config.onstart { - util::run_program(program) + run_program(program) } } fn run_onstop(&self) { if let Some(ref program) = self.config.onstop { - util::run_program(program) + run_program(program) } } @@ -516,3 +517,13 @@ impl Seek for Subfile { } } } + +fn run_program(program: &str) { + info!("Running {}", program); + let mut v: Vec<&str> = program.split_whitespace().collect(); + let status = Command::new(&v.remove(0)) + .args(&v) + .status() + .expect("program failed to start"); + info!("Exit status: {}", status); +}