Single watcher thread

This commit is contained in:
hodasemi 2018-08-15 21:52:26 +02:00
parent dd4210ad6f
commit 21cc37e80c

View file

@ -11,6 +11,7 @@ use std::cell::RefCell;
use std::ops::DerefMut;
use std::sync::{Arc, Mutex, MutexGuard};
use std::thread;
use std::thread::JoinHandle;
use std::time;
macro_rules! check_option {
@ -30,6 +31,7 @@ pub struct Song {
pub struct MediaData {
playlist: RefCell<Vec<Song>>,
current_song: Mutex<RefCell<Option<LockedAudio>>>,
watcher_thread: RefCell<Option<JoinHandle<()>>>,
}
fn guild_id(channel_id: ChannelId) -> Option<GuildId> {
@ -57,6 +59,10 @@ impl MediaData {
self.current_song.lock().unwrap()
}
fn reset_thread(&self) {
*self.watcher_thread.borrow_mut() = None;
}
fn start_thread(
media: &Arc<MediaData>,
channel_id: ChannelId,
@ -67,68 +73,76 @@ impl MediaData {
let media_clone = media.clone();
let manager_clone = manager_lock.clone();
thread::spawn(move || loop {
{
let song_lock = media_clone.song_mut();
let mut borrow = song_lock.borrow_mut();
let mut watcher_mut = media.watcher_thread.borrow_mut();
if let Some(ref mut song) = borrow.deref_mut() {
let song_lock = song.clone();
let audio = song_lock.lock();
if watcher_mut.is_none() {
*watcher_mut = Some(thread::spawn(move || loop {
{
let song_lock = media_clone.song_mut();
let mut borrow = song_lock.borrow_mut();
if audio.finished {
if !Self::next_song(
song,
media_clone.playlist.borrow_mut().deref_mut(),
channel_id,
&manager_clone,
) {
manager_clone
.lock()
.remove(check_option!(guild_id(channel_id)));
if let Some(ref mut song) = borrow.deref_mut() {
let song_lock = song.clone();
let audio = song_lock.lock();
println!("left channel");
if audio.finished {
if !Self::next_song(
song,
media_clone.playlist.borrow_mut().deref_mut(),
channel_id,
&manager_clone,
) {
manager_clone
.lock()
.remove(check_option!(guild_id(channel_id)));
media_clone.reset_thread();
println!("left channel");
return;
}
}
continue;
}
let mut playlist = media_clone.playlist.borrow_mut();
if !playlist.is_empty() {
let mut manager = manager_clone.lock();
if let Some(handler) =
handler(check_option!(guild_id(channel_id)), &mut manager)
{
let first = playlist.remove(0);
*borrow = Some(handler.play_returning(first.source));
super::check_msg(channel_id.say(format!(
"Playing song: {}",
super::youtube::convert_file_name(first.name)
)));
} else {
return;
}
}
continue;
}
let mut playlist = media_clone.playlist.borrow_mut();
if !playlist.is_empty() {
let mut manager = manager_clone.lock();
if let Some(handler) =
handler(check_option!(guild_id(channel_id)), &mut manager)
{
let first = playlist.remove(0);
*borrow = Some(handler.play_returning(first.source));
super::check_msg(channel_id.say(format!(
"Playing song: {}",
super::youtube::convert_file_name(first.name)
)));
} else {
manager_clone
.lock()
.remove(check_option!(guild_id(channel_id)));
media_clone.reset_thread();
println!("left channel");
return;
}
} else {
manager_clone
.lock()
.remove(check_option!(guild_id(channel_id)));
println!("left channel");
return;
}
}
let five_sec = time::Duration::from_secs(5);
thread::sleep(five_sec);
});
let five_sec = time::Duration::from_secs(5);
thread::sleep(five_sec);
}));
}
}
fn next_song(
@ -172,6 +186,7 @@ impl Default for MediaData {
MediaData {
playlist: RefCell::new(Vec::new()),
current_song: Mutex::new(RefCell::new(None)),
watcher_thread: RefCell::new(None),
}
}
}