From 11c823701b12f10933b40044a12cc4048ccf8bd2 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 31 Oct 2020 02:27:26 +0100 Subject: add support for mumctl server list --- mumd/src/state.rs | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) (limited to 'mumd/src/state.rs') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 81b6c98..0d0fad8 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -16,21 +16,29 @@ use mumlib::command::{Command, CommandResponse}; use mumlib::config::Config; use mumlib::error::{ChannelIdentifierError, Error}; use mumlib::state::UserDiff; -use std::net::ToSocketAddrs; +use std::net::{ToSocketAddrs, SocketAddr}; use tokio::sync::{mpsc, watch}; +use mumble_protocol::ping::PongPacket; macro_rules! at { ($event:expr, $generator:expr) => { - (Some($event), Box::new($generator)) + ExecutionContext::TcpEvent($event, Box::new($generator)) }; } macro_rules! now { ($data:expr) => { - (None, Box::new(move |_| $data)) + ExecutionContext::Now(Box::new(move || $data)) }; } +//TODO give me a better name +pub enum ExecutionContext { + TcpEvent(TcpEvent, Box mumlib::error::Result>>), + Now(Box mumlib::error::Result>>), + Ping(Box mumlib::error::Result>, Box mumlib::error::Result>>), +} + #[derive(Clone, Debug, Eq, PartialEq)] pub enum StatePhase { Disconnected, @@ -71,10 +79,7 @@ impl State { pub fn handle_command( &mut self, command: Command, - ) -> ( - Option, - Box) -> mumlib::error::Result>>, - ) { + ) -> ExecutionContext { match command { Command::ChannelJoin { channel_identifier } => { if !matches!(*self.phase_receiver().borrow(), StatePhase::Connected) { @@ -128,7 +133,7 @@ impl State { } Command::ChannelList => { if !matches!(*self.phase_receiver().borrow(), StatePhase::Connected) { - return (None, Box::new(|_| Err(Error::DisconnectedError))); + return now!(Err(Error::DisconnectedError)); } let list = channel::into_channel( self.server.as_ref().unwrap().channels(), @@ -173,7 +178,7 @@ impl State { .unwrap(); at!(TcpEvent::Connected, |e| { //runs the closure when the client is connected - if let Some(TcpEventData::Connected(msg)) = e { + if let TcpEventData::Connected(msg) = e { Ok(Some(CommandResponse::ServerConnect { welcome_message: if msg.has_welcome_text() { Some(msg.get_welcome_text().to_string()) @@ -217,6 +222,21 @@ impl State { self.reload_config(); now!(Ok(None)) } + Command::ServerStatus { host, port } => { + ExecutionContext::Ping(Box::new(move || { + match (host.as_str(), port).to_socket_addrs().map(|mut e| e.next()) { + Ok(Some(v)) => Ok(v), + _ => Err(mumlib::error::Error::InvalidServerAddrError(host, port)), + } + }), Box::new(move |pong| { + Ok(Some(CommandResponse::ServerStatus { + version: pong.version, + users: pong.users, + max_users: pong.max_users, + bandwidth: pong.bandwidth, + })) + })) + } } } @@ -229,9 +249,9 @@ impl State { // check if this is initial state if !self.server().unwrap().users().contains_key(&session) { self.parse_initial_user_state(session, msg); - return None; + None } else { - return Some(self.parse_updated_user_state(session, msg)); + Some(self.parse_updated_user_state(session, msg)) } } -- cgit v1.2.1 From d72b0fe5862a99d9ce1a0ef37938f4517de36ed7 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 31 Oct 2020 02:37:24 +0100 Subject: cargo fmt --- mumd/src/state.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'mumd/src/state.rs') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 0d0fad8..306ded8 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -11,14 +11,14 @@ use crate::network::tcp::{TcpEvent, TcpEventData}; use log::*; use mumble_protocol::control::msgs; use mumble_protocol::control::ControlPacket; +use mumble_protocol::ping::PongPacket; use mumble_protocol::voice::Serverbound; use mumlib::command::{Command, CommandResponse}; use mumlib::config::Config; use mumlib::error::{ChannelIdentifierError, Error}; use mumlib::state::UserDiff; -use std::net::{ToSocketAddrs, SocketAddr}; +use std::net::{SocketAddr, ToSocketAddrs}; use tokio::sync::{mpsc, watch}; -use mumble_protocol::ping::PongPacket; macro_rules! at { ($event:expr, $generator:expr) => { @@ -34,9 +34,15 @@ macro_rules! now { //TODO give me a better name pub enum ExecutionContext { - TcpEvent(TcpEvent, Box mumlib::error::Result>>), + TcpEvent( + TcpEvent, + Box mumlib::error::Result>>, + ), Now(Box mumlib::error::Result>>), - Ping(Box mumlib::error::Result>, Box mumlib::error::Result>>), + Ping( + Box mumlib::error::Result>, + Box mumlib::error::Result>>, + ), } #[derive(Clone, Debug, Eq, PartialEq)] @@ -76,10 +82,7 @@ impl State { } //TODO? move bool inside Result - pub fn handle_command( - &mut self, - command: Command, - ) -> ExecutionContext { + pub fn handle_command(&mut self, command: Command) -> ExecutionContext { match command { Command::ChannelJoin { channel_identifier } => { if !matches!(*self.phase_receiver().borrow(), StatePhase::Connected) { @@ -222,21 +225,25 @@ impl State { self.reload_config(); now!(Ok(None)) } - Command::ServerStatus { host, port } => { - ExecutionContext::Ping(Box::new(move || { - match (host.as_str(), port).to_socket_addrs().map(|mut e| e.next()) { + Command::ServerStatus { host, port } => ExecutionContext::Ping( + Box::new(move || { + match (host.as_str(), port) + .to_socket_addrs() + .map(|mut e| e.next()) + { Ok(Some(v)) => Ok(v), _ => Err(mumlib::error::Error::InvalidServerAddrError(host, port)), } - }), Box::new(move |pong| { + }), + Box::new(move |pong| { Ok(Some(CommandResponse::ServerStatus { version: pong.version, users: pong.users, max_users: pong.max_users, bandwidth: pong.bandwidth, })) - })) - } + }), + ), } } -- cgit v1.2.1 From 46861ce465d6f1d86e80007742a850fd1cfa9bad Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Mon, 2 Nov 2020 20:31:50 +0100 Subject: add mumd support for volume adjustment --- mumd/src/state.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'mumd/src/state.rs') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 81b6c98..2060845 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -209,12 +209,35 @@ impl State { .unwrap(); now!(Ok(None)) } + Command::ConfigReload => { + self.reload_config(); + now!(Ok(None)) + } Command::InputVolumeSet(volume) => { self.audio.set_input_volume(volume); now!(Ok(None)) } - Command::ConfigReload => { - self.reload_config(); + Command::OutputVolumeSet(volume) => { + self.audio.set_output_volume(volume); + now!(Ok(None)) + } + Command::UserVolumeSet(string, volume) => { + if !matches!(*self.phase_receiver().borrow(), StatePhase::Connected) { + return now!(Err(Error::DisconnectedError)); + } + let user_id = match self + .server() + .unwrap() + .users() + .iter() + .find(|e| e.1.name() == &string) + .map(|e| *e.0) + { + None => return now!(Err(Error::InvalidUsernameError(string))), + Some(v) => v, + }; + + self.audio.set_user_volume(user_id, volume); now!(Ok(None)) } } -- cgit v1.2.1 From 0cb39d13bba3dc5ffa3231e6021066e4191a43a4 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Mon, 2 Nov 2020 21:47:47 +0100 Subject: refactor and add audio out config command --- mumd/src/state.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'mumd/src/state.rs') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 2060845..4d8c139 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -39,7 +39,7 @@ pub enum StatePhase { } pub struct State { - config: Option, + config: Config, server: Option, audio: Audio, @@ -375,16 +375,12 @@ impl State { } pub fn reload_config(&mut self) { - if let Some(config) = mumlib::config::read_default_cfg() { - self.config = Some(config); - let config = &self.config.as_ref().unwrap(); - if let Some(audio_config) = &config.audio { - if let Some(input_volume) = audio_config.input_volume { - self.audio.set_input_volume(input_volume); - } - } - } else { - warn!("config file not found"); + self.config = mumlib::config::read_default_cfg(); + if let Some(input_volume) = self.config.audio.input_volume { + self.audio.set_input_volume(input_volume); + } + if let Some(output_volume) = self.config.audio.output_volume { + self.audio.set_output_volume(output_volume); } } -- cgit v1.2.1