From 82699113e50d5f294f907877c44ad7f08db4d493 Mon Sep 17 00:00:00 2001 From: jmwample <8297368+jmwample@users.noreply.github.com> Date: Tue, 12 Mar 2024 23:34:50 -0600 Subject: [PATCH] ptrs remove identity design example in favor of passthrough --- crates/ptrs/src/ident/dialer.rs | 0 crates/ptrs/src/ident/handler.rs | 50 ----------- crates/ptrs/src/ident/listener.rs | 0 crates/ptrs/src/ident/mod.rs | 136 ------------------------------ crates/ptrs/src/ident/wrapper.rs | 1 - crates/ptrs/src/lib.rs | 15 ++-- crates/ptrs/src/passthrough.rs | 20 +++-- 7 files changed, 19 insertions(+), 203 deletions(-) delete mode 100644 crates/ptrs/src/ident/dialer.rs delete mode 100644 crates/ptrs/src/ident/handler.rs delete mode 100644 crates/ptrs/src/ident/listener.rs delete mode 100644 crates/ptrs/src/ident/mod.rs delete mode 100644 crates/ptrs/src/ident/wrapper.rs diff --git a/crates/ptrs/src/ident/dialer.rs b/crates/ptrs/src/ident/dialer.rs deleted file mode 100644 index e69de29..0000000 diff --git a/crates/ptrs/src/ident/handler.rs b/crates/ptrs/src/ident/handler.rs deleted file mode 100644 index 6856672..0000000 --- a/crates/ptrs/src/ident/handler.rs +++ /dev/null @@ -1,50 +0,0 @@ -use super::{Cfg, NAME}; - -use crate::{traits::*, Error, Result}; - -#[derive(Default, Clone, Debug)] -pub struct Handler { - config: Cfg, - is_server: bool, -} - -impl Handler { - fn as_role(role: &Role) -> Result { - match role { - Role::Sender => Ok(Self { - config: Cfg::default(), - is_server: false, - }), - Role::Receiver => Ok(Self { - config: Cfg::default(), - is_server: true, - }), - _ => Err(Error::NotSupported), - } - } -} - -impl Named for Handler { - fn name(&self) -> String { - format!("{NAME}_client") - } -} - -impl TryConfigure for Handler { - fn try_config<'a>(&mut self, config: impl AsRef<[u8]> + 'a) -> Result<&mut Self> { - self.config = Cfg { - s: String::from_utf8(config.as_ref().to_vec())?, - }; - Ok(self) - } -} - -// // TODO: Implement Transport for ident while fixing lifetime -// impl Transport for Handler { -// fn wrap<'a>( -// &self, -// s: impl Stream<'a>, -// ) -> impl Future>> + Send + Sync + 'a { -// async { Ok(s) } -// } -// } diff --git a/crates/ptrs/src/ident/listener.rs b/crates/ptrs/src/ident/listener.rs deleted file mode 100644 index e69de29..0000000 diff --git a/crates/ptrs/src/ident/mod.rs b/crates/ptrs/src/ident/mod.rs deleted file mode 100644 index 275b0e9..0000000 --- a/crates/ptrs/src/ident/mod.rs +++ /dev/null @@ -1,136 +0,0 @@ -use crate::{ - traits::{self}, - Result, -}; - -pub mod handler; -pub mod wrapper; - -const NAME: &str = "identity"; - -#[derive(Default)] -#[allow(non_camel_case_types)] -pub struct Builder { - config: Cfg, -} - -impl traits::Named for Builder { - fn name(&self) -> String { - String::from(NAME) - } -} - -#[derive(Default, Clone, Debug)] -struct Cfg { - s: String, -} - -impl traits::TryConfigure for Builder { - fn try_config<'a>(&mut self, config: impl AsRef<[u8]> + 'a) -> Result<&mut Self> { - let s = String::from_utf8(config.as_ref().to_vec())?; - self.config = Cfg { s }; - Ok(self) - } -} - -// // TODO: fix -// // Example showing that the builder can be configured and take it's own -// // configuration into account when making a transport, which can also be -// // configured independently. -// impl traits::Builder for Builder { -// fn handler(&self, role: &traits::Role) -> Result { -// match role { -// Role::Sender => Ok(Handler::default()), -// Role::Receiver => match self.config.s.as_str() { -// "error" => Err(Error::Other("expected error".into())), -// _ => Ok(Handler::default()), -// }, -// _ => Err(Error::NotSupported), -// } -// } -// } - -/* -#[cfg(test)] -mod test { - use super::*; - use crate::traits::{Builder as _, Transport as _, TryConfigure}; - use futures::executor::block_on; - use std::sync::Arc; - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - use tokio::sync::Mutex; - - #[test] - fn basics() -> Result<()> { - let builder_cfg = b""; - let client_cfg = b""; - let server_cfg = b""; - - let mut builder = Builder::default(); - let mut client_transport = builder.try_config(builder_cfg)?.handler(&Role::Sender)?; - let client_transport = client_transport.try_config(client_cfg)?; - - let mut builder = Builder::default(); - let mut server_transport = builder.try_config(builder_cfg)?.handler(&Role::Receiver)?; - let server_transport = server_transport.try_config(server_cfg)?; - - // imagine either creating a TCP stream or acctping one. - let (mut conn, _) = tokio::io::duplex(128); - - let mut cc = block_on(client_transport.wrap(&mut conn))?; - - let _sc = block_on(server_transport.wrap(&mut cc))?; - - Ok(()) - } - - #[tokio::test] - async fn tokio_basics_test() -> Result<()> { - let msg = String::new(); - let failed = Arc::new(Mutex::new(msg)); - - let client_cfg = ""; - let server_cfg = ""; - let (a, mut b) = tokio::io::duplex(128); - - let f = failed.clone(); - tokio::spawn(async move { - let message = b"wkme;a wme09wemw qweqowme ;qwk2q3 m2 3 0eq@32 q2q23q2 q2rq2"; - - tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; - - b.write_all(message).await.unwrap(); - - let mut buf = vec![0_u8; message.len()]; - let _ = b.read_exact(&mut buf).await.unwrap(); - if message != buf.as_slice() { - let mut err = f.lock().await; - *err = format!("incorrect message read {:?}", buf); - } - - b.shutdown().await.unwrap(); - }); - - let builder = Builder::default(); - let mut client_transport = builder.handler(&Role::Sender).unwrap(); - let client_transport = client_transport.try_config(client_cfg).unwrap(); - - let builder = Builder::default(); - let mut server_transport = builder.handler(&Role::Receiver).unwrap(); - let server_transport = server_transport.try_config(server_cfg).unwrap(); - - let w1 = client_transport.wrap(a).await.unwrap(); - let w2 = server_transport.wrap(w1).await.unwrap(); - - let (mut e1, mut e2) = tokio::io::split(w2); - tokio::io::copy(&mut e1, &mut e2).await.unwrap(); - - let res = failed.lock().await; - if !(*res).is_empty() { - Err(Error::from(&*res.clone())) - } else { - Ok(()) - } - } -} -*/ diff --git a/crates/ptrs/src/ident/wrapper.rs b/crates/ptrs/src/ident/wrapper.rs deleted file mode 100644 index 8b13789..0000000 --- a/crates/ptrs/src/ident/wrapper.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/crates/ptrs/src/lib.rs b/crates/ptrs/src/lib.rs index 17bfa4c..4cb1ea5 100644 --- a/crates/ptrs/src/lib.rs +++ b/crates/ptrs/src/lib.rs @@ -19,7 +19,7 @@ pub use helpers::*; pub trait PluggableTransport { type ClientBuilder: ClientBuilderByTypeInst; - type ServerBuilder: ServerBuilder; + type ServerBuilder: ServerBuilder; // type Client: ClientTransport; // type Server: ServerTransport; @@ -97,7 +97,7 @@ pub trait ClientTransport { pub trait ServerTransport { type OutRW; type OutErr: std::error::Error; - type Builder: ServerBuilder; + type Builder: ServerBuilder; /// Create/accept a connection for the pluggable transport client using the /// provided (pre-existing/pre-connected) Read/Write object as the @@ -105,8 +105,8 @@ pub trait ServerTransport { fn reveal(&self, io: InRW) -> Pin>; } -pub trait ServerBuilder: Default { - type ServerPT; +pub trait ServerBuilder: Default { + type ServerPT: ServerTransport; type Error: std::error::Error; type Transport; @@ -139,9 +139,10 @@ pub trait ServerBuilder: Default { } /// Server Transport trait2 - try using futures instead of actual objects -// This doesn't work because it requires the listener object that was used -// to create the `input` Future must live for `'static` for the future to -// be valid, but that can't be guaranteed and is difficult to work with. +/// +/// This doesn't work because it requires the listener object that was used +/// to create the `input` Future must live for `'static` for the future to +/// be valid, but that can't be guaranteed and is difficult to work with. pub trait ServerTransport2 { type OutRW; type OutErr: std::error::Error; diff --git a/crates/ptrs/src/passthrough.rs b/crates/ptrs/src/passthrough.rs index 7fc462c..94b0d14 100644 --- a/crates/ptrs/src/passthrough.rs +++ b/crates/ptrs/src/passthrough.rs @@ -31,7 +31,7 @@ where #[derive(Debug, Default)] pub struct BuilderS {} -impl ServerBuilder for BuilderS { +impl ServerBuilder for BuilderS { type Error = std::io::Error; type ServerPT = Passthrough; type Transport = Passthrough; @@ -184,7 +184,7 @@ impl Passthrough { mod design_tests { use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, + io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}, net::TcpStream, }; use tracing::info; @@ -456,6 +456,7 @@ mod design_tests { Box, > where + T: AsyncRead + AsyncWrite + Send, B: ClientBuilderByTypeInst, B::ClientPT: ClientTransport, B::Error: std::error::Error + 'static, @@ -520,8 +521,8 @@ mod design_tests { ) -> Result< Pin< FutureResult< - <<

