Преглед изворни кода

Say goodbye when terminating

When librespot is terminated while a session is active it will now send
a goodbye message, so that the Spotify client unregisters the device
from its list.

Closes: #114
Jörg Krause пре 8 година
родитељ
комит
c1e570f48d
4 измењених фајлова са 35 додато и 0 уклоњено
  1. 17 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 11 0
      src/main.rs
  4. 6 0
      src/spirc.rs

+ 17 - 0
Cargo.lock

@@ -32,6 +32,7 @@ dependencies = [
  "serde_json 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_macros 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)",
  "shannon 0.1.1 (git+https://github.com/plietar/rust-shannon)",
+ "simple-signal 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "tempfile 2.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "tremor 0.1.0 (git+https://github.com/plietar/rust-tremor)",
  "url 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -284,6 +285,11 @@ name = "language-tags"
 version = "0.2.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "lazy_static"
+version = "0.1.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "lazy_static"
 version = "0.2.1"
@@ -844,6 +850,15 @@ dependencies = [
  "libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "simple-signal"
+version = "1.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "slab"
 version = "0.1.3"
@@ -1272,6 +1287,7 @@ dependencies = [
 "checksum json_macros 0.3.1 (git+https://github.com/plietar/json_macros)" = "<none>"
 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
 "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
+"checksum lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417"
 "checksum lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "49247ec2a285bb3dcb23cbd9c35193c025e7251bfce77c1d5da97e6362dffe7f"
 "checksum libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)" = "408014cace30ee0f767b1c4517980646a573ec61a57957aeeabcac8ac0a02e8d"
 "checksum liblmdb-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b56d07dcf983f9b6679f768df73c72671d0087bd66329baabb63325f4f592677"
@@ -1335,6 +1351,7 @@ dependencies = [
 "checksum serde_macros 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)" = "10aa279b5b061a3e827639cc15e563be096b7323c9c811e10a4b18ba4607eaf8"
 "checksum shannon 0.1.1 (git+https://github.com/plietar/rust-shannon)" = "<none>"
 "checksum shannon-sys 0.1.0 (git+https://github.com/plietar/rust-shannon)" = "<none>"
+"checksum simple-signal 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2873201dda62b00216e0104de61438e93fb730a514cb381ff0e9a9e10c675c6b"
 "checksum slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d807fd58c4181bbabed77cb3b891ba9748241a552bcc5be698faaebefc54f46e"
 "checksum solicit 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "172382bac9424588d7840732b250faeeef88942e37b6e35317dce98cafdd75b2"
 "checksum syncbox 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "05bc2b72659ac27a2d0e7c4166c8596578197c4c41f767deab12c81f523b85c7"

+ 1 - 0
Cargo.toml

@@ -44,6 +44,7 @@ url             = "~0.5.0"
 log             = "0.3.5"
 env_logger      = "0.3.2"
 shannon         = { git = "https://github.com/plietar/rust-shannon" }
+simple-signal   = "1.0.6"
 
 vorbis          = "~0.0.14"
 tremor          = { git = "https://github.com/plietar/rust-tremor", optional = true }

+ 11 - 0
src/main.rs

@@ -3,6 +3,7 @@ extern crate librespot;
 extern crate env_logger;
 #[macro_use]
 extern crate log;
+extern crate simple_signal;
 
 use std::process::exit;
 use std::thread;
@@ -11,6 +12,8 @@ use std::env;
 use librespot::spirc::SpircManager;
 use librespot::main_helper;
 
+use simple_signal::{Signal, Signals};
+
 fn usage(program: &str, opts: &getopts::Options) -> String {
     let brief = format!("Usage: {} [options]", program);
     format!("{}", opts.usage(&brief))
@@ -44,7 +47,15 @@ fn main() {
     let player = main_helper::create_player(&session, &matches);
 
     let spirc = SpircManager::new(session.clone(), player);
+    let spirc_signal = spirc.clone();
     thread::spawn(move || spirc.run());
+    Signals::set_handler(&[Signal::Int, Signal::Term],
+        move |signals| {
+            println!("Signal received: {:?}. Say goodbye and exit.", signals);
+            spirc_signal.send_goodbye();
+            exit(0);
+        }
+    );
 
     loop {
         session.poll();

+ 6 - 0
src/spirc.rs

@@ -165,6 +165,12 @@ impl SpircManager {
             .send();
     }
 
+    pub fn send_goodbye(&self) {
+        let mut internal = self.0.lock().unwrap();
+        CommandSender::new(&mut *internal, MessageType::kMessageTypeGoodbye)
+            .send();
+    }
+
     pub fn get_queue(&self) -> Vec<SpotifyId> {
         self.0.lock().unwrap().tracks.clone()
     }