feat: implement basic music player and grpc server

This commit is contained in:
Alexander Navarro 2024-05-28 20:11:53 -04:00
parent ad61cf68c6
commit 7382b06bdf
10 changed files with 336 additions and 61 deletions

View file

@ -3,8 +3,8 @@ use std::net::{SocketAddr, TcpListener};
use tonic::async_trait;
use self::client::GRPCClient;
use self::server::GRPCServer;
pub use self::client::GRPCClient;
pub use self::server::GRPCServer;
mod client;
mod server;
@ -18,20 +18,10 @@ pub trait Connection {
async fn connect(&self) -> Result<(), Box<dyn Error>>;
}
fn is_socket_in_use(addr: String) -> bool {
let socket: SocketAddr = addr.parse().expect("Failed to create socket");
match TcpListener::bind(socket) {
Ok(_) => true,
Err(_) => false,
}
}
pub fn run() -> Result<Box<dyn Connection>, Box<dyn Error>> {
let addr = "[::1]:50051";
if is_socket_in_use(addr.to_string()) {
Ok(Box::new(GRPCServer::new(addr.to_string())))
} else {
Ok(Box::new(GRPCClient::new(addr.to_string())))
/// Return true if the addr is already in use, false otherwise
pub fn is_socket_in_use(addr: SocketAddr) -> bool {
match TcpListener::bind(addr) {
Ok(_) => false,
Err(_) => true,
}
}