Skip to content

feat(iroh)!: remove get_protocol and the plumbing required for it #3009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to clone this above anymore

task: Arc::new(Mutex::new(Some(task))),
cancel_token: cancel,
})
Expand Down
Loading