2022-08-25 21:53:42 +01:00
|
|
|
use lazy_static::lazy_static;
|
2022-08-14 14:30:13 +01:00
|
|
|
use mpd_client::commands::responses::Status;
|
|
|
|
use mpd_client::raw::MpdProtocolError;
|
|
|
|
use mpd_client::{Client, Connection};
|
2022-08-25 21:53:42 +01:00
|
|
|
use std::collections::HashMap;
|
2022-08-14 14:30:13 +01:00
|
|
|
use std::path::PathBuf;
|
2022-08-25 21:53:42 +01:00
|
|
|
use std::sync::Arc;
|
2022-08-21 23:36:07 +01:00
|
|
|
use std::time::Duration;
|
2022-08-14 14:30:13 +01:00
|
|
|
use tokio::net::{TcpStream, UnixStream};
|
2022-08-25 21:53:42 +01:00
|
|
|
use tokio::sync::Mutex;
|
2022-08-21 23:36:07 +01:00
|
|
|
use tokio::time::sleep;
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-08-25 21:53:42 +01:00
|
|
|
lazy_static! {
|
|
|
|
static ref CLIENTS: Arc<Mutex<HashMap<String, Arc<Client>>>> =
|
|
|
|
Arc::new(Mutex::new(HashMap::new()));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_connection(host: &str) -> Option<Arc<Client>> {
|
|
|
|
let mut clients = CLIENTS.lock().await;
|
|
|
|
|
|
|
|
match clients.get(host) {
|
|
|
|
Some(client) => Some(Arc::clone(client)),
|
|
|
|
None => {
|
|
|
|
let client = wait_for_connection(host, Duration::from_secs(5), None).await?;
|
|
|
|
let client = Arc::new(client);
|
|
|
|
clients.insert(host.to_string(), Arc::clone(&client));
|
|
|
|
Some(client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn wait_for_connection(
|
|
|
|
host: &str,
|
2022-08-21 23:36:07 +01:00
|
|
|
interval: Duration,
|
|
|
|
max_retries: Option<usize>,
|
|
|
|
) -> Option<Client> {
|
|
|
|
let mut retries = 0;
|
2022-08-25 21:53:42 +01:00
|
|
|
let max_retries = max_retries.unwrap_or(usize::MAX);
|
2022-08-21 23:36:07 +01:00
|
|
|
|
2022-08-25 21:53:42 +01:00
|
|
|
loop {
|
|
|
|
if retries == max_retries {
|
|
|
|
break None;
|
|
|
|
}
|
2022-08-21 23:36:07 +01:00
|
|
|
|
2022-08-25 21:53:42 +01:00
|
|
|
if let Some(conn) = try_get_mpd_conn(host).await {
|
|
|
|
break Some(conn.0);
|
2022-08-21 23:36:07 +01:00
|
|
|
}
|
2022-08-25 21:53:42 +01:00
|
|
|
|
|
|
|
retries += 1;
|
|
|
|
sleep(interval).await;
|
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
/// Cycles through each MPD host and
|
|
|
|
/// returns the first one which connects,
|
|
|
|
/// or none if there are none
|
2022-08-25 21:53:42 +01:00
|
|
|
async fn try_get_mpd_conn(host: &str) -> Option<Connection> {
|
|
|
|
let connection = if is_unix_socket(host) {
|
|
|
|
connect_unix(host).await
|
|
|
|
} else {
|
|
|
|
connect_tcp(host).await
|
|
|
|
};
|
2022-08-21 23:36:07 +01:00
|
|
|
|
2022-08-25 21:53:42 +01:00
|
|
|
connection.ok()
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
fn is_unix_socket(host: &str) -> bool {
|
|
|
|
PathBuf::from(host).is_file()
|
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
async fn connect_unix(host: &str) -> Result<Connection, MpdProtocolError> {
|
|
|
|
let connection = UnixStream::connect(host).await?;
|
2022-08-14 14:30:13 +01:00
|
|
|
Client::connect(connection).await
|
|
|
|
}
|
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
async fn connect_tcp(host: &str) -> Result<Connection, MpdProtocolError> {
|
|
|
|
let connection = TcpStream::connect(host).await?;
|
2022-08-14 14:30:13 +01:00
|
|
|
Client::connect(connection).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the duration of the current song
|
2022-08-21 23:36:07 +01:00
|
|
|
pub fn get_duration(status: &Status) -> Option<u64> {
|
|
|
|
status.duration.map(|duration| duration.as_secs())
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the elapsed time of the current song
|
2022-08-21 23:36:07 +01:00
|
|
|
pub fn get_elapsed(status: &Status) -> Option<u64> {
|
|
|
|
status.elapsed.map(|duration| duration.as_secs())
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|