Skip to content

Commit 2a05590

Browse files
committed
refactor: [#508] move UDP tracker client to production code
We will use the UDP tracker connection request to check if the UDP tracker service is healthy.
1 parent 7421306 commit 2a05590

File tree

7 files changed

+44
-20
lines changed

7 files changed

+44
-20
lines changed

src/servers/udp/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,3 @@ pub type Port = u16;
652652
/// The transaction id. A random number generated byt the peer that is used to
653653
/// match requests and responses.
654654
pub type TransactionId = i64;
655-
656-
/// The maximum number of bytes in a UDP packet.
657-
pub const MAX_PACKET_SIZE: usize = 1496;
658-
/// A magic 64-bit integer constant defined in the protocol that is used to
659-
/// identify the protocol.
660-
pub const PROTOCOL_ID: i64 = 0x0417_2710_1980;

src/servers/udp/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use tokio::task::JoinHandle;
3030

3131
use crate::servers::signals::shutdown_signal;
3232
use crate::servers::udp::handlers::handle_packet;
33-
use crate::servers::udp::MAX_PACKET_SIZE;
33+
use crate::shared::bit_torrent::udp::MAX_PACKET_SIZE;
3434
use crate::tracker::Tracker;
3535

3636
/// Error that can occur when starting or stopping the UDP server.

src/shared/bit_torrent/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@
6969
//!Bencode & bdecode in your browser | <https://github.com/Chocobo1/bencode_online>
7070
pub mod common;
7171
pub mod info_hash;
72+
pub mod udp;

tests/servers/udp/client.rs src/shared/bit_torrent/udp/client.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,49 @@ use std::sync::Arc;
33

44
use aquatic_udp_protocol::{Request, Response};
55
use tokio::net::UdpSocket;
6-
use torrust_tracker::servers::udp::MAX_PACKET_SIZE;
76

8-
use crate::servers::udp::source_address;
7+
use crate::shared::bit_torrent::udp::{source_address, MAX_PACKET_SIZE};
98

109
#[allow(clippy::module_name_repetitions)]
1110
pub struct UdpClient {
1211
pub socket: Arc<UdpSocket>,
1312
}
1413

1514
impl UdpClient {
15+
/// # Panics
16+
///
17+
/// Will panic if the local address can't be bound.
1618
pub async fn bind(local_address: &str) -> Self {
1719
let socket = UdpSocket::bind(local_address).await.unwrap();
1820
Self {
1921
socket: Arc::new(socket),
2022
}
2123
}
2224

25+
/// # Panics
26+
///
27+
/// Will panic if can't connect to the socket.
2328
pub async fn connect(&self, remote_address: &str) {
2429
self.socket.connect(remote_address).await.unwrap();
2530
}
2631

32+
/// # Panics
33+
///
34+
/// Will panic if:
35+
///
36+
/// - Can't write to the socket.
37+
/// - Can't send data.
2738
pub async fn send(&self, bytes: &[u8]) -> usize {
2839
self.socket.writable().await.unwrap();
2940
self.socket.send(bytes).await.unwrap()
3041
}
3142

43+
/// # Panics
44+
///
45+
/// Will panic if:
46+
///
47+
/// - Can't read from the socket.
48+
/// - Can't receive data.
3249
pub async fn receive(&self, bytes: &mut [u8]) -> usize {
3350
self.socket.readable().await.unwrap();
3451
self.socket.recv(bytes).await.unwrap()
@@ -49,6 +66,9 @@ pub struct UdpTrackerClient {
4966
}
5067

5168
impl UdpTrackerClient {
69+
/// # Panics
70+
///
71+
/// Will panic if can't write request to bytes.
5272
pub async fn send(&self, request: Request) -> usize {
5373
// Write request into a buffer
5474
let request_buffer = vec![0u8; MAX_PACKET_SIZE];
@@ -68,6 +88,9 @@ impl UdpTrackerClient {
6888
self.udp_client.send(request_data).await
6989
}
7090

91+
/// # Panics
92+
///
93+
/// Will panic if can't create response from the received payload (bytes buffer).
7194
pub async fn receive(&self) -> Response {
7295
let mut response_buffer = [0u8; MAX_PACKET_SIZE];
7396

src/shared/bit_torrent/udp/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub mod client;
2+
3+
/// The maximum number of bytes in a UDP packet.
4+
pub const MAX_PACKET_SIZE: usize = 1496;
5+
/// A magic 64-bit integer constant defined in the protocol that is used to
6+
/// identify the protocol.
7+
pub const PROTOCOL_ID: i64 = 0x0417_2710_1980;
8+
9+
/// Generates the source address for the UDP client
10+
fn source_address(port: u16) -> String {
11+
format!("127.0.0.1:{port}")
12+
}

tests/servers/udp/contract.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
use core::panic;
77

88
use aquatic_udp_protocol::{ConnectRequest, ConnectionId, Response, TransactionId};
9-
use torrust_tracker::servers::udp::MAX_PACKET_SIZE;
9+
use torrust_tracker::shared::bit_torrent::udp::client::{new_udp_client_connected, UdpTrackerClient};
10+
use torrust_tracker::shared::bit_torrent::udp::MAX_PACKET_SIZE;
1011
use torrust_tracker_test_helpers::configuration;
1112

1213
use crate::servers::udp::asserts::is_error_response;
13-
use crate::servers::udp::client::{new_udp_client_connected, UdpTrackerClient};
1414
use crate::servers::udp::test_environment::running_test_environment;
1515

1616
fn empty_udp_request() -> [u8; MAX_PACKET_SIZE] {
@@ -51,10 +51,10 @@ async fn should_return_a_bad_request_response_when_the_client_sends_an_empty_req
5151

5252
mod receiving_a_connection_request {
5353
use aquatic_udp_protocol::{ConnectRequest, TransactionId};
54+
use torrust_tracker::shared::bit_torrent::udp::client::new_udp_tracker_client_connected;
5455
use torrust_tracker_test_helpers::configuration;
5556

5657
use crate::servers::udp::asserts::is_connect_response;
57-
use crate::servers::udp::client::new_udp_tracker_client_connected;
5858
use crate::servers::udp::test_environment::running_test_environment;
5959

6060
#[tokio::test]
@@ -82,10 +82,10 @@ mod receiving_an_announce_request {
8282
AnnounceEvent, AnnounceRequest, ConnectionId, InfoHash, NumberOfBytes, NumberOfPeers, PeerId, PeerKey, Port,
8383
TransactionId,
8484
};
85+
use torrust_tracker::shared::bit_torrent::udp::client::new_udp_tracker_client_connected;
8586
use torrust_tracker_test_helpers::configuration;
8687

8788
use crate::servers::udp::asserts::is_ipv4_announce_response;
88-
use crate::servers::udp::client::new_udp_tracker_client_connected;
8989
use crate::servers::udp::contract::send_connection_request;
9090
use crate::servers::udp::test_environment::running_test_environment;
9191

@@ -124,10 +124,10 @@ mod receiving_an_announce_request {
124124

125125
mod receiving_an_scrape_request {
126126
use aquatic_udp_protocol::{ConnectionId, InfoHash, ScrapeRequest, TransactionId};
127+
use torrust_tracker::shared::bit_torrent::udp::client::new_udp_tracker_client_connected;
127128
use torrust_tracker_test_helpers::configuration;
128129

129130
use crate::servers::udp::asserts::is_scrape_response;
130-
use crate::servers::udp::client::new_udp_tracker_client_connected;
131131
use crate::servers::udp::contract::send_connection_request;
132132
use crate::servers::udp::test_environment::running_test_environment;
133133

tests/servers/udp/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
pub mod asserts;
2-
pub mod client;
32
pub mod contract;
43
pub mod test_environment;
5-
6-
/// Generates the source address for the UDP client
7-
fn source_address(port: u16) -> String {
8-
format!("127.0.0.1:{port}")
9-
}

0 commit comments

Comments
 (0)