From 7558fd3fe987e347ba6e81cbd7247e954bcfce22 Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Fri, 19 Feb 2021 18:51:09 +0100 Subject: [PATCH] improvements and cleanup --- src/lib.rs | 4 --- src/main.rs | 65 +++++++++++++++++------------------------ src/osc.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++----- src/spawn.rs | 8 ++--- src/switcher.rs | 74 ++++------------------------------------------- src/udp.rs | 6 ++-- 6 files changed, 107 insertions(+), 127 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index df773f5..e340705 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,3 @@ pub mod osc; pub mod spawn; pub mod switcher; pub mod udp; - -pub trait Actor { - fn send_command(command: C); -} diff --git a/src/main.rs b/src/main.rs index 5ec354e..871ff2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,10 @@ - -use async_std::stream::{StreamExt}; - - - +use async_std::stream::StreamExt; use log::*; use rosc::{OscMessage, OscPacket, OscType}; - - - - - - -use studiox::switcher::{SwitcherError, SwitcherEvent, SwitcherHandle}; -// use studiox::udp::UdpStream; use studiox::osc::OscStream; use studiox::spawn::FaustHandle; +use studiox::switcher::{SwitcherError, SwitcherEvent, SwitcherHandle}; pub enum Event { FaustRx(Result), @@ -25,34 +14,32 @@ pub enum Event { #[async_std::main] async fn main() -> anyhow::Result<()> { - env_logger::init(); + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); - // let child_task: JoinHandle> = task::spawn(async { - // let handle = FaustHandle::spawn().await; - // match handle { - // Ok(handle) => { - // info!("FAUST mixer launched: {:?}", handle) - // } - // Err(e) => error!("Launching FAUST mixer failed: {:?}", e), - // } - // eprintln!("spawn done"); - // Ok(()) - // // eprintln!("HANDLE: {:?}", handle); - // }); - - let handle = FaustHandle::spawn().await; - match handle { + let faust_handle = FaustHandle::spawn().await; + match faust_handle.as_ref() { Ok(handle) => { info!("FAUST mixer launched: {:?}", handle) } - Err(e) => error!("Launching FAUST mixer failed: {:?}", e), + Err(e) => { + error!("Launching FAUST mixer failed: {:?}", e); + } } - - let port_faust_tx = 5510; - let port_faust_rx = 5511; - let port_commands = 5590; - let addr_faust_tx = format!("localhost:{}", port_faust_tx); - let addr_faust_rx = format!("localhost:{}", port_faust_rx); + let mut faust_handle = faust_handle?; + let res = main_loop(&faust_handle).await; + match res.as_ref() { + Ok(_) => {} + Err(e) => error!("Error: {:?}", e), + } + faust_handle.child.kill()?; + res +} +async fn main_loop(faust_handle: &FaustHandle) -> anyhow::Result<()> { + // let port_faust_tx = 5510; + // let port_faust_rx = 5511; + let port_commands: u16 = std::env::var("PORT").unwrap_or("5590".into()).parse()?; + let addr_faust_tx = format!("localhost:{}", faust_handle.port_tx); + let addr_faust_rx = format!("localhost:{}", faust_handle.port_rx); let addr_commands = format!("localhost:{}", port_commands); let mut switcher = SwitcherHandle::run(); @@ -65,7 +52,7 @@ async fn main() -> anyhow::Result<()> { let mut events = faust_rx.merge(switcher_rx).merge(command_osc_rx); - info!("start main loop"); + info!("Listening for OSC commands on localhost:{}", port_commands); while let Some(event) = events.next().await { match event { Event::Switcher(message) => { @@ -74,12 +61,12 @@ async fn main() -> anyhow::Result<()> { command_osc_tx.send_to(packet, &addr_faust_tx).await?; } Event::FaustRx(packet) => { - let packet = packet?; debug!("faust rx: {:?}", packet); + let _packet = packet?; } Event::CommandRx(packet) => { - let packet = packet?; debug!("command rx: {:?}", packet); + let packet = packet?; let event = parse_switcher_command(packet); if let Some(event) = event { switcher.send(event).await?; diff --git a/src/osc.rs b/src/osc.rs index 8dfc6b7..a7b1a54 100644 --- a/src/osc.rs +++ b/src/osc.rs @@ -1,16 +1,12 @@ use async_std::net::{ToSocketAddrs, UdpSocket}; -use async_std::stream::{Map, Stream, StreamExt}; - - +use async_std::stream::Stream; use futures_lite::ready; - -use rosc::{OscError, OscMessage, OscPacket, OscType}; +use rosc::{OscMessage, OscPacket, OscType}; +use std::io; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; -use std::{io}; - use crate::switcher::SwitcherError; use crate::udp::UdpStream; @@ -90,3 +86,70 @@ async fn send_osc( Ok(()) } } + +pub trait OscMessageExt { + fn new(addr: impl ToString, args: T) -> OscMessage + where + T: IntoOscArgs; +} + +impl OscMessageExt for OscMessage { + fn new(addr: impl ToString, args: T) -> OscMessage + where + T: IntoOscArgs, + { + let args = args.into_osc_args(); + let addr = addr.to_string(); + OscMessage { addr, args } + } +} + +pub trait IntoOscArgs { + fn into_osc_args(self) -> Vec; +} + +impl IntoOscArgs for Vec +where + T: Into, +{ + fn into_osc_args(self) -> Vec { + let args: Vec = self.into_iter().map(|a| a.into()).collect(); + args + } +} + +impl IntoOscArgs for (T1,) +where + T1: Into, +{ + fn into_osc_args(self) -> Vec { + vec![self.0.into()] + } +} + +impl IntoOscArgs for (T1, T2) +where + T1: Into, + T2: Into, +{ + fn into_osc_args(self) -> Vec { + vec![self.0.into(), self.1.into()] + } +} + +impl IntoOscArgs for (T1, T2, T3) +where + T1: Into, + T2: Into, + T3: Into, +{ + fn into_osc_args(self) -> Vec { + vec![self.0.into(), self.1.into(), self.2.into()] + } +} + +impl IntoOscArgs for OscType { + fn into_osc_args(self) -> Vec { + vec![self] + } +} diff --git a/src/spawn.rs b/src/spawn.rs index 703517b..ba6393d 100644 --- a/src/spawn.rs +++ b/src/spawn.rs @@ -8,10 +8,10 @@ use std::io; #[derive(Debug)] pub struct FaustHandle { - port_tx: u16, - port_rx: u16, - port_error: u16, - child: Child, + pub port_tx: u16, + pub port_rx: u16, + pub port_error: u16, + pub child: Child, } impl FaustHandle { diff --git a/src/switcher.rs b/src/switcher.rs index c7a598e..dbd259c 100644 --- a/src/switcher.rs +++ b/src/switcher.rs @@ -1,10 +1,12 @@ use async_channel::{self, Receiver, Sender}; use async_std::prelude::*; use async_std::task::{self, JoinHandle}; -use rosc::{OscMessage, OscType}; -use std::collections::{VecDeque}; +use rosc::OscMessage; +use std::collections::VecDeque; use thiserror::Error; +use crate::osc::{IntoOscArgs, OscMessageExt}; + #[derive(Error, Debug)] pub enum SwitcherError { #[error("Channel for switcher events full")] @@ -205,76 +207,10 @@ impl Messages { where T: IntoOscArgs, { - self.inner.push_back(osc_message(addr, args)); + self.inner.push_back(OscMessage::new(addr, args)); } fn pop(&mut self) -> Option { self.inner.pop_front() } } - -fn osc_message(addr: impl ToString, args: T) -> OscMessage -where - T: IntoOscArgs, -{ - // let args: Vec = args.into_iter().map(|a| a.into()).collect(); - let args = args.into_osc_args(); - let addr = addr.to_string(); - OscMessage { addr, args } -} - -pub trait IntoOscArgs { - fn into_osc_args(self) -> Vec; -} - -impl IntoOscArgs for Vec -where - T: Into, -{ - fn into_osc_args(self) -> Vec { - let args: Vec = self.into_iter().map(|a| a.into()).collect(); - args - } -} - -impl IntoOscArgs for (T1,) -where - T1: Into, -{ - fn into_osc_args(self) -> Vec { - vec![self.0.into()] - } -} - -impl IntoOscArgs for (T1, T2) -where - T1: Into, - T2: Into, -{ - fn into_osc_args(self) -> Vec { - vec![self.0.into(), self.1.into()] - } -} - -impl IntoOscArgs for (T1, T2, T3) -where - T1: Into, - T2: Into, - T3: Into, -{ - fn into_osc_args(self) -> Vec { - vec![self.0.into(), self.1.into(), self.2.into()] - } -} - -impl IntoOscArgs for OscType { - fn into_osc_args(self) -> Vec { - vec![self] - } -} - -// impl IntoOscArgs for Vec { -// fn into_osc_args(self) -> Vec { -// self -// } -// } diff --git a/src/udp.rs b/src/udp.rs index ffb2e37..bc71e30 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,15 +1,13 @@ use async_std::net::{ToSocketAddrs, UdpSocket}; -use async_std::stream::{Stream, StreamExt}; - +use async_std::stream::Stream; use futures_lite::future::Future; use futures_lite::ready; -use std::io::{Result}; +use std::io::Result; use std::net::SocketAddr; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; - pub struct UdpStream { socket: Arc, fut: Option, usize, SocketAddr)>> + Send + Sync>>>,