diff --git a/src/cli.rs b/src/cli.rs index 4e8bf62e..6a5a7321 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -199,6 +199,10 @@ fn get_endpoint_with_kind(kind: &str) -> Vec { MANAGER.read().unwrap().clap_matches.endpoints.iter().filter(|url| url.scheme() == kind).map(Clone::clone).collect() } +pub fn endpoints() -> Vec { + MANAGER.read().unwrap().clap_matches.endpoints.clone() +} + // Return the command line used to start this application #[instrument(level = "debug")] diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs index 7d0e2966..63cd5bd4 100644 --- a/src/drivers/mod.rs +++ b/src/drivers/mod.rs @@ -2,6 +2,8 @@ pub mod fake; pub mod tcp; pub mod udp; +use std::sync::Arc; + use crate::protocol::Protocol; use anyhow::Result; use tokio::sync::broadcast; @@ -51,9 +53,9 @@ pub trait DriverExt { fn valid_schemes(&self) -> Vec; // Need to return a Result because the parsing can fail fn url_from_legacy(&self, legacy_entry: DriverDescriptionLegacy) -> Url; - fn create_endpoint_from_url(&self, url: &Url) -> Option>; + fn create_endpoint_from_url(&self, url: &Url) -> Option>; - fn create_endpoint(&self, entry: &str) -> Option> { + fn create_endpoint(&self, entry: &str) -> Option> { let url = match self.is_old_format(entry) { Some(legacy_entry) => self.url_from_legacy(legacy_entry), None => Url::parse(entry).ok()?, // Return an Option and handle the error @@ -85,6 +87,16 @@ pub trait DriverExt { } } +pub fn create_driver(url: &Url) -> Option> { + let endpoints = endpoints(); // Assuming `endpoints` is a function returning Vec + + let endpoint = endpoints + .iter() + .find(|endpoint| url.scheme() == endpoint.driverExt.valid_schemes()[0])?; + + endpoint.driverExt.create_endpoint_from_url(url) +} + struct ExtInfo { pub driverExt: Box, pub typ: Type, diff --git a/src/drivers/tcp/client.rs b/src/drivers/tcp/client.rs index 7e6a00ef..4b0331b2 100644 --- a/src/drivers/tcp/client.rs +++ b/src/drivers/tcp/client.rs @@ -90,9 +90,9 @@ impl DriverExt for TcpClientExt { url::Url::parse(&format!("{scheme}://{ip}:{port}")).unwrap() } - fn create_endpoint_from_url(&self, url: &url::Url) -> Option> { + fn create_endpoint_from_url(&self, url: &url::Url) -> Option> { let host = url.host_str().unwrap(); let port = url.port().unwrap(); - return Some(Box::new(TcpClient::new(&format!("{host}:{port}")))); + return Some(Arc::new(TcpClient::new(&format!("{host}:{port}")))); } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 92a47f2c..513c0275 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,48 +27,10 @@ async fn main() -> Result<()> { ) .await; - // Endpoints creation - /* - { - for endpoint in cli::tcp_client_endpoints() { - debug!("Creating TCP Client to {endpoint:?}"); - hub.add_driver(Arc::new(drivers::tcp::client::TcpClient::new(&endpoint))) - .await?; - } - for endpoint in cli::tcp_server_endpoints() { - debug!("Creating TCP Server to {endpoint:?}"); - hub.add_driver(Arc::new(drivers::tcp::server::TcpServer::new(&endpoint))) - .await?; - } - for endpoint in cli::udp_client_endpoints() { - debug!("Creating UDP Client to {endpoint:?}"); - hub.add_driver(Arc::new(drivers::udp::client::UdpClient::new(&endpoint))) - .await?; - } - for endpoint in cli::udp_server_endpoints() { - debug!("Creating UDP Server to {endpoint:?}"); - hub.add_driver(Arc::new(drivers::udp::server::UdpServer::new(&endpoint))) - .await?; - } - for endpoint in cli::udp_broadcast_endpoints() { - debug!("Creating UDP Broadcast to {endpoint:?}"); - - let mut s = endpoint.split(':'); - let _ip = s.next().unwrap(); - let port = s.next().unwrap(); - let broadcast_ip = "255.255.255.255"; - - let endpoint = format!("{broadcast_ip}:{port}"); - - hub.add_driver(Arc::new(drivers::udp::client::UdpClient::new(&endpoint))) - .await?; - continue; - } - for _endpoint in cli::serial_endpoints() { - error!("Serial endpoint not implemented"); - continue; - } - } + for endpoint in cli::endpoints() { + debug!("Creating endpoint {endpoint:?}"); + hub.add_driver(drivers::create_driver(&endpoint).unwrap()).await?; + }; wait_ctrlc().await; @@ -76,7 +38,6 @@ async fn main() -> Result<()> { debug!("Removing driver id {id:?} ({driver_info:?})"); hub.remove_driver(id).await?; } - */ Ok(()) }