>::ServerBuilder as ServerBuilder>::ServerPT as ServerTransport>::OutRW, - <<

>::ServerBuilder as ServerBuilder>::ServerPT as ServerTransport>::OutErr, + <<

>::ServerBuilder as ServerBuilder>::ServerPT as ServerTransport>::OutRW, + <<

>::ServerBuilder as ServerBuilder>::ServerPT as ServerTransport>::OutErr, >, >, Box, @@ -529,8 +530,8 @@ mod design_tests { where P: PluggableTransport, P::ClientBuilder: ClientBuilderByTypeInst, - <

>::ServerBuilder as ServerBuilder>::ServerPT: ServerTransport, - <

>::ServerBuilder as ServerBuilder>::Error: std::error::Error + 'static, + <

>::ServerBuilder as ServerBuilder>::ServerPT: ServerTransport, + <

>::ServerBuilder as ServerBuilder>::Error: std::error::Error + 'static, E: std::error::Error + 'static, { Ok(P::server_builder() @@ -599,14 +600,15 @@ mod design_tests { ) -> Result< Pin< FutureResult< - <::ServerPT as ServerTransport>::OutRW, - <::ServerPT as ServerTransport>::OutErr, + <>::ServerPT as ServerTransport>::OutRW, + <>::ServerPT as ServerTransport>::OutErr, >, >, Box, > where - B: ServerBuilder, + T: AsyncRead + AsyncWrite + Send, + B: ServerBuilder, B::ServerPT: ServerTransport, B::Error: std::error::Error + 'static, {