Skip to content

Commit

Permalink
feat(iroh)!: remove get_protocol and the plumbing required for it
Browse files Browse the repository at this point in the history
it has not turned out to be as useful as originally expected
  • Loading branch information
rklaehn committed Dec 4, 2024
1 parent b76500d commit cafcc05
Showing 1 changed file with 2 additions and 42 deletions.
44 changes: 2 additions & 42 deletions iroh/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! }
//! }
//! ```
use std::{any::Any, collections::BTreeMap, sync::Arc};
use std::{collections::BTreeMap, sync::Arc};

use anyhow::Result;
use futures_buffered::join_all;
Expand Down Expand Up @@ -86,7 +86,6 @@ use crate::{endpoint::Connecting, Endpoint};
#[derive(Clone, Debug)]
pub struct Router {
endpoint: Endpoint,
protocols: Arc<ProtocolMap>,
// `Router` needs to be `Clone + Send`, and we need to `task.await` in its `shutdown()` impl.
task: Arc<Mutex<Option<AbortOnDropHandle<()>>>>,
cancel_token: CancellationToken,
Expand All @@ -108,7 +107,7 @@ pub struct RouterBuilder {
/// Implement this trait on a struct that should handle incoming connections.
/// The protocol handler must then be registered on the node for an ALPN protocol with
/// [`crate::protocol::RouterBuilder::accept`].
pub trait ProtocolHandler: Send + Sync + IntoArcAny + std::fmt::Debug + 'static {
pub trait ProtocolHandler: Send + Sync + std::fmt::Debug + 'static {
/// Handle an incoming connection.
///
/// This runs on a freshly spawned tokio task so this can be long-running.
Expand All @@ -120,33 +119,11 @@ pub trait ProtocolHandler: Send + Sync + IntoArcAny + std::fmt::Debug + 'static
}
}

/// Helper trait to facilite casting from `Arc<dyn T>` to `Arc<dyn Any>`.
///
/// This trait has a blanket implementation so there is no need to implement this yourself.
pub trait IntoArcAny {
/// Casts `Arc<Self>` into `Arc<dyn Any + Send + Sync>`.
fn into_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
}

impl<T: Send + Sync + 'static> IntoArcAny for T {
fn into_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
self
}
}

/// A typed map of protocol handlers, mapping them from ALPNs.
#[derive(Debug, Clone, Default)]
pub struct ProtocolMap(BTreeMap<Vec<u8>, Arc<dyn ProtocolHandler>>);

impl ProtocolMap {
/// Returns the registered protocol handler for an ALPN as a concrete type.
pub fn get_typed<P: ProtocolHandler>(&self, alpn: &[u8]) -> Option<Arc<P>> {
let protocol: Arc<dyn ProtocolHandler> = self.0.get(alpn)?.clone();
let protocol_any: Arc<dyn Any + Send + Sync> = protocol.into_arc_any();
let protocol_ref = Arc::downcast(protocol_any).ok()?;
Some(protocol_ref)
}

/// Returns the registered protocol handler for an ALPN as a [`Arc<dyn ProtocolHandler>`].
pub fn get(&self, alpn: &[u8]) -> Option<Arc<dyn ProtocolHandler>> {
self.0.get(alpn).cloned()
Expand Down Expand Up @@ -177,14 +154,6 @@ impl Router {
RouterBuilder::new(endpoint)
}

/// Returns a protocol handler for an ALPN.
///
/// This downcasts to the concrete type and returns `None` if the handler registered for `alpn`
/// does not match the passed type.
pub fn get_protocol<P: ProtocolHandler>(&self, alpn: &[u8]) -> Option<Arc<P>> {
self.protocols.get_typed(alpn)
}

/// Returns the [`Endpoint`] stored in this router.
pub fn endpoint(&self) -> &Endpoint {
&self.endpoint
Expand Down Expand Up @@ -242,14 +211,6 @@ impl RouterBuilder {
&self.endpoint
}

/// Returns a protocol handler for an ALPN.
///
/// This downcasts to the concrete type and returns `None` if the handler registered for `alpn`
/// does not match the passed type.
pub fn get_protocol<P: ProtocolHandler>(&self, alpn: &[u8]) -> Option<Arc<P>> {
self.protocols.get_typed(alpn)
}

/// Spawns an accept loop and returns a handle to it encapsulated as the [`Router`].
pub async fn spawn(self) -> Result<Router> {
// Update the endpoint with our alpns.
Expand Down Expand Up @@ -335,7 +296,6 @@ impl RouterBuilder {

Ok(Router {
endpoint: self.endpoint,
protocols,
task: Arc::new(Mutex::new(Some(task))),
cancel_token: cancel,
})
Expand Down

0 comments on commit cafcc05

Please sign in to comment.