Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 2e5812d7 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

[WIP] AMALIB: Continue to try to come up with a way to handle unix sockets and...

[WIP] AMALIB: Continue to try to come up with a way to handle unix sockets and tcp sockets the same way for sending commands to lektor
parent 7dddc047
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -86,18 +86,19 @@ impl AmaClientBuilder { ...@@ -86,18 +86,19 @@ impl AmaClientBuilder {
let cache = AmaDB::new(); let cache = AmaDB::new();
let connexion = Box::new(match self.sockty { let connexion: Box<dyn LektorStream> = match self.sockty {
LektorSocketType::TCP => { LektorSocketType::TCP => {
let (Some(hostname), Some(port)) = (self.address, self.port) else { let (Some(hostname), Some(port)) = (self.address, self.port) else {
unreachable!("the state is invalid, with TCP socket we should have a hostname and a port") unreachable!("the state is invalid, with TCP socket we should have a hostname and a port")
}; };
let address = format!("{hostname}:{port}"); LektorConnexion::<TcpStream>::new(format!("{hostname}:{port}"))
let connexion = LektorConnexion::<TcpStream>::new(address); .await
err_ctx!(connexion.await => AmaClientError::TCPClientCreateError) .map_err(|err| err_ctx!(err => AmaClientError::TCPClientCreateError))
} }
#[cfg(unix)] #[cfg(unix)]
LektorSocketType::UNIX => { LektorSocketType::UNIX => {
use tokio::net::UnixStream;
let Some(path) = self.socket else { let Some(path) = self.socket else {
unreachable!("the state is invalid, with UNIX socket we should have a path") unreachable!("the state is invalid, with UNIX socket we should have a path")
}; };
...@@ -108,11 +109,13 @@ impl AmaClientBuilder { ...@@ -108,11 +109,13 @@ impl AmaClientBuilder {
format!("path has no canonical name: {e}") format!("path has no canonical name: {e}")
]) ])
})? })?
.to_string_lossy(); .to_string_lossy()
let connexion = LektorConnexion::<UnixStream, String>::new(address); .to_string();
err_ctx!(connexion.await => AmaClientError::UNIXClientCreateError) LektorConnexion::<UnixStream>::new(address)
.await
.map_err(|err| err_ctx!(err => AmaClientError::UNIXClientCreateError))
} }
}?); }?;
Ok(AmaClient { connexion, cache }) Ok(AmaClient { connexion, cache })
} }
......
...@@ -47,7 +47,7 @@ pub enum LektorSocketType { ...@@ -47,7 +47,7 @@ pub enum LektorSocketType {
pub struct LektorConnexion<Stream> pub struct LektorConnexion<Stream>
where where
Stream: StreamConnect<Stream> + StreamReadWrite, Stream: StreamReadWrite + Sized,
{ {
version: String, version: String,
pub(super) stream: Stream, pub(super) stream: Stream,
...@@ -56,7 +56,7 @@ where ...@@ -56,7 +56,7 @@ where
/// The idle TCP client. /// The idle TCP client.
pub struct LektorIdleConnexion<Stream> pub struct LektorIdleConnexion<Stream>
where where
Stream: StreamConnect<Stream> + StreamReadWrite, Stream: StreamReadWrite + Sized,
{ {
version: String, version: String,
stream: Stream, stream: Stream,
...@@ -65,11 +65,11 @@ where ...@@ -65,11 +65,11 @@ where
impl<Stream> LektorConnexion<Stream> impl<Stream> LektorConnexion<Stream>
where where
Stream: StreamConnect<Stream> + StreamReadWrite, Stream: StreamReadWrite,
{ {
/// Create a new connexion to a lektord server. If the versions mismatch we /// Create a new connexion to a lektord server. If the versions mismatch we
/// log an error but continue... /// log an error but continue...
pub async fn new(addr: impl ToString) -> StackedResult<Self, LektorCommError> { pub async fn new(addr: impl ToString) -> StackedResult<Box<dyn LektorStream>, LektorCommError> {
let addr = addr.to_string(); let addr = addr.to_string();
let stream = Stream::connect(addr.clone()).await.map_err( let stream = Stream::connect(addr.clone()).await.map_err(
|e| err_report!(LektorCommError::Io(e) => [ format!("faild to connect to {addr}") ]), |e| err_report!(LektorCommError::Io(e) => [ format!("faild to connect to {addr}") ]),
...@@ -101,7 +101,7 @@ where ...@@ -101,7 +101,7 @@ where
; error!("got MPD version {version} from {addr}, but amalib is compatible with {}", constants::MPD_VERSION) ; error!("got MPD version {version} from {addr}, but amalib is compatible with {}", constants::MPD_VERSION)
); );
Ok(Self { version, stream }) Ok(Box::new(Self { version, stream }))
} }
pub fn version(&self) -> &str { pub fn version(&self) -> &str {
...@@ -111,7 +111,7 @@ where ...@@ -111,7 +111,7 @@ where
impl<Stream> LektorIdleConnexion<Stream> impl<Stream> LektorIdleConnexion<Stream>
where where
Stream: StreamConnect<Stream> + StreamReadWrite, Stream: StreamReadWrite,
{ {
pub async fn idle( pub async fn idle(
connexion: LektorConnexion<Stream>, connexion: LektorConnexion<Stream>,
......
...@@ -10,13 +10,9 @@ pub trait LektorStream { ...@@ -10,13 +10,9 @@ pub trait LektorStream {
} }
#[async_trait] #[async_trait]
pub trait StreamConnect<Stream: StreamReadWrite> { pub trait StreamReadWrite: AsyncRead + AsyncWrite + Sized {
/// Connect to a lektord server. async fn connect(addr: impl ToString) -> std::io::Result<Self>;
async fn connect(addr: impl ToString) -> std::io::Result<Stream>;
}
#[async_trait]
pub trait StreamReadWrite: AsyncRead + AsyncWrite {
async fn writable(&self) -> std::io::Result<()>; async fn writable(&self) -> std::io::Result<()>;
async fn readable(&self) -> std::io::Result<()>; async fn readable(&self) -> std::io::Result<()>;
...@@ -32,15 +28,13 @@ mod unix { ...@@ -32,15 +28,13 @@ mod unix {
use super::*; use super::*;
use tokio::net::UnixStream; use tokio::net::UnixStream;
impl StreamConnect<UnixStream> for UnixStream { impl StreamReadWrite for UnixStream {
fn connect<'async_trait>( fn connect<'async_trait>(
addr: impl ToString, addr: impl ToString,
) -> Pin<Box<dyn Future<Output = std::io::Result<Self>> + Send + 'async_trait>> { ) -> Pin<Box<dyn Future<Output = std::io::Result<Self>> + Send + 'async_trait>> {
Box::pin(UnixStream::connect(addr.to_string())) Box::pin(UnixStream::connect(addr.to_string()))
} }
}
impl StreamReadWrite for UnixStream {
fn writable<'life0, 'async_trait>( fn writable<'life0, 'async_trait>(
&'life0 self, &'life0 self,
) -> Pin<Box<dyn Future<Output = std::io::Result<()>> + Send + 'async_trait>> ) -> Pin<Box<dyn Future<Output = std::io::Result<()>> + Send + 'async_trait>>
...@@ -111,6 +105,13 @@ mod tcp { ...@@ -111,6 +105,13 @@ mod tcp {
use tokio::net::TcpStream; use tokio::net::TcpStream;
impl StreamReadWrite for TcpStream { impl StreamReadWrite for TcpStream {
fn connect<'async_trait>(
addr: impl ToString,
) -> Pin<Box<dyn Future<Output = std::io::Result<TcpStream>> + Send + 'async_trait>>
{
Box::pin(TcpStream::connect(addr.to_string()))
}
fn writable<'life0, 'async_trait>( fn writable<'life0, 'async_trait>(
&'life0 self, &'life0 self,
) -> Pin<Box<dyn Future<Output = std::io::Result<()>> + Send + 'async_trait>> ) -> Pin<Box<dyn Future<Output = std::io::Result<()>> + Send + 'async_trait>>
...@@ -166,13 +167,4 @@ mod tcp { ...@@ -166,13 +167,4 @@ mod tcp {
Box::pin(super::super::send(&mut self.stream, query)) Box::pin(super::super::send(&mut self.stream, query))
} }
} }
impl StreamConnect<TcpStream> for TcpStream {
fn connect<'async_trait>(
addr: impl ToString,
) -> Pin<Box<dyn Future<Output = std::io::Result<TcpStream>> + Send + 'async_trait>>
{
Box::pin(TcpStream::connect(addr.to_string()))
}
}
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter