2015-06-23 14:38:29 +00:00
|
|
|
use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt};
|
2015-09-01 11:20:37 +00:00
|
|
|
use eventual;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::{Cursor, Read, Write};
|
2015-07-02 21:05:47 +00:00
|
|
|
use std::mem;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
use util::{SpotifyId, FileId};
|
2015-07-02 17:24:25 +00:00
|
|
|
use session::Session;
|
|
|
|
use connection::PacketHandler;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
|
|
|
pub type AudioKey = [u8; 16];
|
2015-07-02 21:05:47 +00:00
|
|
|
|
|
|
|
#[derive(Debug,Hash,PartialEq,Eq,Clone)]
|
|
|
|
struct AudioKeyId(SpotifyId, FileId);
|
|
|
|
|
|
|
|
enum AudioKeyStatus {
|
2015-09-01 11:20:37 +00:00
|
|
|
Loading(Vec<eventual::Complete<AudioKey, ()>>),
|
2015-07-02 21:05:47 +00:00
|
|
|
Loaded(AudioKey)
|
|
|
|
}
|
2015-07-02 17:24:25 +00:00
|
|
|
|
2015-06-23 14:38:29 +00:00
|
|
|
pub struct AudioKeyManager {
|
2015-07-02 21:05:47 +00:00
|
|
|
next_seq: u32,
|
|
|
|
pending: HashMap<u32, AudioKeyId>,
|
|
|
|
cache: HashMap<AudioKeyId, AudioKeyStatus>,
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioKeyManager {
|
2015-07-02 17:24:25 +00:00
|
|
|
pub fn new() -> AudioKeyManager {
|
|
|
|
AudioKeyManager {
|
2015-06-23 14:38:29 +00:00
|
|
|
next_seq: 1,
|
2015-07-02 21:05:47 +00:00
|
|
|
pending: HashMap::new(),
|
|
|
|
cache: HashMap::new()
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
pub fn request(&mut self, session: &Session, track: SpotifyId, file: FileId)
|
2015-09-01 11:20:37 +00:00
|
|
|
-> eventual::Future<AudioKey, ()> {
|
2015-07-02 17:24:25 +00:00
|
|
|
|
2015-07-02 21:05:47 +00:00
|
|
|
let id = AudioKeyId(track, file);
|
2015-09-01 11:20:37 +00:00
|
|
|
self.cache.get_mut(&id).map(|status| match *status {
|
|
|
|
AudioKeyStatus::Loaded(key) => {
|
|
|
|
eventual::Future::of(key.clone())
|
2015-07-02 21:05:47 +00:00
|
|
|
}
|
2015-09-01 11:20:37 +00:00
|
|
|
AudioKeyStatus::Loading(ref mut req) => {
|
|
|
|
let (tx, rx) = eventual::Future::pair();
|
|
|
|
req.push(tx);
|
|
|
|
rx
|
2015-07-02 21:05:47 +00:00
|
|
|
}
|
|
|
|
}).unwrap_or_else(|| {
|
|
|
|
let seq = self.next_seq;
|
|
|
|
self.next_seq += 1;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 21:05:47 +00:00
|
|
|
let mut data : Vec<u8> = Vec::new();
|
2015-07-07 21:40:31 +00:00
|
|
|
data.write(&file.0).unwrap();
|
2015-07-02 21:05:47 +00:00
|
|
|
data.write(&track.to_raw()).unwrap();
|
|
|
|
data.write_u32::<BigEndian>(seq).unwrap();
|
|
|
|
data.write_u16::<BigEndian>(0x0000).unwrap();
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 21:05:47 +00:00
|
|
|
session.send_packet(0xc, &data).unwrap();
|
2015-07-02 17:24:25 +00:00
|
|
|
|
2015-07-02 21:05:47 +00:00
|
|
|
self.pending.insert(seq, id.clone());
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
let (tx, rx) = eventual::Future::pair();
|
|
|
|
self.cache.insert(id, AudioKeyStatus::Loading(vec!{ tx }));
|
|
|
|
rx
|
2015-07-02 21:05:47 +00:00
|
|
|
})
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
impl PacketHandler for AudioKeyManager {
|
|
|
|
fn handle(&mut self, cmd: u8, data: Vec<u8>) {
|
|
|
|
assert_eq!(cmd, 0xd);
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
let mut data = Cursor::new(data);
|
2015-06-23 14:38:29 +00:00
|
|
|
let seq = data.read_u32::<BigEndian>().unwrap();
|
|
|
|
let mut key = [0u8; 16];
|
2015-09-01 11:20:37 +00:00
|
|
|
data.read_exact(&mut key).unwrap();
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 21:05:47 +00:00
|
|
|
if let Some(status) = self.pending.remove(&seq).and_then(|id| { self.cache.get_mut(&id) }) {
|
|
|
|
let status = mem::replace(status, AudioKeyStatus::Loaded(key));
|
|
|
|
|
|
|
|
if let AudioKeyStatus::Loading(cbs) = status {
|
|
|
|
for cb in cbs {
|
2015-09-01 11:20:37 +00:00
|
|
|
cb.complete(key);
|
2015-07-02 21:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|