2015-06-23 14:38:29 +00:00
|
|
|
use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt};
|
|
|
|
use protobuf::{self, Message};
|
|
|
|
use readall::ReadAllExt;
|
|
|
|
use std::collections::{HashMap, LinkedList};
|
|
|
|
use std::io::{Cursor, Read, Write};
|
|
|
|
use std::fmt;
|
|
|
|
use std::mem::replace;
|
2015-07-02 17:24:25 +00:00
|
|
|
use std::sync::{mpsc, Future};
|
2015-06-23 14:38:29 +00:00
|
|
|
|
|
|
|
use librespot_protocol as protocol;
|
2015-07-02 17:24:25 +00:00
|
|
|
use session::Session;
|
|
|
|
use connection::PacketHandler;
|
|
|
|
use util::IgnoreExt;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-01 17:49:03 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2015-06-23 14:38:29 +00:00
|
|
|
pub enum MercuryMethod {
|
|
|
|
GET,
|
|
|
|
SUB,
|
|
|
|
UNSUB,
|
2015-07-01 22:40:38 +00:00
|
|
|
SEND,
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MercuryRequest {
|
|
|
|
pub method: MercuryMethod,
|
2015-07-01 17:49:03 +00:00
|
|
|
pub uri: String,
|
|
|
|
pub content_type: Option<String>,
|
2015-07-01 22:40:38 +00:00
|
|
|
pub payload: Vec<Vec<u8>>
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MercuryResponse {
|
2015-07-01 17:49:03 +00:00
|
|
|
pub uri: String,
|
2015-06-23 14:38:29 +00:00
|
|
|
pub payload: LinkedList<Vec<u8>>
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MercuryPending {
|
|
|
|
parts: LinkedList<Vec<u8>>,
|
|
|
|
partial: Option<Vec<u8>>,
|
2015-07-02 17:24:25 +00:00
|
|
|
callback: Option<mpsc::Sender<MercuryResponse>>
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MercuryManager {
|
|
|
|
next_seq: u32,
|
|
|
|
pending: HashMap<Vec<u8>, MercuryPending>,
|
2015-07-02 17:24:25 +00:00
|
|
|
subscriptions: HashMap<String, mpsc::Sender<MercuryResponse>>,
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for MercuryMethod {
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
formatter.write_str(match *self {
|
|
|
|
MercuryMethod::GET => "GET",
|
|
|
|
MercuryMethod::SUB => "SUB",
|
2015-07-01 22:40:38 +00:00
|
|
|
MercuryMethod::UNSUB => "UNSUB",
|
|
|
|
MercuryMethod::SEND => "SEND"
|
2015-06-23 14:38:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MercuryManager {
|
2015-07-02 17:24:25 +00:00
|
|
|
pub fn new() -> MercuryManager {
|
|
|
|
MercuryManager {
|
2015-06-23 14:38:29 +00:00
|
|
|
next_seq: 0,
|
|
|
|
pending: HashMap::new(),
|
2015-07-01 18:47:51 +00:00
|
|
|
subscriptions: 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, req: MercuryRequest)
|
|
|
|
-> Future<MercuryResponse> {
|
|
|
|
|
2015-06-23 14:38:29 +00:00
|
|
|
let mut seq = [0u8; 4];
|
|
|
|
BigEndian::write_u32(&mut seq, self.next_seq);
|
|
|
|
self.next_seq += 1;
|
|
|
|
let data = self.encode_request(&seq, &req);
|
|
|
|
|
2015-07-01 17:49:03 +00:00
|
|
|
let cmd = match req.method {
|
|
|
|
MercuryMethod::SUB => 0xb3,
|
|
|
|
MercuryMethod::UNSUB => 0xb4,
|
|
|
|
_ => 0xb2,
|
|
|
|
};
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
session.send_packet(cmd, &data).unwrap();
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
self.pending.insert(seq.to_vec(), MercuryPending{
|
|
|
|
parts: LinkedList::new(),
|
|
|
|
partial: None,
|
|
|
|
callback: Some(tx),
|
|
|
|
});
|
|
|
|
|
|
|
|
Future::from_receiver(rx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn subscribe(&mut self, session: &Session, uri: String)
|
|
|
|
-> mpsc::Receiver<MercuryResponse> {
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
self.subscriptions.insert(uri.clone(), tx);
|
|
|
|
|
|
|
|
self.request(session, MercuryRequest{
|
|
|
|
method: MercuryMethod::SUB,
|
|
|
|
uri: uri,
|
|
|
|
content_type: None,
|
|
|
|
payload: Vec::new()
|
|
|
|
});
|
|
|
|
|
|
|
|
rx
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_part(mut s: &mut Read) -> Vec<u8> {
|
|
|
|
let size = s.read_u16::<BigEndian>().unwrap() as usize;
|
|
|
|
let mut buffer = vec![0; size];
|
|
|
|
s.read_all(&mut buffer).unwrap();
|
|
|
|
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:49:03 +00:00
|
|
|
fn complete_request(&mut self, cmd: u8, mut pending: MercuryPending) {
|
2015-06-23 14:38:29 +00:00
|
|
|
let header_data = match pending.parts.pop_front() {
|
|
|
|
Some(data) => data,
|
|
|
|
None => panic!("No header part !")
|
|
|
|
};
|
|
|
|
|
2015-07-01 17:49:03 +00:00
|
|
|
let header : protocol::mercury::Header =
|
2015-06-23 14:38:29 +00:00
|
|
|
protobuf::parse_from_bytes(&header_data).unwrap();
|
|
|
|
|
2015-07-01 18:47:51 +00:00
|
|
|
let callback = if cmd == 0xb5 {
|
|
|
|
self.subscriptions.get(header.get_uri())
|
|
|
|
} else {
|
|
|
|
pending.callback.as_ref()
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(ref ch) = callback {
|
2015-07-02 17:24:25 +00:00
|
|
|
// Ignore send error.
|
|
|
|
// It simply means the receiver was closed
|
2015-07-01 17:49:03 +00:00
|
|
|
ch.send(MercuryResponse{
|
|
|
|
uri: header.get_uri().to_string(),
|
|
|
|
payload: pending.parts
|
2015-07-02 17:24:25 +00:00
|
|
|
}).ignore();
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
fn encode_request(&self, seq: &[u8], req: &MercuryRequest) -> Vec<u8> {
|
|
|
|
let mut packet = Vec::new();
|
|
|
|
packet.write_u16::<BigEndian>(seq.len() as u16).unwrap();
|
|
|
|
packet.write_all(seq).unwrap();
|
|
|
|
packet.write_u8(1).unwrap(); // Flags: FINAL
|
|
|
|
packet.write_u16::<BigEndian>(1 + req.payload.len() as u16).unwrap(); // Part count
|
|
|
|
|
|
|
|
let mut header = protobuf_init!(protocol::mercury::Header::new(), {
|
|
|
|
uri: req.uri.clone(),
|
|
|
|
method: req.method.to_string(),
|
|
|
|
});
|
|
|
|
if let Some(ref content_type) = req.content_type {
|
|
|
|
header.set_content_type(content_type.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.write_u16::<BigEndian>(header.compute_size() as u16).unwrap();
|
|
|
|
header.write_to_writer(&mut packet).unwrap();
|
|
|
|
|
|
|
|
for p in &req.payload {
|
|
|
|
packet.write_u16::<BigEndian>(p.len() as u16).unwrap();
|
|
|
|
packet.write(&p).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
packet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PacketHandler for MercuryManager {
|
|
|
|
fn handle(&mut self, cmd: u8, data: Vec<u8>) {
|
2015-06-23 14:38:29 +00:00
|
|
|
let mut packet = Cursor::new(data);
|
|
|
|
|
|
|
|
let seq = {
|
|
|
|
let seq_length = packet.read_u16::<BigEndian>().unwrap() as usize;
|
|
|
|
let mut seq = vec![0; seq_length];
|
|
|
|
packet.read_all(&mut seq).unwrap();
|
|
|
|
seq
|
|
|
|
};
|
|
|
|
let flags = packet.read_u8().unwrap();
|
|
|
|
let count = packet.read_u16::<BigEndian>().unwrap() as usize;
|
|
|
|
|
2015-07-01 17:49:03 +00:00
|
|
|
let mut pending = if let Some(pending) = self.pending.remove(&seq) {
|
|
|
|
pending
|
|
|
|
} else if cmd == 0xb5 {
|
|
|
|
MercuryPending {
|
|
|
|
parts: LinkedList::new(),
|
|
|
|
partial: None,
|
|
|
|
callback: None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!("Ignore seq {:?} cmd {}", seq, cmd);
|
|
|
|
return
|
|
|
|
};
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-01 17:49:03 +00:00
|
|
|
for i in 0..count {
|
|
|
|
let mut part = Self::parse_part(&mut packet);
|
|
|
|
if let Some(mut data) = replace(&mut pending.partial, None) {
|
|
|
|
data.append(&mut part);
|
|
|
|
part = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == count - 1 && (flags == 2) {
|
|
|
|
pending.partial = Some(part)
|
|
|
|
} else {
|
|
|
|
pending.parts.push_back(part);
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags == 0x1 {
|
2015-07-01 17:49:03 +00:00
|
|
|
self.complete_request(cmd, pending);
|
|
|
|
} else {
|
|
|
|
self.pending.insert(seq, pending);
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|