refactor(config): remove global config and pass as referenced
This commit is contained in:
parent
9cec453498
commit
940093d599
7 changed files with 138 additions and 108 deletions
80
src/main.rs
80
src/main.rs
|
|
@ -1,3 +1,4 @@
|
|||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
|
@ -6,59 +7,74 @@ use tokio::sync::mpsc;
|
|||
|
||||
use crate::player::Player;
|
||||
|
||||
use self::configuration::{Commands, ConfigMode, CONFIG};
|
||||
use self::configuration::{Commands, Config, ConfigMode};
|
||||
|
||||
mod configuration;
|
||||
mod file_explorer;
|
||||
mod grpc;
|
||||
mod player;
|
||||
|
||||
async fn init_server() -> Result<(), Box<dyn Error>> {
|
||||
let (tx, mut rx) = mpsc::channel::<Commands>(32);
|
||||
async fn init_server(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let (tx, mut rx) = mpsc::channel::<grpc::server::GrpcServerMessage>(32);
|
||||
|
||||
tokio::spawn(async move {
|
||||
let _ = grpc::GRPCServer::serve(tx).await;
|
||||
let _ = grpc::GRPCServer::serve(config.address, tx).await;
|
||||
});
|
||||
|
||||
let mut player = Player::new().expect("Error creating player");
|
||||
let mut base_path = env::current_dir().expect("Error accesing the enviroment");
|
||||
let mut volume = 1.0;
|
||||
|
||||
if let Commands::Start {
|
||||
base_path: config_path,
|
||||
volume: config_volume,
|
||||
} = config.command
|
||||
{
|
||||
base_path = config_path.to_owned();
|
||||
volume = config_volume;
|
||||
};
|
||||
|
||||
let mut player = Player::new(base_path, volume).expect("Error creating player");
|
||||
|
||||
println!("Listening for incomming messages...");
|
||||
|
||||
// This macro will wait on multiple futures and will return when the first one resolves
|
||||
tokio::select! {
|
||||
Some(msg) = rx.recv() => {
|
||||
if let Err(err) = player.handle_message(msg) {
|
||||
eprintln!("Error handling player action: {}", err);
|
||||
// TODO: create a break system for shutdown
|
||||
loop {
|
||||
tokio::select! {
|
||||
Some(msg) = rx.recv() => {
|
||||
// TODO: receive message from player and send it back to server so it can be sent to
|
||||
// the client
|
||||
if let Err(err) = player.handle_message(msg.command) {
|
||||
eprintln!("Error handling player action: {}", err);
|
||||
}
|
||||
|
||||
if let Err(_) = msg.responder.send(()) {
|
||||
eprintln!("Error responding to grpc server");
|
||||
}
|
||||
}
|
||||
_ = async {
|
||||
loop {
|
||||
let _ = player.handle_idle();
|
||||
sleep(Duration::from_millis(200)).await;
|
||||
}
|
||||
} => {}
|
||||
else => {
|
||||
println!("player stopped");
|
||||
}
|
||||
}
|
||||
_ = async {
|
||||
loop {
|
||||
let _ = player.handle_idle();
|
||||
sleep(Duration::from_millis(200)).await;
|
||||
}
|
||||
} => {println!("player stopped");}
|
||||
}
|
||||
|
||||
// this traps the main thread, it should run last.
|
||||
while let Some(msg) = rx.recv().await {
|
||||
if let Err(err) = player.handle_message(msg) {
|
||||
eprintln!("Error handling player action: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn init_client() -> Result<(), Box<dyn Error>> {
|
||||
let client = grpc::GRPCClient::default();
|
||||
async fn init_client(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let client = grpc::GRPCClient::new(config.address);
|
||||
|
||||
match &CONFIG.command {
|
||||
match &config.command {
|
||||
Commands::Play => client.play().await?,
|
||||
Commands::Pause => client.pause().await?,
|
||||
Commands::PlayPause => client.play_pause().await?,
|
||||
Commands::SkipSong => client.skip_song().await?,
|
||||
Commands::Set => todo!(),
|
||||
Commands::GetFiles { path: _ } => client.get_files().await?,
|
||||
Commands::GetFiles { path } => client.get_files(&path).await?,
|
||||
Commands::Ping => client.ping().await?,
|
||||
_ => {
|
||||
println!("This command doesn't apply to client mode")
|
||||
|
|
@ -70,9 +86,11 @@ async fn init_client() -> Result<(), Box<dyn Error>> {
|
|||
|
||||
#[tokio::main()]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
match CONFIG.mode {
|
||||
ConfigMode::Server => init_server().await?,
|
||||
ConfigMode::Client => init_client().await?,
|
||||
let config = Config::new();
|
||||
|
||||
match config.mode {
|
||||
ConfigMode::Server => init_server(config).await?,
|
||||
ConfigMode::Client => init_client(config).await?,
|
||||
};
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue