chanmgr: Use ChannelFactory via a Box<dyn<ChannelFactory>>.

This will prepare for supporting multiple different ChannelFactory
implementations.
This commit is contained in:
Nick Mathewson 2022-10-12 10:07:03 -04:00
parent 15108be5ce
commit 6b587b25e1
2 changed files with 28 additions and 4 deletions

View File

@ -1,6 +1,8 @@
//! Traits and code to define different mechanisms for building Channels to
//! different kinds of targets.
use std::sync::Arc;
use async_trait::async_trait;
use futures::{AsyncRead, AsyncWrite};
use tor_linkspec::{HasChanMethod, OwnedChanTarget, TransportId};
@ -136,3 +138,17 @@ impl<R: TransportRegistry + Sync> ChannelFactory for RegistryAsFactory<R> {
factory.connect_via_transport(target).await
}
}
#[async_trait]
impl<'a> ChannelFactory for Arc<(dyn ChannelFactory + Send + Sync + 'a)> {
async fn connect_via_transport(&self, target: &OwnedChanTarget) -> crate::Result<Channel> {
self.as_ref().connect_via_transport(target).await
}
}
#[async_trait]
impl<'a> ChannelFactory for Box<(dyn ChannelFactory + Send + Sync + 'a)> {
async fn connect_via_transport(&self, target: &OwnedChanTarget) -> crate::Result<Channel> {
self.as_ref().connect_via_transport(target).await
}
}

View File

@ -79,6 +79,7 @@ mod mgr;
mod testing;
use educe::Educe;
use factory::ChannelFactory;
use futures::select_biased;
use futures::task::SpawnExt;
use futures::StreamExt;
@ -111,12 +112,14 @@ use tor_rtcompat::scheduler::{TaskHandle, TaskSchedule};
/// get one if it exists.
pub struct ChanMgr<R: Runtime> {
/// Internal channel manager object that does the actual work.
mgr: mgr::AbstractChanMgr<
builder::TimeoutChannelFactory<R, builder::ChanBuilder<R, builder::DefaultTransport<R>>>,
>,
mgr: mgr::AbstractChanMgr<Box<dyn ChannelFactory + Send + Sync + 'static>>,
/// Stream of [`ConnStatus`] events.
bootstrap_status: event::ConnStatusEvents,
/// This currently isn't actually used, but we're keeping a PhantomData here
/// since probably we'll want it again, sooner or later.
runtime: std::marker::PhantomData<fn(R) -> R>,
}
/// Description of how we got a channel.
@ -189,15 +192,20 @@ impl<R: Runtime> ChanMgr<R> {
config: &ChannelConfig,
dormancy: Dormancy,
netparams: &NetParameters,
) -> Self {
) -> Self
where
R: 'static,
{
let (sender, receiver) = event::channel();
let transport = builder::DefaultTransport::new(runtime.clone());
let builder = builder::ChanBuilder::new(runtime.clone(), transport, sender);
let builder = builder::TimeoutChannelFactory::new(runtime, builder);
let builder: Box<dyn ChannelFactory + Send + Sync + 'static> = Box::new(builder);
let mgr = mgr::AbstractChanMgr::new(builder, config, dormancy, netparams);
ChanMgr {
mgr,
bootstrap_status: receiver,
runtime: std::marker::PhantomData,
}
}