From 958d0c74d4ee590a7d3484a65b15da2736944869 Mon Sep 17 00:00:00 2001 From: Simon Persson Date: Sat, 13 Feb 2016 02:09:15 +0100 Subject: [PATCH] Maintain list of connected devices. --- src/spirc.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/spirc.rs b/src/spirc.rs index 887f277e..121164d5 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -9,6 +9,7 @@ use mercury::{MercuryRequest, MercuryMethod}; use player::{Player, PlayerState}; use std::sync::{Mutex, Arc}; +use std::collections::HashMap; use protocol; pub use protocol::spirc::PlayStatus; @@ -37,6 +38,8 @@ struct SpircInternal { tracks: Vec, index: u32, + + devices: HashMap, } impl SpircManager { @@ -66,6 +69,8 @@ impl SpircManager { tracks: Vec::new(), index: 0, + + devices: HashMap::new(), }))) } @@ -118,7 +123,7 @@ impl SpircInternal { } } - fn handle(&mut self, frame: protocol::spirc::Frame) { + fn handle(&mut self, mut frame: protocol::spirc::Frame) { if frame.get_ident() == self.ident || (frame.get_recipient().len() > 0 && !frame.get_recipient().contains(&self.ident)) { return; @@ -128,6 +133,11 @@ impl SpircInternal { self.last_command_ident = frame.get_ident().to_owned(); self.last_command_msgid = frame.get_seq_nr(); } + + if frame.has_ident() && !frame.has_goodbye() && frame.has_device_state() { + self.devices.insert(frame.take_ident(), frame.take_device_state().take_name()); + } + match frame.get_typ() { protocol::spirc::MessageType::kMessageTypeHello => { self.notify(false, Some(frame.get_ident())); @@ -172,6 +182,10 @@ impl SpircInternal { self.is_active = false; self.player.stop(); } + + if frame.has_ident() && frame.has_goodbye() { + self.devices.remove(&frame.take_ident()); + } } protocol::spirc::MessageType::kMessageTypeVolume => { self.player.volume(frame.get_volume() as u16);