Skip to content

Commit

Permalink
refactor: rename UDP server types
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 25, 2024
1 parent 336e0e6 commit c121bf2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/bootstrap/jobs/udp_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tracing::debug;

use crate::core;
use crate::servers::registar::ServiceRegistrationForm;
use crate::servers::udp::server::{Launcher, UdpServer};
use crate::servers::udp::server::{Spawner, UdpServer};
use crate::servers::udp::UDP_TRACKER_LOG_TARGET;

/// It starts a new UDP server with the provided configuration.
Expand All @@ -30,7 +30,7 @@ use crate::servers::udp::UDP_TRACKER_LOG_TARGET;
pub async fn start_job(config: &UdpTracker, tracker: Arc<core::Tracker>, form: ServiceRegistrationForm) -> JoinHandle<()> {
let bind_to = config.bind_address;

let server = UdpServer::new(Launcher::new(bind_to))
let server = UdpServer::new(Spawner::new(bind_to))
.start(tracker, form)
.await
.expect("it should be able to start the udp tracker");
Expand Down
33 changes: 17 additions & 16 deletions src/servers/udp/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub struct UdpServer<S> {
/// A stopped UDP server state.

pub struct Stopped {
launcher: Launcher,
launcher: Spawner,
}

/// A running UDP server state.
Expand All @@ -105,13 +105,13 @@ pub struct Running {
/// The address where the server is bound.
pub binding: SocketAddr,
pub halt_task: tokio::sync::oneshot::Sender<Halted>,
pub task: JoinHandle<Launcher>,
pub task: JoinHandle<Spawner>,
}

impl UdpServer<Stopped> {
/// Creates a new `UdpServer` instance in `stopped`state.
#[must_use]
pub fn new(launcher: Launcher) -> Self {
pub fn new(launcher: Spawner) -> Self {
Self {
state: Stopped { launcher },
}
Expand Down Expand Up @@ -140,7 +140,7 @@ impl UdpServer<Stopped> {
let binding = rx_start.await.expect("it should be able to start the service").address;
let local_addr = format!("udp://{binding}");

form.send(ServiceRegistration::new(binding, Udp::check))
form.send(ServiceRegistration::new(binding, Launcher::check))
.expect("it should be able to send service registration");

let running_udp_server: UdpServer<Running> = UdpServer {
Expand Down Expand Up @@ -186,12 +186,12 @@ impl UdpServer<Running> {
}

#[derive(Constructor, Copy, Clone, Debug)]
pub struct Launcher {
pub struct Spawner {
bind_to: SocketAddr,
}

impl Launcher {
/// It starts the UDP server instance.
impl Spawner {
/// It spawns a new tasks to run the UDP server instance.
///
/// # Panics
///
Expand All @@ -201,10 +201,10 @@ impl Launcher {
tracker: Arc<Tracker>,
tx_start: oneshot::Sender<Started>,
rx_halt: oneshot::Receiver<Halted>,
) -> JoinHandle<Launcher> {
let launcher = Launcher::new(self.bind_to);
) -> JoinHandle<Spawner> {
let launcher = Spawner::new(self.bind_to);
tokio::spawn(async move {
Udp::run_with_graceful_shutdown(tracker, launcher.bind_to, tx_start, rx_halt).await;
Launcher::run_with_graceful_shutdown(tracker, launcher.bind_to, tx_start, rx_halt).await;
launcher
})
}
Expand Down Expand Up @@ -388,9 +388,9 @@ impl Stream for Receiver {

/// A UDP server instance launcher.
#[derive(Constructor)]
pub struct Udp;
pub struct Launcher;

impl Udp {
impl Launcher {
/// It starts the UDP server instance with graceful shutdown.
///
/// # Panics
Expand Down Expand Up @@ -502,7 +502,8 @@ impl Udp {
*/

let abort_handle =
tokio::task::spawn(Udp::process_request(req, tracker.clone(), receiver.bound_socket.clone())).abort_handle();
tokio::task::spawn(Launcher::process_request(req, tracker.clone(), receiver.bound_socket.clone()))
.abort_handle();

if abort_handle.is_finished() {
continue;
Expand Down Expand Up @@ -589,7 +590,7 @@ mod tests {

use crate::bootstrap::app::initialize_with_configuration;
use crate::servers::registar::Registar;
use crate::servers::udp::server::{Launcher, UdpServer};
use crate::servers::udp::server::{Spawner, UdpServer};

#[tokio::test]
async fn it_should_be_able_to_start_and_stop() {
Expand All @@ -600,7 +601,7 @@ mod tests {
let bind_to = config.bind_address;
let register = &Registar::default();

let stopped = UdpServer::new(Launcher::new(bind_to));
let stopped = UdpServer::new(Spawner::new(bind_to));

let started = stopped
.start(tracker, register.give_form())
Expand All @@ -622,7 +623,7 @@ mod tests {
let bind_to = config.bind_address;
let register = &Registar::default();

let stopped = UdpServer::new(Launcher::new(bind_to));
let stopped = UdpServer::new(Spawner::new(bind_to));

let started = stopped
.start(tracker, register.give_form())
Expand Down
4 changes: 2 additions & 2 deletions tests/servers/udp/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use torrust_tracker::bootstrap::app::initialize_with_configuration;
use torrust_tracker::core::Tracker;
use torrust_tracker::servers::registar::Registar;
use torrust_tracker::servers::udp::server::{Launcher, Running, Stopped, UdpServer};
use torrust_tracker::servers::udp::server::{Running, Spawner, Stopped, UdpServer};
use torrust_tracker::shared::bit_torrent::tracker::udp::client::DEFAULT_TIMEOUT;
use torrust_tracker_configuration::{Configuration, UdpTracker};
use torrust_tracker_primitives::info_hash::InfoHash;
Expand Down Expand Up @@ -36,7 +36,7 @@ impl Environment<Stopped> {

let bind_to = config.bind_address;

let server = UdpServer::new(Launcher::new(bind_to));
let server = UdpServer::new(Spawner::new(bind_to));

Self {
config,
Expand Down

0 comments on commit c121bf2

Please sign in to comment.