Skip to content

Commit

Permalink
eliminated a few generics
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Jul 22, 2023
1 parent ec44ced commit 98903e6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 83 deletions.
33 changes: 33 additions & 0 deletions matchbox_signaling/examples/tls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use matchbox_signaling::SignalingServer;
use std::net::Ipv4Addr;
use tracing::info;

#[tokio::main]
async fn main() -> Result<(), matchbox_signaling::Error> {
setup_logging();

let server = SignalingServer::full_mesh_builder((Ipv4Addr::UNSPECIFIED, 3536))
.on_connection_request(|connection| {
info!("Connecting: {connection:?}");
Ok(true) // Allow all connections
})
.on_id_assignment(|(socket, id)| info!("{socket} received {id}"))
.on_peer_connected(|id| info!("Joined: {id}"))
.on_peer_disconnected(|id| info!("Left: {id}"))
.tls("", "")
.await
.trace()
.build();
server.serve().await
}

fn setup_logging() {
use tracing_subscriber::prelude::*;
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "debug".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
}
101 changes: 29 additions & 72 deletions matchbox_signaling/src/signaling_server/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
SignalingCallbacks, SignalingServer, SignalingState,
};
use axum::{response::Response, routing::get, Extension, Router};
use axum_server::{accept::DefaultAcceptor, tls_rustls::RustlsConfig, Handle};
use axum_server::{tls_rustls::RustlsConfig, Handle};
use matchbox_protocol::PeerId;
use std::net::SocketAddr;
use std::path::Path;
Expand All @@ -23,7 +23,7 @@ use tracing::Level;
///
/// Begin with [`SignalingServerBuilder::new`] and add parameters before calling
/// [`SignalingServerBuilder::build`] to produce the desired [`SignalingServer`].
pub struct SignalingServerBuilder<Topology, Cb = NoCallbacks, S = NoState, Tls = DefaultAcceptor>
pub struct SignalingServerBuilder<Topology, Cb = NoCallbacks, S = NoState>
where
Topology: SignalingTopology<Cb, S>,
Cb: SignalingCallbacks,
Expand All @@ -47,8 +47,8 @@ where
/// Arbitrary state accompanying a server
pub(crate) state: S,

/// A server acceptor
pub(crate) acceptor: Tls,
/// Tls config
pub(crate) tls: Option<RustlsConfig>,
}

impl<Topology, Cb, S> SignalingServerBuilder<Topology, Cb, S>
Expand All @@ -58,19 +58,15 @@ where
S: SignalingState,
{
/// Creates a new builder for a [`SignalingServer`].
pub fn new(
socket_addr: impl Into<SocketAddr>,
topology: Topology,
state: S,
) -> SignalingServerBuilder<Topology, Cb, S, DefaultAcceptor> {
SignalingServerBuilder {
pub fn new(socket_addr: impl Into<SocketAddr>, topology: Topology, state: S) -> Self {
Self {
socket_addr: socket_addr.into(),
router: Router::new(),
shared_callbacks: SharedCallbacks::default(),
callbacks: Cb::default(),
topology,
state,
acceptor: DefaultAcceptor,
tls: None,
}
}

Expand Down Expand Up @@ -101,21 +97,10 @@ where
}

/// Configure TLS with a certificate (.pem) and private key (.key) file
pub async fn tls(
self,
cert: impl AsRef<Path>,
key: impl AsRef<Path>,
) -> SignalingServerBuilder<Topology, Cb, S, RustlsConfig> {
pub async fn tls(mut self, cert: impl AsRef<Path>, key: impl AsRef<Path>) -> Self {
let config = RustlsConfig::from_pem_file(cert, key).await.unwrap();
SignalingServerBuilder {
socket_addr: self.socket_addr,
router: self.router,
shared_callbacks: self.shared_callbacks,
callbacks: self.callbacks,
topology: self.topology,
state: self.state,
acceptor: config,
}
self.tls.replace(config);
self
}

/// Apply permissive CORS middleware for debug purposes.
Expand All @@ -141,15 +126,8 @@ where
);
self
}
}

