瀏覽代碼

Encapsulate the session data and configuration.

Paul Lietar 9 年之前
父節點
當前提交
7a1db5f5a9
共有 7 個文件被更改,包括 33 次插入18 次删除
  1. 1 1
      src/audio_file.rs
  2. 6 6
      src/authentication.rs
  3. 3 3
      src/discovery.rs
  4. 2 2
      src/metadata.rs
  5. 1 1
      src/player.rs
  6. 17 1
      src/session.rs
  7. 3 4
      src/spirc.rs

+ 1 - 1
src/audio_file.rs

@@ -219,7 +219,7 @@ impl AudioFileManager {
 
     pub fn cache_dir(session: &Session, file_id: FileId) -> PathBuf {
         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 {

+ 6 - 6
src/authentication.rs

@@ -55,14 +55,14 @@ impl Session {
                 cpu_family: protocol::authentication::CpuFamily::CPU_UNKNOWN,
                 os: protocol::authentication::Os::OS_UNKNOWN,
                 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(),
             appkey => {
-                version: self.0.config.application_key[0] as u32,
-                devkey: self.0.config.application_key[0x1..0x81].to_vec(),
-                signature: self.0.config.application_key[0x81..0x141].to_vec(),
-                useragent: self.0.config.user_agent.clone(),
+                version: self.config().application_key[0] as u32,
+                devkey: self.config().application_key[0x1..0x81].to_vec(),
+                signature: self.config().application_key[0x81..0x141].to_vec(),
+                useragent: self.config().user_agent.clone(),
                 callback_hash: vec![0; 20],
             }
         });
@@ -106,7 +106,7 @@ impl Session {
         let secret = {
             let mut data = [0u8; 20];
             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);
             data
         };

+ 3 - 3
src/discovery.rs

@@ -42,8 +42,8 @@ impl DiscoveryManager {
             "statusString": "ERROR-OK",
             "spotifyError": 0,
             "version": "2.1.0",
-            "deviceID": (self.session.0.data.read().unwrap().device_id),
-            "remoteName": (self.session.0.config.device_name),
+            "deviceID": (self.session.device_id()),
+            "remoteName": (self.session.config().device_name),
             "activeUser": "",
             "publicKey": (public_key),
             "deviceType": "UNKNOWN",
@@ -118,7 +118,7 @@ impl DiscoveryManager {
 
     pub fn run(&mut self) {
         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",
                                        None,
                                        None,

+ 2 - 2
src/metadata.rs

@@ -71,7 +71,7 @@ impl MetadataTrait for Track {
     }
 
     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()
                          .iter()
@@ -157,7 +157,7 @@ impl MetadataTrait for Artist {
     }
 
     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()
                             .iter()

+ 1 - 1
src/player.rs

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

+ 17 - 1
src/session.rs

@@ -43,7 +43,7 @@ pub struct SessionData {
 }
 
 pub struct SessionInternal {
-    pub config: Config,
+    config: Config,
     pub data: RwLock<SessionData>,
 
     mercury: Mutex<MercuryManager>,
@@ -224,4 +224,20 @@ impl Session {
     pub fn mercury_sub(&self, uri: String) -> mpsc::Receiver<MercuryResponse> {
         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()
+    }
 }

+ 3 - 4
src/spirc.rs

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