Encapsulate the session data and configuration.

This commit is contained in:
Paul Lietar 2016-01-26 22:34:57 +00:00
parent 9747bbc470
commit 7a1db5f5a9
7 changed files with 33 additions and 18 deletions

View file

@ -219,7 +219,7 @@ impl AudioFileManager {
pub fn cache_dir(session: &Session, file_id: FileId) -> PathBuf { pub fn cache_dir(session: &Session, file_id: FileId) -> PathBuf {
let name = file_id.to_base16(); let name = file_id.to_base16();
session.0.config.cache_location.join(&name[0..2]) session.config().cache_location.join(&name[0..2])
} }
pub fn cache_path(session: &Session, file_id: FileId) -> PathBuf { pub fn cache_path(session: &Session, file_id: FileId) -> PathBuf {

View file

@ -55,14 +55,14 @@ impl Session {
cpu_family: protocol::authentication::CpuFamily::CPU_UNKNOWN, cpu_family: protocol::authentication::CpuFamily::CPU_UNKNOWN,
os: protocol::authentication::Os::OS_UNKNOWN, os: protocol::authentication::Os::OS_UNKNOWN,
system_information_string: "librespot".to_owned(), system_information_string: "librespot".to_owned(),
device_id: self.0.data.read().unwrap().device_id.clone() device_id: self.device_id().clone()
}, },
version_string: util::version::version_string(), version_string: util::version::version_string(),
appkey => { appkey => {
version: self.0.config.application_key[0] as u32, version: self.config().application_key[0] as u32,
devkey: self.0.config.application_key[0x1..0x81].to_vec(), devkey: self.config().application_key[0x1..0x81].to_vec(),
signature: self.0.config.application_key[0x81..0x141].to_vec(), signature: self.config().application_key[0x81..0x141].to_vec(),
useragent: self.0.config.user_agent.clone(), useragent: self.config().user_agent.clone(),
callback_hash: vec![0; 20], callback_hash: vec![0; 20],
} }
}); });
@ -106,7 +106,7 @@ impl Session {
let secret = { let secret = {
let mut data = [0u8; 20]; let mut data = [0u8; 20];
let mut h = crypto::sha1::Sha1::new(); let mut h = crypto::sha1::Sha1::new();
h.input(&self.0.data.read().unwrap().device_id.as_bytes()); h.input(&self.device_id().as_bytes());
h.result(&mut data); h.result(&mut data);
data data
}; };

View file

@ -42,8 +42,8 @@ impl DiscoveryManager {
"statusString": "ERROR-OK", "statusString": "ERROR-OK",
"spotifyError": 0, "spotifyError": 0,
"version": "2.1.0", "version": "2.1.0",
"deviceID": (self.session.0.data.read().unwrap().device_id), "deviceID": (self.session.device_id()),
"remoteName": (self.session.0.config.device_name), "remoteName": (self.session.config().device_name),
"activeUser": "", "activeUser": "",
"publicKey": (public_key), "publicKey": (public_key),
"deviceType": "UNKNOWN", "deviceType": "UNKNOWN",
@ -118,7 +118,7 @@ impl DiscoveryManager {
pub fn run(&mut self) { pub fn run(&mut self) {
let server = Server::http("0.0.0.0:8000").unwrap(); let server = Server::http("0.0.0.0:8000").unwrap();
let svc = DNSService::register(Some(&self.session.0.config.device_name), let svc = DNSService::register(Some(&self.session.config().device_name),
"_spotify-connect._tcp", "_spotify-connect._tcp",
None, None,
None, None,

View file

@ -71,7 +71,7 @@ impl MetadataTrait for Track {
} }
fn parse(msg: &Self::Message, session: &Session) -> Self { fn parse(msg: &Self::Message, session: &Session) -> Self {
let country = session.0.data.read().unwrap().country.clone(); let country = session.country();
let artists = msg.get_artist() let artists = msg.get_artist()
.iter() .iter()
@ -157,7 +157,7 @@ impl MetadataTrait for Artist {
} }
fn parse(msg: &Self::Message, session: &Session) -> Self { fn parse(msg: &Self::Message, session: &Session) -> Self {
let country = session.0.data.read().unwrap().country.clone(); let country = session.country();
let top_tracks = msg.get_top_track() let top_tracks = msg.get_top_track()
.iter() .iter()

View file

@ -168,7 +168,7 @@ impl PlayerInternal {
.unwrap(); .unwrap();
} }
let format = match self.session.0.config.bitrate { let format = match self.session.config().bitrate {
Bitrate::Bitrate96 => FileFormat::OGG_VORBIS_96, Bitrate::Bitrate96 => FileFormat::OGG_VORBIS_96,
Bitrate::Bitrate160 => FileFormat::OGG_VORBIS_160, Bitrate::Bitrate160 => FileFormat::OGG_VORBIS_160,
Bitrate::Bitrate320 => FileFormat::OGG_VORBIS_320, Bitrate::Bitrate320 => FileFormat::OGG_VORBIS_320,

View file

@ -43,7 +43,7 @@ pub struct SessionData {
} }
pub struct SessionInternal { pub struct SessionInternal {
pub config: Config, config: Config,
pub data: RwLock<SessionData>, pub data: RwLock<SessionData>,
mercury: Mutex<MercuryManager>, mercury: Mutex<MercuryManager>,
@ -224,4 +224,20 @@ impl Session {
pub fn mercury_sub(&self, uri: String) -> mpsc::Receiver<MercuryResponse> { pub fn mercury_sub(&self, uri: String) -> mpsc::Receiver<MercuryResponse> {
self.0.mercury.lock().unwrap().subscribe(self, uri) self.0.mercury.lock().unwrap().subscribe(self, uri)
} }
pub fn config(&self) -> &Config {
&self.0.config
}
pub fn username(&self) -> String {
self.0.data.read().unwrap().canonical_username.clone()
}
pub fn country(&self) -> String {
self.0.data.read().unwrap().country.clone()
}
pub fn device_id(&self) -> String {
self.0.data.read().unwrap().device_id.clone()
}
} }

View file

@ -41,8 +41,8 @@ struct SpircInternal {
impl SpircManager { impl SpircManager {
pub fn new(session: Session, player: Player) -> SpircManager { pub fn new(session: Session, player: Player) -> SpircManager {
let ident = session.0.data.read().unwrap().device_id.clone(); let ident = session.device_id();
let name = session.0.config.device_name.clone(); let name = session.config().device_name.clone();
SpircManager(Arc::new(Mutex::new(SpircInternal { SpircManager(Arc::new(Mutex::new(SpircInternal {
player: player, player: player,
@ -350,7 +350,6 @@ impl SpircInternal {
} }
fn uri(&self) -> String { fn uri(&self) -> String {
format!("hm://remote/user/{}", format!("hm://remote/user/{}", self.session.username())
self.session.0.data.read().unwrap().canonical_username.clone())
} }
} }