impl<Topology, Cb, S> SignalingServerBuilder<Topology, Cb, S, DefaultAcceptor>
where
Topology: SignalingTopology<Cb, S>,
Cb: SignalingCallbacks,
S: SignalingState,
{
/// Create a [`SignalingServer`].
// Create a [`SignalingServer`].
///
/// # Panics
/// This method will panic if the socket address requested cannot be bound.
Expand All @@ -165,46 +143,25 @@ where
.layer(Extension(self.callbacks))
.layer(Extension(self.state));
let handle = Handle::new();
SignalingServer {
server: Box::pin(
axum_server::bind(self.socket_addr)
.handle(handle.clone())
.serve(router.into_make_service_with_connect_info::<SocketAddr>()),
),
handle,
}
}
}

impl<Topology, Cb, S> SignalingServerBuilder<Topology, Cb, S, RustlsConfig>
where
Topology: SignalingTopology<Cb, S>,
Cb: SignalingCallbacks,
S: SignalingState,
{
/// Create a [`SignalingServer`].
///
/// # Panics
/// This method will panic if the socket address requested cannot be bound.
pub fn build(self) -> SignalingServer {
let state_machine: SignalingStateMachine<Cb, S> =
SignalingStateMachine::from_topology(self.topology);
let router = self
.router
.route("/", get(ws_handler::<Cb, S>))
.route("/:path", get(ws_handler::<Cb, S>))
.layer(Extension(state_machine))
.layer(Extension(self.shared_callbacks))
.layer(Extension(self.callbacks))
.layer(Extension(self.state));
let handle = Handle::new();
SignalingServer {
server: Box::pin(
axum_server::bind_rustls(self.socket_addr, self.acceptor)
.handle(handle.clone())
.serve(router.into_make_service_with_connect_info::<SocketAddr>()),
),
handle,
if let Some(config) = self.tls {
SignalingServer {
server: Box::pin(
axum_server::bind_rustls(self.socket_addr, config)
.handle(handle.clone())
.serve(router.into_make_service_with_connect_info::<SocketAddr>()),
),
handle,
}
} else {
SignalingServer {
server: Box::pin(
axum_server::bind(self.socket_addr)
.handle(handle.clone())
.serve(router.into_make_service_with_connect_info::<SocketAddr>()),
),
handle,
}
}
}
}
14 changes: 3 additions & 11 deletions matchbox_signaling/src/signaling_server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
full_mesh::{FullMesh, FullMeshCallbacks, FullMeshState},
},
};
use axum_server::{accept::DefaultAcceptor, Handle};
use axum_server::Handle;
use futures::Future;
use std::{io, net::SocketAddr, pin::Pin};

Expand All @@ -24,22 +24,14 @@ impl SignalingServer {
pub fn full_mesh_builder(
socket_addr: impl Into<SocketAddr>,
) -> SignalingServerBuilder<FullMesh, FullMeshCallbacks, FullMeshState> {
SignalingServerBuilder::<_, _, _, DefaultAcceptor>::new(
socket_addr,
FullMesh,
FullMeshState::default(),
)
SignalingServerBuilder::new(socket_addr, FullMesh, FullMeshState::default())
}

/// Creates a new builder for a [`SignalingServer`] with client-server topology.
pub fn client_server_builder(
socket_addr: impl Into<SocketAddr>,
) -> SignalingServerBuilder<ClientServer, ClientServerCallbacks, ClientServerState> {
SignalingServerBuilder::<_, _, _, DefaultAcceptor>::new(
socket_addr,
ClientServer,
ClientServerState::default(),
)
SignalingServerBuilder::new(socket_addr, ClientServer, ClientServerState::default())
}

/// Returns a clone to a server handle for introspection.
Expand Down

0 comments on commit 98903e6

Please sign in to comment.