Skip to content

Commit

Permalink
refactor: renamed TrackerInterface struct and relevant trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvandijke authored and josecelano committed Mar 9, 2023
1 parent f40e43f commit 4f2b035
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/http/axum_implementation/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use log::info;
use warp::hyper;

use super::routes::router;
use crate::http::tracker_interface::TrackerInterfaceTrait;
use crate::http::tracker_interface::HttpServerLauncher;
use crate::tracker::Tracker;

#[derive(Debug)]
Expand Down Expand Up @@ -77,7 +77,7 @@ impl Server {
}

#[async_trait]
impl TrackerInterfaceTrait for Server {
impl HttpServerLauncher for Server {
fn new() -> Self {
Self {}
}
Expand Down
42 changes: 21 additions & 21 deletions src/http/tracker_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use futures::future::BoxFuture;
use crate::signals::shutdown_signal;
use crate::tracker::Tracker;

/// Trait to be implemented by a http interface for the tracker.
/// Trait to be implemented by a http server launcher for the tracker.
#[allow(clippy::module_name_repetitions)]
pub trait TrackerInterfaceTrait: Sync + Send {
pub trait HttpServerLauncher: Sync + Send {
fn new() -> Self;

fn start_with_graceful_shutdown<F>(
Expand All @@ -28,54 +28,54 @@ pub enum Error {
}

#[allow(clippy::module_name_repetitions)]
pub type StoppedHttpServer<I> = TrackerInterface<Stopped<I>>;
pub type StoppedHttpServer<I> = HttpServer<Stopped<I>>;
#[allow(clippy::module_name_repetitions)]
pub type RunningHttpServer<I> = TrackerInterface<Running<I>>;
pub type RunningHttpServer<I> = HttpServer<Running<I>>;

pub struct TrackerInterface<S> {
pub struct HttpServer<S> {
cfg: torrust_tracker_configuration::HttpTracker,
state: S,
}

pub struct Stopped<I: TrackerInterfaceTrait> {
interface: I,
pub struct Stopped<I: HttpServerLauncher> {
launcher: I,
}

pub struct Running<I: TrackerInterfaceTrait> {
pub struct Running<I: HttpServerLauncher> {
bind_addr: SocketAddr,
task_killer: tokio::sync::oneshot::Sender<u8>,
task: tokio::task::JoinHandle<I>,
}

impl<I: TrackerInterfaceTrait + 'static> TrackerInterface<Stopped<I>> {
pub fn new(cfg: torrust_tracker_configuration::HttpTracker, interface: I) -> Self {
impl<I: HttpServerLauncher + 'static> HttpServer<Stopped<I>> {
pub fn new(cfg: torrust_tracker_configuration::HttpTracker, launcher: I) -> Self {
Self {
cfg,
state: Stopped { interface },
state: Stopped { launcher },
}
}

pub async fn start(self, tracker: Arc<Tracker>) -> Result<TrackerInterface<Running<I>>, Error> {
pub async fn start(self, tracker: Arc<Tracker>) -> Result<HttpServer<Running<I>>, Error> {
let (shutdown_sender, shutdown_receiver) = tokio::sync::oneshot::channel::<u8>();
let (addr_sender, addr_receiver) = tokio::sync::oneshot::channel::<SocketAddr>();

let configuration = self.cfg.clone();
let interface = self.state.interface;
let launcher = self.state.launcher;

let task = tokio::spawn(async move {
let (bind_addr, server) =
interface.start_with_graceful_shutdown(configuration, tracker, shutdown_signal(shutdown_receiver));
launcher.start_with_graceful_shutdown(configuration, tracker, shutdown_signal(shutdown_receiver));

addr_sender.send(bind_addr).unwrap();

server.await;

interface
launcher
});

let bind_address = addr_receiver.await.expect("Could not receive bind_address.");

Ok(TrackerInterface {
Ok(HttpServer {
cfg: self.cfg,
state: Running {
bind_addr: bind_address,
Expand All @@ -86,15 +86,15 @@ impl<I: TrackerInterfaceTrait + 'static> TrackerInterface<Stopped<I>> {
}
}

impl<I: TrackerInterfaceTrait> TrackerInterface<Running<I>> {
pub async fn stop(self) -> Result<TrackerInterface<Stopped<I>>, Error> {
impl<I: HttpServerLauncher> HttpServer<Running<I>> {
pub async fn stop(self) -> Result<HttpServer<Stopped<I>>, Error> {
self.state.task_killer.send(0).unwrap();

let interface = self.state.task.await.map_err(|e| Error::Error(e.to_string()))?;
let launcher = self.state.task.await.map_err(|e| Error::Error(e.to_string()))?;

Ok(TrackerInterface {
Ok(HttpServer {
cfg: self.cfg,
state: Stopped { interface },
state: Stopped { launcher },
})
}
}
4 changes: 2 additions & 2 deletions src/http/warp_implementation/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::Arc;
use futures::future::BoxFuture;

use super::routes;
use crate::http::tracker_interface::TrackerInterfaceTrait;
use crate::http::tracker_interface::HttpServerLauncher;
use crate::tracker;
use crate::tracker::Tracker;

Expand Down Expand Up @@ -50,7 +50,7 @@ impl Server {
}
}

impl TrackerInterfaceTrait for Server {
impl HttpServerLauncher for Server {
fn new() -> Self {
Self {}
}
Expand Down
23 changes: 14 additions & 9 deletions tests/http/test_environment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use torrust_tracker::http::tracker_interface::{RunningHttpServer, StoppedHttpServer, TrackerInterface, TrackerInterfaceTrait};
use torrust_tracker::http::tracker_interface::{HttpServer, HttpServerLauncher, RunningHttpServer, StoppedHttpServer};
use torrust_tracker::protocol::info_hash::InfoHash;
use torrust_tracker::tracker::peer::Peer;
use torrust_tracker::tracker::Tracker;
Expand All @@ -18,11 +18,11 @@ pub struct TestEnvironment<S> {
}

#[allow(dead_code)]
pub struct Stopped<I: TrackerInterfaceTrait> {
pub struct Stopped<I: HttpServerLauncher> {
http_server: StoppedHttpServer<I>,
}

pub struct Running<I: TrackerInterfaceTrait> {
pub struct Running<I: HttpServerLauncher> {
http_server: RunningHttpServer<I>,
}

Expand All @@ -33,7 +33,7 @@ impl<S> TestEnvironment<S> {
}
}

impl<I: TrackerInterfaceTrait + 'static> TestEnvironment<Stopped<I>> {
impl<I: HttpServerLauncher + 'static> TestEnvironment<Stopped<I>> {
#[allow(dead_code)]
pub fn new_stopped() -> Self {
let cfg = tracker_configuration();
Expand All @@ -59,7 +59,7 @@ impl<I: TrackerInterfaceTrait + 'static> TestEnvironment<Stopped<I>> {
}
}

impl<I: TrackerInterfaceTrait + 'static> TestEnvironment<Running<I>> {
impl<I: HttpServerLauncher + 'static> TestEnvironment<Running<I>> {
pub async fn new_running() -> Self {
let test_env = StoppedTestEnvironment::new_stopped();

Expand All @@ -77,19 +77,24 @@ impl<I: TrackerInterfaceTrait + 'static> TestEnvironment<Running<I>> {
}

#[allow(clippy::module_name_repetitions)]
pub async fn running_test_environment<I: TrackerInterfaceTrait + 'static>() -> RunningTestEnvironment<I> {
pub async fn stopped_test_environment<I: HttpServerLauncher + 'static>() -> StoppedTestEnvironment<I> {
TestEnvironment::new_stopped().await
}

#[allow(clippy::module_name_repetitions)]
pub async fn running_test_environment<I: HttpServerLauncher + 'static>() -> RunningTestEnvironment<I> {
TestEnvironment::new_running().await
}

pub fn stopped_http_server<I: TrackerInterfaceTrait + 'static>(
pub fn stopped_http_server<I: HttpServerLauncher + 'static>(
cfg: torrust_tracker_configuration::HttpTracker,
) -> StoppedHttpServer<I> {
let http_server = I::new();

TrackerInterface::new(cfg, http_server)
HttpServer::new(cfg, http_server)
}

pub async fn running_http_server<I: TrackerInterfaceTrait + 'static>(
pub async fn running_http_server<I: HttpServerLauncher + 'static>(
cfg: torrust_tracker_configuration::HttpTracker,
tracker: Arc<Tracker>,
) -> RunningHttpServer<I> {
Expand Down

0 comments on commit 4f2b035

Please sign in to comment.