cache audio keys in memory

This commit is contained in:
Paul Lietar 2015-07-02 23:05:47 +02:00
parent f0bb24d23f
commit 28a7db26be

View file

@ -1,33 +1,53 @@
use std::collections::HashMap; use std::collections::{HashMap, LinkedList};
use std::sync::{mpsc, Future}; use std::sync::{mpsc, Future};
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};
use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt}; use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt};
use readall::ReadAllExt; use readall::ReadAllExt;
use std::mem;
use util::{SpotifyId, FileId, IgnoreExt}; use util::{SpotifyId, FileId, IgnoreExt};
use session::Session; use session::Session;
use connection::PacketHandler; use connection::PacketHandler;
pub type AudioKey = [u8; 16]; pub type AudioKey = [u8; 16];
type AudioKeyId = u32;
#[derive(Debug,Hash,PartialEq,Eq,Clone)]
struct AudioKeyId(SpotifyId, FileId);
enum AudioKeyStatus {
Loading(LinkedList<mpsc::Sender<AudioKey>>),
Loaded(AudioKey)
}
pub struct AudioKeyManager { pub struct AudioKeyManager {
next_seq: AudioKeyId, next_seq: u32,
callbacks: HashMap<AudioKeyId, mpsc::Sender<AudioKey>>, pending: HashMap<u32, AudioKeyId>,
cache: HashMap<AudioKeyId, AudioKeyStatus>,
} }
impl AudioKeyManager { impl AudioKeyManager {
pub fn new() -> AudioKeyManager { pub fn new() -> AudioKeyManager {
AudioKeyManager { AudioKeyManager {
next_seq: 1, next_seq: 1,
callbacks: HashMap::new(), pending: HashMap::new(),
cache: HashMap::new()
} }
} }
pub fn request(&mut self, session: &Session, track: SpotifyId, file: FileId) pub fn request(&mut self, session: &Session, track: SpotifyId, file: FileId)
-> Future<AudioKey> { -> Future<AudioKey> {
let (tx, rx) = mpsc::channel();
let id = AudioKeyId(track, file);
self.cache.get_mut(&id).map(|status| match status {
&mut AudioKeyStatus::Loaded(key) => {
Future::from_value(key.clone())
}
&mut AudioKeyStatus::Loading(ref mut req) => {
let (tx, rx) = mpsc::channel();
req.push_front(tx);
Future::from_receiver(rx)
}
}).unwrap_or_else(|| {
let seq = self.next_seq; let seq = self.next_seq;
self.next_seq += 1; self.next_seq += 1;
@ -39,9 +59,14 @@ impl AudioKeyManager {
session.send_packet(0xc, &data).unwrap(); session.send_packet(0xc, &data).unwrap();
self.callbacks.insert(seq, tx); self.pending.insert(seq, id.clone());
let (tx, rx) = mpsc::channel();
let mut req = LinkedList::new();
req.push_front(tx);
self.cache.insert(id, AudioKeyStatus::Loading(req));
Future::from_receiver(rx) Future::from_receiver(rx)
})
} }
} }
@ -54,10 +79,15 @@ impl PacketHandler for AudioKeyManager {
let mut key = [0u8; 16]; let mut key = [0u8; 16];
data.read_all(&mut key).unwrap(); data.read_all(&mut key).unwrap();
match self.callbacks.remove(&seq) { if let Some(status) = self.pending.remove(&seq).and_then(|id| { self.cache.get_mut(&id) }) {
Some(callback) => callback.send(key).ignore(), let status = mem::replace(status, AudioKeyStatus::Loaded(key));
None => ()
}; if let AudioKeyStatus::Loading(cbs) = status {
for cb in cbs {
cb.send(key).unwrap();
}
}
}
} }
} }