Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
  • Loading branch information
patrickelectric committed Aug 25, 2024
1 parent c6ce232 commit 48480cc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 47 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ fn get_endpoint_with_kind(kind: &str) -> Vec<Url> {
MANAGER.read().unwrap().clap_matches.endpoints.iter().filter(|url| url.scheme() == kind).map(Clone::clone).collect()
}

pub fn endpoints() -> Vec<Url> {
MANAGER.read().unwrap().clap_matches.endpoints.clone()
}

// Return the command line used to start this application

#[instrument(level = "debug")]
Expand Down
16 changes: 14 additions & 2 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,9 +53,9 @@ pub trait DriverExt {
fn valid_schemes(&self) -> Vec<String>;
// 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<Box<dyn Driver>>;
fn create_endpoint_from_url(&self, url: &Url) -> Option<Arc<dyn Driver>>;

fn create_endpoint(&self, entry: &str) -> Option<Box<dyn Driver>> {
fn create_endpoint(&self, entry: &str) -> Option<Arc<dyn Driver>> {
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
Expand Down Expand Up @@ -85,6 +87,16 @@ pub trait DriverExt {
}
}

pub fn create_driver(url: &Url) -> Option<Arc<dyn Driver>> {
let endpoints = endpoints(); // Assuming `endpoints` is a function returning Vec<ExtInfo>

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<dyn DriverExt>,
pub typ: Type,
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/tcp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn Driver>> {
fn create_endpoint_from_url(&self, url: &url::Url) -> Option<Arc<dyn Driver>> {
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}"))));
}
}
47 changes: 4 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,17 @@ 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;

for (id, driver_info) in hub.drivers().await {
debug!("Removing driver id {id:?} ({driver_info:?})");
hub.remove_driver(id).await?;
}
*/

Ok(())
}
Expand Down

0 comments on commit 48480cc

Please sign in to comment.